cpu: lower FRNDINT in the x87 profile #456

Merged
denn0x merged 1 commit from upstream-pr/462 into main 2026-11-22 05:21:22 +00:00
denn0x commented 2026-11-22 05:11:53 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by denn0x on 2026-11-22T05:11:53Z.


Change

Refs #406. This follows #460 (fsqrt). It adds frndint to the opt-in x87 profile in lower(..., x87=True).

Semantics.

  • Rounding: frndint rounds st(0) to an integral value under the rounding control, which the profile fixes at round to nearest-even. The result stays in the extended format.
  • Precision control: the 53-bit precision control does not apply. An integral result between 2^53 and 2^63 keeps all its bits, which the native runs confirm (control trunc53 below).
  • Already integral: zeros, infinities and values of 2^63 and above are returned unchanged.
  • Signs and NaNs: a value that rounds to zero keeps its sign. A NaN is quieted, and an unsupported encoding gives the indefinite.

Out of scope: fscale, fxtract, fprem, fprem1 and every control-word access. They remain rejected.

Lowering (tools/x87_lower.py). x87_slot[top] = x87_rndint(x87_slot[top]);, with an empty stack rejected as an underflow. It shares the one-operand path of fsqrt.

Helper (tools/x87_soft.h). x87_rndint shifts the significand right by the number of fraction bits. It rounds to nearest-even from the bits shifted out, or rounds to zero when the value is below one half. It then renormalizes with a leading-zero count. It does not call x87_round, because the precision control does not apply.

Fixture

tests/cpu/x87_rndint.asm and .def are generated by tests/cpu/x87_rndint.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_rndint). The 8 exports cover:

  • the rounded m80 value stored to m80, m64 and m32;
  • rounded m64 and m32 loads, stored back to the same format;
  • the fractional part x - rndint(x), using fld st(0) and fsubp;
  • frndint before fistp m64;
  • frndint at depth 8, with all eight values stored.

Model. The model rounds with Fraction arithmetic and shares no code with tools/x87_soft.h. The harness, input bytes, guards and live-value check are those of the earlier x87 PRs.

Cases. 7760 cases:

  • the m80 inputs of cpu_aot_x87_integers: integers, halves, quarter steps, and values just past a half, around zero and each integer width's limits;
  • values with a fraction between 2^53 and 2^63, and signaling NaNs;
  • m64 and m32 values from 2^-2 up to just below 2^53 and 2^24.

Negative controls. Five mutated translations must each mismatch:

Control Mutation Mismatches
chop truncation instead of nearest-even 2236/7760
tiesaway ties rounded away from zero 208/7760
zerosign +0 for a value that rounds to zero 321/7760
trunc53 the result cut to 53 significand bits 1207/7760
snan a signaling NaN returned unquieted 42/7760

Rejections. Translation rejects 5 sequences with the expected diagnostic: frndint on an empty stack, fnstenv, fldenv, fbld and fxsave.

Changes to existing tests. cpu_aot_x87_sub used frndint as a rejection case. It becomes fxsave, a state save that the profile will not lower, so the test keeps its number of rejections. Its case and control counts are unchanged.

Verification

