cpu: lower FSCALE in the x87 profile #465

Merged
denn0x merged 1 commit from upstream-pr/471 into main 2026-11-22 05:51:59 +00:00
denn0x commented 2026-11-22 05:43:13 +00:00 (Migrated from github.com)

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


Change

Refs #406. This follows #465 (fxtract). It adds fscale to the opt-in x87 profile in lower(..., x87=True).

Semantics. fscale multiplies st(0) by 2^n, where n is st(1) truncated toward zero. st(1) stays on the stack.

  • Precision control: does not apply. A result in the normal range keeps all 64 significand bits (control pc53 below).
  • Below the normal range: the result is denormalized and rounded to nearest-even at the same bit positions.
  • Above the range: the result overflows to infinity.
  • Infinite st(1): -inf gives a zero and +inf an infinity, both with the sign of st(0). An infinite st(0) scaled by -inf and a zero scaled by +inf give the indefinite.
  • Zero or infinite st(0): otherwise unchanged.
  • NaN and unsupported operands: follow the same rules as fadd.

The native runs confirm each of these rules.

Out of scope: fprem, fprem1, the transcendental instructions and every control-word access. They remain rejected.

Lowering (tools/x87_lower.py). x87_slot[top] = x87_scale(x87_slot[top], x87_slot[top - 1]);. Fewer than two values is an underflow, and the depth is unchanged.

Helper (tools/x87_soft.h). x87_scale(a, b) handles the special operands first. It then truncates st(1) from its unpacked significand and clamps the result to 2^20 in magnitude, far beyond the exponent range, so that adding it to the exponent cannot overflow an int. Finally it rounds through the existing x87_round with a 64-bit format.

Fixture

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

  • an m80 value scaled by an m80 value, stored to m80, m64 and m32;
  • m64 values;
  • a scale by a 32-bit integer from fild;
  • two scalings in a row;
  • a value rebuilt through fxtract; fscale;
  • a scaling at depth 8.

Every export also stores st(1), so a translation that changes or pops it mismatches.

Model. The model scales with Fraction arithmetic and shares no code with tools/x87_soft.h. It reuses the fxtract model of #465 and the integer conversions of cpu_aot_x87_integers. The harness, input bytes, guards and live-value check are those of the earlier x87 PRs.

Cases. 43003 cases:

  • every pair of the 34 special encodings;
  • values with tie and sticky patterns in their low bits, denormals, pseudo-denormals and random values, scaled by each of:
    • integers from -70 to 70;
    • the exponents at the edges of the normal and denormal ranges;
    • huge values;
    • non-integers of both signs;
    • denormals;
  • values with tie patterns scaled into each part of the denormal range;
  • m64 pairs and 32-bit integer scales.

Negative controls. Six mutated translations must each mismatch:

Control Mutation Mismatches
pc53 the result rounds to 53 significand bits 11395/43003
chop a result below the normal range rounds toward zero 1063/43003
floor a negative non-integer scale rounds toward -inf 3068/43003
neginf a scale by -inf gives +0 whatever the sign of st(0) 26/43003
zeroinf a zero scaled by +inf stays zero 10/43003
swap the operands are exchanged 37337/43003

Rejections. Translation rejects 5 sequences with the expected diagnostic: fscale on an empty stack and with one value, fscale that leaves st(1) at the return, fnsave and fxrstor.

Changes to existing tests. cpu_aot_x87_sub used fscale as a rejection case. It becomes fnstenv, an environment store that the profile will not lower, so the test keeps 10 rejections. Its case and control counts are unchanged.

Verification

