cpu: lower FNSTCW, FSTCW and WAIT in the x87 profile #507

Merged
denn0x merged 1 commit from upstream-pr/513 into main 2026-11-22 09:20:41 +00:00
denn0x commented 2026-11-22 08:40:00 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by denn0x on 2026-11-22T08:40:00Z.


Change

Refs #406. This follows #499 (the integer-operand arithmetic). It adds the control-word store and wait to the opt-in x87 profile in lower(..., x87=True): fnstcw m16, fstcw m16 and wait. This is the fixed-control-word part of the next step discussed in #406; fldcw and the dynamic control word come separately.

Semantics.

  • fnstcw m16: stores the profile's control word, 0x027F, in two bytes. It does not touch the register stack.
  • wait: does nothing. Every exception is masked, so none can be pending.
  • fstcw m16: iced decodes it as wait followed by fnstcw, and the profile lowers the two in turn.

The native runs confirm each of these rules.

Out of scope: fldcw stays rejected, also after fstcw, along with every status-word form (fnstsw, fstsw, fnclex) and fninit.

Lowering (tools/x87_lower.py). fnstcw becomes store<uint16_t>(a, 0x027f);, and wait becomes an empty statement. Neither changes the stack depth. No helper is added to tools/x87_soft.h.

Fixture

tests/cpu/x87_fnstcw.asm and .def are generated by tests/cpu/x87_fnstcw.py asm, and emit checks that every decoded x87 instruction is the form its sequence names, including the wait and fnstcw that fstcw decodes into. The .asm registers its test on its first line (; coverage: source cpu_aot_x87_fnstcw). The 6 exports cover:

  • fnstcw alone and fstcw alone;
  • fnstcw and fstcw between two m80 loads, an fdivp and the final store;
  • wait before, between and after loads, an fdivp and the final store;
  • stores to the first two and the last two bytes of the output block, next to the guards;
  • fstcw beside the values of a full stack of eight, all stored afterwards.

Model. The model writes 0x027F for each control-word store and divides with the Fraction model of cpu_aot_x87_div. It shares no code with tools/x87_soft.h. The harness, input bytes, guards and live-value check are those of the earlier x87 PRs. The live-value check enters each export with a caller value and control word 0x0F7F loaded, and the harness switches to 0x027F around the call, so fnstcw must store the profile's word, not the caller's.

Cases. 1776 cases:

  • random input blocks for the exports without values;
  • pairs of the special encodings and random m80 values for the division exports;
  • random m64 values for the full stack.

Negative controls. Four mutated translations must each mismatch:

Control Mutation Mismatches
fninit the store writes 0x037F, the word fninit loads 1068/1776
wide the store writes four bytes 1068/1776
skip the store is dropped 1068/1776
lowbyte the store writes only the low byte 1068/1776

1068 is every case of the five exports that store the word. The wait export has no store, so no control changes it. A wide store at the end of the output block counts as a mismatch because it writes into the guard.

Rejections. Translation rejects 5 sequences with the expected diagnostic: fldcw m16, fldcw after fstcw, fnstsw ax, fnstsw m16 and fnclex.

Changes to existing tests. cpu_aot_x87_add used wait as a rejection case. It becomes ffree st(1), which stays outside the profile, so the test keeps 17 rejections. Its case and control counts are unchanged. The profile sentence in docs/experiments.md now names fldcw and ffree among the rejected instructions instead of wait.

Verification

