cpu: lower the x87 constant loads in the x87 profile #444

Merged
denn0x merged 1 commit from upstream-pr/450 into main 2026-11-22 04:22:28 +00:00
denn0x commented 2026-11-22 04:18:10 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by denn0x on 2026-11-22T04:18:10Z.


Change

Refs #406. This follows #443 (register moves). It adds the seven constant loads to the opt-in x87 profile in lower(..., x87=True): fld1, fldz, fldpi, fldl2t, fldl2e, fldlg2 and fldln2.

Out of scope: integer conversions (fild, fist, ...), comparisons, transcendental instructions and every control-word access. They remain rejected. The entry/exit contract and the static stack depths are unchanged.

Rounding. Each constant is pushed rounded to 64 significand bits under the rounding control. The profile fixes that control at round to nearest-even. The 53-bit precision control does not apply to the constant loads. The native runs confirm this: the control that rounds the constants to 53 bits mismatches the native DLL (see below).

Lowering (tools/x87_lower.py).

  • Table: CONSTANTS holds the (significand, sign and exponent) encoding of each constant, rounded to nearest-even.
  • Statement: a constant load is a static slot store, x87_slot[d] = X87Value{0x...ull, 0x....}; // fldpi.
  • Depth: a constant load at depth 8 is rejected as an overflow, like any other push.

No helper in tools/x87_soft.h changes.

Fixture

tests/cpu/x87_constants.asm and .def are generated by tests/cpu/x87_constants.py asm, and emit checks that every decoded x87 instruction is the form its sequence names. The .asm registers its test for the coverage report on its first line (; coverage: source cpu_aot_x87_constants), as #444 describes. The 30 exports cover:

  • each of the seven constants stored to m80, m64 and m32;
  • fldpi through fst m64, then fstp m80;
  • the constants as operands: x + 1, 1 / x, 0 - x, x * ln 2, pi times an m64 value and log2(e) times an m32 value;
  • fldpi and fldlg2 pushed as the eighth value, with all eight values stored.

Model. The model does not read the CONSTANTS table. It computes the constants from integer series with 320 fraction bits:

  • pi from Machin's formula;
  • ln 2 from the series of 1/(k 2^k);
  • ln 10 as 3 ln 2 + 2 atanh(1/9).

It derives log2(10), log2(e) and log10(2) from these values and rounds each one to 64 bits itself. The arithmetic exports reuse the Fraction models of the add, multiply, subtract and divide tests. The harness, input bytes, guards and live-value check are those of the earlier x87 PRs.

Cases. 2863 cases:

  • the 22 exports that read no input run with eight random input blocks each, which keeps the input guard check meaningful;
  • the four m80 exports run the m80 singles of cpu_aot_x87_add and 200 random values;
  • the m64 and m32 exports run the binary64 and binary32 inputs of cpu_aot_x87_add;
  • the two depth-8 exports run 300 blocks of seven m64 values drawn from those binary64 inputs.

Negative controls. Four mutated translations must each mismatch:

Control Mutation Mismatches
pc53 constants rounded to 53 significand bits 170/2863
chop constants rounded toward zero 40/2863
fldzneg fldz loads -0 25/2863
swaplogs fldlg2 and fldln2 values exchanged 829/2863

Rejections. Translation rejects 11 sequences with the expected diagnostic:

  • each of the seven constant loads at depth 8;
  • fldpi left on the stack at the return;
  • f2xm1, fyl2x and fnstenv.

Changes to existing tests. cpu_aot_x87_moves used fld1 as a rejection case. It becomes fsin, which is still outside the profile, so the test keeps 16 rejections. Its case and control counts are unchanged.

Verification

Tested at d38d260 on 718d683. 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_constants_lifted
ctest --preset dev --no-tests=error -R '^cpu_aot_x87_constants$' -V
2863 cases over 30 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 30 native and translated exports
control pc53: 170/2863 mismatches
control chop: 40/2863 mismatches
control fldzneg: 25/2863 mismatches
control swaplogs: 829/2863 mismatches
rejected 11 sequences outside the profile: fld1 at depth 8; fldl2t at depth 8; fldl2e at depth 8; fldpi at depth 8; fldlg2 at depth 8; fldln2 at depth 8; fldz at depth 8; fldpi without a store; f2xm1; fyl2x; fnstenv
  • x87 regressions: all six x87 tests pass (x87_add, x87_mul, x87_div, x87_sub, x87_moves, x87_constants).
  • Full suite: ctest --preset dev --no-tests=error -j 8 --output-junit test-results.xml gives 176 passed, 1 failed and 2 capability skips out of 179. 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 d38d260 in the fork's Windows checks run 44984076276. Both Windows jobs ran and passed all six x87 tests (151 of 151 tests in each).

Coverage. python tools/form_coverage.py out/build/dev/test-results.xml gives x64 149 of 948 forms, and 142 without cpu_aot_x87_constants. The added forms are fld1, fldz, fldpi, fldl2t, fldl2e, fldlg2 and fldln2.

