x64_recomp: add 8/16-bit adc/sbb, fix Asm.csinv encoding, sign-extend imm, and stash AF (X64-RECOMP-ADCSBB-NARROW) #1329

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

@⁠IlyassRS3 on GitHub · 2026-11-24 21:57 UTC · original
IlyassRS3:feat/x64-recomp-adcsbb-narrow → main · +280 −5 · 2 files · 1 commits · open

Item ID: X64-RECOMP-ADCSBB-NARROW
AI-assisted: yes

Summary

Implements 8-bit and 16-bit adc/sbb and fixes three bugs uncovered while testing _emit_adcsbb on real arm64 hardware in tools/x64_recomp.py:

  1. Fixed Asm.csinv (csetm) encoding bug (0x9A808000 -> 0xDA800000):
    • In ARMv8, CSINV sets bit 30 (op=1, base 0xDA800000 / 32-bit 0x5A800000), not bit 15 (0x9A808000, which set cond[3] inside a CSEL opcode so csetm always returned 0). Verified bit-exact against clang -arch arm64 / otool (csetm w2, hs = 0x5A9F33E2, csetm w2, lo = 0x5A9F23E2, csetm x2, eq = 0xDA9F13E2).
  2. Implemented 8-bit and 16-bit adc and sbb (replacing UnhandledInsn("adc/sbb size 1/2")):
    • Shifts T0 and T1 left by sh = 32 - sz * 8 (24 or 16), fills the low sh bits of T1 with 1s (csetm + lsr sz*8 + orr_r, preserving ARM C) iff x86 CF=1, runs adcs/sbcs, and shifts T0 right by sh.
    • Exhaustively verified mathematically over all 262,144 (dst, src, cf) 8-bit adc + sbb inputs for exact result, SF, ZF, CF, and OF, and verified on Apple M5 arm64 hardware across reg/hi8/mem/imm and chained 16-bit multiword arithmetic.
  3. Sign-extended narrow immediates to destination width in _emit_adcsbb (and Asm.li(-1)):
    • Passed as_size=sz to self._load(ins, src, T1, as_size=sz) so adc ax, -1 (66 83 d0 ff), adc rax, -1 (48 83 d0 ff), and sbb eax, -1 (83 d8 ff) sign-extend instead of loading +0xFF.
    • Added the elif not nzc: self.movn(rd, 0) guard in Asm.li (matching #1205) so -1 materializes cleanly.
  4. Stashed CTX_A/CTX_B for AF on all adc/sbb widths:
    • Because (dst ^ src ^ (dst ± src ± cf)) & 0x10 equals the exact nibble carry/borrow even with carry-in, stashing unshifted T0/T1 when "A" in need or ins.addr in self.mat gives exact AF across 8/16/32/64-bit.

Merge compatibility

  • Keeps lines 1736–1750 inside _emit_adcsbb untouched (zero overlap with #1202), uses identical Asm.li(-1) lines as #1205 (auto-deduplicated), and leaves #1306/#1205 Asm methods untouched.
  • Verified 0 merge conflicts in an 11-way merge with #1174, #1202, #1205, #1211, #1213, #1279, #1296, #1306, #1313, and #1321.

Verification

Tested on Apple M5 arm64 (macOS 27.2.1, Python 3.14; CPU-only, no game files):

  • python3 -I tests/test_x64_recomp.py — 32 tests (6 ok, 26 skipped without runner)
  • python3 -I tests/test_x64_recomp_adcsbb.py -v — 7 tests (4 static ok, 3 exec skipped without runner)
  • Mutation proof: reverting tools/x64_recomp.py to upstream/main fails all static tests in tests/test_x64_recomp_adcsbb.py.
  • Native Apple M5 execution (scratch 11-way merge with #1174, #1202, #1205, #1211, #1213, #1279, #1296, #1306, #1313, #1321 — 0 merge conflicts):
    • tests/test_x64_recomp_adcsbb.py — 7/7 OK (all 3 execution suites + 4 static tests, 0 skips)
    • tests/test_x64_recomp.py — 46/46 OK
    • tests/test_x64_recomp_sse.py — 16/16 OK
    • tests/test_x64_recomp_bitops.py — 9/9 OK
    • tests/test_x64_recomp_rcl_rcr.py — 5/5 OK
    • tests/test_x64_recomp_std_str.py — 5/5 OK
    • tests/test_x64_recomp_muldiv.py — 10/10 OK
    • tests/test_x64_recomp_shifts.py — 9/9 OK
    • tests/test_x64_recomp_atomics.py — 10/10 OK
    • tests/test_x64_recomp_incdec.py — 8/8 OK
    • tests/test_x64_recomp_control_conv.py — 9/9 OK
    • Total: 134/134 native arm64 tests passing.
  • python3 -m compileall -q tools/x64_recomp.py tests/test_x64_recomp_adcsbb.py — OK
  • python3 -I scripts/check_no_game_data.py tools/x64_recomp.py tests/test_x64_recomp_adcsbb.py — OK
  • python3 -I tests/test_ci_registration.py — 15/15 OK
  • python3 -I tests/test_text_write_line_endings.py — 3/3 OK
**@⁠IlyassRS3** on GitHub · 2026-11-24 21:57 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1329) `IlyassRS3:feat/x64-recomp-adcsbb-narrow` → `main` · +280 −5 · 2 files · 1 commits · open Item ID: `X64-RECOMP-ADCSBB-NARROW` AI-assisted: yes ## Summary Implements 8-bit and 16-bit `adc`/`sbb` and fixes three bugs uncovered while testing `_emit_adcsbb` on real arm64 hardware in `tools/x64_recomp.py`: 1. **Fixed `Asm.csinv` (`csetm`) encoding bug (`0x9A808000` -> `0xDA800000`)**: - In ARMv8, `CSINV` sets bit 30 (`op=1`, base `0xDA800000` / 32-bit `0x5A800000`), not bit 15 (`0x9A808000`, which set `cond[3]` inside a `CSEL` opcode so `csetm` always returned `0`). Verified bit-exact against `clang -arch arm64` / `otool` (`csetm w2, hs` = `0x5A9F33E2`, `csetm w2, lo` = `0x5A9F23E2`, `csetm x2, eq` = `0xDA9F13E2`). 2. **Implemented 8-bit and 16-bit `adc` and `sbb` (replacing `UnhandledInsn("adc/sbb size 1/2")`)**: - Shifts `T0` and `T1` left by `sh = 32 - sz * 8` (`24` or `16`), fills the low `sh` bits of `T1` with `1`s (`csetm` + `lsr sz*8` + `orr_r`, preserving ARM `C`) iff x86 `CF=1`, runs `adcs`/`sbcs`, and shifts `T0` right by `sh`. - Exhaustively verified mathematically over all 262,144 `(dst, src, cf)` 8-bit `adc` + `sbb` inputs for exact result, `SF`, `ZF`, `CF`, and `OF`, and verified on Apple M5 arm64 hardware across reg/hi8/mem/imm and chained 16-bit multiword arithmetic. 3. **Sign-extended narrow immediates to destination width in `_emit_adcsbb` (and `Asm.li(-1)`)**: - Passed `as_size=sz` to `self._load(ins, src, T1, as_size=sz)` so `adc ax, -1` (`66 83 d0 ff`), `adc rax, -1` (`48 83 d0 ff`), and `sbb eax, -1` (`83 d8 ff`) sign-extend instead of loading `+0xFF`. - Added the `elif not nzc: self.movn(rd, 0)` guard in `Asm.li` (matching `#1205`) so `-1` materializes cleanly. 4. **Stashed `CTX_A`/`CTX_B` for `AF` on all `adc`/`sbb` widths**: - Because `(dst ^ src ^ (dst ± src ± cf)) & 0x10` equals the exact nibble carry/borrow even with carry-in, stashing unshifted `T0`/`T1` when `"A" in need or ins.addr in self.mat` gives exact `AF` across 8/16/32/64-bit. ## Merge compatibility - Keeps lines 1736–1750 inside `_emit_adcsbb` untouched (zero overlap with `#1202`), uses identical `Asm.li(-1)` lines as `#1205` (auto-deduplicated), and leaves `#1306`/`#1205` `Asm` methods untouched. - Verified **0 merge conflicts** in an 11-way merge with `#1174`, `#1202`, `#1205`, `#1211`, `#1213`, `#1279`, `#1296`, `#1306`, `#1313`, and `#1321`. ## Verification Tested on Apple M5 arm64 (macOS 27.2.1, Python 3.14; CPU-only, no game files): - `python3 -I tests/test_x64_recomp.py` — 32 tests (6 ok, 26 skipped without runner) - `python3 -I tests/test_x64_recomp_adcsbb.py -v` — 7 tests (4 static ok, 3 exec skipped without runner) - Mutation proof: reverting `tools/x64_recomp.py` to `upstream/main` fails all static tests in `tests/test_x64_recomp_adcsbb.py`. - Native Apple M5 execution (scratch 11-way merge with `#1174`, `#1202`, `#1205`, `#1211`, `#1213`, `#1279`, `#1296`, `#1306`, `#1313`, `#1321` — 0 merge conflicts): - `tests/test_x64_recomp_adcsbb.py` — **7/7 OK** (all 3 execution suites + 4 static tests, 0 skips) - `tests/test_x64_recomp.py` — **46/46 OK** - `tests/test_x64_recomp_sse.py` — **16/16 OK** - `tests/test_x64_recomp_bitops.py` — **9/9 OK** - `tests/test_x64_recomp_rcl_rcr.py` — **5/5 OK** - `tests/test_x64_recomp_std_str.py` — **5/5 OK** - `tests/test_x64_recomp_muldiv.py` — **10/10 OK** - `tests/test_x64_recomp_shifts.py` — **9/9 OK** - `tests/test_x64_recomp_atomics.py` — **10/10 OK** - `tests/test_x64_recomp_incdec.py` — **8/8 OK** - `tests/test_x64_recomp_control_conv.py` — **9/9 OK** - Total: **134/134 native arm64 tests passing**. - `python3 -m compileall -q tools/x64_recomp.py tests/test_x64_recomp_adcsbb.py` — OK - `python3 -I scripts/check_no_game_data.py tools/x64_recomp.py tests/test_x64_recomp_adcsbb.py` — OK - `python3 -I tests/test_ci_registration.py` — 15/15 OK - `python3 -I tests/test_text_write_line_endings.py` — 3/3 OK
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:47:36 +00:00
Author
Owner

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

@⁠whoalin1 on GitHub · 2026-11-24 23:25 UTC · original

Independent verification (Devin, AI-assisted — CPU-only, no GPU).

Checked out head efd3645 detached. Diff: tools/x64_recomp.py + tests/test_x64_recomp_adcsbb.py.

Semantics reviewed:

  • Asm.csinv: 0x9A808000 → 0xDA800000 is correct — CSINV64 is sf=1,op=1,S=0 → 0xDA800000; 32-bit form becomes 0x5A800000 via _csel's -0x80000000. Old value was a different/unallocated encoding. csetm (used by the new adc/sbb path) now emits a real CSINV.
  • li(): added movn(rd,0) path when only zero chunks — correct all-ones materialization.
  • adc/sbb 8/16-bit: removed the UnhandledInsn; operands widened by sh=32-w*8, addend low bits filled via csetm+lsr conditioned on x86 CF direction (adc wants C, sbb wants !C via the earlier eor), so ADCS/SBCS carry propagates into bit sh and N/Z/C/V land on the right bit positions. AF stash (CTX_A/B) added when needed. Traced the widening math for sz=1/2 — carry/Z behavior checks out.

Ran:

  • py -3.14 -I tests/test_x64_recomp_adcsbb.py → OK (4 passed + subtests, 3 skipped: native-exec needs Apple Silicon/QEMU)
  • pytest tests/test_x64_recomp_adcsbb.py → 4 passed, 3 skipped
  • py -3.14 -m compileall -q tools tests make_game.py → clean

Verdict: encoding fix verified against ARM64 CSINV layout; tests pass. Execution coverage skipped on this host. Base is main; overlaps tools/x64_recomp.py with #1313/#1321/#1339 but in different functions — will need rebases whichever lands second.

**@⁠whoalin1** on GitHub · 2026-11-24 23:25 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1329#issuecomment-7147387220) Independent verification (Devin, AI-assisted — CPU-only, no GPU). Checked out head efd3645 detached. Diff: tools/x64_recomp.py + tests/test_x64_recomp_adcsbb.py. Semantics reviewed: - Asm.csinv: 0x9A808000 → 0xDA800000 is correct — CSINV64 is sf=1,op=1,S=0 → 0xDA800000; 32-bit form becomes 0x5A800000 via _csel's -0x80000000. Old value was a different/unallocated encoding. csetm (used by the new adc/sbb path) now emits a real CSINV. - li(): added movn(rd,0) path when only zero chunks — correct all-ones materialization. - adc/sbb 8/16-bit: removed the UnhandledInsn; operands widened by sh=32-w*8, addend low bits filled via csetm+lsr conditioned on x86 CF direction (adc wants C, sbb wants !C via the earlier eor), so ADCS/SBCS carry propagates into bit sh and N/Z/C/V land on the right bit positions. AF stash (CTX_A/B) added when needed. Traced the widening math for sz=1/2 — carry/Z behavior checks out. Ran: - `py -3.14 -I tests/test_x64_recomp_adcsbb.py` → OK (4 passed + subtests, 3 skipped: native-exec needs Apple Silicon/QEMU) - `pytest tests/test_x64_recomp_adcsbb.py` → 4 passed, 3 skipped - `py -3.14 -m compileall -q tools tests make_game.py` → clean Verdict: encoding fix verified against ARM64 CSINV layout; tests pass. Execution coverage skipped on this host. Base is main; overlaps tools/x64_recomp.py with #1313/#1321/#1339 but in different functions — will need rebases whichever lands second.
Author
Owner

@⁠kevincommits on GitHub · 2026-11-26 01:18 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 (916 commits behind, no conflicts). Head tested: efd36453d.

Against a reference model this branch is right in all 3,364 checks, across all four operand widths. main cannot run the 8- and 16-bit forms at all, and gets 54 of 1,572 wrong at 32 and 64 bits.

  • The new test file has the wiring bug from #1409: as shipped under QEMU, python -I tests/test_x64_recomp_adcsbb.py is 7 run, 43 failures, because _runner's QEMU branch returns xr.run_kernel (raw bytes) where xr.run_result (the parsed value) is needed. With RUNNER swapped for xr.run_result in memory (no file changed): 7/7.

  • Beyond the file, generated cases under QEMU, each compared against a small Python model:

    • adc and sbb, register-register (10/11, 18/19) and sign-extended imm8 (80 /2, 83 /2, /3) forms;
    • operands 0, 1, the largest positive value, the most negative value, all-ones minus one, and all-ones at the narrow widths;
    • carry-in from stc, clc, a borrowing cmp and a non-borrowing cmp;
    • checked per case: the full 64-bit register (untouched upper bits, zero-extension of a 32-bit write), CF, ZF, SF and OF.
    tool widths checks wrong
    this branch 8, 16, 32, 64 3,364 0
    main 32, 64 1,572 54
    this branch 32, 64 1,572 0

    main raises UnhandledInsn for the 8- and 16-bit forms, so they cannot be run there. Its 54 wrong at 32 and 64 bits are all the imm8 form with a negative immediate: adc eax, -1 adds 0xff (eax=0, CF=1 gives 0x100, want 0, and CF comes back 0, want 1). So the "sign-extend imm" part of this change fixes real results on main, not only the narrow forms.

  • 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 wiring reason.

  • Overlap. The Asm.li(-1) change here is also in #1339 and #1205, and #1339 changes immediate sign-extension in _emit_alu as well; whichever lands later will need to reconcile. I did not test them together.

Not tested: memory destinations, AF and PF, and multi-word chains beyond the file's own test. No game files used. AI-assisted.

**@⁠kevincommits** on GitHub · 2026-11-26 01:18 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1329#issuecomment-7179641896) 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 (916 commits behind, no conflicts). Head tested: `efd36453d`. **Against a reference model this branch is right in all 3,364 checks, across all four operand widths. `main` cannot run the 8- and 16-bit forms at all, and gets 54 of 1,572 wrong at 32 and 64 bits.** - The new test file has the wiring bug from #1409: as shipped under QEMU, `python -I tests/test_x64_recomp_adcsbb.py` is 7 run, 43 failures, because `_runner`'s QEMU branch returns `xr.run_kernel` (raw bytes) where `xr.run_result` (the parsed value) is needed. With `RUNNER` swapped for `xr.run_result` in memory (no file changed): 7/7. - Beyond the file, generated cases under QEMU, each compared against a small Python model: - `adc` and `sbb`, register-register (`10`/`11`, `18`/`19`) and sign-extended `imm8` (`80 /2`, `83 /2`, `/3`) forms; - operands 0, 1, the largest positive value, the most negative value, all-ones minus one, and all-ones at the narrow widths; - carry-in from `stc`, `clc`, a borrowing `cmp` and a non-borrowing `cmp`; - checked per case: the full 64-bit register (untouched upper bits, zero-extension of a 32-bit write), CF, ZF, SF and OF. | tool | widths | checks | wrong | |---|---|---|---| | this branch | 8, 16, 32, 64 | 3,364 | 0 | | `main` | 32, 64 | 1,572 | 54 | | this branch | 32, 64 | 1,572 | 0 | `main` raises `UnhandledInsn` for the 8- and 16-bit forms, so they cannot be run there. Its 54 wrong at 32 and 64 bits are all the `imm8` form with a negative immediate: `adc eax, -1` adds `0xff` (eax=0, CF=1 gives `0x100`, want 0, and CF comes back 0, want 1). So the "sign-extend imm" part of this change fixes real results on `main`, not only the narrow forms. - **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 wiring reason. - **Overlap.** The `Asm.li(-1)` change here is also in #1339 and #1205, and #1339 changes immediate sign-extension in `_emit_alu` as well; whichever lands later will need to reconcile. I did not test them together. Not tested: memory destinations, AF and PF, and multi-word chains beyond the file's own test. 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/1329/head:github-bot/gh-1329'
git switch 'github-bot/gh-1329'

Merge

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