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

Open
opened 2026-11-26 20:20:58 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1329
Author: @IlyassRS3
Created: 11/24/2026
Status: 🔄 Open

Base: main ← Head: feat/x64-recomp-adcsbb-narrow


📝 Commits (1)

  • efd3645 x64_recomp: add 8/16-bit adc/sbb, fix Asm.csinv encoding, sign-extend imm, and stash AF

📊 Changes

2 files changed (+280 additions, -5 deletions)

View changed files

➕ tests/test_x64_recomp_adcsbb.py (+260 -0)
📝 tools/x64_recomp.py (+20 -5)

📄 Description

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

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/1329 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/24/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `feat/x64-recomp-adcsbb-narrow` --- ### 📝 Commits (1) - [`efd3645`](https://github.com/ubiquitarianism/leonida/commit/efd36453d47925220d6403d3461ea532e5c7d83d) x64_recomp: add 8/16-bit adc/sbb, fix Asm.csinv encoding, sign-extend imm, and stash AF ### 📊 Changes **2 files changed** (+280 additions, -5 deletions) <details> <summary>View changed files</summary> ➕ `tests/test_x64_recomp_adcsbb.py` (+260 -0) 📝 `tools/x64_recomp.py` (+20 -5) </details> ### 📄 Description 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 --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
starred/leonida#1359
No description provided.