x64_recomp: rcl and rcr rotate-through-carry emitters (X64-RECOMP-RCL-RCR) #1211

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

@⁠IlyassRS3 on GitHub · 2026-11-24 20:20 UTC · original
IlyassRS3:feat/x64-recomp-rcl-rcr → main · +360 −1 · 2 files · 1 commits · open

Claimed in the coordination issue as: X64-RECOMP-RCL-RCR (#14)

What and why

Implements the rcl and rcr rotate-through-carry emitters called out in Next Step #2 of docs/mobile.md inside tools/x64_recomp.py (independent of #1174, #1202, and #1205, and conflict-free with all open x64_recomp PRs):

  • All operand widths and forms (_emit_rcl_rcr): supports 8/16/32/64-bit registers (including high-byte ah/ch/dh/bh), memory operands, immediate count 1 / imm8, and variable CL counts (including dst == rcx, e.g., rcl ecx, cl).
  • Exact x86-64 count reduction and no-op rule: masks count (& 0x3F for 64-bit, & 0x1F for 8/16/32-bit) and reduces modulo width + 1 (% 9 for 8-bit, % 17 for 16-bit) without clobbering NZCV so effective count 0 leaves both destination and all flags untouched.
  • Branchless shift-by-width-safe rotation: computes (orig >> 1) >> (width - c) (rcl) and (orig << 1) << (width - c) (rcr) so c = 1 and c = width never trigger arm64 shift-by-64 wraparound.
  • Exact flag contract: reads incoming CF from SUB_KIND (inverted arm C), ADD_KIND (arm C), ucomis*/comis* (lt), or materialized CTX_EFLAGS; writes outgoing CF without clobbering N/Z/V, and updates OF (V) when effective count is 1 (MSB(res) ^ new_CF for rcl, MSB(res) ^ MSB-1(res) for rcr).
  • Tests (tests/test_x64_recomp_rcl_rcr.py): covers translation of all 27 forms plus 4 end-to-end execution test suites exercising 8/16/32/64-bit imm/CL, mod-9/mod-17 wrap, zero-count no-op, multi-word shift chaining, sub borrow chaining, high-byte ah, memory operands, rcl ecx, cl, and count-1 OF.

Tested

  • Standalone on main: tests/test_x64_recomp_rcl_rcr.py, tests/test_ci_registration.py, tests/test_text_write_line_endings.py, compileall, make_game.py --help, check_no_game_data.py: all passed.
  • Exhaustive Python equivalence proof against reference (width + 1)-bit rotation across all 7,168 (width, value, cf, count) combinations: 100% match.
  • Native arm64 hardware execution on Apple M5 / macOS 27.2.1 (Python 3.14, cleanly 4-way merged with #1174, #1202, and #1205 in a scratch checkout): tests/test_x64_recomp_rcl_rcr.py 5/5 passed, tests/test_x64_recomp_bitops.py 9/9 passed, tests/test_x64_recomp_sse.py 16/16 passed, tests/test_x64_recomp.py 46/46 passed (76/76 total).
  • Verified zero conflicts (git apply --check) against open x64_recomp PRs #847, #950, #1120, #1121, #1174, #1202, and #1205.

GPUs tested: n/a (CPU x86-64 -> arm64 recompiler; tested natively on Apple M5 arm64, macOS 27.2.1)

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed.
**@⁠IlyassRS3** on GitHub · 2026-11-24 20:20 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1211) `IlyassRS3:feat/x64-recomp-rcl-rcr` → `main` · +360 −1 · 2 files · 1 commits · open Claimed in the coordination issue as: X64-RECOMP-RCL-RCR (https://git.gtahub.st/github/ubiquitarianism--leonida/issues/14) ### What and why Implements the `rcl` and `rcr` rotate-through-carry emitters called out in Next Step #2 of `docs/mobile.md` inside `tools/x64_recomp.py` (independent of `#1174`, `#1202`, and `#1205`, and conflict-free with all open `x64_recomp` PRs): - **All operand widths and forms (`_emit_rcl_rcr`)**: supports 8/16/32/64-bit registers (including high-byte `ah`/`ch`/`dh`/`bh`), memory operands, immediate count `1` / `imm8`, and variable `CL` counts (including `dst == rcx`, e.g., `rcl ecx, cl`). - **Exact x86-64 count reduction and no-op rule**: masks count (`& 0x3F` for 64-bit, `& 0x1F` for 8/16/32-bit) and reduces modulo `width + 1` (`% 9` for 8-bit, `% 17` for 16-bit) without clobbering `NZCV` so effective count `0` leaves both destination and all flags untouched. - **Branchless shift-by-width-safe rotation**: computes `(orig >> 1) >> (width - c)` (`rcl`) and `(orig << 1) << (width - c)` (`rcr`) so `c = 1` and `c = width` never trigger arm64 shift-by-64 wraparound. - **Exact flag contract**: reads incoming `CF` from `SUB_KIND` (inverted arm `C`), `ADD_KIND` (arm `C`), `ucomis*`/`comis*` (`lt`), or materialized `CTX_EFLAGS`; writes outgoing `CF` without clobbering `N`/`Z`/`V`, and updates `OF` (`V`) when effective count is `1` (`MSB(res) ^ new_CF` for `rcl`, `MSB(res) ^ MSB-1(res)` for `rcr`). - **Tests (`tests/test_x64_recomp_rcl_rcr.py`)**: covers translation of all 27 forms plus 4 end-to-end execution test suites exercising 8/16/32/64-bit `imm`/`CL`, mod-9/mod-17 wrap, zero-count no-op, multi-word shift chaining, `sub` borrow chaining, high-byte `ah`, memory operands, `rcl ecx, cl`, and count-1 `OF`. ### Tested - Standalone on `main`: `tests/test_x64_recomp_rcl_rcr.py`, `tests/test_ci_registration.py`, `tests/test_text_write_line_endings.py`, `compileall`, `make_game.py --help`, `check_no_game_data.py`: all passed. - Exhaustive Python equivalence proof against reference `(width + 1)`-bit rotation across all 7,168 `(width, value, cf, count)` combinations: 100% match. - Native arm64 hardware execution on Apple M5 / macOS 27.2.1 (Python 3.14, cleanly 4-way merged with `#1174`, `#1202`, and `#1205` in a scratch checkout): `tests/test_x64_recomp_rcl_rcr.py` **5/5 passed**, `tests/test_x64_recomp_bitops.py` **9/9 passed**, `tests/test_x64_recomp_sse.py` **16/16 passed**, `tests/test_x64_recomp.py` **46/46 passed** (76/76 total). - Verified zero conflicts (`git apply --check`) against open `x64_recomp` PRs `#847`, `#950`, `#1120`, `#1121`, `#1174`, `#1202`, and `#1205`. GPUs tested: n/a (CPU x86-64 -> arm64 recompiler; tested natively on Apple M5 arm64, macOS 27.2.1) AI-assisted: yes ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:43:02 +00:00
Author
Owner

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

@⁠kevincommits on GitHub · 2026-11-25 23:38 UTC · original

Independent run with QEMU. Apple M5 Pro, macOS 27.2.1 (26B131), Python 3.14.2, QEMU 11.1.3 from Homebrew. I merged main at 33bd07cf8 into the branch locally (960 commits behind, no conflicts). Head tested: 621aaeefc.

The emitter matches a reference model in 640 of 656 cases, and the 16 that differ are a bug that is already on main, not one this adds.

  • python -I tests/test_x64_recomp_rcl_rcr.py: 5/5 under QEMU. This file calls x.run_result and gates on HAS_QEMU, so its exec tests really run. tests/test_x64_recomp.py 33/33, tests/test_recomp_census.py 11/11. Full standalone suite: 513 run, 509 pass; the 4 that fail are main's 4.

  • Beyond the file, I ran 656 generated cases under QEMU against a small Python model of rotate-through-carry: rcl and rcr; widths 8, 16, 32, 64; the D0/D1 (by 1), C0/C1 ib and D2/D3 (by cl) forms; counts 1, 3, 9, 17, 31, 33, 63; carry-in set by stc, clc, a borrowing cmp and a non-borrowing cmp; register and [rbx+rsi*1+8] destinations. Each case checks the full 64-bit result (so the untouched upper bits too) and the carry out.

  • 640 match. That covers the modulo-9 and modulo-17 counts, count == width, the 64-bit shifts by 63, carry-in taken from a cmp in borrow polarity, and the memory forms.

  • 16 differ, all the same shape: effective count 0 (rcl al, 9, rcl ax, 17, either form) directly after a cmp. The value is correctly left alone, but a following setc reads the carry inverted. shl/shr/sar/rol with a zero count do exactly the same on main today, so this is the existing behaviour of the flag analysis for a shift that writes no flags. I posted it with a reproducer as a finding in #1608. A fix there should cover these too.

Not tested: the OF result for count 1, and rcl/rcr feeding adc/sbb. No game files used. AI-assisted.

**@⁠kevincommits** on GitHub · 2026-11-25 23:38 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1211#issuecomment-7177836063) Independent run with QEMU. Apple M5 Pro, macOS 27.2.1 (26B131), Python 3.14.2, QEMU 11.1.3 from Homebrew. I merged `main` at `33bd07cf8` into the branch locally (960 commits behind, no conflicts). Head tested: `621aaeefc`. **The emitter matches a reference model in 640 of 656 cases, and the 16 that differ are a bug that is already on `main`, not one this adds.** - `python -I tests/test_x64_recomp_rcl_rcr.py`: 5/5 under QEMU. This file calls `x.run_result` and gates on `HAS_QEMU`, so its exec tests really run. `tests/test_x64_recomp.py` 33/33, `tests/test_recomp_census.py` 11/11. Full standalone suite: 513 run, 509 pass; the 4 that fail are `main`'s 4. - Beyond the file, I ran 656 generated cases under QEMU against a small Python model of rotate-through-carry: `rcl` and `rcr`; widths 8, 16, 32, 64; the `D0/D1` (by 1), `C0/C1 ib` and `D2/D3` (by cl) forms; counts 1, 3, 9, 17, 31, 33, 63; carry-in set by `stc`, `clc`, a borrowing `cmp` and a non-borrowing `cmp`; register and `[rbx+rsi*1+8]` destinations. Each case checks the full 64-bit result (so the untouched upper bits too) and the carry out. - **640 match.** That covers the modulo-9 and modulo-17 counts, count == width, the 64-bit shifts by 63, carry-in taken from a `cmp` in borrow polarity, and the memory forms. - **16 differ, all the same shape:** effective count 0 (`rcl al, 9`, `rcl ax, 17`, either form) directly after a `cmp`. The value is correctly left alone, but a following `setc` reads the carry inverted. `shl`/`shr`/`sar`/`rol` with a zero count do exactly the same on `main` today, so this is the existing behaviour of the flag analysis for a shift that writes no flags. I posted it with a reproducer as a finding in #1608. A fix there should cover these too. Not tested: the OF result for count 1, and `rcl`/`rcr` feeding `adc`/`sbb`. No game files used. AI-assisted.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

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

Merge

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