cpu: lower FILD, FIST, FISTP and FISTTP in the x87 profile #446

Merged
denn0x merged 1 commit from upstream-pr/452 into main 2026-11-22 04:36:42 +00:00
denn0x commented 2026-11-22 04:29:36 +00:00 (Migrated from github.com)

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):

  • fild with an m16, m32 or m64 operand;
  • fistp and fisttp with an m16, m32 or m64 operand;
  • fist with 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.
  • fist and fistp: round st(0) to nearest-even, the rounding control the profile fixes. fist keeps st(0).
  • fisttp: rounds toward zero regardless of the rounding control.
  • Invalid values: a NaN, an infinity, an unsupported encoding or a rounded value outside the destination range stores the integer indefinite, only the sign bit set. This is the masked invalid-operation response. There is no saturation.

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.
  • Stores: store<uintN_t>(a, static_cast<uintN_t>(x87_to_integer(x87_slot[top], N, chop))), with chop true only for fisttp. fistp and fisttp then lower the depth.
  • Depths: fild at 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 unless chop is set, and checks the range against 2^(bits-1), which allows -2^(bits-1) itself. It returns the low bits bits of the two's-complement result.

Both helpers compute in integers, like the rest of the file.

Fixture

tests/cpu/x87_integers.asm and .def are generated by tests/cpu/x87_integers.py asm, and emit checks that every decoded x87 instruction is the form its sequence names. The .asm registers its test on its first line (; coverage: source cpu_aot_x87_integers). The 18 exports cover:

  • fild of each width stored to m80, a 64-bit integer stored to m64 and m32, and a 32-bit integer added to an m80 value;
  • fistp and fisttp to each width, and fist to m16 and m32 followed by fstp m80 of the kept register;
  • round trips through fild/fistp m64 and fild/fisttp m16;
  • two stores from a two-value stack (fistp m32, then fistp m64);
  • fild m16 as the eighth value, with all eight values stored.

Model. The model converts with Fraction arithmetic and shares no code with tools/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:

  • Integer inputs: each width's extremes, small values, values just past the binary64 and binary32 significand widths, and random values.
  • m80 inputs: the special encodings, integers, halfway values, quarter steps, and values a small fraction past a half, around zero and each width's limits.
  • Random: 400 magnitudes from 2^-3 up to just below 2^67.

Negative controls. Six mutated translations must each mismatch:

Control Mutation Mismatches
chopall fist and fistp truncate like fisttp 1510/11126
roundtt fisttp rounds to nearest like fistp 676/11126
saturate an out-of-range value saturates instead of giving the integer indefinite 1049/11126
nanzero a NaN converts to 0 60/11126
zext fild m16 and m32 zero-extend instead of sign-extending 621/11126
fild53 fild keeps only 53 significand bits 170/11126

Rejections. Translation rejects 9 sequences with the expected diagnostic:

  • fild m16 at depth 8;
  • fistp m32, fist m16 and fisttp m64 on an empty stack;
  • fist m32 that leaves its value on the stack at the return;
  • fiadd m32, ficom m16, fbld m80 and fbstp m80.

Changes to existing tests. Two rejection cases of earlier x87 tests used forms that this PR accepts:

  • cpu_aot_x87_add's fild m16 becomes fbld m80;
  • cpu_aot_x87_moves's fist m32 becomes fbstp 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 56f01c6 on 2e7dfa4. main has since moved to ab5b8d4 (#310, #416 and #451). None of those touch the x87 files or tools/ffx_version_query.py, and this branch merges cleanly into ab5b8d4. 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).

