x64_recomp: add movbe and SSE4.2 crc32 emitters #1362
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1362"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from ubiquitarianism/leonida. Originally posted by quietfold on 2026-11-24T22:32:01Z.
Item ID:
X64-RECOMP-MOVBE-CRC32Summary
tools/x64_recomp.pypreviously decoded every0F 38 xxthree-byte opcode as("simd", "V W"), which misdecodedmovbeand SSE4.2crc32(both baseline Zen 2 / Xbox Series instructions) with XMM operands and rejected them during translation.This PR adds decoding, classification, and ARM64 lowering for
0F 38 F0and0F 38 F1:decode_one0F 38map):movbe(rep == ""): decodes0F 38 F0(load) and0F 38 F1(store) across 16/32/64-bit widths (REX.Wtaking precedence over66, resolving RIP-relative addresses). Rejects invalidmod == 3register forms andF3 0F 38 F0/F1asunknownwith accurate instruction length; unrelated0F 38SIMD opcodes remain unchanged.crc32(rep == "f2"): decodesF2 0F 38 F0(Gd/Gq, Eb, ignoring66and preservinghi8vs REXlow8register selection) andF2 0F 38 F1(Gd, Ewwith66,Gd, Eddefault,Gq, EqwithREX.Woverriding66). Note that66 F2 0F 38 F1always writes a 32-bitGddestination (zero-extended to 64 bits), not 16-bitGw.Asm.rev16):REV16(0x5AC00400/0xDAC00400), bit-verified againstclang -arch arm64+otool -t._emit_movbe,_emit_crc32):_emit_movbe: swaps 16-bit viaREV16(preserving upper 48 bits of destination on load and isolating 2-byte stores) and 32/64-bit viaREV(zero-extending 32-bit loads), safe for base/index register aliasing (movbe rcx, [rcx]andmovbe [rcx], rcx)._emit_crc32: lowers all four widths (8/16/32/64-bit) with a unified portable ARMv8.0 Castagnoli (0x82F63B78) bit loop using only non-flag-setting instructions (and_i,sub_rfromXZR,and_r,lsr,eor_r,sub,cbnz), clearing bits 63:32 of the destination register and preserving every CPU flag (NZCVandctx.eflags). Safe fordst == srcaliasing (crc32 eax, eax,crc32 eax, ah,crc32 rax, rax).Tests (
tests/test_x64_recomp_movbe_crc32.py)movbeandcrc32, bit-exactAsm.rev16encoding, and ARM64 codegen checks.movbe16/32/64-bit load/store/partial-store isolation/aliasing;crc32against the RFC 3720 / iSCSI Castagnoli vector (b"123456789"->0xE3069283) across 1/2/4/8-byte & mixed chunks,hi8/REXlow8, upper-32-bit zero-extension, and aliasing; and fullCF/ZF/SF/OF/PFflag preservation across both same-block ARMNZCVand cross-blockctx.eflags.Verification
python3 -I tests/test_x64_recomp_movbe_crc32.py— 6 tests (3 static OK, 3 exec skipped without runner)python3 -I tests/test_x64_recomp.py— OKpython3 -m compileall -q make_game.py tools tests— OKpython3 -I scripts/check_no_game_data.py tools/x64_recomp.py tests/test_x64_recomp_movbe_crc32.py— OKpython3 -I tests/test_text_write_line_endings.py— 3/3 OKtests/test_ci_registration.pycurrently has an unrelated pre-existing failure onmain(51a6074d2,tests/semantic/test_sub_co_same_operand.pyfrom #930, tracked underTEST-CI-REG-LOCAL-MIXIN); all other 14/15 registration checks (including our new test file) pass.tools/x64_recomp.pytoupstream/mainfails all static test methods (22 failures, 1 error).x64_recompPRs, 0 conflicts):tests/test_x64_recomp_movbe_crc32.py: 6/6 passed (0 skipped)test_x64_recomp_*.pysuites combined: 114/114 passed (0 skipped)AI-assisted: yes
Imported from ubiquitarianism/leonida. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-24T22:32:09Z.
Review: COMMENTED
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
Imported from ubiquitarianism/leonida. Originally posted by kevincommits on 2026-11-26T03:09:20Z.
Independent run with QEMU. Apple M5 Pro, macOS 27.2.1 (26B131), Python 3.14.2, QEMU 11.1.3 from Homebrew. I merged
mainat7f3d0095ainto the branch locally (902 commits behind, no conflicts). Head tested:7014994d4.Against a reference model
crc32andmovbeare right in all 174 checks.Test file. As shipped under QEMU,
python -I tests/test_x64_recomp_movbe_crc32.pyis 6 run, 6 failures, from the wiring bug in #1409 (xr.run_kernelwherexr.run_resultis needed). WithRUNNERswapped forxr.run_resultin memory (no file changed): 6/6.Beyond the file, generated cases under QEMU against a small Python model:
crc32register formseax, cl/rax, cl/eax, cx/eax, ecx/rax, rcx: five starting CRCs (0, 1,0xffffffff,0x12345678,0x80000000) by six sources, against a bitwise CRC-32C; the destination starts with junk in its upper half, which must come back zero (150 checks);movbeload and store at[rsp]at 16, 32 and 64 bits, four values each: the full destination register on a load (untouched upper bits at 16, zero-extension at 32) and the full memory qword on a store (neighbouring bytes untouched) (24 checks).There is no
maincolumn:maintreats0f 38 f0/f1as SIMD and does not translate them.Counts.
tests/test_x64_recomp.py44/44 andtests/test_recomp_census.py11/11, the same asmain. Full standalone suite: 514 run, 512 pass; the 2 that fail aremain'stest_mod_pack_output_aliasand the new file, for the wiring reason.Not tested:
crc32with a memory source beyond the file's own cases,movbewith other addressing modes, and flags (neither instruction writes any; the file has its own flag-preservation test, which passes). No game files used. AI-assisted.Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T14:31:46Z.
Red/green: classified READY by dada-lotF (red on main, green on head merged with main); head
7014994d41unchanged since 2026-11-24, before that run.Targeted batch (PR tests, tests importing the changed modules, docs tests, translator when touched) merged on main
d48a957bbin GitHub Actions: no new failure.AI-assisted (Claude Code, Claude Opus 5.5)