Repository navigation
cpu: lower FSUB, FSUBR and the remaining FADD forms in the x87 profile - #440
Merged
Merged
Conversation
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).
5 tasks done
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
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 cpu: lower FMUL forms in the x87 leaf profile #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 gpu_alias_lookup_writes_hardware: array atomics probe reads 256 per buffer on RX 9070 XT #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).