cpu: lower the integer-operand arithmetic of the x87 profile #493

Merged
denn0x merged 1 commit from upstream-pr/499 into main 2026-11-22 08:10:56 +00:00
denn0x commented 2026-11-22 07:51:43 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by denn0x on 2026-11-22T07:51:43Z.


Change

Refs #406. This is the next x87 step after #476 (fcmovcc), which has merged. It adds the integer-operand arithmetic to the opt-in x87 profile in lower(..., x87=True): fiadd, fisub, fisubr, fimul, fidiv and fidivr with an m16 or m32 integer operand.

Semantics. Each form converts the integer exactly, as fild does, then behaves like its floating-point form with st(0) as the destination.

  • Operand order: fisubr computes the integer minus st(0), and fidivr the integer divided by st(0).
  • Rounding: the result rounds to 53 bits under the precision control, like fadd with an m64 operand.
  • Zero integer: gives +0, so -0 + 0 is +0 and -0 - 0 is -0.
  • Special values: NaN, infinity, invalid-operation and divide-by-zero follow the rules of fadd, fsub, fmul and fdiv. An integer is never a NaN, so only st(0) supplies one.

The native runs confirm each of these rules.

Out of scope: ficom and ficomp write the status word, which the profile does not model, so they remain rejected.

Lowering (tools/x87_lower.py). x87_slot[top] = x87_add(x87_slot[top], x87_from_integer(static_cast<int16_t>(load<uint16_t>(a))));, with the form's helper and width. An empty stack is an underflow, and the depth is unchanged. No helper is added to tools/x87_soft.h.

Fixture

tests/cpu/x87_iarith.asm and .def are generated by tests/cpu/x87_iarith.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_iarith). The 15 exports cover:

  • each of the six forms at each width on an m80 value;
  • fimul with an m32 integer on an m64 value stored back to m64;
  • fiadd with an m32 integer followed by fisub with an m16 integer;
  • fidivr at depth 8, with all eight values stored.

Model. The model converts the integer with the fild model of cpu_aot_x87_integers and applies the Fraction models of the add, subtract, multiply and divide tests. It 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. 28512 cases:

  • every pair of the 34 special encodings with the edge integers of each width: zero, small values of both signs, powers of two, the extremes and random values;
  • st(0) values whose low significand bits sit at or beside a 53-bit rounding tie, full 64-bit significands, values at the edges of the exponent range, denormals and random values, with every third edge integer;
  • 300 random pairs per width;
  • m64 values for the m64 export, random values for the chain and random m64 values at depth 8.

The two bytes after each m16 integer are never its sign extension.

Negative controls. Six mutated translations must each mismatch:

Control Mutation Mismatches
subrev fisub computes the integer minus st(0) 4565/28512
divrev fidiv divides the integer by st(0) 3980/28512
zext16 an m16 integer is zero-extended 4593/28512
zext32 an m32 integer is zero-extended 5609/28512
wide16 an m16 integer is read as 32 bits 9857/28512
pc64 the result keeps 64 significand bits 12161/28512

Rejections. Translation rejects 5 sequences with the expected diagnostic: ficom m16, ficomp m32, fiadd m32 and fidivr m16 on an empty stack, and fisub m32 that leaves a value at the return.

Changes to existing tests. The rejection lists of four tests used the new forms. Each case is replaced by an instruction that stays outside the profile, and each list keeps its length:

Test Replaced Now
cpu_aot_x87_div fidiv m32, fidivr m16, fiadd m32 fldenv, fdecstp, fincstp
cpu_aot_x87_mul fimul m32, fimul m16, fiadd m32 fldenv, fdecstp, fincstp
cpu_aot_x87_sub fisub m32, fisubr m16 fldenv, fdecstp
cpu_aot_x87_integers fiadd m32 fldenv

Their case and control counts are unchanged. The matching sentences in docs/experiments.md name the new instructions.

Verification

Tested at 3b7f87a, one commit on 7f13b7f (main). main has since moved to 329510f, and the branch merges into it without conflicts. Of the files those seven commits change, only CMakeLists.txt and docs/experiments.md are also changed here, and in other sections. The machine is 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_iarith_lifted
ctest --preset dev --no-tests=error -R '^cpu_aot_x87_iarith$' -V
28512 cases over 15 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 15 native and translated exports
control subrev: 4565/28512 mismatches
control divrev: 3980/28512 mismatches
control zext16: 4593/28512 mismatches
control zext32: 5609/28512 mismatches
control wide16: 9857/28512 mismatches
control pc64: 12161/28512 mismatches
rejected 5 sequences outside the profile: ficom m16; ficomp m32; fiadd m32 on an empty stack; fidivr m16 on an empty stack; fisub m32 with two values and one store
  • x87 regressions: all fourteen x87 tests pass at 3b7f87a (ctest --preset dev --no-tests=error -R '^cpu_aot_x87_'), including the four with replaced rejection cases.

  • Full suite: ctest --preset dev --no-tests=error -j 8 --output-junit test-results.xml gives 213 passed, 2 failed and 2 capability skips (bvh_wave64_warp, wave_shuffle_warp) out of 217 at 3b7f87a. The run includes the GPU tests on an RX 9070 XT. Both failures are RDNA 4 results that docs/experiments.md records as failing by design:

    • gpu_alias_lookup_writes_hardware, the array-atomics probe of #369;
    • dxr_opacity_hardware, with committed_t_plus_tmin=43 as described in #216. It fails the same way on 7f13b7f itself (run).

    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 at 3b7f87a locally and in the Linux job of the fork CI run below.

  • Fork CI: the Windows build-and-test, Windows /O2 build-and-test, Linux Python and gfx1013 LLVM jobs passed at 3b7f87a in the fork's Windows checks run 44999346622. Both Windows jobs ran and passed all fourteen x87 tests (179 passed and 2 capability skips, bvh_wave64_warp and wave_shuffle_warp, out of 181 in each).

