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

Open
github-bot wants to merge 1 commit from github-bot/gh-1279 into main AGit
Owner

@⁠IlyassRS3 on GitHub · 2026-11-24 21:14 UTC · original
IlyassRS3:feat/x64-recomp-muldiv → main · +734 −50 · 2 files · 1 commits · open

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):

  • Standalone on 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)
    • Revert-source mutation check (reverting tools/x64_recomp.py to upstream/main) -> FAILED (4/4 static failures): 12 unhandled forms (including imul@103c:list index out of range), 0 rt_mkflags calls, 0 asr #63 checks, and mul rd clobbering smulh inputs.
  • Native Apple M5 silicon execution (6-way scratch merge with #1174 + #1202 + #1205 + #1211 + #1213, 0 merge conflicts):
    • 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 OK
    • python3 -I tests/test_x64_recomp_sse.py -> 16/16 OK
    • python3 -I tests/test_x64_recomp_bitops.py -> 9/9 OK
    • python3 -I tests/test_x64_recomp_rcl_rcr.py -> 5/5 OK
    • python3 -I tests/test_x64_recomp_std_str.py -> 5/5 OK
    • Total: 91/91 native Apple Silicon tests OK.
  • Hygiene:
    • python3 -m compileall -q tools/x64_recomp.py tests/test_x64_recomp_muldiv.py -> OK
    • python3 -I scripts/check_no_game_data.py tools/x64_recomp.py tests/test_x64_recomp_muldiv.py -> OK
    • python3 -I tests/test_ci_registration.py -> 15/15 OK
    • python3 -I tests/test_text_write_line_endings.py -> 3/3 OK

Checklist

  • No game files, binaries, shaders, textures, audio, or logs quoting them
  • No keys, decryption, or online services
  • Claimed in the coordination issue (#14) before opening this PR
  • AI-assisted: yes (implemented and verified natively on Apple M5 arm64 with AI assistance)
**@⁠IlyassRS3** on GitHub · 2026-11-24 21:14 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1279) `IlyassRS3:feat/x64-recomp-muldiv` → `main` · +734 −50 · 2 files · 1 commits · open ## 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 - Coordination issue #14 item: `X64-RECOMP-MULDIV` - Depends on: none (edits only inside `_emit_mul`; verified zero conflicts in `tools/x64_recomp.py` against #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): - Standalone on `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) - Revert-source mutation check (reverting `tools/x64_recomp.py` to `upstream/main`) -> **FAILED (4/4 static failures)**: 12 unhandled forms (including `imul@103c:list index out of range`), 0 `rt_mkflags` calls, 0 `asr #63` checks, and `mul` rd clobbering `smulh` inputs. - Native Apple M5 silicon execution (6-way scratch merge with #1174 + #1202 + #1205 + #1211 + #1213, **0 merge conflicts**): - `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 OK** - `python3 -I tests/test_x64_recomp_sse.py` -> **16/16 OK** - `python3 -I tests/test_x64_recomp_bitops.py` -> **9/9 OK** - `python3 -I tests/test_x64_recomp_rcl_rcr.py` -> **5/5 OK** - `python3 -I tests/test_x64_recomp_std_str.py` -> **5/5 OK** - Total: **91/91 native Apple Silicon tests OK**. - Hygiene: - `python3 -m compileall -q tools/x64_recomp.py tests/test_x64_recomp_muldiv.py` -> OK - `python3 -I scripts/check_no_game_data.py tools/x64_recomp.py tests/test_x64_recomp_muldiv.py` -> OK - `python3 -I tests/test_ci_registration.py` -> **15/15 OK** - `python3 -I tests/test_text_write_line_endings.py` -> **3/3 OK** ## Checklist - [x] No game files, binaries, shaders, textures, audio, or logs quoting them - [x] No keys, decryption, or online services - [x] Claimed in the coordination issue (#14) before opening this PR - [x] AI-assisted: yes (implemented and verified natively on Apple M5 arm64 with AI assistance)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:45:39 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-24 21:14 UTC · original · reviewed

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.

**@⁠copilot-pull-request-reviewer[bot]** on GitHub · 2026-11-24 21:14 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1279#pullrequestreview-6047517829) · **reviewed** 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.
Author
Owner

@⁠kevincommits on GitHub · 2026-11-26 01:38 UTC · original

Independent run with QEMU. Apple M5 Pro, macOS 27.2.1 (26B131), Python 3.14.2, QEMU 11.1.3 from Homebrew. I merged main at f1afede5e into 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. main gets 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:

    • the wiring bug from #1409 (xr.run_kernel where xr.run_result is needed). As shipped: 10 run, 50 failures, 11 errors. With RUNNER swapped in memory (no file changed), the 50 failures are gone and 11 timeouts remain;
    • those 11 all load a 64-bit all-ones constant with mov r64, imm64, which hangs in Asm.li on main (the branch elif 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:

    • one-operand mul and imul (f6/f7 /4, /5) at 8, 16, 32, 64 bits: full rax and rdx (untouched upper bits at 8/16, zero-extension at 32), CF and OF;
    • imul r, r/m (0f af) and imul r, r/m, imm8 with -3 and 0x7f at 16, 32, 64 bits: full destination, CF and OF;
    • div and idiv (/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 #DE are left out;
    • operands 0, 1, 7, the largest positive value, the most negative value, all-ones minus one, and all-ones at the narrow widths.
    tool forms checks wrong
    this branch everything above 3,679 0
    main 32/64-bit mul/imul (all three forms), 32-bit div/idiv 1,596 174
    this branch same subset 1,596 0

    The main mismatches I printed (the first 30) are all CF/OF after signed one-operand imul with a negative operand: imul of 1 by 0x80000000 reports overflow (want CF=OF=0), and 0xffffffff by 0x80000000 reports none (want 1). So the "fix mul/imul CF/OF" half of the title fixes real results on main.

  • Counts. tests/test_x64_recomp.py 44/44 and tests/test_recomp_census.py 11/11, the same as main. Full standalone suite: 514 run, 511 pass; the 3 that fail are main'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 brk paths for divide-by-zero and quotient overflow, 64-bit idiv with an all-ones high half, and SF/ZF/AF/PF (undefined after these instructions). No game files used. AI-assisted.

**@⁠kevincommits** on GitHub · 2026-11-26 01:38 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1279#issuecomment-7179979250) Independent run with QEMU. Apple M5 Pro, macOS 27.2.1 (26B131), Python 3.14.2, QEMU 11.1.3 from Homebrew. I merged `main` at `f1afede5e` into 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. `main` gets 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: - the wiring bug from #1409 (`xr.run_kernel` where `xr.run_result` is needed). As shipped: 10 run, 50 failures, 11 errors. With `RUNNER` swapped in memory (no file changed), the 50 failures are gone and 11 timeouts remain; - those 11 all load a 64-bit all-ones constant with `mov r64, imm64`, which hangs in `Asm.li` on `main` (the branch `elif 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: - one-operand `mul` and `imul` (`f6`/`f7 /4`, `/5`) at 8, 16, 32, 64 bits: full `rax` and `rdx` (untouched upper bits at 8/16, zero-extension at 32), CF and OF; - `imul r, r/m` (`0f af`) and `imul r, r/m, imm8` with `-3` and `0x7f` at 16, 32, 64 bits: full destination, CF and OF; - `div` and `idiv` (`/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 `#DE` are left out; - operands 0, 1, 7, the largest positive value, the most negative value, all-ones minus one, and all-ones at the narrow widths. | tool | forms | checks | wrong | |---|---|---|---| | this branch | everything above | 3,679 | 0 | | `main` | 32/64-bit `mul`/`imul` (all three forms), 32-bit `div`/`idiv` | 1,596 | 174 | | this branch | same subset | 1,596 | 0 | The `main` mismatches I printed (the first 30) are all CF/OF after signed one-operand `imul` with a negative operand: `imul` of 1 by `0x80000000` reports overflow (want CF=OF=0), and `0xffffffff` by `0x80000000` reports none (want 1). So the "fix mul/imul CF/OF" half of the title fixes real results on `main`. - **Counts.** `tests/test_x64_recomp.py` 44/44 and `tests/test_recomp_census.py` 11/11, the same as `main`. Full standalone suite: 514 run, 511 pass; the 3 that fail are `main`'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 `brk` paths for divide-by-zero and quotient overflow, 64-bit `idiv` with an all-ones high half, and SF/ZF/AF/PF (undefined after these instructions). No game files used. AI-assisted.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1279/head:github-bot/gh-1279'
git switch 'github-bot/gh-1279'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1279'
git switch 'github-bot/gh-1279'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1279'
git switch 'github-bot/gh-1279'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1279'
git switch 'main'
git merge --squash 'github-bot/gh-1279'
git switch 'main'
git merge --ff-only 'github-bot/gh-1279'
git switch 'main'
git merge 'github-bot/gh-1279'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.