aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


commit bd93cc8fd79eef4c65281a27e48db2b0e5aaaf12
parent 11376489b5e0103d4d5e3f67a9874ddf0601094a
Author: anon <anon@example.com>
Date:   Sat, 17 Oct 2026 21:16:14 +0000

Simplify spatial grid traversal while preserving cell order

Diffstat:
Msrc/Makefile.linux | 7+++++++
Msrc/game/qqr.dll/lifted-0005.c | 340++++++++++++++++---------------------------------------------------------------
Asrc/tests/grid_walk.c | 126+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 200 insertions(+), 273 deletions(-)

diff --git a/src/Makefile.linux b/src/Makefile.linux @@ -229,6 +229,7 @@ test: build/linux/window build/linux/graphics build/linux/atoi build/linux/stric ./build/linux/ground-height ./build/linux/tile-corners ./build/linux/terrain-line + ./build/linux/grid-walk ./build/linux/terrain-tile ./build/linux/shadow-contact ./build/linux/network-queue @@ -482,6 +483,12 @@ build/linux/terrain-line: tests/terrain_line.c game/llr.dll/lifted-0023.c runtim test: build/linux/terrain-line +build/linux/grid-walk: tests/grid_walk.c game/qqr.dll/lifted-0005.c runtime/guest_memory.c runtime/recomp_state.c runtime/recomp_types.h runtime/guest_memory.h + mkdir -p $(@D) + $(CC) $(CPPFLAGS) $(CFLAGS) -ffunction-sections -fdata-sections $(filter %.c,$^) -Wl,--gc-sections -pthread -lm -o $@ + +test: build/linux/grid-walk + build/linux/skinning: tests/skinning.c game/bbr.dll/lifted-0061.c runtime/guest_memory.c runtime/recomp_state.c runtime/recomp_types.h runtime/guest_memory.h mkdir -p $(@D) $(CC) $(CPPFLAGS) $(CFLAGS) -ffunction-sections -fdata-sections $(filter %.c,$^) -Wl,--gc-sections -pthread -lm -o $@ diff --git a/src/game/qqr.dll/lifted-0005.c b/src/game/qqr.dll/lifted-0005.c @@ -6160,282 +6160,76 @@ L_4200F95F: } void sub_4200F990(void) { - int _fpu_cmp = 0; - uint32_t _cf = 0; - int _df = 1; - uint32_t _flag_k = FK_NONE; - uint32_t _flag_a = 0, _flag_b = 0; - uint32_t _itail_tgt = 0; + uint32_t _cf = 0, _flag_k = FK_NONE, _flag_a = 0, _flag_b = 0; RECOMP_REGS_LOCALS RECOMP_ENTER(0x4200F990u); -L_4200F990: - PUSH32(esp, ebp); /* 0x4200F990: push ebp */ - ebp = esp; /* 0x4200F991: mov ebp, esp */ - _flag_a = (uint32_t)(esp); _flag_b = (uint32_t)(0x18u); - _cf = (uint32_t)CMP_B(_flag_a, _flag_b); - esp = esp - 0x18u; /* 0x4200F993: sub esp, 0x18 */ - _flag_k = FK_CMP; - fp_push((double)(int32_t)MEM32(ebp + 0x14)); /* 0x4200F996: fild dword ptr [ebp + 0x14] */ - eax = MEM32(ebp + 0x8); /* 0x4200F999: mov eax, dword ptr [ebp + 8] */ - PUSH32(esp, ebx); /* 0x4200F99C: push ebx */ - PUSH32(esp, esi); /* 0x4200F99D: push esi */ - _st[0] = (double)*(float*)ADDR(ebp + 0x1C) / _st[0]; /* 0x4200F99E: fdivr dword ptr [ebp + 0x1c] */ - PUSH32(esp, edi); /* 0x4200F9A1: push edi */ - fp_push(*(float*)&MEM32(eax)); /* 0x4200F9A2: fld dword ptr [eax] */ - _st[0] /= _st[1]; /* 0x4200F9A4: fdiv st(1) */ - { float _v = (float)fp_pop(); *(float*)ADDR(ebp + (-0x4)) = _v; } /* 0x4200F9A6: fstp dword ptr [ebp - 4] */ - fp_push(*(float*)&MEM32(eax + 0x8)); /* 0x4200F9A9: fld dword ptr [eax + 8] */ - _st[0] /= _st[1]; /* 0x4200F9AC: fdiv st(1) */ - eax = MEM32(ebp + 0xC); /* 0x4200F9AE: mov eax, dword ptr [ebp + 0xc] */ - { float _v = (float)fp_pop(); *(float*)ADDR(ebp + (-0x8)) = _v; } /* 0x4200F9B1: fstp dword ptr [ebp - 8] */ - fp_push(*(float*)&MEM32(eax)); /* 0x4200F9B4: fld dword ptr [eax] */ - _st[0] /= _st[1]; /* 0x4200F9B6: fdiv st(1) */ - { float _v = (float)fp_pop(); *(float*)ADDR(ebp + 0x1C) = _v; } /* 0x4200F9B8: fstp dword ptr [ebp + 0x1c] */ - _st[0] = (double)*(float*)ADDR(eax + 0x8) / _st[0]; /* 0x4200F9BB: fdivr dword ptr [eax + 8] */ - fp_push(*(float*)&MEM32(ebp + (-0x4))); /* 0x4200F9BE: fld dword ptr [ebp - 4] */ - RECOMP_ICALL(0x42016F3Cu); /* 0x4200F9C1: call 0x42016f3c */ /* not lifted */ - RECOMP_FLAGS_IN(); - fp_push(*(float*)&MEM32(ebp + (-0x8))); /* 0x4200F9C6: fld dword ptr [ebp - 8] */ - ebx = eax; /* 0x4200F9C9: mov ebx, eax */ - RECOMP_ICALL(0x42016F3Cu); /* 0x4200F9CB: call 0x42016f3c */ /* not lifted */ - RECOMP_FLAGS_IN(); - fp_push(*(float*)&MEM32(ebp + 0x1C)); /* 0x4200F9D0: fld dword ptr [ebp + 0x1c] */ - esi = eax; /* 0x4200F9D3: mov esi, eax */ - RECOMP_ICALL(0x42016F3Cu); /* 0x4200F9D5: call 0x42016f3c */ /* not lifted */ - RECOMP_FLAGS_IN(); - fp_push(_st[0]); /* 0x4200F9DA: fld st(0) */ - edi = eax; /* 0x4200F9DC: mov edi, eax */ - RECOMP_ICALL(0x42016F3Cu); /* 0x4200F9DE: call 0x42016f3c */ /* not lifted */ - RECOMP_FLAGS_IN(); - fp_push(*(float*)&MEM32(ebp + 0x1C)); /* 0x4200F9E3: fld dword ptr [ebp + 0x1c] */ - _st[0] -= (double)*(float*)ADDR(ebp + (-0x4)); /* 0x4200F9E6: fsub dword ptr [ebp - 4] */ - MEM32(ebp + (-0x10)) = eax; /* 0x4200F9E9: mov dword ptr [ebp - 0x10], eax */ - { float _v = (float)fp_pop(); *(float*)ADDR(ebp + 0x1C) = _v; } /* 0x4200F9EC: fstp dword ptr [ebp + 0x1c] */ - _st[0] -= (double)*(float*)ADDR(ebp + (-0x8)); /* 0x4200F9EF: fsub dword ptr [ebp - 8] */ - { float _v = (float)fp_pop(); *(float*)ADDR(ebp + 0x8) = _v; } /* 0x4200F9F2: fstp dword ptr [ebp + 8] */ - fp_push(*(float*)&MEM32(ebp + 0x1C)); /* 0x4200F9F5: fld dword ptr [ebp + 0x1c] */ - _fpu_cmp = FPU_CMP(_st[0], (double)*(float*)ADDR(0x420180F8)); /* 0x4200F9F8: fcomp dword ptr [0x420180f8] */ - fp_pop(); - eax = (eax & 0xFFFF0000u) | ((_fpu_cmp >= 0x10000) ? (uint32_t)(_fpu_cmp & 0x4700) : _fpu_cmp == 2 ? 0x4500u : _fpu_cmp < 0 ? 0x0100u : _fpu_cmp == 0 ? 0x4000u : 0x0000u); /* 0x4200F9FE: fnstsw ax */ - /* test HI8(eax), 1 */ /* 0x4200FA00: test ah, 1 */ - _flag_a = (uint32_t)(HI8(eax)) << 24; _flag_b = (uint32_t)(1) << 24; - _cf = 0; - _flag_k = FK_NARROW(FK_TEST, 24); - if (TEST_NZ(_flag_a, _flag_b)) goto L_4200FA35; /* 0x4200FA03: jne 0x4200fa35 */ -L_4200FA05: - fp_push(*(float*)&MEM32(0x42018430)); /* 0x4200FA05: fld dword ptr [0x42018430] */ - _flag_a = (uint32_t)(esp); _flag_b = (uint32_t)(8); - _cf = (uint32_t)CMP_B(_flag_a, _flag_b); - esp = esp - 8; /* 0x4200FA0B: sub esp, 8 */ - _flag_k = FK_CMP; - _st[0] /= (double)*(float*)ADDR(ebp + 0x1C); /* 0x4200FA0E: fdiv dword ptr [ebp + 0x1c] */ - MEM32(ebp + (-0xC)) = 1; /* 0x4200FA11: mov dword ptr [ebp - 0xc], 1 */ - { float _v = (float)fp_pop(); *(float*)ADDR(ebp + 0x1C) = _v; } /* 0x4200FA18: fstp dword ptr [ebp + 0x1c] */ - fp_push(*(float*)&MEM32(ebp + (-0x4))); /* 0x4200FA1B: fld dword ptr [ebp - 4] */ - { double _v = fp_pop(); *(double*)ADDR(esp) = _v; } /* 0x4200FA1E: fstp qword ptr [esp] */ - RECOMP_ICALL(MEM32(0x42018054)); /* 0x4200FA21: call dword ptr [0x42018054] */ - RECOMP_FLAGS_IN(); - _st[0] += *(double*)ADDR(0x42018428); /* 0x4200FA27: fadd qword ptr [0x42018428] */ - _flag_a = (uint32_t)(esp); _flag_b = (uint32_t)(8); - _cf = (uint32_t)((uint32_t)(_flag_a + _flag_b) < (uint32_t)_flag_a); - esp = esp + 8; /* 0x4200FA2D: add esp, 8 */ - _flag_k = FK_ADD; - _st[0] -= (double)*(float*)ADDR(ebp + (-0x4)); /* 0x4200FA30: fsub dword ptr [ebp - 4] */ - goto L_4200FA62; /* 0x4200FA33: jmp 0x4200fa62 */ -L_4200FA35: - fp_push(*(float*)&MEM32(0x42018420)); /* 0x4200FA35: fld dword ptr [0x42018420] */ - MEM32(ebp + (-0xC)) = 0xFFFFFFFFu; /* 0x4200FA3B: mov dword ptr [ebp - 0xc], 0xffffffff */ - _st[0] /= (double)*(float*)ADDR(ebp + 0x1C); /* 0x4200FA42: fdiv dword ptr [ebp + 0x1c] */ - { float _v = (float)fp_pop(); *(float*)ADDR(ebp + 0x1C) = _v; } /* 0x4200FA45: fstp dword ptr [ebp + 0x1c] */ - fp_push(*(float*)&MEM32(ebp + (-0x4))); /* 0x4200FA48: fld dword ptr [ebp - 4] */ - { double _v = fp_pop(); *(double*)ADDR(ebp + (-0x18)) = _v; } /* 0x4200FA4B: fstp qword ptr [ebp - 0x18] */ - eax = MEM32(ebp + (-0x14)); /* 0x4200FA4E: mov eax, dword ptr [ebp - 0x14] */ - ecx = MEM32(ebp + (-0x18)); /* 0x4200FA51: mov ecx, dword ptr [ebp - 0x18] */ - PUSH32(esp, eax); /* 0x4200FA54: push eax */ - PUSH32(esp, ecx); /* 0x4200FA55: push ecx */ - RECOMP_ICALL(MEM32(0x42018054)); /* 0x4200FA56: call dword ptr [0x42018054] */ - RECOMP_FLAGS_IN(); - _st[0] = *(double*)ADDR(ebp + (-0x18)) - _st[0]; /* 0x4200FA5C: fsubr qword ptr [ebp - 0x18] */ - _flag_a = (uint32_t)(esp); _flag_b = (uint32_t)(8); - _cf = (uint32_t)((uint32_t)(_flag_a + _flag_b) < (uint32_t)_flag_a); - esp = esp + 8; /* 0x4200FA5F: add esp, 8 */ - _flag_k = FK_ADD; -L_4200FA62: - _st[0] *= (double)*(float*)ADDR(ebp + 0x1C); /* 0x4200FA62: fmul dword ptr [ebp + 0x1c] */ - { float _v = (float)fp_pop(); *(float*)ADDR(ebp + 0xC) = _v; } /* 0x4200FA65: fstp dword ptr [ebp + 0xc] */ - fp_push(*(float*)&MEM32(ebp + 0x8)); /* 0x4200FA68: fld dword ptr [ebp + 8] */ - _fpu_cmp = FPU_CMP(_st[0], (double)*(float*)ADDR(0x420180F8)); /* 0x4200FA6B: fcomp dword ptr [0x420180f8] */ - fp_pop(); - eax = (eax & 0xFFFF0000u) | ((_fpu_cmp >= 0x10000) ? (uint32_t)(_fpu_cmp & 0x4700) : _fpu_cmp == 2 ? 0x4500u : _fpu_cmp < 0 ? 0x0100u : _fpu_cmp == 0 ? 0x4000u : 0x0000u); /* 0x4200FA71: fnstsw ax */ - /* test HI8(eax), 1 */ /* 0x4200FA73: test ah, 1 */ - _flag_a = (uint32_t)(HI8(eax)) << 24; _flag_b = (uint32_t)(1) << 24; - _cf = 0; - _flag_k = FK_NARROW(FK_TEST, 24); - if (TEST_NZ(_flag_a, _flag_b)) goto L_4200FAA8; /* 0x4200FA76: jne 0x4200faa8 */ -L_4200FA78: - fp_push(*(float*)&MEM32(0x42018430)); /* 0x4200FA78: fld dword ptr [0x42018430] */ - _flag_a = (uint32_t)(esp); _flag_b = (uint32_t)(8); - _cf = (uint32_t)CMP_B(_flag_a, _flag_b); - esp = esp - 8; /* 0x4200FA7E: sub esp, 8 */ - _flag_k = FK_CMP; - _st[0] /= (double)*(float*)ADDR(ebp + 0x8); /* 0x4200FA81: fdiv dword ptr [ebp + 8] */ - MEM32(ebp + (-0x4)) = 1; /* 0x4200FA84: mov dword ptr [ebp - 4], 1 */ - { float _v = (float)fp_pop(); *(float*)ADDR(ebp + 0x8) = _v; } /* 0x4200FA8B: fstp dword ptr [ebp + 8] */ - fp_push(*(float*)&MEM32(ebp + (-0x8))); /* 0x4200FA8E: fld dword ptr [ebp - 8] */ - { double _v = fp_pop(); *(double*)ADDR(esp) = _v; } /* 0x4200FA91: fstp qword ptr [esp] */ - RECOMP_ICALL(MEM32(0x42018054)); /* 0x4200FA94: call dword ptr [0x42018054] */ - RECOMP_FLAGS_IN(); - _st[0] += *(double*)ADDR(0x42018428); /* 0x4200FA9A: fadd qword ptr [0x42018428] */ - _flag_a = (uint32_t)(esp); _flag_b = (uint32_t)(8); - _cf = (uint32_t)((uint32_t)(_flag_a + _flag_b) < (uint32_t)_flag_a); - esp = esp + 8; /* 0x4200FAA0: add esp, 8 */ - _flag_k = FK_ADD; - _st[0] -= (double)*(float*)ADDR(ebp + (-0x8)); /* 0x4200FAA3: fsub dword ptr [ebp - 8] */ - goto L_4200FAD5; /* 0x4200FAA6: jmp 0x4200fad5 */ -L_4200FAA8: - fp_push(*(float*)&MEM32(0x42018420)); /* 0x4200FAA8: fld dword ptr [0x42018420] */ - MEM32(ebp + (-0x4)) = 0xFFFFFFFFu; /* 0x4200FAAE: mov dword ptr [ebp - 4], 0xffffffff */ - _st[0] /= (double)*(float*)ADDR(ebp + 0x8); /* 0x4200FAB5: fdiv dword ptr [ebp + 8] */ - { float _v = (float)fp_pop(); *(float*)ADDR(ebp + 0x8) = _v; } /* 0x4200FAB8: fstp dword ptr [ebp + 8] */ - fp_push(*(float*)&MEM32(ebp + (-0x8))); /* 0x4200FABB: fld dword ptr [ebp - 8] */ - { double _v = fp_pop(); *(double*)ADDR(ebp + (-0x18)) = _v; } /* 0x4200FABE: fstp qword ptr [ebp - 0x18] */ - edx = MEM32(ebp + (-0x14)); /* 0x4200FAC1: mov edx, dword ptr [ebp - 0x14] */ - eax = MEM32(ebp + (-0x18)); /* 0x4200FAC4: mov eax, dword ptr [ebp - 0x18] */ - PUSH32(esp, edx); /* 0x4200FAC7: push edx */ - PUSH32(esp, eax); /* 0x4200FAC8: push eax */ - RECOMP_ICALL(MEM32(0x42018054)); /* 0x4200FAC9: call dword ptr [0x42018054] */ - RECOMP_FLAGS_IN(); - _st[0] = *(double*)ADDR(ebp + (-0x18)) - _st[0]; /* 0x4200FACF: fsubr qword ptr [ebp - 0x18] */ - _flag_a = (uint32_t)(esp); _flag_b = (uint32_t)(8); - _cf = (uint32_t)((uint32_t)(_flag_a + _flag_b) < (uint32_t)_flag_a); - esp = esp + 8; /* 0x4200FAD2: add esp, 8 */ - _flag_k = FK_ADD; -L_4200FAD5: - eax = MEM32(ebp + (-0x10)); /* 0x4200FAD5: mov eax, dword ptr [ebp - 0x10] */ - _flag_a = (uint32_t)(eax); _flag_b = (uint32_t)(esi); - _cf = (uint32_t)CMP_B(_flag_a, _flag_b); - eax = eax - esi; /* 0x4200FAD8: sub eax, esi */ - _flag_k = FK_CMP; - edx = ((int32_t)eax < 0) ? 0xFFFFFFFFu : 0; /* 0x4200FADA: cdq */ - ecx = eax; /* 0x4200FADB: mov ecx, eax */ - _flag_a = (uint32_t)((ecx ^ edx)); _flag_b = (uint32_t)((ecx ^ edx)); - ecx = ecx ^ edx; /* 0x4200FADD: xor ecx, edx */ - _cf = 0; - _flag_k = FK_TEST; - eax = edi; /* 0x4200FADF: mov eax, edi */ - _flag_a = (uint32_t)(eax); _flag_b = (uint32_t)(ebx); - _cf = (uint32_t)CMP_B(_flag_a, _flag_b); - eax = eax - ebx; /* 0x4200FAE1: sub eax, ebx */ - _flag_k = FK_CMP; - _flag_a = (uint32_t)(ecx); _flag_b = (uint32_t)(edx); - _cf = (uint32_t)CMP_B(_flag_a, _flag_b); - ecx = ecx - edx; /* 0x4200FAE3: sub ecx, edx */ - _flag_k = FK_CMP; - edx = ((int32_t)eax < 0) ? 0xFFFFFFFFu : 0; /* 0x4200FAE5: cdq */ - _flag_a = (uint32_t)((eax ^ edx)); _flag_b = (uint32_t)((eax ^ edx)); - eax = eax ^ edx; /* 0x4200FAE6: xor eax, edx */ - _cf = 0; - _flag_k = FK_TEST; - _flag_a = (uint32_t)(eax); _flag_b = (uint32_t)(edx); - _cf = (uint32_t)CMP_B(_flag_a, _flag_b); - eax = eax - edx; /* 0x4200FAE8: sub eax, edx */ - _flag_k = FK_CMP; - _st[0] *= (double)*(float*)ADDR(ebp + 0x8); /* 0x4200FAEA: fmul dword ptr [ebp + 8] */ - _flag_a = (uint32_t)(ecx); _flag_b = (uint32_t)(eax); - _cf = (uint32_t)((uint32_t)(_flag_a + _flag_b) < (uint32_t)_flag_a); - ecx = ecx + eax; /* 0x4200FAED: add ecx, eax */ - _flag_k = FK_ADD; - MEM32(ebp + (-0x10)) = ecx; /* 0x4200FAEF: mov dword ptr [ebp - 0x10], ecx */ - { float _v = (float)fp_pop(); *(float*)ADDR(ebp + (-0x8)) = _v; } /* 0x4200FAF2: fstp dword ptr [ebp - 8] */ - if (/* add result */ recomp_cond(_flag_k, _flag_a, _flag_b, CC_S)) goto L_4200FB5E; /* 0x4200FAF5: js 0x4200fb5e */ -L_4200FAF7: - ecx = MEM32(ebp + 0x14); /* 0x4200FAF7: mov ecx, dword ptr [ebp + 0x14] */ - eax = MEM32(ebp + (-0x4)); /* 0x4200FAFA: mov eax, dword ptr [ebp - 4] */ - edi = esi; /* 0x4200FAFD: mov edi, esi */ - edi = (uint32_t)((int32_t)edi * (int32_t)ecx); /* 0x4200FAFF: imul edi, ecx */ - eax = (uint32_t)((int32_t)eax * (int32_t)ecx); /* 0x4200FB02: imul eax, ecx */ - MEM32(ebp + (-0x14)) = eax; /* 0x4200FB05: mov dword ptr [ebp - 0x14], eax */ - goto L_4200FB0D; /* 0x4200FB08: jmp 0x4200fb0d */ -L_4200FB0A: - ecx = MEM32(ebp + 0x14); /* 0x4200FB0A: mov ecx, dword ptr [ebp + 0x14] */ -L_4200FB0D: - /* test ebx, ebx */ /* 0x4200FB0D: test ebx, ebx */ - _flag_a = (uint32_t)(ebx); _flag_b = (uint32_t)(ebx); - _cf = 0; - _flag_k = FK_TEST; - if (TEST_S(_flag_a, _flag_b)) goto L_4200FB5E; /* 0x4200FB0F: jl 0x4200fb5e */ -L_4200FB11: - /* test esi, esi */ /* 0x4200FB11: test esi, esi */ - _flag_a = (uint32_t)(esi); _flag_b = (uint32_t)(esi); - _cf = 0; - _flag_k = FK_TEST; - if (TEST_S(_flag_a, _flag_b)) goto L_4200FB5E; /* 0x4200FB13: jl 0x4200fb5e */ -L_4200FB15: - /* cmp ebx, ecx */ /* 0x4200FB15: cmp ebx, ecx */ - _flag_a = (uint32_t)(ebx); _flag_b = (uint32_t)(ecx); - _cf = (uint32_t)CMP_B(_flag_a, _flag_b); - _flag_k = FK_CMP; - if (CMP_GE(_flag_a, _flag_b)) goto L_4200FB5E; /* 0x4200FB17: jge 0x4200fb5e */ -L_4200FB19: - /* cmp esi, MEM32(ebp + 0x18) */ /* 0x4200FB19: cmp esi, dword ptr [ebp + 0x18] */ - _flag_a = (uint32_t)(esi); _flag_b = (uint32_t)(MEM32(ebp + 0x18)); - _cf = (uint32_t)CMP_B(_flag_a, _flag_b); - _flag_k = FK_CMP; - if (CMP_GE(_flag_a, _flag_b)) goto L_4200FB5E; /* 0x4200FB1C: jge 0x4200fb5e */ -L_4200FB1E: - ecx = MEM32(ebp + 0x10); /* 0x4200FB1E: mov ecx, dword ptr [ebp + 0x10] */ - edx = MEM32(ecx); /* 0x4200FB21: mov edx, dword ptr [ecx] */ - eax = edi + ebx; /* 0x4200FB23: lea eax, [edi + ebx] */ - PUSH32(esp, eax); /* 0x4200FB26: push eax */ - RECOMP_ICALL(MEM32(edx)); /* 0x4200FB27: call dword ptr [edx] */ - RECOMP_FLAGS_IN(); - fp_push(*(float*)&MEM32(ebp + 0xC)); /* 0x4200FB29: fld dword ptr [ebp + 0xc] */ - _fpu_cmp = FPU_CMP(_st[0], (double)*(float*)ADDR(ebp + (-0x8))); /* 0x4200FB2C: fcomp dword ptr [ebp - 8] */ - fp_pop(); - eax = (eax & 0xFFFF0000u) | ((_fpu_cmp >= 0x10000) ? (uint32_t)(_fpu_cmp & 0x4700) : _fpu_cmp == 2 ? 0x4500u : _fpu_cmp < 0 ? 0x0100u : _fpu_cmp == 0 ? 0x4000u : 0x0000u); /* 0x4200FB2F: fnstsw ax */ - /* test HI8(eax), 1 */ /* 0x4200FB31: test ah, 1 */ - _flag_a = (uint32_t)(HI8(eax)) << 24; _flag_b = (uint32_t)(1) << 24; - _cf = 0; - _flag_k = FK_NARROW(FK_TEST, 24); - if (TEST_Z(_flag_a, _flag_b)) goto L_4200FB46; /* 0x4200FB34: je 0x4200fb46 */ -L_4200FB36: - fp_push(*(float*)&MEM32(ebp + 0xC)); /* 0x4200FB36: fld dword ptr [ebp + 0xc] */ - eax = MEM32(ebp + (-0xC)); /* 0x4200FB39: mov eax, dword ptr [ebp - 0xc] */ - _st[0] += (double)*(float*)ADDR(ebp + 0x1C); /* 0x4200FB3C: fadd dword ptr [ebp + 0x1c] */ - _flag_a = (uint32_t)(ebx); _flag_b = (uint32_t)(eax); - _cf = (uint32_t)((uint32_t)(_flag_a + _flag_b) < (uint32_t)_flag_a); - ebx = ebx + eax; /* 0x4200FB3F: add ebx, eax */ - _flag_k = FK_ADD; - { float _v = (float)fp_pop(); *(float*)ADDR(ebp + 0xC) = _v; } /* 0x4200FB41: fstp dword ptr [ebp + 0xc] */ - goto L_4200FB59; /* 0x4200FB44: jmp 0x4200fb59 */ -L_4200FB46: - fp_push(*(float*)&MEM32(ebp + (-0x8))); /* 0x4200FB46: fld dword ptr [ebp - 8] */ - ecx = MEM32(ebp + (-0x4)); /* 0x4200FB49: mov ecx, dword ptr [ebp - 4] */ - _st[0] += (double)*(float*)ADDR(ebp + 0x8); /* 0x4200FB4C: fadd dword ptr [ebp + 8] */ - eax = MEM32(ebp + (-0x14)); /* 0x4200FB4F: mov eax, dword ptr [ebp - 0x14] */ - _flag_a = (uint32_t)(esi); _flag_b = (uint32_t)(ecx); - _cf = (uint32_t)((uint32_t)(_flag_a + _flag_b) < (uint32_t)_flag_a); - esi = esi + ecx; /* 0x4200FB52: add esi, ecx */ - _flag_k = FK_ADD; - _flag_a = (uint32_t)(edi); _flag_b = (uint32_t)(eax); - _cf = (uint32_t)((uint32_t)(_flag_a + _flag_b) < (uint32_t)_flag_a); - edi = edi + eax; /* 0x4200FB54: add edi, eax */ - _flag_k = FK_ADD; - { float _v = (float)fp_pop(); *(float*)ADDR(ebp + (-0x8)) = _v; } /* 0x4200FB56: fstp dword ptr [ebp - 8] */ -L_4200FB59: - _flag_a = (uint32_t)(MEM32(ebp + (-0x10))); _flag_b = (uint32_t)(1); - MEM32(ebp + (-0x10)) = MEM32(ebp + (-0x10)) - 1; /* 0x4200FB59: dec dword ptr [ebp - 0x10] */ - _flag_k = FK_DEC; - RECOMP_BACKEDGE(); - if (/* dec result */ CMP_NS(_flag_a, _flag_b)) goto L_4200FB0A; /* 0x4200FB5C: jns 0x4200fb0a */ -L_4200FB5E: - edi = POP32_VAL(esp); /* 0x4200FB5E: pop edi */ - esi = POP32_VAL(esp); /* 0x4200FB5F: pop esi */ - ebx = POP32_VAL(esp); /* 0x4200FB60: pop ebx */ - esp = ebp; /* 0x4200FB61: mov esp, ebp */ - ebp = POP32_VAL(esp); /* 0x4200FB63: pop ebp */ - RECOMP_FLAGS_OUT(); esp += 4; { RECOMP_REGS_OUT(); return; } /* 0x4200FB64: ret */ + /* qqr.dll!GridSpace_t::IterateCellsAlongLine, RVA 0xF990. + * Keep the guest frame and callback ABI; the final extent argument is unused. */ + PUSH32(esp, ebp); + ebp = esp; + esp -= 0x18; + PUSH32(esp, ebx); + PUSH32(esp, esi); + PUSH32(esp, edi); + uint32_t start = MEM32(ebp + 8), end = MEM32(ebp + 12); + uint32_t worker = MEM32(ebp + 16); + int32_t width = MEM32(ebp + 20), height = MEM32(ebp + 24); + + /* Preserve the float stores and the unrounded end Z used by the x87 code. + * Cell indices truncate toward zero, even for negative fractional positions. */ + double cell_size = (double)*(float *)ADDR(ebp + 28) / width; + float start_x = (float)(*(float *)ADDR(start) / cell_size); + float start_z = (float)(*(float *)ADDR(start + 8) / cell_size); + float end_x = (float)(*(float *)ADDR(end) / cell_size); + double end_z = *(float *)ADDR(end + 8) / cell_size; + ebx = (uint32_t)(int64_t)trunc(start_x); + esi = (uint32_t)(int64_t)trunc(start_z); + uint32_t distance_x = (uint32_t)(int64_t)trunc(end_x) - ebx; + uint32_t distance_z = (uint32_t)(int64_t)trunc(end_z) - esi; + if ((int32_t)distance_x < 0) distance_x = 0u - distance_x; + if ((int32_t)distance_z < 0) distance_z = 0u - distance_z; + uint32_t remaining = distance_x + distance_z; + + /* Crossing distances use percentages. Ties visit the Z neighbor first. */ + float dx = (float)((double)end_x - start_x); + float dz = (float)(end_z - start_z); + int32_t step_x = dx >= 0 ? 1 : -1, step_z = dz >= 0 ? 1 : -1; + float delta_x = (float)((step_x > 0 ? 100.0 : -100.0) / dx); + float delta_z = (float)((step_z > 0 ? 100.0 : -100.0) / dz); + float next_x = (float)((step_x > 0 ? floor(start_x) + 1 - start_x : + start_x - floor(start_x)) * delta_x); + float next_z = (float)((step_z > 0 ? floor(start_z) + 1 - start_z : + start_z - floor(start_z)) * delta_z); + edi = esi * (uint32_t)width; + uint32_t row_step = (uint32_t)step_z * (uint32_t)width; + + while ((int32_t)remaining >= 0 && (int32_t)ebx >= 0 && (int32_t)esi >= 0 && + (int32_t)ebx < width && (int32_t)esi < height) { + ecx = worker; + PUSH32(esp, edi + ebx); + RECOMP_ICALL(MEM32(MEM32(worker))); + RECOMP_FLAGS_IN(); + + /* x87's C0 branch also takes X when the comparison is unordered. */ + if (!(next_x >= next_z)) { + ebx += step_x; + next_x = (float)((double)next_x + delta_x); + } else { + esi += step_z; + edi += row_step; + next_z = (float)((double)next_z + delta_z); + } + --remaining; + RECOMP_BACKEDGE(); + } + + edi = POP32_VAL(esp); + esi = POP32_VAL(esp); + ebx = POP32_VAL(esp); + esp = ebp; + ebp = POP32_VAL(esp); + esp += 4; RECOMP_REGS_OUT(); } diff --git a/src/tests/grid_walk.c b/src/tests/grid_walk.c @@ -0,0 +1,126 @@ +#include "recomp_types.h" +#include <assert.h> + +void sub_4200F990(void); +static uint32_t visited[32]; +static unsigned visits; + +static void visit_cell(void) +{ + assert(g_ecx == 0x50000100); + assert(visits < 32); + visited[visits++] = MEM32(g_esp + 4); + /* The visitor may clobber caller-saved registers and use the floating stack. */ + g_eax = 0x12345678; g_ecx = 0xabcdef; g_edx = 0x87654321; + fp_push_impl(g_st, &g_fp_top, 123.5); + fp_pop_impl(g_st, &g_fp_top); + g_esp += 8; +} +static void to_integer(void) +{ + int64_t value = fp_st0_to_i64(g_st[0], g_st_i64[0], 0x0c7f); + fp_pop_impl(g_st, &g_fp_top); + g_eax = (uint32_t)value; + g_edx = (uint32_t)((uint64_t)value >> 32); + g_esp += 4; +} +static void round_down(void) +{ + fp_push_impl(g_st, &g_fp_top, floor(MEMD(g_esp + 4))); + g_esp += 4; +} +recomp_func_t recomp_lookup_manual(uint32_t address) { return NULL; } +recomp_func_t recomp_lookup_import(uint32_t address) { abort(); } +recomp_func_t recomp_lookup(uint32_t address) +{ + switch (address) { + case 0x42016f3c: return to_integer; + case 0x504: return round_down; + case 0x508: return visit_cell; + default: fprintf(stderr, "Unexpected call %08x\n", address); abort(); + } +} + +static void run(const float points[4], float spacing) +{ + g_esp = 0x5001ff00; + g_ebp = 123; g_ebx = 234; g_esi = 345; g_edi = 456; + g_fp_top = 3; + for (unsigned i = 0; i < 8; ++i) { + g_st[i] = i + .25; + g_st_i64[i] = INT64_MAX - i; + } + *(float *)ADDR(0x50000200) = points[0]; + *(float *)ADDR(0x50000208) = points[1]; + *(float *)ADDR(0x50000210) = points[2]; + *(float *)ADDR(0x50000218) = points[3]; + MEM32(g_esp + 4) = 0x50000200; + MEM32(g_esp + 8) = 0x50000210; + MEM32(g_esp + 12) = 0x50000100; + MEM32(g_esp + 16) = 8; + MEM32(g_esp + 20) = 6; + *(float *)ADDR(g_esp + 24) = 8 * spacing; + *(float *)ADDR(g_esp + 28) = 6 * spacing; + visits = 0; + + sub_4200F990(); + + assert(g_esp == 0x5001ff04); + assert(g_ebp == 123 && g_ebx == 234 && g_esi == 345 && g_edi == 456); + assert(g_fp_top == 3); + for (unsigned i = 0; i < 3; ++i) { + assert(g_st[i] == i + .25); + assert(g_st_i64[i] == INT64_MAX - i); + } +} + +int main(void) +{ + assert(guest_map("grid fixtures", 0x50000000, 0x20000)); + assert(guest_map("grid constants", 0x42010000, 0x10000)); + MEM32(0x50000100) = 0x50000110; + MEM32(0x50000110) = 0x508; + MEM32(0x42018054) = 0x504; + *(float *)ADDR(0x420180f8) = 0; + *(float *)ADDR(0x42018430) = 100; + *(float *)ADDR(0x42018420) = -100; + *(double *)ADDR(0x42018428) = 1; + /* Stationary, axis aligned, ties, negative fractional starts, bounds, and + * a zero X direction on a cell boundary while moving backward in Z. */ + const float points[][4] = { + {.5f,.5f, .5f,.5f}, + {.5f,.5f, 3.5f,.5f}, + {.5f,.5f, .5f,3.5f}, + {.5f,.5f, 2.5f,2.5f}, + {2.5f,2.5f, .5f,.5f}, + {-.5f,.5f, 2.5f,.5f}, + {-1,.5f, 2.5f,.5f}, + {7.5f,4.5f, 9.5f,4.5f}, + {6.5f,5.5f, 6.5f,7.5f}, + {2,2, 2,0}, + {2,2, 0,2}, + {2,2, 0,0}, + }; + const unsigned counts[] = {1,4,4,5,5,3,0,1,1,3,3,5}; + const uint32_t paths[][5] = { + {0}, {0,1,2,3}, {0,8,16,24}, {0,8,9,17,18}, + {18,10,9,1,0}, {0,1,2}, {0}, {39}, {46}, + {18,10,2}, {18,17,16}, {18,10,9,1,0}, + }; + for (unsigned i = 0; i < sizeof(counts) / sizeof(*counts); ++i) { + run(points[i], 1); + + assert(visits == counts[i]); + assert(memcmp(visited, paths[i], visits * sizeof(*visited)) == 0); + } + /* Rounding end Z to float would incorrectly visit cell 24 as well. */ + const float boundary[] = {.85f,.85f, .85f,5.1f}; + const uint32_t boundary_path[] = {0,8,16}; + + run(boundary, 1.7f); + + assert(visits == 3); + assert(memcmp(visited, boundary_path, sizeof(boundary_path)) == 0); + puts("grid walk PASS"); + return 0; +}