cpu: lower FSUB, FSUBR and the remaining FADD forms in the x87 profile #434
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/440"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from R0Y00/Xbox2Windows. Originally posted by denn0x on 2026-11-22T03:14:59Z.
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:fsubandfsubr: with an m32 or m64 operand,st, st(i)andst(i), st;fsubpandfsubrp:st(i), stforifrom 1 to 7;faddforms besidesfaddp: an m32 or m64 operand,st, st(i)andst(i), st.fsubrandfsubrpsubtract the destination from the source. Every arithmetic mnemonic of the profile now has the same five forms, and popping forms withst(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,fabsor 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 existingx87_special_operands, so a NaN result keeps its own sign. It then flips the sign ofband callsx87_add.x - xis +0, -0 - +0 is -0, andinf - infwith equal signs gives the indefinite.x87_subr(a, b): this isx87_sub(b, a).fadd: the newfaddforms call the existingx87_add. Memory operands use the non-quietingx87_operand_f32/x87_operand_f64from #431.Fixture
tests/cpu/x87_sub.asmand.defare generated bytests/cpu/x87_sub.py asm. The step format, the assembler and the decode check of every x87 form come fromtests/cpu/x87_div.py. The 22 exports cover:fsubp st(1), ststored to m80, m64 and m32;fsubrp st(1), st;st, st(i)/st(i), stforms offsubandfsubr;fadd;x - xandx + xthroughst(0);fsub,fsubrandfaddwith an m32 and an m64 operand;float - float,double - doubleanddouble + double;fsubp st(2), standfsubrp st(1), st.Harness and checks. As in the earlier x87 PRs:
fnsave/frstorharness with control word0x027F;Fractionmodel;emitchecks that every decoded x87 instruction is the form its sequence names, so anfsub/fsubrencoding 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 - xfor finite values, zeros and infinities;Negative controls. Seven mutated translations must each mismatch:
pc64doublenansignreversefsubrsubtracts in thefsubordercancelnostickymemquietRejections. Translation rejects ten sequences with the expected diagnostic:
fsubp st(0), standfsubrp st(0), st;fsub m32on an empty stack;fadd st, st(1)with one value;fsubr st(2), stwith two values;fisub m32andfisubr m16;fchs,fabsandfcom st(1).Changes to existing tests. Three rejection cases of the earlier x87 tests used forms that this PR accepts:
cpu_aot_x87_add'sfadd st, st(1)becomesfcom st(1);cpu_aot_x87_mul'sfsub st, st(1)becomesfcomp st(1), and itsfadd m32becomesfiadd m32;cpu_aot_x87_div'sfadd m32becomesfiadd 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
4d26cc4onfb4fb85. 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).x87_add,x87_mul,x87_div,x87_sub).ctest --preset dev --no-tests=error -j 8 --output-junit <repo>/out/test-results.xmlgives 157 passed, 1 failed and 2 capability skips out of 160. The run includes the GPU tests. The failure isgpu_alias_lookup_writes_hardware, the RDNA 4 array-atomics probe recorded in #369, which fails the same way onmain. This change touches no GPU code.tests/form_coverage_check.py,tests/docs_links_check.pyandtests/bidi_controls_check.pypass.4d26cc4in the fork'sWindows checksrun 44979460058. All four x87 tests ran and passed there (tests 76 to 79 of 132).Coverage.
python tools/form_coverage.py out/test-results.xmlgives x64 93 of 948 forms, and 88 withoutcpu_aot_x87_sub. The added forms arefadd,fsub,fsubp,fsubrandfsubrp.Not measured:
st(3)tost(7)operands. Onlyst(0),st(1)andst(2)are exercised.Overlap
I trial-merged this branch with all 52 open
cpu:PRs. Every merge that is clean againstmainis also clean against this branch. The changes stay inside the x87 files, oneCPU_FIXTURESline after"x87_div", a CMake block after thecpu_aot_x87_divtest, and the x87 section ofdocs/experiments.md. No open PR touches x87.Evidence
The fixture is synthetic and original to this repository (MIT).
docs/experiments.mddocuments the subtraction forms, the NaN-sign rule, the controls and the reproduction command.Checklist
AI-assisted (Claude Code).