cpu: lower x87 register moves, FCHS and FABS in the x87 profile #437

Merged
denn0x merged 1 commit from upstream-pr/443 into main 2026-11-22 04:06:40 +00:00
denn0x commented 2026-11-22 03:36:59 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by denn0x on 2026-11-22T03:36:59Z.


Change

Refs #406. This follows #422 (add), #431 (multiply), #433 (divide) and #440 (subtract). It adds register data movement to the opt-in x87 profile in lower(..., x87=True). The profile now also accepts:

  • fld st(i): pushes a copy of st(i);
  • fst st(i): copies st(0) into st(i);
  • fstp st(i): the same copy, then a pop;
  • fxch st(i): exchanges st(0) and st(i);
  • fchs and fabs;
  • fst with an m32 or m64 operand: stores st(0) without popping.

Out of scope: constants (fld1, fldz, ...), integer conversions (fild, fist, ...), comparisons, ffree and fincstp/fdecstp. They remain rejected, and the check confirms it for several of them. The entry/exit contract and the static stack depths are unchanged.

Lowering (tools/x87_lower.py). Each new form is a static slot operation, since the stack depth is known at translation time:

  • fld st(i): x87_slot[d] = x87_slot[d-1-i];
  • fst st(i) and fstp st(i): x87_slot[top-i] = x87_slot[top], and fstp then lowers the depth;
  • fxch st(i): a swap of two slots.

Rejections (tools/x87_lower.py).

  • Stack depth: an st(i) operand at or beyond the current depth is rejected as an underflow, and fld st(i) at depth 8 as an overflow.
  • Alias encodings: the lifter keys on the iced mnemonic, so it would otherwise accept the undocumented aliases of fstp st(i) (DF D0+i, DF D8+i) and of fxch st(i) (DD C8+i, DF C8+i). Assemblers do not emit them and the fixture does not measure them, so translation rejects them by encoding (ALIASES).

Helpers (tools/x87_soft.h).

  • x87_chs and x87_abs: these change only the sign bit, of every encoding. Zeros, NaNs (signaling stays signaling), unnormals and pseudo-denormals keep their other bits.
  • Copies and fst: register copies move the whole 80-bit value. fst m32/m64 reuses the existing x87_to_f32/x87_to_f64 rounding of fstp.

Fixture

tests/cpu/x87_moves.asm and .def are generated by tests/cpu/x87_moves.py asm, and emit checks that every decoded x87 instruction is the form its sequence names. The 26 exports cover:

  • fld st(0), st(1) and st(2), and fld st(6) at depth 7;
  • fst st(1) and st(2);
  • fstp st(0), st(1) and st(2);
  • fxch st(0), st(1) and st(2), and fxch st(7) at depth 8;
  • fchs and fabs, each stored to m80, m64 and m32, and fchs; fabs; fchs;
  • fst m32 and m64, and fst m64; fst m32, each followed by fstp m80 of the register;
  • movement combined with arithmetic: a + (-b), |a| * |b|, and fsubp after fxch.

The depth-7 and depth-8 exports load eight m64 values and store all eight.

Harness and checks. As in the earlier x87 PRs:

  • the exports run through the fnsave/frstor harness with control word 0x027F;
  • native and translated DLLs get identical input bytes;
  • the output block and 64 guard bytes on each side are compared against a model;
  • the live-value check runs around all 26 exports.

The model in tests/cpu/x87_moves.py moves raw encodings and shares no code with tools/x87_soft.h. For the three arithmetic exports it reuses the Fraction models of the add, multiply and subtract tests.

Cases. 20224 cases:

  • every pair of the 34 special encodings, plus 300 random pairs, through the two-value exports;
  • 300 triples of special encodings and 200 random triples through the three-value exports;
  • the m80 singles of cpu_aot_x87_add and 200 random values through the one-value exports;
  • 300 blocks of eight m64 values, drawn from the special and random binary64 encodings.

Negative controls. Seven mutated translations must each mismatch:

