cpu: lower RCL, RCR, CLC, STC and CMC in the shared lifter #373

Merged
denn0x merged 1 commit from upstream-pr/379 into main 2026-11-22 03:17:37 +00:00
denn0x commented 2026-11-21 23:51:09 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by denn0x on 2026-11-21T23:51:09Z.


Change

Advances CPU translation coverage. The rotations through the carry flag and the carry-flag instructions are an independent problem: lower() rejects rcl, rcr, clc, stc and cmc today. No open PR or issue claims them; #291 covers rol, ror, shld and shrd only. There is no prerequisite PR. The shared interface is lower() in tools/ffx_version_query.py and CPU_FIXTURES in tools/form_coverage.py, which #287, #289, #291, #326, #328, #331, #335 and #368 also extend. This change adds elif branches only, so whichever lands later needs a short merge.

Lowering.

  • Forms: rcl and rcr in 8-, 16-, 32- and 64-bit register and memory forms, including ah, with a count of 1, an immediate or CL.
  • Stepping: one bit per step through CF, as in the Intel SDM pseudocode.
  • Count masking: the count is masked to five bits, or six for a 64-bit operand. The 8- and 16-bit forms rotate 9 and 17 bits. Their counts are also reduced modulo 9 and 17, which only bounds the loop because those rotations repeat with that period.
  • Flags: a zero step count keeps CF, and ZF is never affected. OF, which the SDM defines only for a count of 1, is not modelled, like the other flags lower() omits.
  • Destination writes: a 32-bit register destination is written, and its upper half cleared, even when the masked count is zero. The native fixture confirms this, as it did for the zero-count shifts.
  • Carry instructions: clc, stc and cmc clear, set and complement CF.

Fixture

tests/cpu/carry_rotates.asm is generated by python tests/cpu/carry_rotates.py asm tests/cpu/carry_rotates.asm tests/cpu/carry_rotates.def and builds 102 exports:

  • Rotations: rcl and rcr for every destination (al, ah, ax, eax, rax, and byte to qword memory) with counts 1 and CL, plus immediates 0, 5, 9, 17, 33 and 65 for al, ax, eax, rax and a dword in memory.
  • Flag instructions: clc, stc, cmc, and stc followed by cmc.
  • Multiword idioms: the 128-bit shifts shl + rcl and shr + rcr.

Calling shape. Each export sets ZF and CF with add r8, r8 from one of four presets, which cover every ZF/CF combination. It then reports RAX, the memory operand and ZF/CF.

Check. tests/cpu/carry_rotates.py check compares 84000 cases against the native DLL, the translation and a Python reference written from the SDM pseudocode. CL counts run 0 to 70, plus 96, 128 and 255. The values include all-ones, single high and low bits, and upper-half noise for the 32-bit zero-extension.

Negative controls. Three translations of the same image are mutated after lowering, and each must mismatch:

Control Mutation Mismatches
nomask count mask widened to eight bits 28760/84000
nocarry the rotated-out bit fed back instead of CF, turning the instructions into rol/ror 49540/84000
stc stc clears CF 224/84000

A first version also had a control that dropped the modulo 9/17 reduction. It matched every case, which is how I noticed that the reduction cannot change the result, so it was replaced by nomask. On main, lower() rejects d1 d0 c3 with unsupported query operation at 0x1000: rcl eax,1, so emit cannot translate the fixture.

Verification

Tested at 9ce4ecd on 9bdbb20. Windows 11 build 26200, MSVC 19.50.35720, Windows SDK 10.0.26100.0, Python 3.13, AMD Ryzen 7 7700 (Zen 4).

ctest --preset dev --no-tests=error -R '^cpu_aot_carry_rotates$' -V
95: 84000 cases over 102 exports (RAX, memory, ZF/CF) matched the reference in native and translated DLLs
95: control nomask: 28760/84000 mismatches
95: control nocarry: 49540/84000 mismatches
95: control stc: 224/84000 mismatches
1/1 Test #95: cpu_aot_carry_rotates ............   Passed    1.07 sec
  • Full suite: ctest --preset dev --no-tests=error --output-junit <repo>/out/test-results.xml gives 144 passed, 1 failed and 2 capability skips (bvh_wave64_warp, wave_shuffle_warp). The run includes the GPU tests. The failure is gpu_alias_lookup_writes_hardware, which also fails on main at 9bdbb20 on this RX 9070 XT; see the comment on #332. This change touches no GPU code.
  • Pure-Python checks: tests/form_coverage_check.py and tests/docs_links_check.py pass.
  • Fork CI: the Windows build-and-test, Linux Python and gfx1013 LLVM jobs passed at 9ce4ecd in the fork's Windows checks run 44963535724.
  • With #368: a trial merge of #368 into this branch merges without conflicts. The insertions sit next to the shift-count fixture rather than at the same anchors. Built together, ctest --preset dev --no-tests=error -R '^cpu_' gives 44/44 passed.

