Skip to content

cpu: lower x87 register moves, FCHS and FABS in the x87 profile - #443

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

R0Y00 merged 1 commit into
R0Y00:mainfrom
denn0x:cpu/lower-x87-moves

Conversation

@denn0x

@denn0x denn0x commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Change

Refs #406. This follows #422 (add), #431 (multiply), #433 (divide) and #440 (subtract). It adds register data movement to the opt-in x87 profile in lower(..., x87=True). The profile now also accepts:

  • fld st(i): pushes a copy of st(i);
  • fst st(i): copies st(0) into st(i);
  • fstp st(i): the same copy, then a pop;
  • fxch st(i): exchanges st(0) and st(i);
  • fchs and fabs;
  • fst with an m32 or m64 operand: stores st(0) without popping.

Out of scope: constants (fld1, fldz, ...), integer conversions (fild, fist, ...), comparisons, ffree and fincstp/fdecstp. They remain rejected, and the check confirms it for several of them. The entry/exit contract and the static stack depths are unchanged.

Lowering (tools/x87_lower.py). Each new form is a static slot operation, since the stack depth is known at translation time:

  • fld st(i): x87_slot[d] = x87_slot[d-1-i];
  • fst st(i) and fstp st(i): x87_slot[top-i] = x87_slot[top], and fstp then lowers the depth;
  • fxch st(i): a swap of two slots.

Rejections (tools/x87_lower.py).

  • Stack depth: an st(i) operand at or beyond the current depth is rejected as an underflow, and fld st(i) at depth 8 as an overflow.
  • Alias encodings: the lifter keys on the iced mnemonic, so it would otherwise accept the undocumented aliases of fstp st(i) (DF D0+i, DF D8+i) and of fxch st(i) (DD C8+i, DF C8+i). Assemblers do not emit them and the fixture does not measure them, so translation rejects them by encoding (ALIASES).

Helpers (tools/x87_soft.h).

  • x87_chs and x87_abs: these change only the sign bit, of every encoding. Zeros, NaNs (signaling stays signaling), unnormals and pseudo-denormals keep their other bits.
  • Copies and fst: register copies move the whole 80-bit value. fst m32/m64 reuses the existing x87_to_f32/x87_to_f64 rounding of fstp.

Fixture

tests/cpu/x87_moves.asm and .def are generated by tests/cpu/x87_moves.py asm, and emit checks that every decoded x87 instruction is the form its sequence names. The 26 exports cover:

  • fld st(0), st(1) and st(2), and fld st(6) at depth 7;
  • fst st(1) and st(2);
  • fstp st(0), st(1) and st(2);
  • fxch st(0), st(1) and st(2), and fxch st(7) at depth 8;
  • fchs and fabs, each stored to m80, m64 and m32, and fchs; fabs; fchs;
  • fst m32 and m64, and fst m64; fst m32, each followed by fstp m80 of the register;
  • movement combined with arithmetic: a + (-b), |a| * |b|, and fsubp after fxch.

The depth-7 and depth-8 exports load eight m64 values and store all eight.

Harness and checks. As in the earlier x87 PRs:

  • the exports run through the fnsave/frstor harness with control word 0x027F;
  • native and translated DLLs get identical input bytes;
  • the output block and 64 guard bytes on each side are compared against a model;
  • the live-value check runs around all 26 exports.

The model in tests/cpu/x87_moves.py moves raw encodings and shares no code with tools/x87_soft.h. For the three arithmetic exports it reuses the Fraction models of the add, multiply and subtract tests.

Cases. 20224 cases:

  • every pair of the 34 special encodings, plus 300 random pairs, through the two-value exports;
  • 300 triples of special encodings and 200 random triples through the three-value exports;
  • the m80 singles of cpu_aot_x87_add and 200 random values through the one-value exports;
  • 300 blocks of eight m64 values, drawn from the special and random binary64 encodings.

Negative controls. Seven mutated translations must each mismatch:

Control Mutation Mismatches
chssub fchs computes 0 - x 969/20224
absnan fabs leaves -0 and negative NaNs unchanged 225/20224
fxchnone fxch does nothing 3116/20224
fldtop fld st(i) always copies st(0) 2212/20224
fstpdrop fstp st(i) only pops 1922/20224
fstwrite fst m32/m64 also writes the rounded value back to st(0) 1357/20224
copyquiet register copies quiet a signaling NaN 267/20224