cmake --build --preset dev --target cpu_x87_integers_lifted
ctest --preset dev --no-tests=error -R '^cpu_aot_x87_integers$' -V
11126 cases over 18 exports (output block and guards) matched the reference in native and translated DLLs; each export returned with control 0x027f and an empty stack
processor: AMD64 Family 25 Model 97 Stepping 2, AuthenticAMD
x87_call kept a live caller ST(0) value and control word around all 18 native and translated exports
control chopall: 1510/11126 mismatches
control roundtt: 676/11126 mismatches
control saturate: 1049/11126 mismatches
control nanzero: 60/11126 mismatches
control zext: 621/11126 mismatches
control fild53: 170/11126 mismatches
rejected 9 sequences outside the profile: fild m16 at depth 8; fistp m32 on an empty stack; fist m16 on an empty stack; fisttp m64 on an empty stack; fist m32 without a pop; fiadd m32; ficom m16; fbld m80; fbstp m80
  • x87 regressions: all seven x87 tests pass (x87_add, x87_mul, x87_div, x87_sub, x87_moves, x87_constants, x87_integers).
  • Full suite: ctest --preset dev --no-tests=error -j 8 --output-junit test-results.xml gives 177 passed, 1 failed and 2 capability skips out of 180. The run includes the GPU tests. The failure is gpu_alias_lookup_writes_hardware, the RDNA 4 array-atomics probe recorded in #369, which fails the same way on main. This change touches no GPU code.
  • Pure-Python checks: tests/form_coverage_check.py, tests/docs_links_check.py and tests/bidi_controls_check.py pass.
  • Fork CI: the Windows build-and-test, Windows /O2 build-and-test, Linux Python and gfx1013 LLVM jobs passed at 56f01c6 in the fork's Windows checks run 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.xml gives x64 153 of 948 forms, and 149 without cpu_aot_x87_integers. The added forms are fild, fist, fistp and fisttp.

Not measured:

  • Intel CPUs. All measurements are on one AMD Zen 4 part.
  • The other rounding modes, because the profile rejects fldcw. They would change fist and fistp, but not fisttp.
  • The status word, including the invalid-operation and precision flags, as in #422.

Overlap

I trial-merged this branch with all 90 open PRs. Each one conflicts in the same files against main and against this branch: the 18 that merge cleanly into main also 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 of tests/cpu/x87_add.py and tests/cpu/x87_moves.py, a CMake block after the cpu_aot_x87_constants test, and the x87 section of docs/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.md documents the integer conversions, the controls and the reproduction command.

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed.
  • I did not include game files, dumps, distribution links or secrets.

