x64_recomp: add shld and shrd double-precision shift emitters #1349

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

@⁠IlyassRS3 on GitHub · 2026-11-24 22:12 UTC · original
IlyassRS3:feat/x64-recomp-shld-shrd → main · +349 −1 · 2 files · 1 commits · open

Item ID: X64-RECOMP-SHLD-SHRD

Summary

Adds decoding, classification, and ARM64 code generation for x86-64 double-precision shifts shld (0F A4 imm8 / 0F A5 CL) and shrd (0F AC imm8 / 0F AD CL) in tools/x64_recomp.py:

  • Decoder (OF table): adds 0xA4 (shld, Ev,Gv,Ib), 0xA5 (shld, Ev,Gv,CL), 0xAC (shrd, Ev,Gv,Ib), and 0xAD (shrd, Ev,Gv,CL).
  • Flag tracking & classification: registers shld/shrd in FLAG_WRITERS ("NZCVP") and classifies them as ins.kind = "dshift".
  • Emitter (_emit_dshift):
    • Count == 0: guaranteed no-op (leaves destination and all flags untouched, including materialized ctx.eflags).
    • 32-bit and 64-bit immediate counts: emits single-instruction ARM64 EXTR (extr(rd, src, dst, 32-c) / extr(rd, dst, src, c) with c &= 31 or c &= 63).
    • 32-bit and 64-bit CL variable counts: computes (dst << c) | ((src >> 1) >> (W - 1 - c)) (and symmetric right shift) so c == 0 / c == W - 1 never produce an out-of-range shift width.
    • 16-bit (0x66) operands: concatenates (dst << 16) | src (or (src << 16) | dst for shrd) in a 32-bit register and masks count & 31 (matching Intel/AMD hardware behavior when count == 16, where dst becomes src and CF receives bit 0 of dst for shld / bit 15 of dst for shrd).
    • Flags (SF, ZF, CF, OF, PF): computes the exact last-shifted-out bit into CF (bit W - c for shld, bit c - 1 for shrd), OF = old_sign ^ new_sign (for 1-bit shifts), updates live ARM NZCV and CTX_PF, and when flags are materialized writes ctx.eflags preserving AF.
    • Operand aliasing & memory: loads source and count before overwriting destination (safe for dst == src, dst == rcx, and [base+disp] memory operands, including 128-bit __shiftleft128/__shiftright128 pairs).

Tests (tests/test_x64_recomp_shld_shrd.py)

  • 3 static decode/compile/encoding tests (decoding all 4 opcodes across 16/32/64-bit + memory, verifying ARM64 EXTR emission, and verifying FLAG_WRITERS & classification).
  • 3 hardware execution suites covering 32/64-bit imm & CL + count==0 flag preservation, 16-bit imm & CL including count == 16, and memory operands / register aliasing (dst == src, dst == rcx) / 128-bit __shiftleft128.

