x64_recomp: fix mul/imul CF/OF bugs and add 8/16/64-bit mul/div/idiv emitters (X64-RECOMP-MULDIV) #1279
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1279"
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-24T21:14:10Z.
What changed
Fixes four CF/OF correctness bugs in
FunctionTranslator._emit_mul(tools/x64_recomp.py) and completes width coverage formul/imul/div/idivacross 8-, 16-, 32-, and 64-bit operands (including full 128-by-64 unsigned and signed division):Bug fixes in
tools/x64_recomp.py(_emit_mul)imulclobberedT0before overflow check:a.mul(T0, T1, T0, sf)overwroteT0with the product beforesmulh(T2, T1, T0)/smaddl(T2, T1, T0, 31), computingT1 * (T1 * src)instead ofT1 * src(e.g.100000 * 10000 = 10^9falsely setCF=OF=1). Fixed by placing the product inT2(and computing 32-bit products directly withsmaddl+sxtwcomparison). Also avoidsAsm.li(rd, -1)on 64-bitimul r64, r/m64, -1viamovn.imulchecked unsigned zero instead of sign extension: checkedrdx != 0/edx != 0for bothmulandimul, wrongly settingCF=OF=1on every negative non-overflowing signed product (e.g.-2 * 3 = -6,edx = -1). Signedimulnow compares the high half againstasr(low, width - 1).imul: used 32-bitsmaddland bit-31 sign extension instead ofsxth*sxthand 16-bit signed-fit comparison.CF/OFmaterialization: neither 2-/3-operandimulnor 1-operandmul/imulcalledself._maybe_mkflags(ins)whenins.addr in self.mat, leaving staleCTX_EFLAGSfor cross-blockjo/jno/jc/jnc.New width emitters in
_emit_mulmul/imul(al * r/m8 -> ax) and 16-bitmul/imul(ax * r/m16 -> dx:ax), preserving upper 48 bits ofrax/rdxviabfiand setting exact unsigned/signedCF=OF.div/idiv(ax / r/m8 -> al, ah) and 16-bitdiv/idiv(dx:ax / r/m16 -> ax, dx), preserving upper 48 bits ofrax/rdx, with divide-by-zero (brk 0xD1) and quotient-overflow (brk 0xD2) traps.div/idivadds quotient-range overflow trapping (brk 0xD2) alongside divide-by-zero.div/idiv(rdx:rax / r/m64 -> rax, rdx):udiv+msubwhenrdx == 0;sdiv+msubwhenrdx == (rax >> 63)anddivisor != -1).UINT64_MAXdivisors, exactINT64_MIN/-2^127two's-complement negation viasubs_r/sbcs,(+2^63) / (-1) = -2^63, and exact signed/unsigned#DEoverflow traps).tests/test_x64_recomp_muldiv.py(4 static translation/code-shape tests that run everywhere + 6 end-to-end execution test methods covering 61 hardware cases whenCAN_RUN_MACHOor QEMU is available).Item ID
X64-RECOMP-MULDIV_emit_mul; verified zero conflicts intools/x64_recomp.pyagainst #1174, #1202, #1205, #1211, #1213, #1120, #1121, and #1246)Verification
Tested on macOS 27.2.1 (Apple M5 arm64), Python 3.14 (no game files, standard library only):
feat/x64-recomp-muldiv:python3 -I tests/test_x64_recomp_muldiv.py -v-> 4/4 static OK (6 exec skipped without runner)python3 -I tests/test_x64_recomp.py-> 6/6 static OK (26 exec skipped)tools/x64_recomp.pytoupstream/main) -> FAILED (4/4 static failures): 12 unhandled forms (includingimul@103c:list index out of range), 0rt_mkflagscalls, 0asr #63checks, andmulrd clobberingsmulhinputs.python3 -I tests/test_x64_recomp_muldiv.py -v-> 10/10 OK (0 skipped; all 61 hardware cases bit-exact!)python3 -I tests/test_x64_recomp.py-> 46/46 OKpython3 -I tests/test_x64_recomp_sse.py-> 16/16 OKpython3 -I tests/test_x64_recomp_bitops.py-> 9/9 OKpython3 -I tests/test_x64_recomp_rcl_rcr.py-> 5/5 OKpython3 -I tests/test_x64_recomp_std_str.py-> 5/5 OKpython3 -m compileall -q tools/x64_recomp.py tests/test_x64_recomp_muldiv.py-> OKpython3 -I scripts/check_no_game_data.py tools/x64_recomp.py tests/test_x64_recomp_muldiv.py-> OKpython3 -I tests/test_ci_registration.py-> 15/15 OKpython3 -I tests/test_text_write_line_endings.py-> 3/3 OKChecklist
Imported from ubiquitarianism/leonida. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-24T21:14:24Z.
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-26T01:38:38Z.
Independent run with QEMU. Apple M5 Pro, macOS 27.2.1 (26B131), Python 3.14.2, QEMU 11.1.3 from Homebrew. I merged
mainatf1afede5einto the branch locally (934 commits behind, no conflicts). Head tested:fbba8eebd.Against a reference model this branch is right in all 3,679 checks across 8, 16, 32 and 64 bits.
maingets 174 of 1,596 wrong on the forms it can translate.The new test file needs two things before it shows green under QEMU, and neither is the emitters:
xr.run_kernelwherexr.run_resultis needed). As shipped: 10 run, 50 failures, 11 errors. WithRUNNERswapped in memory (no file changed), the 50 failures are gone and 11 timeouts remain;mov r64, imm64, which hangs inAsm.lionmain(the branchelif not nzc: movn(rd, 0)that #1339, #1205 and #1329 each add). With that one branch also patched in memory: 10/10.Beyond the file, generated cases under QEMU, each compared against a small Python model:
mulandimul(f6/f7 /4,/5) at 8, 16, 32, 64 bits: fullraxandrdx(untouched upper bits at 8/16, zero-extension at 32), CF and OF;imul r, r/m(0f af) andimul r, r/m, imm8with-3and0x7fat 16, 32, 64 bits: full destination, CF and OF;divandidiv(/6,/7) at 8, 16, 32, 64 bits with a zero, sign-extended and non-zero high half: quotient and remainder in the full registers; cases that would raise#DEare left out;mainmul/imul(all three forms), 32-bitdiv/idivThe
mainmismatches I printed (the first 30) are all CF/OF after signed one-operandimulwith a negative operand:imulof 1 by0x80000000reports overflow (want CF=OF=0), and0xffffffffby0x80000000reports none (want 1). So the "fix mul/imul CF/OF" half of the title fixes real results onmain.Counts.
tests/test_x64_recomp.py44/44 andtests/test_recomp_census.py11/11, the same asmain. Full standalone suite: 514 run, 511 pass; the 3 that fail aremain's 2 (test_image_load_typed,test_mod_pack_output_alias) plus the new file, for the two reasons above.Not tested: memory operands, the
brkpaths for divide-by-zero and quotient overflow, 64-bitidivwith an all-ones high half, and SF/ZF/AF/PF (undefined after these instructions). No game files used. AI-assisted.Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T19:37:32Z.
Red/green: tests/test_x64_recomp_muldiv.py fails 4 static emitter assertions on main (mul/imul CF/OF and 8/16/64-bit mul/div emission) and passes with the PR (cloud run on main
7daa4a4; execution tests skipped).Targeted batch (PR tests, tests importing the changed modules, docs tests, translator when touched) merged on main
7daa4a4a5in GitHub Actions: no new failure.AI-assisted (Claude Code, Claude Opus 5.5)