Coverage. python tools/form_coverage.py out/test-results.xml gives x64 60 of 948 forms. The same JUnit file without cpu_aot_carry_rotates gives 55. The five added forms are rcl, rcr, clc, stc and cmc.

Measured, not established: results on one host CPU. OF and the other unmodelled flags are not compared.

Evidence

The fixture is synthetic and original to this repository (MIT). docs/experiments.md documents the lowering, the fixture and its controls.

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed.
  • I did not include game files, dumps, distribution links or secrets.

AI-assisted (Claude Code).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6535984791 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/379). Originally posted by [denn0x](https://github.com/denn0x) on 2026-11-21T23:51:09Z. --- ## Change Advances CPU translation coverage. The rotations through the carry flag and the carry-flag instructions are an independent problem: `lower()` rejects `rcl`, `rcr`, `clc`, `stc` and `cmc` today. No open PR or issue claims them; #291 covers `rol`, `ror`, `shld` and `shrd` only. There is no prerequisite PR. The shared interface is `lower()` in `tools/ffx_version_query.py` and `CPU_FIXTURES` in `tools/form_coverage.py`, which #287, #289, #291, #326, #328, #331, #335 and #368 also extend. This change adds `elif` branches only, so whichever lands later needs a short merge. **Lowering.** - **Forms:** `rcl` and `rcr` in 8-, 16-, 32- and 64-bit register and memory forms, including `ah`, with a count of 1, an immediate or CL. - **Stepping:** one bit per step through CF, as in the Intel SDM pseudocode. - **Count masking:** the count is masked to five bits, or six for a 64-bit operand. The 8- and 16-bit forms rotate 9 and 17 bits. Their counts are also reduced modulo 9 and 17, which only bounds the loop because those rotations repeat with that period. - **Flags:** a zero step count keeps CF, and ZF is never affected. OF, which the SDM defines only for a count of 1, is not modelled, like the other flags `lower()` omits. - **Destination writes:** a 32-bit register destination is written, and its upper half cleared, even when the masked count is zero. The native fixture confirms this, as it did for the zero-count shifts. - **Carry instructions:** `clc`, `stc` and `cmc` clear, set and complement CF. ## Fixture `tests/cpu/carry_rotates.asm` is generated by `python tests/cpu/carry_rotates.py asm tests/cpu/carry_rotates.asm tests/cpu/carry_rotates.def` and builds 102 exports: - **Rotations:** `rcl` and `rcr` for every destination (`al`, `ah`, `ax`, `eax`, `rax`, and byte to qword memory) with counts 1 and CL, plus immediates 0, 5, 9, 17, 33 and 65 for `al`, `ax`, `eax`, `rax` and a dword in memory. - **Flag instructions:** `clc`, `stc`, `cmc`, and `stc` followed by `cmc`. - **Multiword idioms:** the 128-bit shifts `shl` + `rcl` and `shr` + `rcr`. **Calling shape.** Each export sets ZF and CF with `add r8, r8` from one of four presets, which cover every ZF/CF combination. It then reports RAX, the memory operand and ZF/CF. **Check.** `tests/cpu/carry_rotates.py check` compares 84000 cases against the native DLL, the translation and a Python reference written from the SDM pseudocode. CL counts run 0 to 70, plus 96, 128 and 255. The values include all-ones, single high and low bits, and upper-half noise for the 32-bit zero-extension. **Negative controls.** Three translations of the same image are mutated after lowering, and each must mismatch: | Control | Mutation | Mismatches | |---|---|---| | `nomask` | count mask widened to eight bits | 28760/84000 | | `nocarry` | the rotated-out bit fed back instead of CF, turning the instructions into `rol`/`ror` | 49540/84000 | | `stc` | `stc` clears CF | 224/84000 | A first version also had a control that dropped the modulo 9/17 reduction. It matched every case, which is how I noticed that the reduction cannot change the result, so it was replaced by `nomask`. On `main`, `lower()` rejects `d1 d0 c3` with `unsupported query operation at 0x1000: rcl eax,1`, so `emit` cannot translate the fixture. ## Verification Tested at `9ce4ecd` on `9bdbb20`. Windows 11 build 26200, MSVC 19.50.35720, Windows SDK 10.0.26100.0, Python 3.13, AMD Ryzen 7 7700 (Zen 4). ```powershell ctest --preset dev --no-tests=error -R '^cpu_aot_carry_rotates$' -V ``` ```text 95: 84000 cases over 102 exports (RAX, memory, ZF/CF) matched the reference in native and translated DLLs 95: control nomask: 28760/84000 mismatches 95: control nocarry: 49540/84000 mismatches 95: control stc: 224/84000 mismatches 1/1 Test #95: cpu_aot_carry_rotates ............ Passed 1.07 sec ``` - **Full suite:** `ctest --preset dev --no-tests=error --output-junit <repo>/out/test-results.xml` gives 144 passed, 1 failed and 2 capability skips (`bvh_wave64_warp`, `wave_shuffle_warp`). The run includes the GPU tests. The failure is `gpu_alias_lookup_writes_hardware`, which also fails on `main` at `9bdbb20` on this RX 9070 XT; see the [comment on #332](https://github.com/R0Y00/Xbox2Windows/pull/332#issuecomment-7071003250). This change touches no GPU code. - **Pure-Python checks:** `tests/form_coverage_check.py` and `tests/docs_links_check.py` pass. - **Fork CI:** the Windows build-and-test, Linux Python and gfx1013 LLVM jobs passed at `9ce4ecd` in the fork's `Windows checks` run [44963535724](https://github.com/denn0x/Xbox2Windows/actions/runs/44963535724). - **With #368:** a trial merge of #368 into this branch merges without conflicts. The insertions sit next to the shift-count fixture rather than at the same anchors. Built together, `ctest --preset dev --no-tests=error -R '^cpu_'` gives 44/44 passed. **Coverage.** `python tools/form_coverage.py out/test-results.xml` gives x64 **60 of 948** forms. The same JUnit file without `cpu_aot_carry_rotates` gives 55. The five added forms are `rcl`, `rcr`, `clc`, `stc` and `cmc`. **Measured, not established:** results on one host CPU. OF and the other unmodelled flags are not compared. ## Evidence The fixture is synthetic and original to this repository (MIT). `docs/experiments.md` documents the lowering, the fixture and its controls. ## Checklist - [x] This pull request covers one reviewable behavior. - [x] I included the exact command and result for every check I ran. - [x] I considered a negative control or failure case for a new fixture. - [x] I updated README/docs when a capability or build requirement changed. - [x] I did not include game files, dumps, distribution links or secrets. _AI-assisted (Claude Code)._
upstream-sync closed this pull request 2026-11-24 06:42:43 +00:00
grahamster2 commented 2026-11-24 06:48:20 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by grahamster2 on 2026-11-22T03:01:05Z.


Independent check on Windows: differential fuzzing of this PR merged locally onto main 73ed4b7, run with Windows 11 build 26200, MSVC 19.50 and ML64. It generated 1,250 random functions with RCL/RCR at 8, 16, 32 and 64 bits on registers (including AH, CH and DH) and memory operands, with counts of 1, imm8 and CL, including 8, 9, 16, 17, 33 and 63 and CL values with high bits set, mixed with CLC/STC/CMC and the CF/ZF consumers the lifter models. Each ran natively and translated on 200 inputs, comparing RAX and a 256-byte buffer. Result: 0 mismatches. The harness detects deliberate faults in rotate carry, SHRD's zero flag, the count mask and MOVBE byte order.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7073273938 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/379#issuecomment-7073273938). Originally posted by [grahamster2](https://github.com/grahamster2) on 2026-11-22T03:01:05Z. --- Independent check on Windows: differential fuzzing of this PR merged locally onto main `73ed4b7`, run with Windows 11 build 26200, MSVC 19.50 and ML64. It generated 1,250 random functions with RCL/RCR at 8, 16, 32 and 64 bits on registers (including AH, CH and DH) and memory operands, with counts of 1, imm8 and CL, including 8, 9, 16, 17, 33 and 63 and CL values with high bits set, mixed with CLC/STC/CMC and the CF/ZF consumers the lifter models. Each ran natively and translated on 200 inputs, comparing RAX and a 256-byte buffer. Result: 0 mismatches. The harness detects deliberate faults in rotate carry, SHRD's zero flag, the count mask and MOVBE byte order.
Sign in to join this conversation.
No reviewers
No labels
No milestone
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.

Reference
R0Y00/Xbox2Windows!373
No description provided.