Coverage. python tools/form_coverage.py out/build/dev/test-results.xml gives x64 195 of 948 forms, and 189 without cpu_aot_x87_iarith. The added forms are fiadd, fisub, fisubr, fimul, fidiv and fidivr.

Not measured:

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

Overlap

I trial-merged this branch with all 83 open PRs. Each one conflicts in the same files against main and against this branch: the 48 that merge cleanly into main also merge cleanly here, and this branch adds no conflict to the others. The changes stay inside tools/x87_lower.py, the new fixture, the rejection lists of four x87 tests, a CMake block after the cpu_aot_x87_fcmov test, and the x87 section of docs/experiments.md.

Evidence

The fixture is synthetic and original to this repository (MIT). docs/experiments.md documents the six forms, 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:6539985298 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/499). Originally posted by [denn0x](https://github.com/denn0x) on 2026-11-22T07:51:43Z. --- ## Change Refs #406. This is the next x87 step after #476 (`fcmovcc`), which has merged. It adds the integer-operand arithmetic to the opt-in x87 profile in `lower(..., x87=True)`: `fiadd`, `fisub`, `fisubr`, `fimul`, `fidiv` and `fidivr` with an m16 or m32 integer operand. **Semantics.** Each form converts the integer exactly, as `fild` does, then behaves like its floating-point form with `st(0)` as the destination. - **Operand order:** `fisubr` computes the integer minus `st(0)`, and `fidivr` the integer divided by `st(0)`. - **Rounding:** the result rounds to 53 bits under the precision control, like `fadd` with an m64 operand. - **Zero integer:** gives +0, so `-0 + 0` is +0 and `-0 - 0` is -0. - **Special values:** NaN, infinity, invalid-operation and divide-by-zero follow the rules of `fadd`, `fsub`, `fmul` and `fdiv`. An integer is never a NaN, so only `st(0)` supplies one. The native runs confirm each of these rules. **Out of scope:** `ficom` and `ficomp` write the status word, which the profile does not model, so they remain rejected. **Lowering (`tools/x87_lower.py`).** `x87_slot[top] = x87_add(x87_slot[top], x87_from_integer(static_cast<int16_t>(load<uint16_t>(a))));`, with the form's helper and width. An empty stack is an underflow, and the depth is unchanged. No helper is added to `tools/x87_soft.h`. ## Fixture `tests/cpu/x87_iarith.asm` and `.def` are generated by `tests/cpu/x87_iarith.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_iarith`). The 15 exports cover: - each of the six forms at each width on an m80 value; - `fimul` with an m32 integer on an m64 value stored back to m64; - `fiadd` with an m32 integer followed by `fisub` with an m16 integer; - `fidivr` at depth 8, with all eight values stored. **Model.** The model converts the integer with the `fild` model of `cpu_aot_x87_integers` and applies the `Fraction` models of the add, subtract, multiply and divide tests. It 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.** 28512 cases: - every pair of the 34 special encodings with the edge integers of each width: zero, small values of both signs, powers of two, the extremes and random values; - `st(0)` values whose low significand bits sit at or beside a 53-bit rounding tie, full 64-bit significands, values at the edges of the exponent range, denormals and random values, with every third edge integer; - 300 random pairs per width; - m64 values for the m64 export, random values for the chain and random m64 values at depth 8. The two bytes after each m16 integer are never its sign extension. **Negative controls.** Six mutated translations must each mismatch: | Control | Mutation | Mismatches | |---|---|---| | `subrev` | `fisub` computes the integer minus `st(0)` | 4565/28512 | | `divrev` | `fidiv` divides the integer by `st(0)` | 3980/28512 | | `zext16` | an m16 integer is zero-extended | 4593/28512 | | `zext32` | an m32 integer is zero-extended | 5609/28512 | | `wide16` | an m16 integer is read as 32 bits | 9857/28512 | | `pc64` | the result keeps 64 significand bits | 12161/28512 | **Rejections.** Translation rejects 5 sequences with the expected diagnostic: `ficom m16`, `ficomp m32`, `fiadd m32` and `fidivr m16` on an empty stack, and `fisub m32` that leaves a value at the return. **Changes to existing tests.** The rejection lists of four tests used the new forms. Each case is replaced by an instruction that stays outside the profile, and each list keeps its length: | Test | Replaced | Now | |---|---|---| | `cpu_aot_x87_div` | `fidiv m32`, `fidivr m16`, `fiadd m32` | `fldenv`, `fdecstp`, `fincstp` | | `cpu_aot_x87_mul` | `fimul m32`, `fimul m16`, `fiadd m32` | `fldenv`, `fdecstp`, `fincstp` | | `cpu_aot_x87_sub` | `fisub m32`, `fisubr m16` | `fldenv`, `fdecstp` | | `cpu_aot_x87_integers` | `fiadd m32` | `fldenv` | Their case and control counts are unchanged. The matching sentences in `docs/experiments.md` name the new instructions. ## Verification Tested at `3b7f87a`, one commit on `7f13b7f` (`main`). `main` has since moved to `329510f`, and the branch merges into it without conflicts. Of the files those seven commits change, only `CMakeLists.txt` and `docs/experiments.md` are also changed here, and in other sections. The machine is 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_iarith_lifted ctest --preset dev --no-tests=error -R '^cpu_aot_x87_iarith$' -V ``` ```text 28512 cases over 15 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 15 native and translated exports control subrev: 4565/28512 mismatches control divrev: 3980/28512 mismatches control zext16: 4593/28512 mismatches control zext32: 5609/28512 mismatches control wide16: 9857/28512 mismatches control pc64: 12161/28512 mismatches rejected 5 sequences outside the profile: ficom m16; ficomp m32; fiadd m32 on an empty stack; fidivr m16 on an empty stack; fisub m32 with two values and one store ``` - **x87 regressions:** all fourteen x87 tests pass at `3b7f87a` (`ctest --preset dev --no-tests=error -R '^cpu_aot_x87_'`), including the four with replaced rejection cases. - **Full suite:** `ctest --preset dev --no-tests=error -j 8 --output-junit test-results.xml` gives 213 passed, 2 failed and 2 capability skips (`bvh_wave64_warp`, `wave_shuffle_warp`) out of 217 at `3b7f87a`. The run includes the GPU tests on an RX 9070 XT. Both failures are RDNA 4 results that `docs/experiments.md` records as failing by design: - `gpu_alias_lookup_writes_hardware`, the array-atomics probe of #369; - `dxr_opacity_hardware`, with `committed_t_plus_tmin=43` as described in #216. It fails the same way on `7f13b7f` itself ([run](https://github.com/R0Y00/Xbox2Windows/pull/216#issuecomment-7076078123)). 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 at `3b7f87a` locally and in the Linux job of the fork CI run below. - **Fork CI:** the Windows build-and-test, Windows `/O2` build-and-test, Linux Python and gfx1013 LLVM jobs passed at `3b7f87a` in the fork's `Windows checks` run [44999346622](https://github.com/denn0x/Xbox2Windows/actions/runs/44999346622). Both Windows jobs ran and passed all fourteen x87 tests (179 passed and 2 capability skips, `bvh_wave64_warp` and `wave_shuffle_warp`, out of 181 in each). **Coverage.** `python tools/form_coverage.py out/build/dev/test-results.xml` gives x64 **195 of 948** forms, and 189 without `cpu_aot_x87_iarith`. The added forms are `fiadd`, `fisub`, `fisubr`, `fimul`, `fidiv` and `fidivr`. **Not measured:** - Intel CPUs. All measurements are on one AMD Zen 4 part. - The other rounding and precision modes, because the profile rejects `fldcw`. - The status word, as in #422. ## Overlap I trial-merged this branch with all 83 open PRs. Each one conflicts in the same files against `main` and against this branch: the 48 that merge cleanly into `main` also merge cleanly here, and this branch adds no conflict to the others. The changes stay inside `tools/x87_lower.py`, the new fixture, the rejection lists of four x87 tests, a CMake block after the `cpu_aot_x87_fcmov` test, and the x87 section of `docs/experiments.md`. ## Evidence The fixture is synthetic and original to this repository (MIT). `docs/experiments.md` documents the six forms, 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:22 +00:00
R0Y00 commented 2026-11-24 06:43:22 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-22T08:10:52Z.

Review: APPROVED


Reviewed 3b7f87a and reproduced cpu_aot_x87_iarith on Intel64 Family 6 Model 183 Stepping 1 with x64 MSVC 19.51. All 28,512 inputs across 15 exports matched the independent reference in both native and translated DLLs. Each returned with the expected control word and empty stack. All six mutations produced mismatches, and all five unsupported sequences were rejected. This independently adds an Intel result to the author's AMD measurement.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6011173113 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/499#pullrequestreview-6011173113). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-22T08:10:52Z. **Review:** APPROVED --- Reviewed 3b7f87a and reproduced cpu_aot_x87_iarith on Intel64 Family 6 Model 183 Stepping 1 with x64 MSVC 19.51. All 28,512 inputs across 15 exports matched the independent reference in both native and translated DLLs. Each returned with the expected control word and empty stack. All six mutations produced mismatches, and all five unsupported sequences were rejected. This independently adds an Intel result to the author's AMD measurement.
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!493
No description provided.