x64_recomp: add 8/16-bit adc/sbb, fix Asm.csinv encoding, sign-extend imm, and stash AF (X64-RECOMP-ADCSBB-NARROW) #1329
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1329"
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:57:12Z.
Item ID:
X64-RECOMP-ADCSBB-NARROWAI-assisted: yes
Summary
Implements 8-bit and 16-bit
adc/sbband fixes three bugs uncovered while testing_emit_adcsbbon real arm64 hardware intools/x64_recomp.py:Asm.csinv(csetm) encoding bug (0x9A808000->0xDA800000):CSINVsets bit 30 (op=1, base0xDA800000/ 32-bit0x5A800000), not bit 15 (0x9A808000, which setcond[3]inside aCSELopcode socsetmalways returned0). Verified bit-exact againstclang -arch arm64/otool(csetm w2, hs=0x5A9F33E2,csetm w2, lo=0x5A9F23E2,csetm x2, eq=0xDA9F13E2).adcandsbb(replacingUnhandledInsn("adc/sbb size 1/2")):T0andT1left bysh = 32 - sz * 8(24or16), fills the lowshbits ofT1with1s (csetm+lsr sz*8+orr_r, preserving ARMC) iff x86CF=1, runsadcs/sbcs, and shiftsT0right bysh.(dst, src, cf)8-bitadc+sbbinputs for exact result,SF,ZF,CF, andOF, and verified on Apple M5 arm64 hardware across reg/hi8/mem/imm and chained 16-bit multiword arithmetic._emit_adcsbb(andAsm.li(-1)):as_size=sztoself._load(ins, src, T1, as_size=sz)soadc ax, -1(66 83 d0 ff),adc rax, -1(48 83 d0 ff), andsbb eax, -1(83 d8 ff) sign-extend instead of loading+0xFF.elif not nzc: self.movn(rd, 0)guard inAsm.li(matching#1205) so-1materializes cleanly.CTX_A/CTX_BforAFon alladc/sbbwidths:(dst ^ src ^ (dst ± src ± cf)) & 0x10equals the exact nibble carry/borrow even with carry-in, stashing unshiftedT0/T1when"A" in need or ins.addr in self.matgives exactAFacross 8/16/32/64-bit.Merge compatibility
_emit_adcsbbuntouched (zero overlap with#1202), uses identicalAsm.li(-1)lines as#1205(auto-deduplicated), and leaves#1306/#1205Asmmethods untouched.#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)tools/x64_recomp.pytoupstream/mainfails all static tests intests/test_x64_recomp_adcsbb.py.#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 OKtests/test_x64_recomp_sse.py— 16/16 OKtests/test_x64_recomp_bitops.py— 9/9 OKtests/test_x64_recomp_rcl_rcr.py— 5/5 OKtests/test_x64_recomp_std_str.py— 5/5 OKtests/test_x64_recomp_muldiv.py— 10/10 OKtests/test_x64_recomp_shifts.py— 9/9 OKtests/test_x64_recomp_atomics.py— 10/10 OKtests/test_x64_recomp_incdec.py— 8/8 OKtests/test_x64_recomp_control_conv.py— 9/9 OKpython3 -m compileall -q tools/x64_recomp.py tests/test_x64_recomp_adcsbb.py— OKpython3 -I scripts/check_no_game_data.py tools/x64_recomp.py tests/test_x64_recomp_adcsbb.py— OKpython3 -I tests/test_ci_registration.py— 15/15 OKpython3 -I tests/test_text_write_line_endings.py— 3/3 OKImported from ubiquitarianism/leonida. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-24T21:57:21Z.
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 whoalin1 on 2026-11-24T23:25:40Z.
Independent verification (Devin, AI-assisted — CPU-only, no GPU).
Checked out head
efd3645detached. Diff: tools/x64_recomp.py + tests/test_x64_recomp_adcsbb.py.Semantics reviewed:
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 skippedpy -3.14 -m compileall -q tools tests make_game.py→ cleanVerdict: 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.
Imported from ubiquitarianism/leonida. Originally posted by kevincommits on 2026-11-26T01:18:26Z.
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 (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.
maincannot 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.pyis 7 run, 43 failures, because_runner's QEMU branch returnsxr.run_kernel(raw bytes) wherexr.run_result(the parsed value) is needed. WithRUNNERswapped forxr.run_resultin memory (no file changed): 7/7.Beyond the file, generated cases under QEMU, each compared against a small Python model:
adcandsbb, register-register (10/11,18/19) and sign-extendedimm8(80 /2,83 /2,/3) forms;stc,clc, a borrowingcmpand a non-borrowingcmp;mainmainraisesUnhandledInsnfor the 8- and 16-bit forms, so they cannot be run there. Its 54 wrong at 32 and 64 bits are all theimm8form with a negative immediate:adc eax, -1adds0xff(eax=0, CF=1 gives0x100, want 0, and CF comes back 0, want 1). So the "sign-extend imm" part of this change fixes real results onmain, not only the narrow forms.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 wiring reason.Overlap. The
Asm.li(-1)change here is also in #1339 and #1205, and #1339 changes immediate sign-extension in_emit_aluas 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.
Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-11-28T23:19:16Z.
#1329 | head
efd36453d4(merged cleanly with mainadfb3821a2) |python -I tests/test_x64_recomp_adcsbb.py -v+ exhaustive262,144-case 8-bit &900-case 16-bit ARM64ADCS/SBCSwidening proof +python -I tests/test_x64_recomp.py+python -I tests/test_recomp_census.py| red on main:FAILED (failures=7, skipped=3)(Asm.csinvemitted0x1A8083E2instead of0x5A9F33E2, 8/16-bitadc/sbbraisedUnhandledInsn,adc rax, -1emittedMOVZ X1, #0xFFinstead ofMOVN X1, #0, andadc/sbbomittedCTX_A/CTX_BAF stashes) | green on PR+main:7 ran, OK (skipped=3)(test_x64_recomp_adcsbb.py— all 4 static encoding, sign-extension, and AF-stash checks pass; QEMU/Mach-O exec tests skipped on Windows x64 withoutqemu-system-aarch64),262,144/262,1448-bit(dst8, src8, cf)combinations +900/90016-bit boundary pairs matched x86 result andSF/ZF/CF/OF(0mismatches),45 ran, OK (skipped=32)(test_x64_recomp.py), and11 ran, OK(test_recomp_census.py) | Windows 11 (10.0.26200.9974), Python 3.12.10 | AI-assisted: yesAudited
efd36453d4(tools/x64_recomp.pyandtests/test_x64_recomp_adcsbb.py) merged cleanly ontomain(adfb3821a2) on Windows 11:256 * 256 * 2 * 2 = 262,144adc/sbbinputs) plus90016-bit boundary cases that widening bysh = 32 - sz * 8and filling the lowshbits ofT1viacsetm+lsrconditioned oncs/ccproduces bit-exact narrow results, clean zero low bits, and exactN, Z, C, Vflags.Asm.li(-1)hunk is now already onmain(adfb3821a2) with zero conflict, and a combined 3-way merge of#1213+#1306+#1329ontomain(adfb3821a2) merges cleanly and passes all fivex64_recomp/recomp_censustest suites (plusscripts/check_no_game_data.py).tests/test_x64_recomp_adcsbb.pyline 58):_runner()'s QEMU branch returnsxr.run_kernel(code, GUEST_VA)(raw(bytes, tr)) instead ofxr.run_result(code, GUEST_VA)(parsed(int, tr)).Imported from ubiquitarianism/leonida. Originally posted by TheGeogeo on 2026-11-29T00:17:36Z.
Tested
efd36453dmerged (no commit) withmain829530dbd: Python 3.12.4, Windows 11 10.0.26300, CPU only.check_no_game_data.pyis clean.tests/test_x64_recomp_adcsbb.py, 7 tests pass (3 QEMU tests skipped).tools/x64_recomp.pyback tomain, 7 failures (e.g.[446673890, ...] != [1520382946, ...]). The QEMU run itself is not checked here.No blocker from my side.
AI-assisted (Claude Code).
Imported from ubiquitarianism/leonida. Originally posted by TheGeogeo on 2026-11-29T11:52:05Z.
Tested green here earlier on this same head (see my comment above). The only thing missing for a merge is your claim in the coordination issue #2570.
AI-assisted (Claude Code).
Imported from ubiquitarianism/leonida. Originally posted by TheGeogeo on 2026-11-29T18:49:58Z.
Tested
efd36453dmerged (no commit) withmain7c330c76d: Python 3.12.4, Windows 11 10.0.26300, CPU only.tests/test_x64_recomp_adcsbb.pygives 4 OK. 3 are skipped here because they need a Darwin arm64 Mach-O runner orqemu-system-aarch64, so they are not covered.tools/x64_recomp.pyback tomain, all 4 tests that run fail (7 subtest failures, e.g.test_8bit_and_16bit_adc_sbb_translate_cleanlyandtest_csinv_csetm_encoding).check_no_game_data.pyis clean.AI-assisted (Claude Code).
Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-29T19:36:24Z.
Verified by OreoFR on macOS 27.2.1 (26B131), Apple M5, APFS (case-insensitive, NFD), Python 3.12.16, main
dd87aa405, qemu-system-aarch64 11.1.3 on PATH, PR headefd36453d: green 4/7, 0 skipped — exec tests run here and failComplements the Windows run, where the 3 exec tests were skipped (no aarch64 runner).
git merge-tree); does not force a rebase of #2566. Red = main + the merged test file._runner()→xr.run_kernel, raw bytes, becausexr.CAN_RUN_MACHOdoes not exist on main): red 0/7 (4 static tests fail for real — "adc size 1" unhandled, csinv encoding; 3 exec tests time out under qemu at 15 s); green 4/7 — the 4 static tests pass, the 3 exec tests fail (partly bytes vs int, same runner issue as mosttest_x64_recomp_*on main, see the finding in #2570).xr.run_result(PR unchanged): red 0/7; green still 4/7. The 3 exec tests fail on one bit: CF read bypushfaftersbbis inverted. All 20 adc 8-bit corners pass; all 20 sbb 8-bit corners fail (e.g. sbb(0,0,cf=0) → 0x451 instead of 0x441); sbb 16-bit andsbb rax,064-bit too.sub rax,1on 0x10 thenpushfgives 0x15 instead of 0x14 on both main and the merge. The x86 expectations in the tests are right.tests/test_x64_recomp*.pygive identical per-file results on main and on the merge (no regression).Verdict: FAILS on macOS: the static fix holds (red 0/4 → green 4/4), but the new exec tests fail, because of the
run_kernelrunner and of a pre-existing CF-after-subtract bug inpushfmaterialisation on main.AI-assisted: Claude Code
Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-12-01T11:40:52Z.
Review: APPROVED
Reviewed the diff against
main:tools/x64_recomp.py:862-863(li(-1)),tools/x64_recomp.py:1114(Asm.csinvencoding0xDA800000), andtools/x64_recomp.py:1733-1774(8/16-bit shiftedADCS/SBCScarry/borrow propagation,as_size=szsign-extension, andCTX_A/CTX_BAF stashing) are mathematically exact and well-tested intests/test_x64_recomp_adcsbb.py. Clean-room verified. Approving; awaiting a green CI run before merge.(AI-assisted review.)