Not measured:

  • Intel CPUs. All measurements are on one AMD Zen 4 part.
  • The other rounding modes, because the profile rejects fldcw. The five irrational constants are inexact, so each one rounds differently from nearest-even under one of round up and round down.
  • The status word, including C1, 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 16 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, one rejection case of tests/cpu/x87_moves.py, a CMake block after the cpu_aot_x87_moves 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 constant loads, their rounding, 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:6538359322 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/450). Originally posted by [denn0x](https://github.com/denn0x) on 2026-11-22T04:18:10Z. --- ## Change Refs #406. This follows #443 (register moves). It adds the seven constant loads to the opt-in x87 profile in `lower(..., x87=True)`: `fld1`, `fldz`, `fldpi`, `fldl2t`, `fldl2e`, `fldlg2` and `fldln2`. **Out of scope:** integer conversions (`fild`, `fist`, ...), comparisons, transcendental instructions and every control-word access. They remain rejected. The entry/exit contract and the static stack depths are unchanged. **Rounding.** Each constant is pushed rounded to 64 significand bits under the rounding control. The profile fixes that control at round to nearest-even. The 53-bit precision control does not apply to the constant loads. The native runs confirm this: the control that rounds the constants to 53 bits mismatches the native DLL (see below). **Lowering (`tools/x87_lower.py`).** - **Table:** `CONSTANTS` holds the (significand, sign and exponent) encoding of each constant, rounded to nearest-even. - **Statement:** a constant load is a static slot store, `x87_slot[d] = X87Value{0x...ull, 0x....}; // fldpi`. - **Depth:** a constant load at depth 8 is rejected as an overflow, like any other push. No helper in `tools/x87_soft.h` changes. ## Fixture `tests/cpu/x87_constants.asm` and `.def` are generated by `tests/cpu/x87_constants.py asm`, and `emit` checks that every decoded x87 instruction is the form its sequence names. The `.asm` registers its test for the coverage report on its first line (`; coverage: source cpu_aot_x87_constants`), as #444 describes. The 30 exports cover: - each of the seven constants stored to m80, m64 and m32; - `fldpi` through `fst` m64, then `fstp` m80; - the constants as operands: `x + 1`, `1 / x`, `0 - x`, `x * ln 2`, `pi` times an m64 value and `log2(e)` times an m32 value; - `fldpi` and `fldlg2` pushed as the eighth value, with all eight values stored. **Model.** The model does not read the `CONSTANTS` table. It computes the constants from integer series with 320 fraction bits: - pi from Machin's formula; - ln 2 from the series of 1/(k 2^k); - ln 10 as 3 ln 2 + 2 atanh(1/9). It derives log2(10), log2(e) and log10(2) from these values and rounds each one to 64 bits itself. The arithmetic exports reuse the `Fraction` models of the add, multiply, subtract and divide tests. The harness, input bytes, guards and live-value check are those of the earlier x87 PRs. **Cases.** 2863 cases: - the 22 exports that read no input run with eight random input blocks each, which keeps the input guard check meaningful; - the four m80 exports run the m80 singles of `cpu_aot_x87_add` and 200 random values; - the m64 and m32 exports run the binary64 and binary32 inputs of `cpu_aot_x87_add`; - the two depth-8 exports run 300 blocks of seven m64 values drawn from those binary64 inputs. **Negative controls.** Four mutated translations must each mismatch: | Control | Mutation | Mismatches | |---|---|---| | `pc53` | constants rounded to 53 significand bits | 170/2863 | | `chop` | constants rounded toward zero | 40/2863 | | `fldzneg` | `fldz` loads -0 | 25/2863 | | `swaplogs` | `fldlg2` and `fldln2` values exchanged | 829/2863 | **Rejections.** Translation rejects 11 sequences with the expected diagnostic: - each of the seven constant loads at depth 8; - `fldpi` left on the stack at the return; - `f2xm1`, `fyl2x` and `fnstenv`. **Changes to existing tests.** `cpu_aot_x87_moves` used `fld1` as a rejection case. It becomes `fsin`, which is still outside the profile, so the test keeps 16 rejections. Its case and control counts are unchanged. ## Verification Tested at `d38d260` on `718d683`. 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_constants_lifted ctest --preset dev --no-tests=error -R '^cpu_aot_x87_constants$' -V ``` ```text 2863 cases over 30 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 30 native and translated exports control pc53: 170/2863 mismatches control chop: 40/2863 mismatches control fldzneg: 25/2863 mismatches control swaplogs: 829/2863 mismatches rejected 11 sequences outside the profile: fld1 at depth 8; fldl2t at depth 8; fldl2e at depth 8; fldpi at depth 8; fldlg2 at depth 8; fldln2 at depth 8; fldz at depth 8; fldpi without a store; f2xm1; fyl2x; fnstenv ``` - **x87 regressions:** all six x87 tests pass (`x87_add`, `x87_mul`, `x87_div`, `x87_sub`, `x87_moves`, `x87_constants`). - **Full suite:** `ctest --preset dev --no-tests=error -j 8 --output-junit test-results.xml` gives 176 passed, 1 failed and 2 capability skips out of 179. 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 `d38d260` in the fork's `Windows checks` run [44984076276](https://github.com/denn0x/Xbox2Windows/actions/runs/44984076276). Both Windows jobs ran and passed all six x87 tests (151 of 151 tests in each). **Coverage.** `python tools/form_coverage.py out/build/dev/test-results.xml` gives x64 **149 of 948** forms, and 142 without `cpu_aot_x87_constants`. The added forms are `fld1`, `fldz`, `fldpi`, `fldl2t`, `fldl2e`, `fldlg2` and `fldln2`. **Not measured:** - Intel CPUs. All measurements are on one AMD Zen 4 part. - The other rounding modes, because the profile rejects `fldcw`. The five irrational constants are inexact, so each one rounds differently from nearest-even under one of round up and round down. - The status word, including C1, 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 16 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, one rejection case of `tests/cpu/x87_moves.py`, a CMake block after the `cpu_aot_x87_moves` 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 constant loads, their rounding, 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!444
No description provided.