Tested at 1ba6f04 on main at 14e5456. The machine runs 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_fnstcw_lifted
ctest --preset dev --no-tests=error -R '^cpu_aot_x87_fnstcw$' -V
1776 cases over 6 exports (output block and guards) matched the reference in native and translated DLLs; each export returned with control 0x027f and an empty stack
each of the 8 control-word stores wrote 0x027f in the native and translated DLLs
processor: AMD64 Family 25 Model 97 Stepping 2, AuthenticAMD
x87_call kept a live caller ST(0) value and control word around all 6 native and translated exports
control fninit: 1068/1776 mismatches
control wide: 1068/1776 mismatches
control skip: 1068/1776 mismatches
control lowbyte: 1068/1776 mismatches
rejected 5 sequences outside the profile: fldcw m16; fstcw then fldcw; fnstsw ax; fnstsw m16; fnclex
  • x87 regressions: all fifteen x87 tests pass at 1ba6f04 (ctest --preset dev --no-tests=error -R '^cpu_aot_x87_'), including cpu_aot_x87_add with its replaced rejection case.
  • Full suite: ctest --preset dev --no-tests=error -j 8 --output-junit test-results.xml gives 231 passed, 2 failed and 2 capability skips (bvh_wave64_warp, wave_shuffle_warp) out of 235 at 1ba6f04. The run includes the GPU tests on an AMD Radeon RX 9070 XT. The two failures are gpu_alias_lookup_writes_hardware, the RDNA 4 array-atomics probe recorded in #369, and dxr_opacity_hardware, the RDNA 4 result recorded in #216. main at 14e5456 gives the same two failures and skips in the same session (230 passed out of 234). 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 at 1ba6f04 locally and in the Linux job of the fork CI run below.
  • Fork CI: the Windows build-and-test, Windows /O2 build-and-test, Linux Python and gfx1013 LLVM jobs passed at 1ba6f04 in the fork's Windows checks run 45003191737. Both Windows jobs ran and passed all fifteen x87 tests (192 passed and 2 capability skips, bvh_wave64_warp and wave_shuffle_warp, out of 194 in each).

Coverage. python tools/form_coverage.py out/build/dev/test-results.xml gives x64 373 of 948 forms at 1ba6f04, and 370 on main at 14e5456 in the same session. The added forms are fnstcw, fstcw and wait.

Not measured:

  • Intel CPUs. All measurements are on one AMD Zen 4 part.
  • A pending unmasked exception at wait, which the fixed control word cannot produce.

Overlap

I trial-merged this branch with all 72 open PRs. Each one conflicts in the same files against main and against main with this branch merged: the 9 that merge cleanly into main also merge cleanly with it, 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_add.py, a CMake block after the cpu_aot_x87_iarith test, and the x87 section of docs/experiments.md.

Evidence