Tested at 54da9dd on 44b3707 (the head of #460). main has since moved to d5af443 (the merges of #460, #457, #454 and #321), and none of those touch tools/ffx_version_query.py or any x87 file beyond #460 itself. This branch merges cleanly into d5af443. 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_rndint_lifted
ctest --preset dev --no-tests=error -R '^cpu_aot_x87_rndint$' -V
7760 cases over 8 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 8 native and translated exports
control chop: 2236/7760 mismatches
control tiesaway: 208/7760 mismatches
control zerosign: 321/7760 mismatches
control trunc53: 1207/7760 mismatches
control snan: 42/7760 mismatches
rejected 5 sequences outside the profile: frndint on an empty stack; fnstenv; fldenv; fbld; fxsave
  • x87 regressions: all ten x87 tests pass (x87_add, x87_mul, x87_div, x87_sub, x87_moves, x87_constants, x87_integers, x87_compare, x87_sqrt, x87_rndint).
  • Full suite: ctest --preset dev --no-tests=error -j 8 --output-junit test-results.xml gives 184 passed, 1 failed and 2 capability skips out of 187. 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 54da9dd in the fork's Windows checks run 44987344719. Both Windows jobs ran and passed all ten x87 tests (157 passed and 2 capability skips, bvh_wave64_warp and wave_shuffle_warp, out of 159 in each).

Coverage. python tools/form_coverage.py out/build/dev/test-results.xml gives x64 159 of 948 forms, and 158 without cpu_aot_x87_rndint. The added form is frndint.

Not measured:

  • Intel CPUs. All measurements are on one AMD Zen 4 part.
  • The other rounding modes, because the profile rejects fldcw.
  • The status word, including the precision flag, as in #422.

Overlap

I trial-merged this branch with all 85 open PRs, against main merged with this branch. Each one conflicts in the same files against main and against this branch: the 8 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 of tests/cpu/x87_sub.py, a CMake block after the cpu_aot_x87_sqrt test, and the x87 section of docs/experiments.md.

Evidence

The fixture is synthetic and original to this repository (MIT). docs/experiments.md documents frndint, the precision-control measurement, 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:6538760318 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/462). Originally posted by [denn0x](https://github.com/denn0x) on 2026-11-22T05:11:53Z. --- ## Change Refs #406. This follows #460 (`fsqrt`). It adds `frndint` to the opt-in x87 profile in `lower(..., x87=True)`. **Semantics.** - **Rounding:** `frndint` rounds `st(0)` to an integral value under the rounding control, which the profile fixes at round to nearest-even. The result stays in the extended format. - **Precision control:** the 53-bit precision control does not apply. An integral result between 2^53 and 2^63 keeps all its bits, which the native runs confirm (control `trunc53` below). - **Already integral:** zeros, infinities and values of 2^63 and above are returned unchanged. - **Signs and NaNs:** a value that rounds to zero keeps its sign. A NaN is quieted, and an unsupported encoding gives the indefinite. **Out of scope:** `fscale`, `fxtract`, `fprem`, `fprem1` and every control-word access. They remain rejected. **Lowering (`tools/x87_lower.py`).** `x87_slot[top] = x87_rndint(x87_slot[top]);`, with an empty stack rejected as an underflow. It shares the one-operand path of `fsqrt`. **Helper (`tools/x87_soft.h`).** `x87_rndint` shifts the significand right by the number of fraction bits. It rounds to nearest-even from the bits shifted out, or rounds to zero when the value is below one half. It then renormalizes with a leading-zero count. It does not call `x87_round`, because the precision control does not apply. ## Fixture `tests/cpu/x87_rndint.asm` and `.def` are generated by `tests/cpu/x87_rndint.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_rndint`). The 8 exports cover: - the rounded m80 value stored to m80, m64 and m32; - rounded m64 and m32 loads, stored back to the same format; - the fractional part `x - rndint(x)`, using `fld st(0)` and `fsubp`; - `frndint` before `fistp` m64; - `frndint` at depth 8, with all eight values stored. **Model.** The model rounds with `Fraction` arithmetic and shares no code with `tools/x87_soft.h`. The harness, input bytes, guards and live-value check are those of the earlier x87 PRs. **Cases.** 7760 cases: - the m80 inputs of `cpu_aot_x87_integers`: integers, halves, quarter steps, and values just past a half, around zero and each integer width's limits; - values with a fraction between 2^53 and 2^63, and signaling NaNs; - m64 and m32 values from 2^-2 up to just below 2^53 and 2^24. **Negative controls.** Five mutated translations must each mismatch: | Control | Mutation | Mismatches | |---|---|---| | `chop` | truncation instead of nearest-even | 2236/7760 | | `tiesaway` | ties rounded away from zero | 208/7760 | | `zerosign` | +0 for a value that rounds to zero | 321/7760 | | `trunc53` | the result cut to 53 significand bits | 1207/7760 | | `snan` | a signaling NaN returned unquieted | 42/7760 | **Rejections.** Translation rejects 5 sequences with the expected diagnostic: `frndint` on an empty stack, `fnstenv`, `fldenv`, `fbld` and `fxsave`. **Changes to existing tests.** `cpu_aot_x87_sub` used `frndint` as a rejection case. It becomes `fxsave`, a state save that the profile will not lower, so the test keeps its number of rejections. Its case and control counts are unchanged. ## Verification Tested at `54da9dd` on `44b3707` (the head of #460). `main` has since moved to `d5af443` (the merges of #460, #457, #454 and #321), and none of those touch `tools/ffx_version_query.py` or any x87 file beyond #460 itself. This branch merges cleanly into `d5af443`. 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_rndint_lifted ctest --preset dev --no-tests=error -R '^cpu_aot_x87_rndint$' -V ``` ```text 7760 cases over 8 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 8 native and translated exports control chop: 2236/7760 mismatches control tiesaway: 208/7760 mismatches control zerosign: 321/7760 mismatches control trunc53: 1207/7760 mismatches control snan: 42/7760 mismatches rejected 5 sequences outside the profile: frndint on an empty stack; fnstenv; fldenv; fbld; fxsave ``` - **x87 regressions:** all ten x87 tests pass (`x87_add`, `x87_mul`, `x87_div`, `x87_sub`, `x87_moves`, `x87_constants`, `x87_integers`, `x87_compare`, `x87_sqrt`, `x87_rndint`). - **Full suite:** `ctest --preset dev --no-tests=error -j 8 --output-junit test-results.xml` gives 184 passed, 1 failed and 2 capability skips out of 187. 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 `54da9dd` in the fork's `Windows checks` run [44987344719](https://github.com/denn0x/Xbox2Windows/actions/runs/44987344719). Both Windows jobs ran and passed all ten x87 tests (157 passed and 2 capability skips, `bvh_wave64_warp` and `wave_shuffle_warp`, out of 159 in each). **Coverage.** `python tools/form_coverage.py out/build/dev/test-results.xml` gives x64 **159 of 948** forms, and 158 without `cpu_aot_x87_rndint`. The added form is `frndint`. **Not measured:** - Intel CPUs. All measurements are on one AMD Zen 4 part. - The other rounding modes, because the profile rejects `fldcw`. - The status word, including the precision flag, as in #422. ## Overlap I trial-merged this branch with all 85 open PRs, against `main` merged with this branch. Each one conflicts in the same files against `main` and against this branch: the 8 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 of `tests/cpu/x87_sub.py`, a CMake block after the `cpu_aot_x87_sqrt` test, and the x87 section of `docs/experiments.md`. ## Evidence The fixture is synthetic and original to this repository (MIT). `docs/experiments.md` documents `frndint`, the precision-control measurement, 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:09 +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!456
No description provided.