Repository navigation
cpu: lower RCL, RCR, CLC, STC and CMC in the shared lifter - #379
Merged
Merged
Conversation
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).
This was referenced Nov 22, 2026
Contributor
|
Independent check on Windows: differential fuzzing of this PR merged locally onto main |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Change
Advances CPU translation coverage. The rotations through the carry flag and the carry-flag instructions are an independent problem:
lower()rejectsrcl,rcr,clc,stcandcmctoday. No open PR or issue claims them; #291 coversrol,ror,shldandshrdonly. There is no prerequisite PR. The shared interface islower()intools/ffx_version_query.pyandCPU_FIXTURESintools/form_coverage.py, which #287, #289, #291, #326, #328, #331, #335 and #368 also extend. This change addselifbranches only, so whichever lands later needs a short merge.Lowering.
rclandrcrin 8-, 16-, 32- and 64-bit register and memory forms, includingah, with a count of 1, an immediate or CL.lower()omits.clc,stcandcmcclear, set and complement CF.Fixture
tests/cpu/carry_rotates.asmis generated bypython tests/cpu/carry_rotates.py asm tests/cpu/carry_rotates.asm tests/cpu/carry_rotates.defand builds 102 exports:rclandrcrfor 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 foral,ax,eax,raxand a dword in memory.clc,stc,cmc, andstcfollowed bycmc.shl+rclandshr+rcr.Calling shape. Each export sets ZF and CF with
add r8, r8from 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 checkcompares 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:
nomasknocarryrol/rorstcstcclears CFA 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. Onmain,lower()rejectsd1 d0 c3withunsupported query operation at 0x1000: rcl eax,1, soemitcannot translate the fixture.Verification
Tested at
9ce4ecdon9bdbb20. 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 --output-junit <repo>/out/test-results.xmlgives 144 passed, 1 failed and 2 capability skips (bvh_wave64_warp,wave_shuffle_warp). The run includes the GPU tests. The failure isgpu_alias_lookup_writes_hardware, which also fails onmainat9bdbb20on this RX 9070 XT; see the comment on #332. This change touches no GPU code.tests/form_coverage_check.pyandtests/docs_links_check.pypass.9ce4ecdin the fork'sWindows checksrun 44963535724.ctest --preset dev --no-tests=error -R '^cpu_'gives 44/44 passed.Coverage.
python tools/form_coverage.py out/test-results.xmlgives x64 60 of 948 forms. The same JUnit file withoutcpu_aot_carry_rotatesgives 55. The five added forms arercl,rcr,clc,stcandcmc.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.mddocuments the lowering, the fixture and its controls.Checklist
AI-assisted (Claude Code).