cpu: lower FMUL forms in the x87 leaf profile #425
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/431"
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:09:39Z.
Change
Refs #406. This is the multiplication step of the x87 plan, on top of the leaf profile from #422. The opt-in profile of
lower(..., x87=True)now also accepts:fmul m32andfmul m64(ST(0) = ST(0) × operand);fmul st, st(i)andfmul st(i), st;fmulp st(i), stforifrom 1 to 7.The default
x87=Falseis unchanged, and so are the entry/exit contract, the static stack depths and every existing rejection.fmulp st(0), stis rejected likefaddp st(0), st.Deliberately out of scope: this PR does not add
fadd m32/m64or the otherfaddregister forms. They remain rejected (the check confirmsfadd m32), so this PR covers one behavior. Division, conversions, constants, comparisons and stack faults remain separate steps.Arithmetic (
tools/x87_soft.h).x87_mul: the product of the two 64-bit significands is exact in 128 bits (_umul128) and is normalized. It then goes through the samex87_roundas the add: 53 bits, extended exponent range, denormalized first below the normal range.x87_special_operands.fmulconverts exactly but keeps a signaling NaN signaling (x87_operand_f32/x87_operand_f64). The NaN rules therefore see the operand's own kind; a plainfldstill quiets on load. To do this,x87_from_binarygained a defaultedquiet = trueparameter. Existing call sites are unchanged, andcpu_aot_x87_addpasses unchanged.Fixture
tests/cpu/x87_mul.asmand.defare generated bytests/cpu/x87_mul.py asm. The 12 exports are:fmulp st(1), ststored to m80, m64 and m32;fmul st, st(1)andfmul st(1), st, with both stack values stored;fmul st, st(0);fmul m32andfmul m64against an m80 value;float × floatanddouble × doublethroughfld/fmul m/fstp;fmulp st(2), st;a * b + cwith the product rounded before the add.Harness and checks. The exports run through the harness from #422 (
fnsave/frstor, control word0x027F), with identical input bytes for the native and translated DLLs. The check compares the output block and 64 guard bytes on each side of the input and output against an independentFractionmodel. It reuses the exact-rational helpers oftests/cpu/x87_add.pyand adds a product model of its own. The live-value check confirms that a caller's ST(0) value and control word survive each of the 12 exports.Cases. The 14951 cases include:
(2^63 + u)(2^63 + v)withu + v = 1024(mod 2048), whose upper 64 bits sit exactly on a tie so that only the low 64 bits decide the rounding;x * 1, which still rounds a 64-bit significand to 53 bits;Negative controls. Six mutated translations must each mismatch:
pc64doublenolowsigninfzeromemquietRejections. With
x87=True, translation rejects nine sequences with the expected diagnostic:fmulp st(0), st;fmul m32on an empty stack;fmul st, st(1)with one value;fmul st(2), stwith two values;fmulpwith one value;fimul m32andfimul m16;fdiv st, st(1);fadd m32.Verification
Tested at
649e460onc2fc84c. 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_' -j 8passes.cpu_aot_x87_addkeeps its 8085 cases and its control counts.ctest --preset dev --no-tests=error -j 8 --output-junit <repo>/out/test-results.xmlgives 147 passed, 1 failed and 2 capability skips (bvh_wave64_warp,wave_shuffle_warp) out of 150. 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.649e460in the fork'sWindows checksrun 44974517755.cpu_aot_x87_addandcpu_aot_x87_mulran and passed there (tests 71 and 72 of 122).Coverage.
python tools/form_coverage.py out/test-results.xmlgives x64 61 of 948 forms, and 59 withoutcpu_aot_x87_mul. The added forms arefmulandfmulp.Not measured:
st(3)tost(7)operands. Onlyst(0),st(1)andst(2)are exercised; the others use the same slot arithmetic.Overlap
I trial-merged this branch with every open
cpu:PR. Every merge that is clean againstmainis also clean against this branch. The changes stay inside the x87 files from #422, oneCPU_FIXTURESline after"x87_add", a CMake block after thecpu_aot_x87_addtest, 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 multiplication forms, the memory-operand NaN rule, the controls and the reproduction command.Checklist
AI-assisted (Claude Code).