AI-assisted (Claude Code).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6538444601 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/452). Originally posted by [denn0x](https://github.com/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)`: - **`fild`** with an m16, m32 or m64 operand; - **`fistp` and `fisttp`** with an m16, m32 or m64 operand; - **`fist`** with 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. - **`fist` and `fistp`:** round `st(0)` to nearest-even, the rounding control the profile fixes. `fist` keeps `st(0)`. - **`fisttp`:** rounds toward zero regardless of the rounding control. - **Invalid values:** a NaN, an infinity, an unsupported encoding or a rounded value outside the destination range stores the integer indefinite, only the sign bit set. This is the masked invalid-operation response. There is no saturation. **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. - **Stores:** `store<uintN_t>(a, static_cast<uintN_t>(x87_to_integer(x87_slot[top], N, chop)))`, with `chop` true only for `fisttp`. `fistp` and `fisttp` then lower the depth. - **Depths:** `fild` at 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 unless `chop` is set, and checks the range against `2^(bits-1)`, which allows `-2^(bits-1)` itself. It returns the low `bits` bits of the two's-complement result. Both helpers compute in integers, like the rest of the file. ## Fixture `tests/cpu/x87_integers.asm` and `.def` are generated by `tests/cpu/x87_integers.py asm`, and `emit` checks that every decoded x87 instruction is the form its sequence names. The `.asm` registers its test on its first line (`; coverage: source cpu_aot_x87_integers`). The 18 exports cover: - `fild` of each width stored to m80, a 64-bit integer stored to m64 and m32, and a 32-bit integer added to an m80 value; - `fistp` and `fisttp` to each width, and `fist` to m16 and m32 followed by `fstp` m80 of the kept register; - round trips through `fild`/`fistp` m64 and `fild`/`fisttp` m16; - two stores from a two-value stack (`fistp` m32, then `fistp` m64); - `fild` m16 as the eighth value, with all eight values stored. **Model.** The model converts with `Fraction` arithmetic and shares no code with `tools/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: - **Integer inputs:** each width's extremes, small values, values just past the binary64 and binary32 significand widths, and random values. - **m80 inputs:** the special encodings, integers, halfway values, quarter steps, and values a small fraction past a half, around zero and each width's limits. - **Random:** 400 magnitudes from 2^-3 up to just below 2^67. **Negative controls.** Six mutated translations must each mismatch: | Control | Mutation | Mismatches | |---|---|---| | `chopall` | `fist` and `fistp` truncate like `fisttp` | 1510/11126 | | `roundtt` | `fisttp` rounds to nearest like `fistp` | 676/11126 | | `saturate` | an out-of-range value saturates instead of giving the integer indefinite | 1049/11126 | | `nanzero` | a NaN converts to 0 | 60/11126 | | `zext` | `fild` m16 and m32 zero-extend instead of sign-extending | 621/11126 | | `fild53` | `fild` keeps only 53 significand bits | 170/11126 | **Rejections.** Translation rejects 9 sequences with the expected diagnostic: - `fild` m16 at depth 8; - `fistp` m32, `fist` m16 and `fisttp` m64 on an empty stack; - `fist` m32 that leaves its value on the stack at the return; - `fiadd` m32, `ficom` m16, `fbld` m80 and `fbstp` m80. **Changes to existing tests.** Two rejection cases of earlier x87 tests used forms that this PR accepts: - `cpu_aot_x87_add`'s `fild m16` becomes `fbld m80`; - `cpu_aot_x87_moves`'s `fist m32` becomes `fbstp 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 `56f01c6` on `2e7dfa4`. `main` has since moved to `ab5b8d4` (#310, #416 and #451). None of those touch the x87 files or `tools/ffx_version_query.py`, and this branch merges cleanly into `ab5b8d4`. 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`). ```powershell cmake --build --preset dev --target cpu_x87_integers_lifted ctest --preset dev --no-tests=error -R '^cpu_aot_x87_integers$' -V ``` ```text 11126 cases over 18 exports (output block and guards) matched the reference in native and translated DLLs; each export returned with control 0x027f and an empty stack processor: AMD64 Family 25 Model 97 Stepping 2, AuthenticAMD x87_call kept a live caller ST(0) value and control word around all 18 native and translated exports control chopall: 1510/11126 mismatches control roundtt: 676/11126 mismatches control saturate: 1049/11126 mismatches control nanzero: 60/11126 mismatches control zext: 621/11126 mismatches control fild53: 170/11126 mismatches rejected 9 sequences outside the profile: fild m16 at depth 8; fistp m32 on an empty stack; fist m16 on an empty stack; fisttp m64 on an empty stack; fist m32 without a pop; fiadd m32; ficom m16; fbld m80; fbstp m80 ``` - **x87 regressions:** all seven x87 tests pass (`x87_add`, `x87_mul`, `x87_div`, `x87_sub`, `x87_moves`, `x87_constants`, `x87_integers`). - **Full suite:** `ctest --preset dev --no-tests=error -j 8 --output-junit test-results.xml` gives 177 passed, 1 failed and 2 capability skips out of 180. The run includes the GPU tests. The failure is `gpu_alias_lookup_writes_hardware`, the RDNA 4 array-atomics probe recorded in #369, which fails the same way on `main`. This change touches no GPU code. - **Pure-Python checks:** `tests/form_coverage_check.py`, `tests/docs_links_check.py` and `tests/bidi_controls_check.py` pass. - **Fork CI:** the Windows build-and-test, Windows `/O2` build-and-test, Linux Python and gfx1013 LLVM jobs passed at `56f01c6` in the fork's `Windows checks` run [44984887591](https://github.com/denn0x/Xbox2Windows/actions/runs/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.xml` gives x64 **153 of 948** forms, and 149 without `cpu_aot_x87_integers`. The added forms are `fild`, `fist`, `fistp` and `fisttp`. **Not measured:** - Intel CPUs. All measurements are on one AMD Zen 4 part. - The other rounding modes, because the profile rejects `fldcw`. They would change `fist` and `fistp`, but not `fisttp`. - The status word, including the invalid-operation and precision flags, as in #422. ## Overlap I trial-merged this branch with all 90 open PRs. Each one conflicts in the same files against `main` and against this branch: the 18 that merge cleanly into `main` also 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 of `tests/cpu/x87_add.py` and `tests/cpu/x87_moves.py`, a CMake block after the `cpu_aot_x87_constants` test, and the x87 section of `docs/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.md` documents the integer conversions, the controls and the reproduction command. ## Checklist - [x] This pull request covers one reviewable behavior. - [x] I included the exact command and result for every check I ran. - [x] I considered a negative control or failure case for a new fixture. - [x] I updated README/docs when a capability or build requirement changed. - [x] I did not include game files, dumps, distribution links or secrets. _AI-assisted (Claude Code)._
upstream-sync closed this pull request 2026-11-24 06:43:06 +00:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
R0Y00/Xbox2Windows!446
No description provided.