Verification

  • Host: macOS (Apple M5, arm64), Python 3.
  • Standalone suite (no game files):
    • python3 -I tests/test_x64_recomp_shld_shrd.py — 6 tests (3 static OK, 3 exec skipped without native runner)
    • python3 -I tests/test_x64_recomp.py — OK
    • python3 -m compileall -q make_game.py tools tests — OK
    • python3 -I scripts/check_no_game_data.py tools/x64_recomp.py tests/test_x64_recomp_shld_shrd.py — OK
    • python3 -I tests/test_ci_registration.py — 15/15 OK
    • python3 -I tests/test_text_write_line_endings.py — 3/3 OK
  • Mutation proof: reverting tools/x64_recomp.py to upstream/main fails all 3 static test methods (14 failures).
  • Native Apple M5 hardware execution (13-way scratch merge with #1174 and open x64_recomp PRs, 0 conflicts):
    • tests/test_x64_recomp_shld_shrd.py: 6/6 passed (0 skipped)
    • All 13 x64_recomp test suites combined: 148/148 passed (0 skipped)

AI-assisted: yes

**@⁠IlyassRS3** on GitHub · 2026-11-24 22:12 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1349) `IlyassRS3:feat/x64-recomp-shld-shrd` → `main` · +349 −1 · 2 files · 1 commits · open Item ID: `X64-RECOMP-SHLD-SHRD` ### Summary Adds decoding, classification, and ARM64 code generation for x86-64 double-precision shifts `shld` (`0F A4` imm8 / `0F A5` `CL`) and `shrd` (`0F AC` imm8 / `0F AD` `CL`) in `tools/x64_recomp.py`: - **Decoder (`OF` table)**: adds `0xA4` (`shld`, `Ev,Gv,Ib`), `0xA5` (`shld`, `Ev,Gv,CL`), `0xAC` (`shrd`, `Ev,Gv,Ib`), and `0xAD` (`shrd`, `Ev,Gv,CL`). - **Flag tracking & classification**: registers `shld`/`shrd` in `FLAG_WRITERS` (`"NZCVP"`) and classifies them as `ins.kind = "dshift"`. - **Emitter (`_emit_dshift`)**: - **Count == 0**: guaranteed no-op (leaves destination and all flags untouched, including materialized `ctx.eflags`). - **32-bit and 64-bit immediate counts**: emits single-instruction ARM64 `EXTR` (`extr(rd, src, dst, 32-c)` / `extr(rd, dst, src, c)` with `c &= 31` or `c &= 63`). - **32-bit and 64-bit `CL` variable counts**: computes `(dst << c) | ((src >> 1) >> (W - 1 - c))` (and symmetric right shift) so `c == 0` / `c == W - 1` never produce an out-of-range shift width. - **16-bit (`0x66`) operands**: concatenates `(dst << 16) | src` (or `(src << 16) | dst` for `shrd`) in a 32-bit register and masks count `& 31` (matching Intel/AMD hardware behavior when `count == 16`, where `dst` becomes `src` and `CF` receives bit 0 of `dst` for `shld` / bit 15 of `dst` for `shrd`). - **Flags (`SF`, `ZF`, `CF`, `OF`, `PF`)**: computes the exact last-shifted-out bit into `CF` (bit `W - c` for `shld`, bit `c - 1` for `shrd`), `OF = old_sign ^ new_sign` (for 1-bit shifts), updates live ARM `NZCV` and `CTX_PF`, and when flags are materialized writes `ctx.eflags` preserving `AF`. - **Operand aliasing & memory**: loads source and count before overwriting destination (safe for `dst == src`, `dst == rcx`, and `[base+disp]` memory operands, including 128-bit `__shiftleft128`/`__shiftright128` pairs). ### Tests (`tests/test_x64_recomp_shld_shrd.py`) - 3 static decode/compile/encoding tests (decoding all 4 opcodes across 16/32/64-bit + memory, verifying ARM64 `EXTR` emission, and verifying `FLAG_WRITERS` & classification). - 3 hardware execution suites covering 32/64-bit imm & `CL` + count==0 flag preservation, 16-bit imm & `CL` including `count == 16`, and memory operands / register aliasing (`dst == src`, `dst == rcx`) / 128-bit `__shiftleft128`. ### Verification - **Host**: macOS (Apple M5, arm64), Python 3. - **Standalone suite (no game files)**: - `python3 -I tests/test_x64_recomp_shld_shrd.py` — 6 tests (3 static OK, 3 exec skipped without native runner) - `python3 -I tests/test_x64_recomp.py` — OK - `python3 -m compileall -q make_game.py tools tests` — OK - `python3 -I scripts/check_no_game_data.py tools/x64_recomp.py tests/test_x64_recomp_shld_shrd.py` — OK - `python3 -I tests/test_ci_registration.py` — 15/15 OK - `python3 -I tests/test_text_write_line_endings.py` — 3/3 OK - **Mutation proof**: reverting `tools/x64_recomp.py` to `upstream/main` fails all 3 static test methods (14 failures). - **Native Apple M5 hardware execution (13-way scratch merge with #1174 and open `x64_recomp` PRs, 0 conflicts)**: - `tests/test_x64_recomp_shld_shrd.py`: **6/6 passed** (0 skipped) - All 13 `x64_recomp` test suites combined: **148/148 passed** (0 skipped) AI-assisted: yes
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:48:20 +00:00
Author
Owner

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

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

