cpu: lower FXTRACT in the x87 profile #459

Merged
denn0x merged 1 commit from upstream-pr/465 into main 2026-11-22 05:37:06 +00:00
denn0x commented 2026-11-22 05:25:49 +00:00 (Migrated from github.com)

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


Change

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

Semantics. fxtract replaces st(0) by its unbiased exponent and pushes its significand. Afterwards st(0) is the significand and st(1) the exponent.

  • Significand: keeps the sign and gets biased exponent 16383, so its magnitude lies in [1, 2).
  • Exponent: the unbiased exponent as an exact extended value; an exponent of 0 gives +0.
  • Exactness: both results are exact, and the precision control does not apply.
  • Denormals: a denormal or pseudo-denormal is normalized first, so its exponent is that of its value.
  • Zero: keeps its sign and gives an exponent of -inf, the masked divide-by-zero response.
  • Infinity: keeps its sign and gives an exponent of +inf.
  • NaN: a NaN is quieted into both results.
  • Unsupported encodings: an unnormal, a pseudo-infinity or a pseudo-NaN gives the indefinite in both.

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). { X87Value e; const X87Value s = x87_xtract(x87_slot[top], e); x87_slot[top] = e; x87_slot[depth] = s; }. fxtract on an empty stack is an underflow and at depth 8 an overflow.

Helper (tools/x87_soft.h). x87_xtract(v, exponent) returns the significand and writes the exponent. It reuses x87_unpack for the normalization and x87_from_integer for the exponent.

Fixture

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

  • an m80 value split, with both results stored to m80, m64 and m32;
  • m64 and m32 loads split, with both results stored to m80;
  • the significand split again (its exponent is +0);
  • the two results added;
  • a split at depth 8, with all eight values stored.

Model. The model splits each value with Fraction arithmetic and shares no code with tools/x87_soft.h. The addition export reuses the Fraction model of the add test. The harness, input bytes, guards and live-value check are those of the earlier x87 PRs.

Cases. 5895 cases:

  • the m80 singles of cpu_aot_x87_add, and 400 random values over the whole exponent range;
  • two values of exponent 0 for each of the 64 leading-bit positions (denormals, and pseudo-denormals at bit 63);
  • values at small and extreme exponents, and signaling NaNs;
  • 30 each of pseudo-denormals, unnormals, and pseudo-infinities or pseudo-NaNs;
  • the m64 and m32 special encodings and random bit patterns.

Negative controls. Six mutated translations must each mismatch:

Control Mutation Mismatches
zeroexp a zero gives an exponent of +inf 16/5895
denormal a denormal is split without normalizing it 660/5895
biased the exponent keeps its bias 5296/5895
unsigned the significand loses the sign 2692/5895
snan a signaling NaN is returned unquieted 84/5895
order the two results are pushed in the other order 4448/5895

Rejections. Translation rejects 5 sequences with the expected diagnostic: fxtract on an empty stack and at depth 8, fxtract with only one result stored, frstor and fxrstor.

Changes to existing tests: none.

Verification