The fixture is synthetic and original to this repository (MIT). docs/experiments.md documents the three forms, 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:6540369601 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/513). Originally posted by [denn0x](https://github.com/denn0x) on 2026-11-22T08:40:00Z. --- ## Change Refs #406. This follows #499 (the integer-operand arithmetic). It adds the control-word store and `wait` to the opt-in x87 profile in `lower(..., x87=True)`: `fnstcw m16`, `fstcw m16` and `wait`. This is the fixed-control-word part of the next step discussed in #406; `fldcw` and the dynamic control word come separately. **Semantics.** - **`fnstcw m16`:** stores the profile's control word, `0x027F`, in two bytes. It does not touch the register stack. - **`wait`:** does nothing. Every exception is masked, so none can be pending. - **`fstcw m16`:** iced decodes it as `wait` followed by `fnstcw`, and the profile lowers the two in turn. The native runs confirm each of these rules. **Out of scope:** `fldcw` stays rejected, also after `fstcw`, along with every status-word form (`fnstsw`, `fstsw`, `fnclex`) and `fninit`. **Lowering (`tools/x87_lower.py`).** `fnstcw` becomes `store<uint16_t>(a, 0x027f);`, and `wait` becomes an empty statement. Neither changes the stack depth. No helper is added to `tools/x87_soft.h`. ## Fixture `tests/cpu/x87_fnstcw.asm` and `.def` are generated by `tests/cpu/x87_fnstcw.py asm`, and `emit` checks that every decoded x87 instruction is the form its sequence names, including the `wait` and `fnstcw` that `fstcw` decodes into. The `.asm` registers its test on its first line (`; coverage: source cpu_aot_x87_fnstcw`). The 6 exports cover: - `fnstcw` alone and `fstcw` alone; - `fnstcw` and `fstcw` between two m80 loads, an `fdivp` and the final store; - `wait` before, between and after loads, an `fdivp` and the final store; - stores to the first two and the last two bytes of the output block, next to the guards; - `fstcw` beside the values of a full stack of eight, all stored afterwards. **Model.** The model writes `0x027F` for each control-word store and divides with the `Fraction` model of `cpu_aot_x87_div`. It shares no code with `tools/x87_soft.h`. The harness, input bytes, guards and live-value check are those of the earlier x87 PRs. The live-value check enters each export with a caller value and control word `0x0F7F` loaded, and the harness switches to `0x027F` around the call, so `fnstcw` must store the profile's word, not the caller's. **Cases.** 1776 cases: - random input blocks for the exports without values; - pairs of the special encodings and random m80 values for the division exports; - random m64 values for the full stack. **Negative controls.** Four mutated translations must each mismatch: | Control | Mutation | Mismatches | |---|---|---| | `fninit` | the store writes `0x037F`, the word `fninit` loads | 1068/1776 | | `wide` | the store writes four bytes | 1068/1776 | | `skip` | the store is dropped | 1068/1776 | | `lowbyte` | the store writes only the low byte | 1068/1776 | 1068 is every case of the five exports that store the word. The `wait` export has no store, so no control changes it. A `wide` store at the end of the output block counts as a mismatch because it writes into the guard. **Rejections.** Translation rejects 5 sequences with the expected diagnostic: `fldcw m16`, `fldcw` after `fstcw`, `fnstsw ax`, `fnstsw m16` and `fnclex`. **Changes to existing tests.** `cpu_aot_x87_add` used `wait` as a rejection case. It becomes `ffree st(1)`, which stays outside the profile, so the test keeps 17 rejections. Its case and control counts are unchanged. The profile sentence in `docs/experiments.md` now names `fldcw` and `ffree` among the rejected instructions instead of `wait`. ## Verification Tested at `1ba6f04` on `main` at `14e5456`. The machine runs 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_fnstcw_lifted ctest --preset dev --no-tests=error -R '^cpu_aot_x87_fnstcw$' -V ``` ```text 1776 cases over 6 exports (output block and guards) matched the reference in native and translated DLLs; each export returned with control 0x027f and an empty stack each of the 8 control-word stores wrote 0x027f in the native and translated DLLs processor: AMD64 Family 25 Model 97 Stepping 2, AuthenticAMD x87_call kept a live caller ST(0) value and control word around all 6 native and translated exports control fninit: 1068/1776 mismatches control wide: 1068/1776 mismatches control skip: 1068/1776 mismatches control lowbyte: 1068/1776 mismatches rejected 5 sequences outside the profile: fldcw m16; fstcw then fldcw; fnstsw ax; fnstsw m16; fnclex ``` - **x87 regressions:** all fifteen x87 tests pass at `1ba6f04` (`ctest --preset dev --no-tests=error -R '^cpu_aot_x87_'`), including `cpu_aot_x87_add` with its replaced rejection case. - **Full suite:** `ctest --preset dev --no-tests=error -j 8 --output-junit test-results.xml` gives 231 passed, 2 failed and 2 capability skips (`bvh_wave64_warp`, `wave_shuffle_warp`) out of 235 at `1ba6f04`. The run includes the GPU tests on an AMD Radeon RX 9070 XT. The two failures are `gpu_alias_lookup_writes_hardware`, the RDNA 4 array-atomics probe recorded in #369, and `dxr_opacity_hardware`, the RDNA 4 result recorded in #216. `main` at `14e5456` gives the same two failures and skips in the same session (230 passed out of 234). 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 at `1ba6f04` locally and in the Linux job of the fork CI run below. - **Fork CI:** the Windows build-and-test, Windows `/O2` build-and-test, Linux Python and gfx1013 LLVM jobs passed at `1ba6f04` in the fork's `Windows checks` run [45003191737](https://github.com/denn0x/Xbox2Windows/actions/runs/45003191737). Both Windows jobs ran and passed all fifteen x87 tests (192 passed and 2 capability skips, `bvh_wave64_warp` and `wave_shuffle_warp`, out of 194 in each). **Coverage.** `python tools/form_coverage.py out/build/dev/test-results.xml` gives x64 **373 of 948** forms at `1ba6f04`, and 370 on `main` at `14e5456` in the same session. The added forms are `fnstcw`, `fstcw` and `wait`. **Not measured:** - Intel CPUs. All measurements are on one AMD Zen 4 part. - A pending unmasked exception at `wait`, which the fixed control word cannot produce. ## Overlap I trial-merged this branch with all 72 open PRs. Each one conflicts in the same files against `main` and against `main` with this branch merged: the 9 that merge cleanly into `main` also merge cleanly with it, 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_add.py`, a CMake block after the `cpu_aot_x87_iarith` test, and the x87 section of `docs/experiments.md`. ## Evidence The fixture is synthetic and original to this repository (MIT). `docs/experiments.md` documents the three forms, 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:26 +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!507
No description provided.