cpu: lower FMUL forms in the x87 leaf profile #425

Merged
denn0x merged 1 commit from upstream-pr/431 into main 2026-11-22 02:26:33 +00:00
denn0x commented 2026-11-22 02:09:39 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by denn0x on 2026-11-22T02:09:39Z.


Change

Refs #406. This is the multiplication step of the x87 plan, on top of the leaf profile from #422. The opt-in profile of lower(..., x87=True) now also accepts:

  • fmul m32 and fmul m64 (ST(0) = ST(0) × operand);
  • fmul st, st(i) and fmul st(i), st;
  • fmulp st(i), st for i from 1 to 7.

The default x87=False is unchanged, and so are the entry/exit contract, the static stack depths and every existing rejection. fmulp st(0), st is rejected like faddp st(0), st.

Deliberately out of scope: this PR does not add fadd m32/m64 or the other fadd register forms. They remain rejected (the check confirms fadd m32), so this PR covers one behavior. Division, conversions, constants, comparisons and stack faults remain separate steps.

Arithmetic (tools/x87_soft.h).

  • x87_mul: the product of the two 64-bit significands is exact in 128 bits (_umul128) and is normalized. It then goes through the same x87_round as the add: 53 bits, extended exponent range, denormalized first below the normal range.
  • Sign: the exclusive or of the operand signs, including zero and infinite results.
  • Special operands: infinity × zero gives the indefinite. NaNs and invalid encodings go through the existing x87_special_operands.
  • Memory operands: an m32 or m64 operand of fmul converts exactly but keeps a signaling NaN signaling (x87_operand_f32/x87_operand_f64). The NaN rules therefore see the operand's own kind; a plain fld still quiets on load. To do this, x87_from_binary gained a defaulted quiet = true parameter. Existing call sites are unchanged, and cpu_aot_x87_add passes unchanged.

Fixture

tests/cpu/x87_mul.asm and .def are generated by tests/cpu/x87_mul.py asm. The 12 exports are:

  • fmulp st(1), st stored to m80, m64 and m32;
  • fmul st, st(1) and fmul st(1), st, with both stack values stored;
  • the square through fmul st, st(0);
  • fmul m32 and fmul m64 against an m80 value;
  • float × float and double × double through fld/fmul m/fstp;
  • a three-value chain through fmulp st(2), st;
  • a * b + c with the product rounded before the add.

Harness and checks. The exports run through the harness from #422 (fnsave/frstor, control word 0x027F), with identical input bytes for the native and translated DLLs. The check compares the output block and 64 guard bytes on each side of the input and output against an independent Fraction model. It reuses the exact-rational helpers of tests/cpu/x87_add.py and adds a product model of its own. The live-value check confirms that a caller's ST(0) value and control word survive each of the 12 exports.

Cases. The 14951 cases include:

  • every pair of the 34 special 80-bit encodings;
  • each special m80 value times each of 17 m32 and 18 m64 special encodings;
  • products of 27-bit odd factors, which are exact halfway cases at 53 bits;
  • products (2^63 + u)(2^63 + v) with u + v = 1024 (mod 2048), whose upper 64 bits sit exactly on a tie so that only the low 64 bits decide the rounding;
  • x * 1, which still rounds a 64-bit significand to 53 bits;
  • products beyond the binary64 range, stored to m80, m64 and m32;
  • products at the extended overflow and underflow thresholds, and denormal results;
  • NaN ties by sign;
  • random operands.

Negative controls. Six mutated translations must each mismatch:

Control Mutation Mismatches
pc64 the product rounds to 64 significand bits 4836/14951
double the product rounds in the binary64 exponent range 8059/14951
nolow the low 64 bits of the 128-bit product are dropped 62/14951
sign the product takes the first operand's sign 4409/14951
infzero infinity × zero gives infinity 73/14951
memquiet m32/m64 operands are quieted before the NaN rules 36/14951

Rejections. With x87=True, translation rejects nine sequences with the expected diagnostic:

  • fmulp st(0), st;
  • fmul m32 on an empty stack;
  • fmul st, st(1) with one value;
  • fmul st(2), st with two values;
  • fmulp with one value;
  • fimul m32 and fimul m16;
  • fdiv st, st(1);
  • fadd m32.

Verification

