Skip to content

x64_recomp: fix mul/imul CF/OF bugs and add 8/16/64-bit mul/div/idiv emitters (X64-RECOMP-MULDIV) - #1279

Merged
MoonEyes merged 1 commit into
ProjectLeonida:mainfrom
quietfold:feat/x64-recomp-muldiv
Nov 28, 2026
Merged

MoonEyes merged 1 commit into
ProjectLeonida:mainfrom
quietfold:feat/x64-recomp-muldiv

Conversation

@quietfold

Copy link
Copy Markdown
Contributor

What changed

Fixes four CF/OF correctness bugs in FunctionTranslator._emit_mul (tools/x64_recomp.py) and completes width coverage for mul/imul/div/idiv across 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)

  1. 2-/3-operand imul clobbered T0 before overflow check: a.mul(T0, T1, T0, sf) overwrote T0 with the product before smulh(T2, T1, T0) / smaddl(T2, T1, T0, 31), computing T1 * (T1 * src) instead of T1 * src (e.g. 100000 * 10000 = 10^9 falsely set CF=OF=1). Fixed by placing the product in T2 (and computing 32-bit products directly with smaddl + sxtw comparison). Also avoids Asm.li(rd, -1) on 64-bit imul r64, r/m64, -1 via movn.
  2. 1-operand imul checked unsigned zero instead of sign extension: checked rdx != 0 / edx != 0 for both mul and imul, wrongly setting CF=OF=1 on every negative non-overflowing signed product (e.g. -2 * 3 = -6, edx = -1). Signed imul now compares the high half against asr(low, width - 1).
  3. 16-bit 2-/3-operand imul: used 32-bit smaddl and bit-31 sign extension instead of sxth * sxth and 16-bit signed-fit comparison.
  4. Cross-block CF/OF materialization: neither 2-/3-operand imul nor 1-operand mul/imul called self._maybe_mkflags(ins) when ins.addr in self.mat, leaving stale CTX_EFLAGS for cross-block jo/jno/jc/jnc.

New width emitters in _emit_mul

  • 8-bit mul/imul (al * r/m8 -> ax) and 16-bit mul/imul (ax * r/m16 -> dx:ax), preserving upper 48 bits of rax/rdx via bfi and setting exact unsigned/signed CF=OF.
  • 8-bit div/idiv (ax / r/m8 -> al, ah) and 16-bit div/idiv (dx:ax / r/m16 -> ax, dx), preserving upper 48 bits of rax/rdx, with divide-by-zero (brk 0xD1) and quotient-overflow (brk 0xD2) traps.
  • 32-bit div/idiv adds quotient-range overflow trapping (brk 0xD2) alongside divide-by-zero.
  • 64-bit div/idiv (rdx:rax / r/m64 -> rax, rdx):
    • Fast path (udiv + msub when rdx == 0; sdiv + msub when rdx == (rax >> 63) and divisor != -1).
    • Full 128-by-64 unsigned and signed restoring-division slow paths (handling 65th-bit carry-out on near-UINT64_MAX divisors, exact INT64_MIN / -2^127 two's-complement negation via subs_r/sbcs, (+2^63) / (-1) = -2^63, and exact signed/unsigned #DE overflow traps).
  • Adds 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 when CAN_RUN_MACHO or QEMU is available).

Item ID

Verification

Tested on macOS 27.2.1 (Apple M5 arm64), Python 3.14 (no game files, standard library only):

Checklist

  • No game files, binaries, shaders, textures, audio, or logs quoting them
  • No keys, decryption, or online services
  • Claimed in the coordination issue (Coordinating who works on what #14) before opening this PR
  • AI-assisted: yes (implemented and verified natively on Apple M5 arm64 with AI assistance)

Copilot AI balanced review requested due to automatic review settings November 24, 2026 21:14

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@MoonEyes

Copy link
Copy Markdown
Collaborator

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 7daa4a4 in GitHub Actions: no new failure.
AI-assisted (Claude Code, Claude Opus 5.5)

@MoonEyes
MoonEyes merged commit 23ff0b9 into ProjectLeonida:main Nov 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants