Skip to content

cpu: lower FSUB, FSUBR and the remaining FADD forms in the x87 profile - #440

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

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

Conversation

@denn0x

@denn0x denn0x commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Change

Refs #406. This follows #422 (add), #431 (multiply) and #433 (divide). It completes the four basic arithmetic operations of the opt-in x87 profile in lower(..., x87=True). The profile now also accepts:

  • fsub and fsubr: with an m32 or m64 operand, st, st(i) and st(i), st;
  • fsubp and fsubrp: st(i), st for i from 1 to 7;
  • the fadd forms besides faddp: an m32 or m64 operand, st, st(i) and st(i), st.

fsubr and fsubrp subtract the destination from the source. Every arithmetic mnemonic of the profile now has the same five forms, and popping forms with st(0) as the destination stay rejected. The entry/exit contract, the static stack depths and the other rejections are unchanged.

Out of scope: this PR does not add fisub, fiadd, fchs, fabs or comparisons. They remain rejected, and the check confirms it. Conversions, constants, comparisons with PF and stack faults remain separate steps.

Arithmetic (tools/x87_soft.h).

  • x87_sub(a, b): selects a NaN or invalid operand first, through the existing x87_special_operands, so a NaN result keeps its own sign. It then flips the sign of b and calls x87_add.
  • Special results: the rounding, the sticky bit, infinities and zeros follow from the reviewed add. x - x is +0, -0 - +0 is -0, and inf - inf with equal signs gives the indefinite.
  • x87_subr(a, b): this is x87_sub(b, a).
  • fadd: the new fadd forms call the existing x87_add. Memory operands use the non-quieting x87_operand_f32/x87_operand_f64 from cpu: lower FMUL forms in the x87 leaf profile #431.

Fixture

tests/cpu/x87_sub.asm and .def are generated by tests/cpu/x87_sub.py asm. The step format, the assembler and the decode check of every x87 form come from tests/cpu/x87_div.py. The 22 exports cover:

  • fsubp st(1), st stored to m80, m64 and m32;
  • fsubrp st(1), st;
  • all four st, st(i) / st(i), st forms of fsub and fsubr;
  • both register forms of fadd;
  • x - x and x + x through st(0);
  • fsub, fsubr and fadd with an m32 and an m64 operand;
  • float - float, double - double and double + double;
  • a three-value chain through fsubp st(2), st and fsubrp st(1), st.

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 of the input and output are compared against an independent Fraction model;
  • the live-value check runs around all 22 exports;
  • emit checks that every decoded x87 instruction is the form its sequence names, so an fsub/fsubr encoding swap would fail before anything runs.

Cases. The 35023 cases reuse the addition pairs of cpu_aot_x87_add, and again with the second operand negated. These cover specials, halfway sums, sticky tails, cancellation, range edges and random operands. On top of that:

  • x - x for finite values, zeros and infinities;
  • special m80 values against 17 m32 and 18 m64 special encodings;
  • random m80, m32 and m64 operands;
  • random three-value chains.

Negative controls. Seven mutated translations must each mismatch:

Control Mutation Mismatches
pc64 the difference rounds to 64 significand bits 15680/35023
double the difference rounds in the binary64 exponent range 24289/35023
nansign the subtrahend is negated before the NaN rules 2022/35023
reverse fsubr subtracts in the fsub order 7753/35023
cancel an exact cancellation gives -0 777/35023
nosticky bits shifted out while aligning are dropped 108/35023
memquiet m32/m64 operands are quieted before the NaN rules 81/35023

Rejections. Translation rejects ten sequences with the expected diagnostic:

  • fsubp st(0), st and fsubrp st(0), st;
  • fsub m32 on an empty stack;
  • fadd st, st(1) with one value;
  • fsubr st(2), st with two values;
  • fisub m32 and fisubr m16;
  • fchs, fabs and fcom st(1).

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

  • cpu_aot_x87_add's fadd st, st(1) becomes fcom st(1);
  • cpu_aot_x87_mul's fsub st, st(1) becomes fcomp st(1), and its fadd m32 becomes fiadd m32;
  • cpu_aot_x87_div's fadd m32 becomes fiadd m32.

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 4d26cc4 on fb4fb85. 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_sub_lifted
ctest --preset dev --no-tests=error -R '^cpu_aot_x87_sub$' -V
35023 cases over 22 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 22 native and translated exports
control pc64: 15680/35023 mismatches
control double: 24289/35023 mismatches
control nansign: 2022/35023 mismatches
control reverse: 7753/35023 mismatches
control cancel: 777/35023 mismatches
control nosticky: 108/35023 mismatches
control memquiet: 81/35023 mismatches
rejected 10 sequences outside the profile: fsubp st(0), st; fsubrp st(0), st; fsub m32 on an empty stack; fadd st, st(1) with one value; fsubr st(2), st with two values; fisub m32; fisubr m16; fchs; fabs; fcom st(1)
  • x87 regressions: all four x87 tests pass (x87_add, x87_mul, x87_div, x87_sub).
  • Full suite: ctest --preset dev --no-tests=error -j 8 --output-junit <repo>/out/test-results.xml gives 157 passed, 1 failed and 2 capability skips out of 160. 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 4d26cc4 in the fork's Windows checks run 44979460058. All four x87 tests ran and passed there (tests 76 to 79 of 132).

Coverage. python tools/form_coverage.py out/test-results.xml gives x64 93 of 948 forms, and 88 without cpu_aot_x87_sub. The added forms are fadd, fsub, fsubp, fsubr and fsubrp.

Not measured:

Overlap

I trial-merged this branch with all 52 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_div", a CMake block after the cpu_aot_x87_div 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 subtraction forms, the NaN-sign rule, 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 profile of lower() now accepts FADD, FSUB and FSUBR with
m32 and m64 operands, ST(0), ST(i) and ST(i), ST(0), and FSUBP and
FSUBRP ST(i), ST(0). x87_sub selects NaN and invalid operands before
negating the subtrahend, then adds; popping forms with ST(0) as the
destination stay rejected.

cpu_aot_x87_sub compares 22 exports over 35023 cases through the x87
harness, with the live-value check, the decode check and seven mutated
controls. Rejection cases of the earlier x87 tests that this profile now
accepts (fadd st, st(1); fadd m32; fsub st, st(1)) move to fcom, fcomp
and fiadd, which stay outside it.

Refs R0Y00#406

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