Tested at 649e460 on c2fc84c. 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_mul_lifted
ctest --preset dev --no-tests=error -R '^cpu_aot_x87_mul$' -V
97: 14951 cases over 12 exports (output block and guards) matched the reference in native and translated DLLs; each export returned with control 0x027f and an empty stack
97: processor: AMD64 Family 25 Model 97 Stepping 2, AuthenticAMD
97: x87_call kept a live caller ST(0) value and control word around all 12 native and translated exports
97: control pc64: 4836/14951 mismatches
97: control double: 8059/14951 mismatches
97: control nolow: 62/14951 mismatches
97: control sign: 4409/14951 mismatches
97: control infzero: 73/14951 mismatches
97: control memquiet: 36/14951 mismatches
97: rejected 9 sequences outside the profile: fmulp st(0), st; fmul m32 on an empty stack; fmul st, st(1) with one value; fmul st(2), st with two values; fmulp with one value; fimul m32; fimul m16; fdiv st, st(1); fadd m32
1/1 Test #97: cpu_aot_x87_mul ..................   Passed
  • Existing lowerer regressions: ctest --preset dev --no-tests=error -R '^cpu_' -j 8 passes. cpu_aot_x87_add keeps its 8085 cases and its control counts.
  • Full suite: ctest --preset dev --no-tests=error -j 8 --output-junit <repo>/out/test-results.xml gives 147 passed, 1 failed and 2 capability skips (bvh_wave64_warp, wave_shuffle_warp) out of 150. 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 and tests/docs_links_check.py pass.
  • Fork CI: the Windows build-and-test, Linux Python and gfx1013 LLVM jobs passed at 649e460 in the fork's Windows checks run 44974517755. cpu_aot_x87_add and cpu_aot_x87_mul ran and passed there (tests 71 and 72 of 122).

Coverage. python tools/form_coverage.py out/test-results.xml gives x64 61 of 948 forms, and 59 without cpu_aot_x87_mul. The added forms are fmul and fmulp.

Not measured:

  • Intel CPUs. All measurements are on one AMD Zen 4 part, and the fork CI runner's vendor is not recorded.
  • st(3) to st(7) operands. Only st(0), st(1) and st(2) are exercised; the others use the same slot arithmetic.
  • The status word, as in #422.

Overlap

