cpu: add a checked x87 entry that follows FLDCW #524
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/530"
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-22T09:39:09Z.
Change
Refs #406. This follows #513 (
fnstcw,fstcw,wait). It adds the dynamic control word discussed in #406 as a checked entry:lower(..., x87=x87_lower.Checked(reject)). The checked entry acceptsfldcw m16and leaves through the caller'srejectstatement when the guest would leave the profile. The fixed profile (x87=True) keeps its ABI and still rejectsfldcw, and its generated code is byte-identical (all 105cpu_x87_*.cppfiles of the 15 earlier x87 tests compared).There are two commits.
1.
cpu: pass the x87 control word to the rounding helpers. Every rounding helper intools/x87_soft.htakes the control wordcw. Its precision control selects 24, 53 or 64 bits for the arithmetic results andfsqrt, and its rounding control selects the direction. The fixed profile passes the constantx87_cw = 0x027f. Two helpers needed more than a parameter:x87_divandx87_sqrtnow carry the bit below the 64-bit quotient or root as well as the sticky bit. Before, they had only the sticky bit, which was enough at 53 bits but not at 64.x87_addandx87_subis -0 when rounding toward negative infinity.At
0x027Fevery x87 test gives the same case and control counts as before, except thepc64controls ofcpu_aot_x87_div,cpu_aot_x87_sqrtandcpu_aot_x87_iarith:divpc64sqrtpc64iarithpc64That mutation makes the helper round to 64 bits, which it now does with the round bit it lacked. Restoring the sticky-only bit in the two helpers gives the old counts exactly.
docs/experiments.mdhas the new counts. In the earlier tests only the control patterns change. They follow the new helper signatures and the new round-bit lines.2.
cpu: add a checked x87 entry that follows FLDCW. The checked entry keeps the control word in a local,x87_cw, which starts at0x027F.Rounding sites: the arithmetic,
fsqrt,fscale,frndint,fistandfistp, the m32 and m64 stores,fnstcwand the five inexact constant loads all followx87_cw. The precision control applies only to the arithmetic andfsqrt.fisttpstill chops.Constants: each one stores the value below the exact constant and the next value up. The rounding control picks between them.
fldcw: accepts a word with all six exceptions masked and precision control 00, 10 or 11. It keeps the word asfnstcwreads it back: bit 6 reads as 1, bits 7 and 13 to 15 as 0, and bit 12 is kept.Statuses: the function stops through
reject, whose{status}is replaced by a distinct number. Guest stores before that point stay done.unmasked)fldcwwhose word unmasks an exceptionprecision)fldcwwhose word has the reserved precision control 01unrestored)0x027FThe return check is emitted at every
retof a function that contains x87 code. It compares the word itself, so a restore from a saved slot, from an input or from a different slot on each return is checked the same way.Static rejections: a function that loads the control word must not contain a call, an indirect call or branch, or an external edge, because the word could be live across it. A checked function without
fldcwkeeps the earlier rules.Native measurements (AMD Ryzen 7 7700). These come from a scratch probe and from the fixture's native runs.
fscaletruncates its scale toward zero under every rounding control and ignores the precision control.frndint, the m32 and m64 stores andfistpfollow the rounding control only.fxtractare exact.Fixture
tests/cpu/x87_fldcw.asmand.defare generated bytests/cpu/x87_fldcw.py asm, andemitchecks every decoded x87 instruction against its sequence, including thewaitandfnstcwthatfstcwdecodes into. The.asmregisters its test on its first line (; coverage: source cpu_aot_x87_fldcw). Each export loads its words from the input block. The 15 exports cover:faddp,fsubp,fmulp,fdivp,fsqrt,fadd m64andfdivr m32,fimul m32andfidivr m16,fstm32 and m64,fistm32 and m16,fistpm64,fisttp,frndint,fscaleand the six constant loads withfldz;fnstcwandfstcwafter each loaded word, andfnstcwagain after the restore;fnstcw,movzx,or eax, 0C00h,fldcw,fistpandfldcwof the saved word;The translation is a checked entry
uint32_t name(word input, word output, word* result). It returns the status and writes*resultonly for status 0, and the check starts*resultat a sentinel to see that.Model. The model rounds with exact rational arithmetic under the word's precision and rounding control. For special operands it calls the fixed-word models of the earlier x87 tests, whose results do not depend on the control word. It shares no code with
tools/x87_soft.h. At0x027Fthe check compares it with those fixed-word models, which agree on 13231 operations. The harness, input bytes, guards and live-value check are those of the earlier x87 PRs.Cases. 15944 cases. Each value of each export runs under each of the twelve supported words (precision control 00, 10 or 11 with each rounding control). Then come the words the checked entry stops at:
fldcwand at the restore;0x037F,0x0E7F,0x127F).The constant,
fnstcwand MSVC exports also flip the reserved bits (bit 6 clear, bit 7, bit 12, bits 13 to 15). Thefnstcwand two-return exports restore from words that read back as0x027Fwith reserved bits set, as well as from words that do not.The values are:
2^62 + 1/2, half-integers, eighths and values at2^63and above;fscale: fractional and large scales of both signs.Native runs. A native run that loads a word with an exception unmasked would trap, so those 570 cases run only translated. The 380 cases with precision control 01 run natively as an observation and are compared with a 64-bit model, not with the translation. Every other case runs natively and is compared with the model, including the control word the export leaves.
Negative controls. Ten mutated translations must each mismatch:
rcnearestpc53fistnearestfistandfistpignore the rounding controlconstnearestrawwordfldcwkeeps the raw wordnounmaskedpc01noreturnonereturndeferredRejections. Translation rejects 6 sequences with the expected diagnostic:
fldcwin the fixed profile; in the checked entry,fldcwwith a call, with an indirect jump, with an external edge, and with a value left at the return; andfnstsw ax. It translates 2 sequences: a checked function with a call and nofldcw, andfldcwaround a load and a store.Verification
Tested at
290bc3donmainat26544c6. The machine runs 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).290bc3d(ctest --preset dev --no-tests=error -R '^cpu_aot_x87_'). Apart from the threepc64counts above, their case and control counts are unchanged.cpu_x87_*.cppfiles of the 15 earlier tests is byte-identical before and after the second commit (6a0c2eband290bc3d), built in the same build directory. So are the other 105 generated sources of the build.ctest --preset dev --no-tests=error -j 8 --output-junit test-results.xmlgives 240 passed, 2 failed and 2 capability skips (bvh_wave64_warp,wave_shuffle_warp) out of 244 at290bc3d. The run includes the GPU tests on an AMD Radeon RX 9070 XT. The two failures aregpu_alias_lookup_writes_hardware, the RDNA 4 array-atomics probe recorded in #369, anddxr_opacity_hardware, the RDNA 4 result recorded in #216.mainat26544c6gives the same two failures and skips in the same session (239 passed out of 243). This change touches no GPU code.tests/form_coverage_check.py,tests/docs_links_check.pyandtests/bidi_controls_check.pypass at290bc3dlocally and in the Linux job of the fork CI run below./O2build-and-test, Linux Python and gfx1013 LLVM jobs passed at290bc3din the fork'sWindows checksrun 45007551442. Both Windows jobs ran and passed all sixteen x87 tests (200 passed and 2 capability skips,bvh_wave64_warpandwave_shuffle_warp, out of 202 in each).Coverage.
python tools/form_coverage.py out/build/dev/test-results.xmlgives x64 424 of 948 forms at290bc3d, and 423 atmain(26544c6) in the same session. The added form isfldcw.Not measured:
fldenv,frstorand the status word, which stay rejected.Overlap
I trial-merged this branch with the 65 open PRs. Each one conflicts in the same files against
mainand againstmainwith this branch merged: the 11 that merge cleanly intomainalso merge cleanly with it, and this branch adds no conflict to the others. The changes stay insidetools/x87_lower.py,tools/x87_soft.h, the x87 branch oftools/ffx_version_query.py, the new fixture, the helper-call patterns of eight x87 tests' controls, a CMake block after thecpu_aot_x87_fnstcwtest, and the x87 section ofdocs/experiments.md.Evidence
The fixture is synthetic and original to this repository (MIT).
docs/experiments.mddocuments the checked entry, the statuses, the native measurements, the controls and the reproduction command.Checklist
AI-assisted (Claude Code).