cpu: lower the x87 constant loads in the x87 profile #444
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/450"
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-22T04:18:10Z.
Change
Refs #406. This follows #443 (register moves). It adds the seven constant loads to the opt-in x87 profile in
lower(..., x87=True):fld1,fldz,fldpi,fldl2t,fldl2e,fldlg2andfldln2.Out of scope: integer conversions (
fild,fist, ...), comparisons, transcendental instructions and every control-word access. They remain rejected. The entry/exit contract and the static stack depths are unchanged.Rounding. Each constant is pushed rounded to 64 significand bits under the rounding control. The profile fixes that control at round to nearest-even. The 53-bit precision control does not apply to the constant loads. The native runs confirm this: the control that rounds the constants to 53 bits mismatches the native DLL (see below).
Lowering (
tools/x87_lower.py).CONSTANTSholds the (significand, sign and exponent) encoding of each constant, rounded to nearest-even.x87_slot[d] = X87Value{0x...ull, 0x....}; // fldpi.No helper in
tools/x87_soft.hchanges.Fixture
tests/cpu/x87_constants.asmand.defare generated bytests/cpu/x87_constants.py asm, andemitchecks that every decoded x87 instruction is the form its sequence names. The.asmregisters its test for the coverage report on its first line (; coverage: source cpu_aot_x87_constants), as #444 describes. The 30 exports cover:fldpithroughfstm64, thenfstpm80;x + 1,1 / x,0 - x,x * ln 2,pitimes an m64 value andlog2(e)times an m32 value;fldpiandfldlg2pushed as the eighth value, with all eight values stored.Model. The model does not read the
CONSTANTStable. It computes the constants from integer series with 320 fraction bits:It derives log2(10), log2(e) and log10(2) from these values and rounds each one to 64 bits itself. The arithmetic exports reuse the
Fractionmodels of the add, multiply, subtract and divide tests. The harness, input bytes, guards and live-value check are those of the earlier x87 PRs.Cases. 2863 cases:
cpu_aot_x87_addand 200 random values;cpu_aot_x87_add;Negative controls. Four mutated translations must each mismatch:
pc53chopfldznegfldzloads -0swaplogsfldlg2andfldln2values exchangedRejections. Translation rejects 11 sequences with the expected diagnostic:
fldpileft on the stack at the return;f2xm1,fyl2xandfnstenv.Changes to existing tests.
cpu_aot_x87_movesusedfld1as a rejection case. It becomesfsin, which is still outside the profile, so the test keeps 16 rejections. Its case and control counts are unchanged.Verification
Tested at
d38d260on718d683. 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,x87_moves,x87_constants).ctest --preset dev --no-tests=error -j 8 --output-junit test-results.xmlgives 176 passed, 1 failed and 2 capability skips out of 179. 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./O2build-and-test, Linux Python and gfx1013 LLVM jobs passed atd38d260in the fork'sWindows checksrun 44984076276. Both Windows jobs ran and passed all six x87 tests (151 of 151 tests in each).Coverage.
python tools/form_coverage.py out/build/dev/test-results.xmlgives x64 149 of 948 forms, and 142 withoutcpu_aot_x87_constants. The added forms arefld1,fldz,fldpi,fldl2t,fldl2e,fldlg2andfldln2.Not measured:
fldcw. The five irrational constants are inexact, so each one rounds differently from nearest-even under one of round up and round down.Overlap
I trial-merged this branch with all 90 open PRs. Each one conflicts in the same files against
mainand against this branch: the 16 that merge cleanly intomainalso merge cleanly here, and this branch adds no conflict to the others. The changes stay insidetools/x87_lower.py, the new fixture, one rejection case oftests/cpu/x87_moves.py, a CMake block after thecpu_aot_x87_movestest, 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 constant loads, their rounding, the controls and the reproduction command.Checklist
AI-assisted (Claude Code).