cpu: lower FILD, FIST, FISTP and FISTTP in the x87 profile #446
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/452"
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:29:36Z.
Change
Refs #406. This follows #450 (constant loads). It adds the integer conversions to the opt-in x87 profile in
lower(..., x87=True):fildwith an m16, m32 or m64 operand;fistpandfisttpwith an m16, m32 or m64 operand;fistwith an m16 or m32 operand (the instruction has no m64 form).Out of scope: integer arithmetic and compares (
fiadd,ficom, ...), packed BCD (fbld,fbstp), comparisons and every control-word access. They remain rejected. The entry/exit contract and the static stack depths are unchanged.Semantics.
fild: a two's-complement integer converts exactly, and zero gives +0. The 53-bit precision control does not apply to it: a 64-bit integer keeps all 64 bits in the register.fistandfistp: roundst(0)to nearest-even, the rounding control the profile fixes.fistkeepsst(0).fisttp: rounds toward zero regardless of the rounding control.Lowering (
tools/x87_lower.py).INTEGERS: maps the operand size to the C++ signed type, unsigned type and width.fild:x87_slot[d] = x87_from_integer(static_cast<int16_t>(load<uint16_t>(a))), and likewise for the other widths.store<uintN_t>(a, static_cast<uintN_t>(x87_to_integer(x87_slot[top], N, chop))), withchoptrue only forfisttp.fistpandfisttpthen lower the depth.fildat depth 8 is an overflow and a store on an empty stack is an underflow.Helpers (
tools/x87_soft.h).x87_from_integer(int64_t): normalizes the magnitude with a leading-zero count.x87_to_integer(v, bits, chop): shifts the significand by the exponent, rounds to nearest-even from the remaining bits unlesschopis set, and checks the range against2^(bits-1), which allows-2^(bits-1)itself. It returns the lowbitsbits of the two's-complement result.Both helpers compute in integers, like the rest of the file.
Fixture
tests/cpu/x87_integers.asmand.defare generated bytests/cpu/x87_integers.py asm, andemitchecks that every decoded x87 instruction is the form its sequence names. The.asmregisters its test on its first line (; coverage: source cpu_aot_x87_integers). The 18 exports cover:fildof each width stored to m80, a 64-bit integer stored to m64 and m32, and a 32-bit integer added to an m80 value;fistpandfisttpto each width, andfistto m16 and m32 followed byfstpm80 of the kept register;fild/fistpm64 andfild/fisttpm16;fistpm32, thenfistpm64);fildm16 as the eighth value, with all eight values stored.Model. The model converts with
Fractionarithmetic and shares no code withtools/x87_soft.h. It rounds the exact value to nearest-even or toward zero and applies the range check itself. The harness, input bytes, guards and live-value check are those of the earlier x87 PRs.Cases. 11126 cases:
Negative controls. Six mutated translations must each mismatch:
chopallfistandfistptruncate likefisttproundttfisttprounds to nearest likefistpsaturatenanzerozextfildm16 and m32 zero-extend instead of sign-extendingfild53fildkeeps only 53 significand bitsRejections. Translation rejects 9 sequences with the expected diagnostic:
fildm16 at depth 8;fistpm32,fistm16 andfisttpm64 on an empty stack;fistm32 that leaves its value on the stack at the return;fiaddm32,ficomm16,fbldm80 andfbstpm80.Changes to existing tests. Two rejection cases of earlier x87 tests used forms that this PR accepts:
cpu_aot_x87_add'sfild m16becomesfbld m80;cpu_aot_x87_moves'sfist m32becomesfbstp m80.Both replacements are packed BCD, which this profile will not lower. Each test keeps its number of rejections, and the case and control counts are unchanged.
Verification
Tested at
56f01c6on2e7dfa4.mainhas since moved toab5b8d4(#310, #416 and #451). None of those touch the x87 files ortools/ffx_version_query.py, and this branch merges cleanly intoab5b8d4. 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,x87_integers).ctest --preset dev --no-tests=error -j 8 --output-junit test-results.xmlgives 177 passed, 1 failed and 2 capability skips out of 180. 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 at56f01c6in the fork'sWindows checksrun 44984887591. Both Windows jobs ran and passed all seven x87 tests (152 of 152 tests in each).Coverage.
python tools/form_coverage.py out/build/dev/test-results.xmlgives x64 153 of 948 forms, and 149 withoutcpu_aot_x87_integers. The added forms arefild,fist,fistpandfisttp.Not measured:
fldcw. They would changefistandfistp, but notfisttp.Overlap
I trial-merged this branch with all 90 open PRs. Each one conflicts in the same files against
mainand against this branch: the 18 that merge cleanly intomainalso merge cleanly here, and this branch adds no conflict to the others. The changes stay inside the x87 files, the new fixture, one rejection case each oftests/cpu/x87_add.pyandtests/cpu/x87_moves.py, a CMake block after thecpu_aot_x87_constantstest, and the x87 section ofdocs/experiments.md.#449 defines the float-to-integer conversion of the FFX dispatch translator at NaN and saturation boundaries. It touches only HLSL and
tools/ffx_dispatch_aot.py, not the CPU lowering, and merges cleanly with this branch.Evidence
The fixture is synthetic and original to this repository (MIT).
docs/experiments.mddocuments the integer conversions, the controls and the reproduction command.Checklist
AI-assisted (Claude Code).