cpu: lower FDIV and FDIVR forms in the x87 leaf profile #427
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/433"
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-22T02:39:05Z.
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 #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 #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).
Imported from R0Y00/Xbox2Windows. Originally posted by grahamster2 on 2026-11-22T03:01:04Z.
Independent check on Windows: differential fuzzing of this PR merged locally onto main
73ed4b7, run with Windows 11 build 26200, MSVC 19.50 and ML64. It generated 1,250 random x87-profile functions using every division form this PR adds, mixed with FLD/FSTP (m32/m64/m80), FADDP and FMUL, at stack depths up to 8. Each ran natively and translated on about 300 inputs, throughtests/cpu/x87_harness.asm. Inputs included ±0, binary and extended denormals, pseudo-denormals and unnormals, ±Inf, signaling and quiet NaNs with payloads, range-boundary values and rounding ties. The 256-byte output block, guard bytes, control word and tags were compared bit-exactly. Result: 0 mismatches. The same harness detects five deliberate faults injected intox87_soft.h: precision, NaN ordering, sign of zero, denormal rounding position, and the denormal-to-normal carry.