Tested at efffd90 on 54da9dd (the head of #462). main has since moved to 5ee73e6, the merge of #462. None of the commits on main that 54da9dd lacks touch tools/ffx_version_query.py or any x87 file. This branch merges cleanly into 5ee73e6. 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_xtract_lifted
ctest --preset dev --no-tests=error -R '^cpu_aot_x87_xtract$' -V
5895 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 zeroexp: 16/5895 mismatches
control denormal: 660/5895 mismatches
control biased: 5296/5895 mismatches
control unsigned: 2692/5895 mismatches
control snan: 84/5895 mismatches
control order: 4448/5895 mismatches
rejected 5 sequences outside the profile: fxtract on an empty stack; fxtract at depth 8; fxtract with one store; frstor; fxrstor
  • x87 regressions: all eleven x87 tests pass.
  • Full suite: ctest --preset dev --no-tests=error -j 8 --output-junit test-results.xml gives 185 passed, 1 failed and 2 capability skips out of 188. 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 efffd90 in the fork's Windows checks run 44987847314. Both Windows jobs ran and passed all eleven x87 tests (158 passed and 2 capability skips, bvh_wave64_warp and wave_shuffle_warp, out of 160 in each).

Coverage. python tools/form_coverage.py out/build/dev/test-results.xml gives x64 160 of 948 forms, and 159 without cpu_aot_x87_xtract. The added form is fxtract. These totals are at efffd90; 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 status word, including the divide-by-zero flag for a zero and the invalid-operation flag, as in #422.

Overlap

I trial-merged this branch with all 87 open PRs. Each one conflicts in the same files against main and against this branch: the 10 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, a CMake block after the cpu_aot_x87_rndint test, and the x87 section of docs/experiments.md.

Evidence

The fixture is synthetic and original to this repository (MIT). docs/experiments.md documents fxtract, the encodings it was measured on, 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:6538855943 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/465). Originally posted by [denn0x](https://github.com/denn0x) on 2026-11-22T05:25:49Z. --- ## Change Refs #406. This follows #462 (`frndint`). It adds `fxtract` to the opt-in x87 profile in `lower(..., x87=True)`. **Semantics.** `fxtract` replaces `st(0)` by its unbiased exponent and pushes its significand. Afterwards `st(0)` is the significand and `st(1)` the exponent. - **Significand:** keeps the sign and gets biased exponent 16383, so its magnitude lies in [1, 2). - **Exponent:** the unbiased exponent as an exact extended value; an exponent of 0 gives +0. - **Exactness:** both results are exact, and the precision control does not apply. - **Denormals:** a denormal or pseudo-denormal is normalized first, so its exponent is that of its value. - **Zero:** keeps its sign and gives an exponent of -inf, the masked divide-by-zero response. - **Infinity:** keeps its sign and gives an exponent of +inf. - **NaN:** a NaN is quieted into both results. - **Unsupported encodings:** an unnormal, a pseudo-infinity or a pseudo-NaN gives the indefinite in both. 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`).** `{ X87Value e; const X87Value s = x87_xtract(x87_slot[top], e); x87_slot[top] = e; x87_slot[depth] = s; }`. `fxtract` on an empty stack is an underflow and at depth 8 an overflow. **Helper (`tools/x87_soft.h`).** `x87_xtract(v, exponent)` returns the significand and writes the exponent. It reuses `x87_unpack` for the normalization and `x87_from_integer` for the exponent. ## Fixture `tests/cpu/x87_xtract.asm` and `.def` are generated by `tests/cpu/x87_xtract.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_xtract`). The 8 exports cover: - an m80 value split, with both results stored to m80, m64 and m32; - m64 and m32 loads split, with both results stored to m80; - the significand split again (its exponent is +0); - the two results added; - a split at depth 8, with all eight values stored. **Model.** The model splits each value with `Fraction` arithmetic and shares no code with `tools/x87_soft.h`. The addition export reuses the `Fraction` model of the add test. The harness, input bytes, guards and live-value check are those of the earlier x87 PRs. **Cases.** 5895 cases: - the m80 singles of `cpu_aot_x87_add`, and 400 random values over the whole exponent range; - two values of exponent 0 for each of the 64 leading-bit positions (denormals, and pseudo-denormals at bit 63); - values at small and extreme exponents, and signaling NaNs; - 30 each of pseudo-denormals, unnormals, and pseudo-infinities or pseudo-NaNs; - the m64 and m32 special encodings and random bit patterns. **Negative controls.** Six mutated translations must each mismatch: | Control | Mutation | Mismatches | |---|---|---| | `zeroexp` | a zero gives an exponent of +inf | 16/5895 | | `denormal` | a denormal is split without normalizing it | 660/5895 | | `biased` | the exponent keeps its bias | 5296/5895 | | `unsigned` | the significand loses the sign | 2692/5895 | | `snan` | a signaling NaN is returned unquieted | 84/5895 | | `order` | the two results are pushed in the other order | 4448/5895 | **Rejections.** Translation rejects 5 sequences with the expected diagnostic: `fxtract` on an empty stack and at depth 8, `fxtract` with only one result stored, `frstor` and `fxrstor`. **Changes to existing tests:** none. ## Verification Tested at `efffd90` on `54da9dd` (the head of #462). `main` has since moved to `5ee73e6`, the merge of #462. None of the commits on `main` that `54da9dd` lacks touch `tools/ffx_version_query.py` or any x87 file. This branch merges cleanly into `5ee73e6`. 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_xtract_lifted ctest --preset dev --no-tests=error -R '^cpu_aot_x87_xtract$' -V ``` ```text 5895 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 zeroexp: 16/5895 mismatches control denormal: 660/5895 mismatches control biased: 5296/5895 mismatches control unsigned: 2692/5895 mismatches control snan: 84/5895 mismatches control order: 4448/5895 mismatches rejected 5 sequences outside the profile: fxtract on an empty stack; fxtract at depth 8; fxtract with one store; frstor; fxrstor ``` - **x87 regressions:** all eleven x87 tests pass. - **Full suite:** `ctest --preset dev --no-tests=error -j 8 --output-junit test-results.xml` gives 185 passed, 1 failed and 2 capability skips out of 188. 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 `efffd90` in the fork's `Windows checks` run [44987847314](https://github.com/denn0x/Xbox2Windows/actions/runs/44987847314). Both Windows jobs ran and passed all eleven x87 tests (158 passed and 2 capability skips, `bvh_wave64_warp` and `wave_shuffle_warp`, out of 160 in each). **Coverage.** `python tools/form_coverage.py out/build/dev/test-results.xml` gives x64 **160 of 948** forms, and 159 without `cpu_aot_x87_xtract`. The added form is `fxtract`. These totals are at `efffd90`; `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 status word, including the divide-by-zero flag for a zero and the invalid-operation flag, as in #422. ## Overlap I trial-merged this branch with all 87 open PRs. Each one conflicts in the same files against `main` and against this branch: the 10 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, a CMake block after the `cpu_aot_x87_rndint` test, and the x87 section of `docs/experiments.md`. ## Evidence The fixture is synthetic and original to this repository (MIT). `docs/experiments.md` documents `fxtract`, the encodings it was measured on, 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:10 +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!459
No description provided.