x64_recomp: fix 64-bit shift CF and narrow shift/rotate semantics (X64-RECOMP-SHIFT-WIDTH-CF) #1296
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1296"
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:25:58Z.
Item ID:
X64-RECOMP-SHIFT-WIDTH-CFAI-assisted: yes
Summary
Fixes six correctness bugs in
tools/x64_recomp.py(_emit_shift,_shift_cf_imm,_shift_cf_var):shl/shr/sar/rorCF extraction usedsf=0:_shift_cf_immpassedsf=0toa.ubfx(T3, ..., bit, 1, sf=0), emitting illegal 32-bitUBFMencodings (immr/imms >= 32) for any 64-bit shift/rotate extracting bit>= 32(shl rax, 1,shr rax, 40,sar rax, 40,ror rax, 1, etc.)._shift_cf_varpassedsf=0tolsrv/lsr, silently truncating the 64-bit shift amount mod 32 and reading only the low 32 bits ofT2/T0.sf = 1 if width == 64 else 0.sarshifted zero-extendedT0:_loadzero-extends 8/16-bit operands to 32 bits, sosar al, .../sar ax, ...shifted in 0s instead of the sign bit for negative bytes/words (e.g.sar al, 1on0x80returned0x40instead of0xC0).T0viasxtb/sxthbeforeasr/asrvand masking back withuxtb/uxth.rol/rorrotated as 32-bit values and emitted illegalextrforcval % width == 0:ror/rorvshifted zeros into the byte/word, andcval % width or widthproduced an invalidextr(..., 0)/ror(..., width)whencvalwas a multiple ofwidth(e.g.rol al, 8,ror ax, 16).orr_rshift 8/16) before rotating and masking back withuxtb/uxth, and skipping the rotate instruction on immediatecval % width == 0while still computingCF.shl/shr/sarcomputedSF/ZFat 32-bit width and zeroedCFoncval == width:T0missedSF(bit 7/15) and missedZFwhenshlshifted non-zero bits into bits 8..31. Fixed by maskingT0towidth, then sign-extending intoT3viasxtb/sxthbeforetst_r.shl/shrwithcval == width, x86 setsCFto the last bit shifted out (orig[0]/orig[width-1]) rather than0; onsarwithcval >= width,CFreceives the sign bit (orig[width-1]). Fixed in_shift_cf_imm(cval > widthandmin(cval - 1, width - 1)) and_shift_cf_var(asrvforsar).rol/rorclobberedN/Zwithtst_rand clobberedPF/ctx.eflagson materialization:rol/rorupdateCF(andOFwhen count == 1) without modifyingSF,ZF,AF, orPF.tst_rand_stash_resforrol/ror, and when materialized (ins.addr in self.mat), patch only bit 0 (EF_CF) ofctx.eflagsdirectly instead of callingrt_mkflags.Merge compatibility
_emit_shift,_shift_cf_imm, and_shift_cf_var(preserving a 3-line buffer around lines 1882–1885 and_emit_mul) so it merges with 0 conflicts alongside#1174,#1202,#1205,#1211,#1213, and#1279.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_shifts.py -v— 9 tests (4 static ok, 5 exec skipped without runner)tools/x64_recomp.pytoupstream/maincauses 10 failures across all 4 static tests intests/test_x64_recomp_shifts.py.#1174,#1202,#1205,#1211,#1213,#1279— 0 merge conflicts):tests/test_x64_recomp_shifts.py— 9/9 OK (all 5 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 OKpython3 -m compileall -q tools/x64_recomp.py tests/test_x64_recomp_shifts.py— OKpython3 -I scripts/check_no_game_data.py tools/x64_recomp.py tests/test_x64_recomp_shifts.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:26:08Z.
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 kevincommits on 2026-11-26T00:34:55Z.
Independent run with QEMU. Apple M5 Pro, macOS 27.2.1 (26B131), Python 3.14.2, QEMU 11.1.3 from Homebrew. I merged
mainat61732aca1into the branch locally (925 commits behind, no conflicts). Head tested:75d6d3469.Against a reference model: 509 of 2,496 checks are wrong on
main, 0 of 2,496 on this branch.The new test file has the wiring bug from #1409: as shipped under QEMU,
python -I tests/test_x64_recomp_shifts.pyis 9 run, 64 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): 9/9.Beyond the file, generated cases under QEMU, each compared against a small Python model of the x86 rules:
shl,shr,sar,rol,roronrax/eax/ax/al;D0/D1),imm8(C0/C1) andcl(D2/D3) forms;stcsurvives.main(61732aca1)Counts.
tests/test_x64_recomp.py33/33 andtests/test_recomp_census.py11/11, the same asmain. Full standalone suite: 514 run, 508 pass; the 6 that fail aremain's 5 plus the new file, for the wiring reason.Overlap. #1211 (rcl/rcr) adds a branch at the top of the same
_emit_shift; the two will need reconciling by whichever lands second. I did not test them together.Not tested: memory destinations, OF, PF and AF, and CF for shift counts above the operand width (the model skips it as undefined). The zero-count-after-
cmpcase from my finding in #1608 is separate and unchanged by this. No game files used. AI-assisted.Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T19:38:11Z.
Red/green: tests/test_x64_recomp_shifts.py fails 10 static emitter assertions on main (64-bit shift CF, narrow sar/rotate encodings) 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
7daa4a4a5in GitHub Actions: no new failure.AI-assisted (Claude Code, Claude Opus 5.5)