Repository navigation
cpu: lower FDIV and FDIVR forms in the x87 leaf profile - #433
Conversation
The opt-in x87 profile of lower() now accepts FDIV and FDIVR with m32 and m64 operands, ST(0), ST(i) and ST(i), ST(0), and FDIVP and FDIVRP ST(i), ST(0). x87_div rounds a 64-bit significand quotient with a sticky bit for the remainder to 53 bits in the extended exponent range; zero by zero and infinity by infinity give the indefinite, a finite value by zero gives a signed infinity. Popping forms with ST(0) as the destination stay rejected. cpu_aot_x87_div compares 17 exports over 23939 cases through the x87 harness, with the live-value check, a decode check of every x87 form and seven mutated controls. cpu_aot_x87_mul's rejection of fdiv, now in the profile, becomes a rejection of fsub. Refs R0Y00#406 AI-assisted (Claude Code).
|
Independent check on Windows: differential fuzzing of this PR merged locally onto main |
Change
Refs #406. This is the division step of the x87 plan, on top of the add (#422) and multiply (#431) profiles. The opt-in profile of
lower(..., x87=True)now also accepts these forms of bothfdivandfdivr:m32orm64operand;st, st(i)andst(i), st;fdivp st(i), storfdivrp st(i), st, forifrom 1 to 7.fdivrandfdivrpdivide the source by the destination. The entry/exit contract, the static stack depths and the existing rejections are unchanged. As before, a popping form withst(0)as the destination is rejected.Out of scope: this PR does not add
fsub/fsubr,faddmemory and register forms,fidiv,fsqrtorfprem. They remain rejected and are checked. Conversions, constants, comparisons and stack faults remain separate steps.Arithmetic (
tools/x87_soft.h).x87_div: divides the 64-bit significands with_udiv128. The dividend isx << 64, orx << 63whenx >= y, so the quotient lies in[2^63, 2^64). A nonzero remainder becomes a sticky bit, which is enough to round to 53 bits. The result goes through the samex87_roundas the sum and the product: extended exponent range, denormalized first below the normal range.x87_special_operands, and memory operands keep a signaling NaN signaling, as in cpu: lower FMUL forms in the x87 leaf profile #431.x87_divr(a, b): this isx87_div(b, a).Fixture
tests/cpu/x87_div.asmand.defare generated bytests/cpu/x87_div.py asm. The 17 exports are:fdivp st(1), ststored to m80, m64 and m32;fdivrp st(1), st;st, st(i)/st(i), stforms offdivandfdivr;x / xthroughfdiv st, st(0);fdivandfdivrwith an m32 and an m64 operand;float / floatanddouble / double;fdivp st(1), st,fdivp st(2), standfdivrp st(1), st.Decode check.
emitrequires each export's decoded x87 instructions to be exactly the forms its sequence names (mnemonic plus operands or memory size). The register encodingsdc f0+ianddc f8+iare the ones that some assemblers swap betweenfdivandfdivr, so a swap would fail before anything runs.Harness and checks. As in #422 and #431:
fnsave/frstorharness with control word0x027F;Fractionmodel;Cases. The 23939 cases include:
(2^53 + 1) k / k, which sit halfway between 53-bit neighbours;x / 1,1 / x,x / xand small-integer quotients;Negative controls. Seven mutated translations must each mismatch:
pc64doublenostickydivzeroreversefdivrdivides in thefdivorderzerosignmemquietRejections. Translation rejects ten sequences with the expected diagnostic:
fdivp st(0), standfdivrp st(0), st;fdiv m32on an empty stack;fdivr st(2), stwith two values;fdivrpwith one value;fidiv m32andfidivr m16;fsqrt,fpremandfadd m32.Change to an existing test.
cpu_aot_x87_mullistedfdiv st, st(1)among its rejections. This PR brings that form into the profile, so that case now usesfsub st, st(1), which is still rejected. The test keeps its nine rejections and its case and control counts.Verification
Tested at
6335f4aon97016d9. 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).ctest --preset dev --no-tests=error -R '^cpu_aot_x87_'gives 3 of 3 passed (x87_add,x87_mul,x87_div).ctest --preset dev --no-tests=error -j 8 --output-junit <repo>/out/test-results.xmlgives 148 passed, 1 failed and 2 capability skips (bvh_wave64_warp,wave_shuffle_warp) out of 151. 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.pyandtests/docs_links_check.pypass.6335f4ain the fork'sWindows checksrun 44976826150. All three x87 tests ran and passed there (tests 71 to 73 of 123).Coverage.
python tools/form_coverage.py out/test-results.xmlgives x64 65 of 948 forms, and 61 withoutcpu_aot_x87_div. The added forms arefdiv,fdivp,fdivrandfdivrp.Not measured:
st(3)tost(7)operands. Onlyst(0),st(1)andst(2)are exercised.Overlap
I trial-merged this branch with all 49 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_mul", a CMake block after thecpu_aot_x87_multest, 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 division forms, the special cases, the decode check, the controls and the reproduction command.Checklist
AI-assisted (Claude Code).