Control Mutation Mismatches
chssub fchs computes 0 - x 969/20224
absnan fabs leaves -0 and negative NaNs unchanged 225/20224
fxchnone fxch does nothing 3116/20224
fldtop fld st(i) always copies st(0) 2212/20224
fstpdrop fstp st(i) only pops 1922/20224
fstwrite fst m32/m64 also writes the rounded value back to st(0) 1357/20224
copyquiet register copies quiet a signaling NaN 267/20224

Rejections. Translation rejects 16 sequences with the expected diagnostic:

  • fld st(1) with one value, and fld st(7) with seven values;
  • fld st(0) at depth 8;
  • fst st(2) with two values;
  • fstp st(0) on an empty stack;
  • fxch st(1) with one value;
  • fchs and fabs on an empty stack;
  • the four alias encodings DF D1, DF D9, DD C9 and DF C9;
  • fist m32, fld1, ffree st(1) and fdecstp.

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

  • cpu_aot_x87_add's fld st(1) becomes fild m16;
  • cpu_aot_x87_sub's fchs and fabs become frndint and fscale.

Each replacement is still outside the profile, so every test keeps its number of rejections. Their case and control counts are unchanged.

Verification

Tested at 2236668 on fb7547c. 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_moves_lifted
ctest --preset dev --no-tests=error -R '^cpu_aot_x87_moves$' -V
20224 cases over 26 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 26 native and translated exports
control chssub: 969/20224 mismatches
control absnan: 225/20224 mismatches
control fxchnone: 3116/20224 mismatches
control fldtop: 2212/20224 mismatches
control fstpdrop: 1922/20224 mismatches
control fstwrite: 1357/20224 mismatches
control copyquiet: 267/20224 mismatches
rejected 16 sequences outside the profile: fld st(1) with one value; fld st(0) at depth 8; fst st(2) with two values; fstp st(0) on an empty stack; fxch st(1) with one value; fchs on an empty stack; fabs on an empty stack; fld st(7) with seven values; fstp st(1) as DF D1; fstp st(1) as DF D9; fxch st(1) as DD C9; fxch st(1) as DF C9; fist m32; fld1; ffree st(1); fdecstp
  • x87 regressions: all five x87 tests pass (x87_add, x87_mul, x87_div, x87_sub, x87_moves).
  • Full suite: ctest --preset dev --no-tests=error -j 8 --output-junit <repo>/out/test-results.xml gives 165 passed, 1 failed and 2 capability skips out of 168. 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, Linux Python and gfx1013 LLVM jobs passed at 2236668 in the fork's Windows checks run 44981173792. All five x87 tests ran and passed there (tests 83 to 87 of 140).

Coverage. python tools/form_coverage.py out/test-results.xml gives x64 131 of 948 forms, and 127 without cpu_aot_x87_moves. The added forms are fst, fxch, fchs and fabs. fld and fstp were already covered.

Not measured:

  • Intel CPUs. All measurements are on one AMD Zen 4 part.
  • st(3) to st(7) operands, apart from fld st(6) and fxch st(7).
  • The status word, including C1 after fxch, fchs and fabs, as in #422.

Overlap

I trial-merged this branch with all 46 open cpu: PRs. Every merge that is clean against main is also clean against this branch. The changes stay inside the x87 files, one CPU_FIXTURES line after "x87_sub", a CMake block after the cpu_aot_x87_sub test, and the x87 section of docs/experiments.md. No open PR touches x87.

Evidence

