mirror of
https://github.com/ubiquitarianism/leonida.git
synced 2026-11-27 00:25:52 +01:00
[PR #1296] x64_recomp: fix 64-bit shift CF and narrow shift/rotate semantics (X64-RECOMP-SHIFT-WIDTH-CF) #1327
Labels
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
starred/leonida#1327
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
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?
📋 Pull Request Information
Original PR: https://github.com/ubiquitarianism/leonida/pull/1296
Author: @IlyassRS3
Created: 11/24/2026
Status: 🔄 Open
Base:
main← Head:fix/x64-recomp-shift-width-cf📝 Commits (1)
75d6d34x64_recomp: fix 64-bit shift CF and narrow shift/rotate semantics📊 Changes
2 files changed (+480 additions, -36 deletions)
View changed files
➕
tests/test_x64_recomp_shifts.py(+368 -0)📝
tools/x64_recomp.py(+112 -36)📄 Description
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 OK🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.