Checked out head 395103b detached. Diff: tools/x64_recomp.py + tests/test_x64_recomp_shld_shrd.py — new 0F A4/A5/AC/AD decode, dshift kind, and _emit_dshift.

Semantics reviewed against Intel SDM / felixcloutier:

  • imm path uses EXTR for 32/64-bit: shld = (dst:src)>>(width-count), shrd = (src:dst)>>count — operand order verified correct.
  • 16-bit path builds the 32-bit pair and shifts — correct for count<=16; count 17-31 is undefined per Intel, output acceptable.
  • Count masking: 0x3F for 64-bit, 0x1F otherwise; count==0 returns early with flags untouched — matches x86.
  • CF: imm uses ubfx at width-count (shld) / count-1 (shrd) of original dst — matches SDM 'BIT[DEST, COUNT-1]'/'last bit out'; variable path recomputes index width-count / count-1. Verified.
  • OF: eor of original vs result MSB = 'sign change' definition for count==1 — matches SDM ('OF set if a sign change occurred'); computed for all counts but OF is undefined for count!=1, so harmless.
  • ZF/SF via tst (sxth for 16-bit so SF reads bit15); V bfi'd into NZCV bit28. Memory-dst RMW is not atomic — same caveat as other emitters, acceptable.

Ran:

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

Verdict: correct on inspection; encoding/static tests pass, flag-execution tests skipped on this host. Base is main.

**@⁠whoalin1** on GitHub · 2026-11-24 23:28 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1349#issuecomment-7147443319) Independent verification (Devin, AI-assisted — CPU-only, no GPU). Checked out head 395103b detached. Diff: tools/x64_recomp.py + tests/test_x64_recomp_shld_shrd.py — new 0F A4/A5/AC/AD decode, dshift kind, and _emit_dshift. Semantics reviewed against Intel SDM / felixcloutier: - imm path uses EXTR for 32/64-bit: shld = (dst:src)>>(width-count), shrd = (src:dst)>>count — operand order verified correct. - 16-bit path builds the 32-bit pair and shifts — correct for count<=16; count 17-31 is undefined per Intel, output acceptable. - Count masking: 0x3F for 64-bit, 0x1F otherwise; count==0 returns early with flags untouched — matches x86. - CF: imm uses ubfx at width-count (shld) / count-1 (shrd) of original dst — matches SDM 'BIT[DEST, COUNT-1]'/'last bit out'; variable path recomputes index width-count / count-1. Verified. - OF: eor of original vs result MSB = 'sign change' definition for count==1 — matches SDM ('OF set if a sign change occurred'); computed for all counts but OF is undefined for count!=1, so harmless. - ZF/SF via tst (sxth for 16-bit so SF reads bit15); V bfi'd into NZCV bit28. Memory-dst RMW is not atomic — same caveat as other emitters, acceptable. Ran: - `py -3.14 -I tests/test_x64_recomp_shld_shrd.py` → OK (3 passed + subtests, 3 skipped: native-exec needs Apple Silicon/QEMU) - `pytest tests/test_x64_recomp_shld_shrd.py` → 3 passed, 3 skipped, 12 subtests passed - `py -3.14 -m compileall -q tools tests make_game.py` → clean Verdict: correct on inspection; encoding/static tests pass, flag-execution tests skipped on this host. Base is main.
Author
Owner

@⁠kevincommits on GitHub · 2026-11-26 02:45 UTC · original

Independent run with QEMU. Apple M5 Pro, macOS 27.2.1 (26B131), Python 3.14.2, QEMU 11.1.3 from Homebrew. Head tested: 395103b0d, merged locally with main at 86dc3d86e (930 commits behind).