Rejections. Translation rejects 16 sequences with the expected diagnostic:

  • fld st(1) with one value, and fld st(7) with seven values;
  • fld st(0) at depth 8;
  • fst st(2) with two values;
  • fstp st(0) on an empty stack;
  • fxch st(1) with one value;
  • fchs and fabs on an empty stack;
  • the four alias encodings DF D1, DF D9, DD C9 and DF C9;
  • fist m32, fld1, ffree st(1) and fdecstp.

Changes to existing tests. Three rejection cases of the earlier x87 tests used forms that this PR accepts:

  • cpu_aot_x87_add's fld st(1) becomes fild m16;
  • cpu_aot_x87_sub's fchs and fabs become frndint and fscale.

Each replacement is still outside the profile, so every test keeps its number of rejections. Their case and control counts are unchanged.

Verification

Tested at 2236668 on fb7547c. Windows 11 build 26200, MSVC 19.50.35720, Windows SDK 10.0.26100.0, Python 3.13, AMD Ryzen 7 7700 (AMD64 Family 25 Model 97 Stepping 2, AuthenticAMD).

cmake --build --preset dev --target cpu_x87_moves_lifted
ctest --preset dev --no-tests=error -R '^cpu_aot_x87_moves$' -V
20224 cases over 26 exports (output block and guards) matched the reference in native and translated DLLs; each export returned with control 0x027f and an empty stack
processor: AMD64 Family 25 Model 97 Stepping 2, AuthenticAMD
x87_call kept a live caller ST(0) value and control word around all 26 native and translated exports
control chssub: 969/20224 mismatches
control absnan: 225/20224 mismatches
control fxchnone: 3116/20224 mismatches
control fldtop: 2212/20224 mismatches
control fstpdrop: 1922/20224 mismatches
control fstwrite: 1357/20224 mismatches
control copyquiet: 267/20224 mismatches
rejected 16 sequences outside the profile: fld st(1) with one value; fld st(0) at depth 8; fst st(2) with two values; fstp st(0) on an empty stack; fxch st(1) with one value; fchs on an empty stack; fabs on an empty stack; fld st(7) with seven values; fstp st(1) as DF D1; fstp st(1) as DF D9; fxch st(1) as DD C9; fxch st(1) as DF C9; fist m32; fld1; ffree st(1); fdecstp
  • x87 regressions: all five x87 tests pass (x87_add, x87_mul, x87_div, x87_sub, x87_moves).
  • Full suite: ctest --preset dev --no-tests=error -j 8 --output-junit <repo>/out/test-results.xml gives 165 passed, 1 failed and 2 capability skips out of 168. The run includes the GPU tests. The failure is gpu_alias_lookup_writes_hardware, the RDNA 4 array-atomics probe recorded in gpu_alias_lookup_writes_hardware: array atomics probe reads 256 per buffer on RX 9070 XT #369, which fails the same way on main. This change touches no GPU code.
  • Pure-Python checks: tests/form_coverage_check.py, tests/docs_links_check.py and tests/bidi_controls_check.py pass.
  • Fork CI: the Windows build-and-test, Linux Python and gfx1013 LLVM jobs passed at 2236668 in the fork's Windows checks run 44981173792. All five x87 tests ran and passed there (tests 83 to 87 of 140).

Coverage. python tools/form_coverage.py out/test-results.xml gives x64 131 of 948 forms, and 127 without cpu_aot_x87_moves. The added forms are fst, fxch, fchs and fabs. fld and fstp were already covered.

Not measured:

Overlap

I trial-merged this branch with all 46 open cpu: PRs. Every merge that is clean against main is also clean against this branch. The changes stay inside the x87 files, one CPU_FIXTURES line after "x87_sub", a CMake block after the cpu_aot_x87_sub test, and the x87 section of docs/experiments.md. No open PR touches x87.

Evidence

The fixture is synthetic and original to this repository (MIT). docs/experiments.md documents the data-movement forms, the alias rejection, the controls and the reproduction command.

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).

The opt-in x87 leaf profile now accepts FLD, FST and FSTP with an ST(i)
operand, FST with an m32 or m64 operand, FXCH ST(i), FCHS and FABS.
Register copies keep the full 80-bit encoding; FCHS and FABS change only
the sign bit. The undocumented FSTP and FXCH alias encodings stay
rejected. cpu_aot_x87_moves compares 26 native and translated exports
against an independent raw-encoding model, with seven negative controls.

AI-assisted (Claude Code).
@R0Y00
R0Y00 merged commit c29586d 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.

2 participants