Skip to content

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

Merged
R0Y00 merged 1 commit into
R0Y00:mainfrom
denn0x:cpu/lower-carry-rotates
Nov 22, 2026
Merged

R0Y00 merged 1 commit into
R0Y00:mainfrom
denn0x:cpu/lower-carry-rotates

Conversation

@denn0x

@denn0x denn0x commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

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 cpu: lower x64 string instructions in the shared lifter #368: a trial merge of cpu: lower x64 string instructions in the shared lifter #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).

Lower the rotations through the carry flag in 8-, 16-, 32- and 64-bit
register and memory forms, including AH, with a count of 1, an immediate
or CL, and the carry-flag instructions CLC, STC and CMC.

The rotation steps one bit at a time as in the Intel SDM pseudocode. The
count masks to five bits (six for 64-bit); a zero step count keeps CF,
ZF is never affected, and OF is not modelled. A 32-bit register
destination is written even for a zero masked count, which the native
fixture confirms clears the upper half.

cpu_aot_carry_rotates builds 102 exports from MASM source and compares
84000 cases of RAX, the memory operand, ZF and CF across the native DLL,
the translation and a Python reference. Three mutated translations (the
count mask widened, the rotated-out bit fed back instead of CF, and STC
clearing CF) must mismatch.

AI-assisted (Claude Code).
@grahamster2

Copy link
Copy Markdown
Contributor

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.

@R0Y00
R0Y00 merged commit 8afb68c into R0Y00:main Nov 22, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants