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

Open
opened 2026-11-26 20:20:34 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1211
Author: @IlyassRS3
Created: 11/24/2026
Status: 🔄 Open

Base: main ← Head: feat/x64-recomp-rcl-rcr


📝 Commits (1)

  • 621aaee x64_recomp: rcl and rcr rotate-through-carry emitters (X64-RECOMP-RCL-RCR)

📊 Changes

2 files changed (+360 additions, -1 deletions)

View changed files

➕ tests/test_x64_recomp_rcl_rcr.py (+238 -0)
📝 tools/x64_recomp.py (+122 -1)

📄 Description

Claimed in the coordination issue as: X64-RECOMP-RCL-RCR (https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7143389193)

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.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/1211 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/24/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `feat/x64-recomp-rcl-rcr` --- ### 📝 Commits (1) - [`621aaee`](https://github.com/ubiquitarianism/leonida/commit/621aaeefc1c06aa4315ca9f81fc4ed798aab286d) x64_recomp: rcl and rcr rotate-through-carry emitters (X64-RECOMP-RCL-RCR) ### 📊 Changes **2 files changed** (+360 additions, -1 deletions) <details> <summary>View changed files</summary> ➕ `tests/test_x64_recomp_rcl_rcr.py` (+238 -0) 📝 `tools/x64_recomp.py` (+122 -1) </details> ### 📄 Description Claimed in the coordination issue as: X64-RECOMP-RCL-RCR (https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7143389193) ### 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. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
starred/leonida#1243
No description provided.