The fixture is synthetic and original to this repository (MIT). docs/experiments.md documents the data-movement forms, the alias rejection, 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:6538001765 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/443). Originally posted by [denn0x](https://github.com/denn0x) on 2026-11-22T03:36:59Z. --- ## Change Refs #406. This follows #422 (add), #431 (multiply), #433 (divide) and #440 (subtract). It adds register data movement to the opt-in x87 profile in `lower(..., x87=True)`. The profile now also accepts: - **`fld st(i)`:** pushes a copy of `st(i)`; - **`fst st(i)`:** copies `st(0)` into `st(i)`; - **`fstp st(i)`:** the same copy, then a pop; - **`fxch st(i)`:** exchanges `st(0)` and `st(i)`; - **`fchs` and `fabs`;** - **`fst` with an m32 or m64 operand:** stores `st(0)` without popping. **Out of scope:** constants (`fld1`, `fldz`, ...), integer conversions (`fild`, `fist`, ...), comparisons, `ffree` and `fincstp`/`fdecstp`. They remain rejected, and the check confirms it for several of them. The entry/exit contract and the static stack depths are unchanged. **Lowering (`tools/x87_lower.py`).** Each new form is a static slot operation, since the stack depth is known at translation time: - **`fld st(i)`:** `x87_slot[d] = x87_slot[d-1-i]`; - **`fst st(i)` and `fstp st(i)`:** `x87_slot[top-i] = x87_slot[top]`, and `fstp` then lowers the depth; - **`fxch st(i)`:** a swap of two slots. **Rejections (`tools/x87_lower.py`).** - **Stack depth:** an `st(i)` operand at or beyond the current depth is rejected as an underflow, and `fld st(i)` at depth 8 as an overflow. - **Alias encodings:** the lifter keys on the iced mnemonic, so it would otherwise accept the undocumented aliases of `fstp st(i)` (`DF D0+i`, `DF D8+i`) and of `fxch st(i)` (`DD C8+i`, `DF C8+i`). Assemblers do not emit them and the fixture does not measure them, so translation rejects them by encoding (`ALIASES`). **Helpers (`tools/x87_soft.h`).** - **`x87_chs` and `x87_abs`:** these change only the sign bit, of every encoding. Zeros, NaNs (signaling stays signaling), unnormals and pseudo-denormals keep their other bits. - **Copies and `fst`:** register copies move the whole 80-bit value. `fst` m32/m64 reuses the existing `x87_to_f32`/`x87_to_f64` rounding of `fstp`. ## Fixture `tests/cpu/x87_moves.asm` and `.def` are generated by `tests/cpu/x87_moves.py asm`, and `emit` checks that every decoded x87 instruction is the form its sequence names. The 26 exports cover: - `fld st(0)`, `st(1)` and `st(2)`, and `fld st(6)` at depth 7; - `fst st(1)` and `st(2)`; - `fstp st(0)`, `st(1)` and `st(2)`; - `fxch st(0)`, `st(1)` and `st(2)`, and `fxch st(7)` at depth 8; - `fchs` and `fabs`, each stored to m80, m64 and m32, and `fchs; fabs; fchs`; - `fst` m32 and m64, and `fst m64; fst m32`, each followed by `fstp` m80 of the register; - movement combined with arithmetic: `a + (-b)`, `|a| * |b|`, and `fsubp` after `fxch`. The depth-7 and depth-8 exports load eight m64 values and store all eight. **Harness and checks.** As in the earlier x87 PRs: - the exports run through the `fnsave`/`frstor` harness with control word `0x027F`; - native and translated DLLs get identical input bytes; - the output block and 64 guard bytes on each side are compared against a model; - the live-value check runs around all 26 exports. The model in `tests/cpu/x87_moves.py` moves raw encodings and shares no code with `tools/x87_soft.h`. For the three arithmetic exports it reuses the `Fraction` models of the add, multiply and subtract tests. **Cases.** 20224 cases: - every pair of the 34 special encodings, plus 300 random pairs, through the two-value exports; - 300 triples of special encodings and 200 random triples through the three-value exports; - the m80 singles of `cpu_aot_x87_add` and 200 random values through the one-value exports; - 300 blocks of eight m64 values, drawn from the special and random binary64 encodings. **Negative controls.** Seven mutated translations must each mismatch: | Control | Mutation | Mismatches | |---|---|---| | `chssub` | `fchs` computes `0 - x` | 969/20224 | | `absnan` | `fabs` leaves -0 and negative NaNs unchanged | 225/20224 | | `fxchnone` | `fxch` does nothing | 3116/20224 | | `fldtop` | `fld st(i)` always copies `st(0)` | 2212/20224 | | `fstpdrop` | `fstp st(i)` only pops | 1922/20224 | | `fstwrite` | `fst` m32/m64 also writes the rounded value back to `st(0)` | 1357/20224 | | `copyquiet` | register copies quiet a signaling NaN | 267/20224 | **Rejections.** Translation rejects 16 sequences with the expected diagnostic: - `fld st(1)` with one value, and `fld st(7)` with seven values; - `fld st(0)` at depth 8; - `fst st(2)` with two values; - `fstp st(0)` on an empty stack; - `fxch st(1)` with one value; - `fchs` and `fabs` on an empty stack; - the four alias encodings `DF D1`, `DF D9`, `DD C9` and `DF C9`; - `fist m32`, `fld1`, `ffree st(1)` and `fdecstp`. **Changes to existing tests.** Three rejection cases of the earlier x87 tests used forms that this PR accepts: - `cpu_aot_x87_add`'s `fld st(1)` becomes `fild m16`; - `cpu_aot_x87_sub`'s `fchs` and `fabs` become `frndint` and `fscale`. Each replacement is still outside the profile, so every test keeps its number of rejections. Their case and control counts are unchanged. ## Verification Tested at `2236668` on `fb7547c`. 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_moves_lifted ctest --preset dev --no-tests=error -R '^cpu_aot_x87_moves$' -V ``` ```text 20224 cases over 26 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 26 native and translated exports control chssub: 969/20224 mismatches control absnan: 225/20224 mismatches control fxchnone: 3116/20224 mismatches control fldtop: 2212/20224 mismatches control fstpdrop: 1922/20224 mismatches control fstwrite: 1357/20224 mismatches control copyquiet: 267/20224 mismatches rejected 16 sequences outside the profile: fld st(1) with one value; fld st(0) at depth 8; fst st(2) with two values; fstp st(0) on an empty stack; fxch st(1) with one value; fchs on an empty stack; fabs on an empty stack; fld st(7) with seven values; fstp st(1) as DF D1; fstp st(1) as DF D9; fxch st(1) as DD C9; fxch st(1) as DF C9; fist m32; fld1; ffree st(1); fdecstp ``` - **x87 regressions:** all five x87 tests pass (`x87_add`, `x87_mul`, `x87_div`, `x87_sub`, `x87_moves`). - **Full suite:** `ctest --preset dev --no-tests=error -j 8 --output-junit <repo>/out/test-results.xml` gives 165 passed, 1 failed and 2 capability skips out of 168. 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, Linux Python and gfx1013 LLVM jobs passed at `2236668` in the fork's `Windows checks` run [44981173792](https://github.com/denn0x/Xbox2Windows/actions/runs/44981173792). All five x87 tests ran and passed there (tests 83 to 87 of 140). **Coverage.** `python tools/form_coverage.py out/test-results.xml` gives x64 **131 of 948** forms, and 127 without `cpu_aot_x87_moves`. The added forms are `fst`, `fxch`, `fchs` and `fabs`. `fld` and `fstp` were already covered. **Not measured:** - Intel CPUs. All measurements are on one AMD Zen 4 part. - `st(3)` to `st(7)` operands, apart from `fld st(6)` and `fxch st(7)`. - The status word, including C1 after `fxch`, `fchs` and `fabs`, as in #422. ## Overlap I trial-merged this branch with all 46 open `cpu:` PRs. Every merge that is clean against `main` is also clean against this branch. The changes stay inside the x87 files, one `CPU_FIXTURES` line after `"x87_sub"`, a CMake block after the `cpu_aot_x87_sub` test, and the x87 section of `docs/experiments.md`. No open PR touches x87. ## Evidence The fixture is synthetic and original to this repository (MIT). `docs/experiments.md` documents the data-movement forms, the alias rejection, 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:04 +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!437
No description provided.