Tested at c2a3d6c on efffd90 (the head of #465). main has since moved to 86dff9f, after the merge of #465. None of the commits on main that efffd90 lacks touch tools/ffx_version_query.py or any x87 file. This branch merges cleanly into 86dff9f. 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_scale_lifted
ctest --preset dev --no-tests=error -R '^cpu_aot_x87_scale$' -V
43003 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 pc53: 11395/43003 mismatches
control chop: 1063/43003 mismatches
control floor: 3068/43003 mismatches
control neginf: 26/43003 mismatches
control zeroinf: 10/43003 mismatches
control swap: 37337/43003 mismatches
rejected 5 sequences outside the profile: fscale on an empty stack; fscale with one value; fscale with one store; fnsave; fxrstor
  • x87 regressions: all twelve x87 tests pass.
  • Full suite: ctest --preset dev --no-tests=error -j 8 --output-junit test-results.xml gives 186 passed, 1 failed and 2 capability skips out of 189. 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 c2a3d6c in the fork's Windows checks run 44987849706. Both Windows jobs ran and passed all twelve x87 tests (159 passed and 2 capability skips, bvh_wave64_warp and wave_shuffle_warp, out of 161 in each).

Coverage. python tools/form_coverage.py out/build/dev/test-results.xml gives x64 161 of 948 forms, and 160 without cpu_aot_x87_scale. The added form is fscale. These totals are at c2a3d6c; main now also registers the cpu_aot_wide_arithmetic and cpu_aot_dead_divides fixtures of #321, so its totals are higher.

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, as in #422.

Overlap

I trial-merged this branch with all 86 open PRs. Each one conflicts in the same files against main and against this branch: the 9 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_xtract test, and the x87 section of docs/experiments.md.

Evidence

The fixture is synthetic and original to this repository (MIT). docs/experiments.md documents fscale, 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:6538976617 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/471). Originally posted by [denn0x](https://github.com/denn0x) on 2026-11-22T05:43:13Z. --- ## Change Refs #406. This follows #465 (`fxtract`). It adds `fscale` to the opt-in x87 profile in `lower(..., x87=True)`. **Semantics.** `fscale` multiplies `st(0)` by 2^n, where n is `st(1)` truncated toward zero. `st(1)` stays on the stack. - **Precision control:** does not apply. A result in the normal range keeps all 64 significand bits (control `pc53` below). - **Below the normal range:** the result is denormalized and rounded to nearest-even at the same bit positions. - **Above the range:** the result overflows to infinity. - **Infinite `st(1)`:** -inf gives a zero and +inf an infinity, both with the sign of `st(0)`. An infinite `st(0)` scaled by -inf and a zero scaled by +inf give the indefinite. - **Zero or infinite `st(0)`:** otherwise unchanged. - **NaN and unsupported operands:** follow the same rules as `fadd`. The native runs confirm each of these rules. **Out of scope:** `fprem`, `fprem1`, the transcendental instructions and every control-word access. They remain rejected. **Lowering (`tools/x87_lower.py`).** `x87_slot[top] = x87_scale(x87_slot[top], x87_slot[top - 1]);`. Fewer than two values is an underflow, and the depth is unchanged. **Helper (`tools/x87_soft.h`).** `x87_scale(a, b)` handles the special operands first. It then truncates `st(1)` from its unpacked significand and clamps the result to 2^20 in magnitude, far beyond the exponent range, so that adding it to the exponent cannot overflow an `int`. Finally it rounds through the existing `x87_round` with a 64-bit format. ## Fixture `tests/cpu/x87_scale.asm` and `.def` are generated by `tests/cpu/x87_scale.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_scale`). The 8 exports cover: - an m80 value scaled by an m80 value, stored to m80, m64 and m32; - m64 values; - a scale by a 32-bit integer from `fild`; - two scalings in a row; - a value rebuilt through `fxtract; fscale`; - a scaling at depth 8. Every export also stores `st(1)`, so a translation that changes or pops it mismatches. **Model.** The model scales with `Fraction` arithmetic and shares no code with `tools/x87_soft.h`. It reuses the `fxtract` model of #465 and the integer conversions of `cpu_aot_x87_integers`. The harness, input bytes, guards and live-value check are those of the earlier x87 PRs. **Cases.** 43003 cases: - every pair of the 34 special encodings; - values with tie and sticky patterns in their low bits, denormals, pseudo-denormals and random values, scaled by each of: - integers from -70 to 70; - the exponents at the edges of the normal and denormal ranges; - huge values; - non-integers of both signs; - denormals; - values with tie patterns scaled into each part of the denormal range; - m64 pairs and 32-bit integer scales. **Negative controls.** Six mutated translations must each mismatch: | Control | Mutation | Mismatches | |---|---|---| | `pc53` | the result rounds to 53 significand bits | 11395/43003 | | `chop` | a result below the normal range rounds toward zero | 1063/43003 | | `floor` | a negative non-integer scale rounds toward -inf | 3068/43003 | | `neginf` | a scale by -inf gives +0 whatever the sign of `st(0)` | 26/43003 | | `zeroinf` | a zero scaled by +inf stays zero | 10/43003 | | `swap` | the operands are exchanged | 37337/43003 | **Rejections.** Translation rejects 5 sequences with the expected diagnostic: `fscale` on an empty stack and with one value, `fscale` that leaves `st(1)` at the return, `fnsave` and `fxrstor`. **Changes to existing tests.** `cpu_aot_x87_sub` used `fscale` as a rejection case. It becomes `fnstenv`, an environment store that the profile will not lower, so the test keeps 10 rejections. Its case and control counts are unchanged. ## Verification Tested at `c2a3d6c` on `efffd90` (the head of #465). `main` has since moved to `86dff9f`, after the merge of #465. None of the commits on `main` that `efffd90` lacks touch `tools/ffx_version_query.py` or any x87 file. This branch merges cleanly into `86dff9f`. 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_scale_lifted ctest --preset dev --no-tests=error -R '^cpu_aot_x87_scale$' -V ``` ```text 43003 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 pc53: 11395/43003 mismatches control chop: 1063/43003 mismatches control floor: 3068/43003 mismatches control neginf: 26/43003 mismatches control zeroinf: 10/43003 mismatches control swap: 37337/43003 mismatches rejected 5 sequences outside the profile: fscale on an empty stack; fscale with one value; fscale with one store; fnsave; fxrstor ``` - **x87 regressions:** all twelve x87 tests pass. - **Full suite:** `ctest --preset dev --no-tests=error -j 8 --output-junit test-results.xml` gives 186 passed, 1 failed and 2 capability skips out of 189. 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 `c2a3d6c` in the fork's `Windows checks` run [44987849706](https://github.com/denn0x/Xbox2Windows/actions/runs/44987849706). Both Windows jobs ran and passed all twelve x87 tests (159 passed and 2 capability skips, `bvh_wave64_warp` and `wave_shuffle_warp`, out of 161 in each). **Coverage.** `python tools/form_coverage.py out/build/dev/test-results.xml` gives x64 **161 of 948** forms, and 160 without `cpu_aot_x87_scale`. The added form is `fscale`. These totals are at `c2a3d6c`; `main` now also registers the `cpu_aot_wide_arithmetic` and `cpu_aot_dead_divides` fixtures of #321, so its totals are higher. **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, as in #422. ## Overlap I trial-merged this branch with all 86 open PRs. Each one conflicts in the same files against `main` and against this branch: the 9 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_xtract` test, and the x87 section of `docs/experiments.md`. ## Evidence The fixture is synthetic and original to this repository (MIT). `docs/experiments.md` documents `fscale`, 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:13 +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!465
No description provided.