cpu: lower an opt-in x87 leaf profile with FADDP #416
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/422"
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-22T01:22:06Z.
Change
Refs #406. This is the first PR of the plan agreed there: an opt-in x87 leaf-function profile in the shared
lower()with one register-add form. Multiplication, division, conversions, constants, comparisons (with PF) and stack faults are left for later PRs, each with its own evidence.Interface.
lower(..., x87=False)is the default and still rejects every x87 instruction, so existing callers are unchanged. Withx87=Truethe profile accepts:fldandfstpwith an m32, m64 or m80 memory operand;faddp st(i), stforifrom 1 to 7.Stack model (
tools/x87_lower.py).0x027F(53-bit precision, round to nearest-even, all exceptions masked), TOP 0, eight empty registers and a clear status word. The function must return with the stack empty.x87_slot[n], so no TOP exists at run time.Rejected at translation:
fldcw,fnstsw,fninit,fnstenvandwait, plus register forms offld,fadd st, st(i)andfaddp st(0), st;faddpwithout enough values;Arithmetic (
tools/x87_soft.h). The helpers use integers only, with no host floating point.Accepted encodings. Every 80-bit encoding is accepted as an operand, so there is no restriction to canonical finite values; the special-value semantics above are implemented and checked. The status word, exceptions and the other rounding modes are not modelled. This is safe only because the profile rejects every instruction that could observe or change them.
Measured, not taken from the SDM: when two NaNs of the same kind have equal significands, the positive one wins in either operand order. My first model returned the destination operand, and 7 of 8061 native cases disagreed, all of them
-NaN(x) + +NaN(x).Fixture
tests/cpu/x87_add.asmand.defare generated bytests/cpu/x87_add.py asmfrom theSEQUENCEStable. The 16 exports are:faddp st(2), stand thenfaddp st(1), st.Harness.
tests/cpu/x87_harness.asmis native-only and never translated;CPU_FIXTURESmaps it toNone. For each call it:fnsave, environment and all eight register values, which also leaves TOP 0 and an empty stack;0x027F;frstor.Harness check.
x87_seededenters each of the 16 exports with a live caller value in ST(0): pi with all 64 significand bits, held in the physical register each export reuses. It also loads control word0x0F7F(64-bit precision, round toward zero). After the harness returns, it requires:A control harness,
x87_call_envonly, keeps only the environment (fnstenv/fninit/fldenv). It must lose the value around every export, and it does: the register comes back holding the fixture's last value.The native and translated exports both run through it with identical input bytes.
Check.
tests/cpu/x87_add.py checkcompares each 64-byte output block, plus 64 guard bytes on each side of the input and the output, against an independentfractions.Fractionmodel. It requires every export to return with control word0x027F, TOP 0 and all tags empty. The 8085 cases include:Negative controls. Six mutated translations of the same image must each mismatch:
pc64doublem80doublenostickynanordersignedzeroRejections. The check also confirms that the default
lower()rejectsfld. It then confirms thatx87=Truerejects 16 sequences outside the profile, each with its expected diagnostic.Verification
Tested at
cace6d2one5e0079(it merges cleanly withac48a50). 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 8gives 44 of 44 passed.ctest --preset dev --no-tests=error -j 8 --output-junit <repo>/out/test-results.xmlgives 145 passed, 1 failed and 2 capability skips (bvh_wave64_warp,wave_shuffle_warp) out of 148. 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.cace6d2in the fork'sWindows checksrun 44971715289.cpu_aot_x87_addran and passed there (test 70 of 120). CI runs without-V, so the runner's CPU vendor is not in the log.Coverage.
python tools/form_coverage.py out/test-results.xmlgives x64 58 of 948 forms, and 55 withoutcpu_aot_x87_add. The added forms arefld,fstpandfaddp. The harness is native-only, so itsfnstenv,fninit,fldcwandfldenvare not counted.Not measured:
faddp st(3)tost(7), st. Onlyst(1)andst(2)are exercised; the others use the same slot arithmetic.Overlap
The shared interfaces are
lower()intools/ffx_version_query.pyandCPU_FIXTURESintools/form_coverage.py. I trial-merged this branch with every opencpu:PR.mainis also clean against this branch, including #287, #289, #291, #326, #328, #331, #335, #377, #379, #386, #390, #403, #404, #412 and #415. To keep #390 and #404 clean, the depth check sits right after theallowedset, away from their vector setup. To keep #373 clean, the docs section sits before## Mapping an image without LoadLibrary.string_op()directly abovedef lower, so the keyword-onlyx87=Falseon the signature line conflicts textually. I'll rebase whichever of the two lands second.mainalready, so they were not compared.No open PR touches x87.
Evidence
The fixture is synthetic and original to this repository (MIT).
docs/experiments.mddocuments the profile, its accepted encodings, its rejection boundaries, the controls and the reproduction command.Checklist
AI-assisted (Claude Code).
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-22T01:25:53Z.
One harness issue at
dd96335:x87_callsaves the caller withfnstenv [rsp+72], then usesfninitand calls the native fixture.fldenvrestores TOP and tags, but the saved environment contains no ST register payloads. A caller entering with a live value can therefore get a valid tag pointing to data overwritten by the fixture. Please save and restore the full x87 state around the harness call (a correctly sizedfnsave/frstorarea is one option). Add a native wrapper control that seeds a distinctive 80-bit ST value and non-default control word, invokes a fixture that reuses its slot, and checks both value bytes and environment on return. The fixture itself should still start with the agreed empty-stack profile.Imported from R0Y00/Xbox2Windows. Originally posted by denn0x on 2026-11-22T01:33:17Z.
@R0Y00 Fixed in
cace6d2.x87_callnow saves the caller's whole x87 state withfnsaveinto a 108-byte area and restores it withfrstor.fnsavealso leaves the agreed entry state (TOP 0, empty tags, clear status); the harness then loads0x027Fbefore the call.New wrapper check.
x87_seededenters each of the 16 exports with a live caller value in ST(0): pi with all 64 significand bits, in the physical register the export reuses. It also loads control word0x0F7F(PC=64, round toward zero). On return it checks:0x3FFF;Negative control. The previous environment-only harness is kept as
x87_call_envonly, and it must fail the wrapper check. It fails on 16 of 16 exports: the register comes back with the fixture's last value, for example +inf foradd_three, instead of pi.The rest of the output is unchanged.
^cpu_gives 44 of 44 passed, and fork CI run 44971715289 passed withcpu_aot_x87_addrunning. The PR body is updated to match.AI-assisted (Claude Code).