The branch now conflicts with main, in one place. After resolving it locally, the register forms of shld/shrd are right in all 1,344 checks against a reference model at 16, 32 and 64 bits.

  • Conflict. tools/x64_recomp.py, in _classify: #2150 (zero-count shifts write no flags) rewrote the two flags_w/flags_r lines of the shl/shr/... branch that this PR's new elif mnem in ("shld", "shrd") sits right after. Resolution I used: keep main's block as is and put the shld/shrd branch after it. Nothing else conflicts.

  • Test file (on the merged tree): as shipped under QEMU, python -I tests/test_x64_recomp_shld_shrd.py is 6 run, 19 failures, from the wiring bug in #1409 (xr.run_kernel where xr.run_result is needed). With RUNNER swapped for xr.run_result in memory: 6/6.

  • Beyond the file, generated cases under QEMU against a small Python model, shld/shrd with destination rax and source rdx:

    • both count forms, imm8 and cl, with counts 0, 1, 3, width−1, and width+1 at 32/64 bits (which the count mask turns into 1);
    • checked per case: the full 64-bit destination (untouched upper bits at 16, zero-extension at 32); CF, ZF, SF for a non-zero count; OF for a count of 1 only; and for a count of 0, that the destination is unchanged and a carry set by stc just before is still set afterwards;
    • four destinations and three sources per width.
    tool checks wrong
    this branch (merged) 1,344 0

    There is no main column: main does not decode 0f a4/a5/ac/ad.

  • Counts. tests/test_x64_recomp.py 44/44 and tests/test_recomp_census.py 11/11. Full standalone suite on the merged tree: 514 run, 512 pass; the 2 that fail are main's test_mod_pack_output_alias and the new file, for the wiring reason.

Not tested or not judged: memory destinations beyond the file's own cases, 16-bit counts above 16 (undefined on x86), OF for counts other than 1 (undefined), AF and PF, and whether a zero-count 32-bit form clears the upper half of the destination. No game files used. AI-assisted.

**@⁠kevincommits** on GitHub · 2026-11-26 02:45 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1349#issuecomment-7181086008) Independent run with QEMU. Apple M5 Pro, macOS 27.2.1 (26B131), Python 3.14.2, QEMU 11.1.3 from Homebrew. Head tested: `395103b0d`, merged locally with `main` at `86dc3d86e` (930 commits behind). **The branch now conflicts with `main`, in one place. After resolving it locally, the register forms of `shld`/`shrd` are right in all 1,344 checks against a reference model at 16, 32 and 64 bits.** - **Conflict.** `tools/x64_recomp.py`, in `_classify`: #2150 (zero-count shifts write no flags) rewrote the two `flags_w`/`flags_r` lines of the `shl`/`shr`/... branch that this PR's new `elif mnem in ("shld", "shrd")` sits right after. Resolution I used: keep `main`'s block as is and put the `shld`/`shrd` branch after it. Nothing else conflicts. - **Test file** (on the merged tree): as shipped under QEMU, `python -I tests/test_x64_recomp_shld_shrd.py` is 6 run, 19 failures, from the wiring bug in #1409 (`xr.run_kernel` where `xr.run_result` is needed). With `RUNNER` swapped for `xr.run_result` in memory: **6/6**. - Beyond the file, generated cases under QEMU against a small Python model, `shld`/`shrd` with destination `rax` and source `rdx`: - both count forms, `imm8` and `cl`, with counts 0, 1, 3, width−1, and width+1 at 32/64 bits (which the count mask turns into 1); - checked per case: the full 64-bit destination (untouched upper bits at 16, zero-extension at 32); CF, ZF, SF for a non-zero count; OF for a count of 1 only; and for a count of 0, that the destination is unchanged and a carry set by `stc` just before is still set afterwards; - four destinations and three sources per width. | tool | checks | wrong | |---|---|---| | this branch (merged) | 1,344 | 0 | There is no `main` column: `main` does not decode `0f a4`/`a5`/`ac`/`ad`. - **Counts.** `tests/test_x64_recomp.py` 44/44 and `tests/test_recomp_census.py` 11/11. Full standalone suite on the merged tree: 514 run, 512 pass; the 2 that fail are `main`'s `test_mod_pack_output_alias` and the new file, for the wiring reason. Not tested or not judged: memory destinations beyond the file's own cases, 16-bit counts above 16 (undefined on x86), OF for counts other than 1 (undefined), AF and PF, and whether a zero-count 32-bit form clears the upper half of the destination. No game files used. AI-assisted.
This pull request has changes conflicting with the target branch.
  • tools/x64_recomp.py
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1349/head:github-bot/gh-1349'
git switch 'github-bot/gh-1349'

Merge

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