I trial-merged this branch with every open cpu: PR. Every merge that is clean against main is also clean against this branch. The changes stay inside the x87 files from #422, one CPU_FIXTURES line after "x87_add", a CMake block after the cpu_aot_x87_add 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 multiplication forms, the memory-operand NaN rule, 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:6537232972 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/431). Originally posted by [denn0x](https://github.com/denn0x) on 2026-11-22T02:09:39Z. --- ## Change Refs #406. This is the multiplication step of the x87 plan, on top of the leaf profile from #422. The opt-in profile of `lower(..., x87=True)` now also accepts: - `fmul m32` and `fmul m64` (ST(0) = ST(0) × operand); - `fmul st, st(i)` and `fmul st(i), st`; - `fmulp st(i), st` for `i` from 1 to 7. The default `x87=False` is unchanged, and so are the entry/exit contract, the static stack depths and every existing rejection. `fmulp st(0), st` is rejected like `faddp st(0), st`. **Deliberately out of scope:** this PR does not add `fadd m32/m64` or the other `fadd` register forms. They remain rejected (the check confirms `fadd m32`), so this PR covers one behavior. Division, conversions, constants, comparisons and stack faults remain separate steps. **Arithmetic (`tools/x87_soft.h`).** - **`x87_mul`:** the product of the two 64-bit significands is exact in 128 bits (`_umul128`) and is normalized. It then goes through the same `x87_round` as the add: 53 bits, extended exponent range, denormalized first below the normal range. - **Sign:** the exclusive or of the operand signs, including zero and infinite results. - **Special operands:** infinity × zero gives the indefinite. NaNs and invalid encodings go through the existing `x87_special_operands`. - **Memory operands:** an m32 or m64 operand of `fmul` converts exactly but keeps a signaling NaN signaling (`x87_operand_f32`/`x87_operand_f64`). The NaN rules therefore see the operand's own kind; a plain `fld` still quiets on load. To do this, `x87_from_binary` gained a defaulted `quiet = true` parameter. Existing call sites are unchanged, and `cpu_aot_x87_add` passes unchanged. ## Fixture `tests/cpu/x87_mul.asm` and `.def` are generated by `tests/cpu/x87_mul.py asm`. The 12 exports are: - `fmulp st(1), st` stored to m80, m64 and m32; - `fmul st, st(1)` and `fmul st(1), st`, with both stack values stored; - the square through `fmul st, st(0)`; - `fmul m32` and `fmul m64` against an m80 value; - `float × float` and `double × double` through `fld`/`fmul m`/`fstp`; - a three-value chain through `fmulp st(2), st`; - `a * b + c` with the product rounded before the add. **Harness and checks.** The exports run through the harness from #422 (`fnsave`/`frstor`, control word `0x027F`), with identical input bytes for the native and translated DLLs. The check compares the output block and 64 guard bytes on each side of the input and output against an independent `Fraction` model. It reuses the exact-rational helpers of `tests/cpu/x87_add.py` and adds a product model of its own. The live-value check confirms that a caller's ST(0) value and control word survive each of the 12 exports. **Cases.** The 14951 cases include: - every pair of the 34 special 80-bit encodings; - each special m80 value times each of 17 m32 and 18 m64 special encodings; - products of 27-bit odd factors, which are exact halfway cases at 53 bits; - products `(2^63 + u)(2^63 + v)` with `u + v = 1024` (mod 2048), whose upper 64 bits sit exactly on a tie so that only the low 64 bits decide the rounding; - `x * 1`, which still rounds a 64-bit significand to 53 bits; - products beyond the binary64 range, stored to m80, m64 and m32; - products at the extended overflow and underflow thresholds, and denormal results; - NaN ties by sign; - random operands. **Negative controls.** Six mutated translations must each mismatch: | Control | Mutation | Mismatches | |---|---|---| | `pc64` | the product rounds to 64 significand bits | 4836/14951 | | `double` | the product rounds in the binary64 exponent range | 8059/14951 | | `nolow` | the low 64 bits of the 128-bit product are dropped | 62/14951 | | `sign` | the product takes the first operand's sign | 4409/14951 | | `infzero` | infinity × zero gives infinity | 73/14951 | | `memquiet` | m32/m64 operands are quieted before the NaN rules | 36/14951 | **Rejections.** With `x87=True`, translation rejects nine sequences with the expected diagnostic: - `fmulp st(0), st`; - `fmul m32` on an empty stack; - `fmul st, st(1)` with one value; - `fmul st(2), st` with two values; - `fmulp` with one value; - `fimul m32` and `fimul m16`; - `fdiv st, st(1)`; - `fadd m32`. ## Verification Tested at `649e460` on `c2fc84c`. 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_mul_lifted ctest --preset dev --no-tests=error -R '^cpu_aot_x87_mul$' -V ``` ```text 97: 14951 cases over 12 exports (output block and guards) matched the reference in native and translated DLLs; each export returned with control 0x027f and an empty stack 97: processor: AMD64 Family 25 Model 97 Stepping 2, AuthenticAMD 97: x87_call kept a live caller ST(0) value and control word around all 12 native and translated exports 97: control pc64: 4836/14951 mismatches 97: control double: 8059/14951 mismatches 97: control nolow: 62/14951 mismatches 97: control sign: 4409/14951 mismatches 97: control infzero: 73/14951 mismatches 97: control memquiet: 36/14951 mismatches 97: rejected 9 sequences outside the profile: fmulp st(0), st; fmul m32 on an empty stack; fmul st, st(1) with one value; fmul st(2), st with two values; fmulp with one value; fimul m32; fimul m16; fdiv st, st(1); fadd m32 1/1 Test #97: cpu_aot_x87_mul .................. Passed ``` - **Existing lowerer regressions:** `ctest --preset dev --no-tests=error -R '^cpu_' -j 8` passes. `cpu_aot_x87_add` keeps its 8085 cases and its control counts. - **Full suite:** `ctest --preset dev --no-tests=error -j 8 --output-junit <repo>/out/test-results.xml` gives 147 passed, 1 failed and 2 capability skips (`bvh_wave64_warp`, `wave_shuffle_warp`) out of 150. 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` and `tests/docs_links_check.py` pass. - **Fork CI:** the Windows build-and-test, Linux Python and gfx1013 LLVM jobs passed at `649e460` in the fork's `Windows checks` run [44974517755](https://github.com/denn0x/Xbox2Windows/actions/runs/44974517755). `cpu_aot_x87_add` and `cpu_aot_x87_mul` ran and passed there (tests 71 and 72 of 122). **Coverage.** `python tools/form_coverage.py out/test-results.xml` gives x64 **61 of 948** forms, and 59 without `cpu_aot_x87_mul`. The added forms are `fmul` and `fmulp`. **Not measured:** - Intel CPUs. All measurements are on one AMD Zen 4 part, and the fork CI runner's vendor is not recorded. - `st(3)` to `st(7)` operands. Only `st(0)`, `st(1)` and `st(2)` are exercised; the others use the same slot arithmetic. - The status word, as in #422. ## Overlap I trial-merged this branch with every open `cpu:` PR. Every merge that is clean against `main` is also clean against this branch. The changes stay inside the x87 files from #422, one `CPU_FIXTURES` line after `"x87_add"`, a CMake block after the `cpu_aot_x87_add` 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 multiplication forms, the memory-operand NaN rule, 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:01 +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!425
No description provided.