cpu: model PF of FCOMI and lower FCMOVU and FCMOVNU in the x87 profile #542

Merged
denn0x merged 2 commits from upstream-pr/548 into main 2026-11-22 13:19:37 +00:00
denn0x commented 2026-11-22 10:46:19 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by denn0x on 2026-11-22T10:46:19Z.


Change

Refs #406. This follows #535 (the status word), whose PF rule it extends. It adds the PF of fcomi, fcomip, fucomi and fucomip, and the two conditional moves that read it, fcmovu and fcmovnu, to the opt-in x87 profile. That PR deferred these to a follow-up.

Semantics. The four comparisons set ZF, PF and CF to 1 1 1 for unordered, and PF to 0 otherwise. They also clear OF, SF and AF, which the integer lowering has no reader for. fcmovu st, st(i) copies st(i) into st(0) when PF is set, and fcmovnu when it is clear. Like the other moves, the copy keeps the whole 80-bit encoding, so a signaling NaN stays signaling.

Lowering.

  • tools/ffx_version_query.py declares the integer lowering's parity in a function with jp, jnp, fcmovu or fcmovnu, as it already did for jp and jnp.
  • In such a function, x87_lower.statement emits parity = r == 3; for each of the four comparisons. Functions without such a reader keep their generated code.
  • assigns_parity counts the four comparisons as writers of PF. A jp, jnp, fcmovu or fcmovnu therefore translates when one of them, sahf or an 8-bit test or and wrote PF last on every path. After any other writer, for example cmp, inc or add, it is still rejected.
  • fcmovu and fcmovnu join the table of conditional moves, with the conditions parity and !parity.

Generated code. I compared every generated source against main at 075413d, built in the same build directory:

  • 225 of the 233 earlier generated sources are byte-identical, including 117 of the 125 cpu_x87_*.cpp files.
  • The eight sources of cpu_aot_x87_status differ in two lines each. The fcomi and fucomip statements of fcom_fcomi and fucom_fucomip now also set parity, because their functions contain jp. The sahf after them writes PF again before any reader, so the counts of that test are unchanged.

Fixture

tests/cpu/x87_parity.asm and .def are generated by tests/cpu/x87_parity.py asm, and emit checks every decoded x87 instruction against its sequence. A record stores whether jp, jnp, je and jb are taken, with only flag-neutral movs in between.

The 16 exports cover:

  • each comparison with st(1), read through the record;
  • fcmovu and fcmovnu after fucomi, fcomi, fucomip st, st(2) and fcomip, and fcmovu with st(7) at depth 8;
  • the equality test that compilers emit, fucomip; fstp st(0); jp; jne, and a classification into unordered, equal and neither by jnp and je;
  • the last writer of PF: fucomi after sahf, sahf after fucomi, and test al, al after fucomi;
  • fcmovu after sahf on all 256 AH values loaded by mov;
  • a join, chosen by an input dword, of a fucomi path and an fcom; fnstsw ax; sahf path.

Model. The model compares with the Fraction model of cpu_aot_x87_compare and copies raw encodings. 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.

Cases. 17672 cases:

  • every pair of the 34 special encodings;
  • values against themselves, their neighbour in the lowest significand bit and their negation;
  • random values, random triples of the special encodings and random m64 values.

In the four records, jp was taken in 2124 of 6624 cases. The moves read PF set in 2361 cases and clear in 4675.

Negative controls. Five mutated translations must each mismatch, and each must mismatch in a named export:

Control Mutation Mismatches In the named export
pfnever the comparisons clear PF 4959/17672 531 in equal_branch
pfequal PF is set for equal instead of unordered 6244/17672 660 in classify
pfkeep the comparisons leave PF as it was 4904/17672 127 in sahf_fucomi
uinvert fcmovu moves on a clear PF 4050/17672 1522 in fucomi_fcmovu
nuinvert fcmovnu moves on a set PF 2122/17672 1522 in fcomi_fcmovnu

pfkeep can mismatch in sahf_fucomi only where the sahf before fucomi left a different PF, which separates it from pfnever.

Rejections. Translation rejects 10 sequences with the expected diagnostic:

  • fcmovu before any flag writer;
  • fcmovnu after test eax, eax, fcmovu after fucomi and inc eax, and jp after fucomi and add eax, 1;
  • fcmovu after a call;
  • a join of a fucomi path and a cmp path;
  • sahf on fnstsw ax without a comparison, then fcmovu (the status-word diagnostic);
  • fcmovu st, st(2) with two values and fcmovnu st, st(1) with one value;
  • setp after fucomi, which the integer lowering does not implement.

Five twins must still translate:

  • fcmovu after fucomi;
  • fcmovnu after fucomi and mov eax, [rcx];
  • jp after fucomip and fstp st(0);
  • a join of a fucomi path and a sahf path;
  • fcmovu after fucomi and lea.

Changes to existing tests. Rejection cases that the profile now accepts are replaced by sequences that stay outside it, and each list keeps its length:

Test Replaced Now
cpu_aot_x87_compare jp and jnp after fucomi jp and jnp after fucomi and inc eax
cpu_aot_x87_status jp after fucomi jp after fucomi and inc eax
cpu_aot_x87_fcmov fcmovu and fcmovnu (unsupported operation) the same after cmp, now rejected by the PF rule
cpu_aot_x87_compare jo after fucomi cmovs after fucomi

jo was rejected only because the integer lowering has no jo. Since #534 the comparisons clear OF, which seto and adox read. cmovs reads SF, which the comparisons do not model, so the list again names a reader of an unmodelled flag. The PF diagnostic now names the four comparisons, and the needle in cpu_aot_x87_status follows it. Case and control counts of these tests are unchanged. The matching sentences in docs/experiments.md are updated.

Verification

Tested at 9332372 on main at 075413d. The machine runs Windows 11 build 26200, MSVC 19.50.35720, Windows SDK 10.0.26100.0 and Python 3.13 on an AMD Ryzen 7 7700 (AMD64 Family 25 Model 97 Stepping 2, AuthenticAMD).

cmake --build --preset dev --target cpu_x87_parity_lifted
ctest --preset dev --no-tests=error -R '^cpu_aot_x87_parity$' -V
17672 cases over 16 exports (output block and guards) matched the reference in native and translated DLLs; each export returned with control 0x027f and an empty stack
FCOMI-family records: JP taken in 2124 of 6624 cases; FCMOVU and FCMOVNU read PF set in 2361 and clear in 4675 moves
processor: AMD64 Family 25 Model 97 Stepping 2, AuthenticAMD
x87_call kept a live caller ST(0) value and control word around all 16 native and translated exports
control pfnever: 4959/17672 mismatches, 531 in equal_branch
control pfequal: 6244/17672 mismatches, 660 in classify
control pfkeep: 4904/17672 mismatches, 127 in sahf_fucomi
control uinvert: 4050/17672 mismatches, 1522 in fucomi_fcmovu
control nuinvert: 2122/17672 mismatches, 1522 in fcomi_fcmovnu
rejected 10 sequences: FCMOVU before any flag writer; FCMOVNU after TEST EAX, EAX; FCMOVU after FUCOMI and INC EAX; JP after FUCOMI and ADD EAX, 1; FCMOVU after FUCOMIP and a call; a join of a FUCOMI path and a CMP path; SAHF on FNSTSW AX with no comparison, then FCMOVU; FCMOVU ST(2) with two values; FCMOVNU ST(1) with one value; SETP after FUCOMI
translated 5 sequences whose PF the integer lowering models: FCMOVU after FUCOMI; FCMOVNU after FUCOMI and MOV EAX, [RCX]; JP after FUCOMIP and FSTP ST(0); a join of a FUCOMI path and a SAHF path; FCMOVU after FUCOMI and LEA
  • x87 regressions: all eighteen x87 tests pass at 9332372 (ctest --preset dev --no-tests=error -R '^cpu_aot_x87_'), including the three with replaced rejection cases.
  • Full suite: ctest --preset dev --no-tests=error -j 8 --output-junit test-results.xml gives 251 passed, 2 failed and 2 capability skips (bvh_wave64_warp, wave_shuffle_warp) out of 255 at 9332372. 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 075413d gives 250 passed, with the same two failures and skips, out of 254 in the same session. 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 9332372, 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 9332372 in the fork's Windows checks run 45013424622. Both Windows jobs ran and passed all eighteen x87 tests (208 passed and 2 capability skips, bvh_wave64_warp and wave_shuffle_warp, out of 210 in each).

Coverage. python tools/form_coverage.py out/build/dev/test-results.xml gives x64 460 of 948 forms, against 458 on main in the same session. The added forms are fcmovu and fcmovnu.

Not measured:

  • Intel CPUs. All measurements are on one AMD Zen 4 part.
  • setp, setnp, cmovp and cmovnp, which the integer lowering does not implement.

Overlap

I trial-merged this branch with the 56 open PRs. Each one conflicts in the same files against main and against main with this branch merged: the 8 that merge cleanly into main also merge cleanly with it, and this branch adds no conflict to the others.

Evidence

The fixture is synthetic and original to this repository (MIT). docs/experiments.md documents the 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:6541461845 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/548). Originally posted by [denn0x](https://github.com/denn0x) on 2026-11-22T10:46:19Z. --- ## Change Refs #406. This follows #535 (the status word), whose PF rule it extends. It adds the PF of `fcomi`, `fcomip`, `fucomi` and `fucomip`, and the two conditional moves that read it, `fcmovu` and `fcmovnu`, to the opt-in x87 profile. That PR deferred these to a follow-up. **Semantics.** The four comparisons set ZF, PF and CF to `1 1 1` for unordered, and PF to 0 otherwise. They also clear OF, SF and AF, which the integer lowering has no reader for. `fcmovu st, st(i)` copies `st(i)` into `st(0)` when PF is set, and `fcmovnu` when it is clear. Like the other moves, the copy keeps the whole 80-bit encoding, so a signaling NaN stays signaling. **Lowering.** - `tools/ffx_version_query.py` declares the integer lowering's `parity` in a function with `jp`, `jnp`, `fcmovu` or `fcmovnu`, as it already did for `jp` and `jnp`. - In such a function, `x87_lower.statement` emits `parity = r == 3;` for each of the four comparisons. Functions without such a reader keep their generated code. - `assigns_parity` counts the four comparisons as writers of PF. A `jp`, `jnp`, `fcmovu` or `fcmovnu` therefore translates when one of them, `sahf` or an 8-bit `test` or `and` wrote PF last on every path. After any other writer, for example `cmp`, `inc` or `add`, it is still rejected. - `fcmovu` and `fcmovnu` join the table of conditional moves, with the conditions `parity` and `!parity`. **Generated code.** I compared every generated source against `main` at `075413d`, built in the same build directory: - 225 of the 233 earlier generated sources are byte-identical, including 117 of the 125 `cpu_x87_*.cpp` files. - The eight sources of `cpu_aot_x87_status` differ in two lines each. The `fcomi` and `fucomip` statements of `fcom_fcomi` and `fucom_fucomip` now also set `parity`, because their functions contain `jp`. The `sahf` after them writes PF again before any reader, so the counts of that test are unchanged. ## Fixture `tests/cpu/x87_parity.asm` and `.def` are generated by `tests/cpu/x87_parity.py asm`, and `emit` checks every decoded x87 instruction against its sequence. A record stores whether `jp`, `jnp`, `je` and `jb` are taken, with only flag-neutral `mov`s in between. **The 16 exports** cover: - each comparison with `st(1)`, read through the record; - `fcmovu` and `fcmovnu` after `fucomi`, `fcomi`, `fucomip st, st(2)` and `fcomip`, and `fcmovu` with `st(7)` at depth 8; - the equality test that compilers emit, `fucomip; fstp st(0); jp; jne`, and a classification into unordered, equal and neither by `jnp` and `je`; - the last writer of PF: `fucomi` after `sahf`, `sahf` after `fucomi`, and `test al, al` after `fucomi`; - `fcmovu` after `sahf` on all 256 AH values loaded by `mov`; - a join, chosen by an input dword, of a `fucomi` path and an `fcom; fnstsw ax; sahf` path. **Model.** The model compares with the `Fraction` model of `cpu_aot_x87_compare` and copies raw encodings. 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. **Cases.** 17672 cases: - every pair of the 34 special encodings; - values against themselves, their neighbour in the lowest significand bit and their negation; - random values, random triples of the special encodings and random m64 values. In the four records, `jp` was taken in 2124 of 6624 cases. The moves read PF set in 2361 cases and clear in 4675. **Negative controls.** Five mutated translations must each mismatch, and each must mismatch in a named export: | Control | Mutation | Mismatches | In the named export | |---|---|---|---| | `pfnever` | the comparisons clear PF | 4959/17672 | 531 in `equal_branch` | | `pfequal` | PF is set for equal instead of unordered | 6244/17672 | 660 in `classify` | | `pfkeep` | the comparisons leave PF as it was | 4904/17672 | 127 in `sahf_fucomi` | | `uinvert` | `fcmovu` moves on a clear PF | 4050/17672 | 1522 in `fucomi_fcmovu` | | `nuinvert` | `fcmovnu` moves on a set PF | 2122/17672 | 1522 in `fcomi_fcmovnu` | `pfkeep` can mismatch in `sahf_fucomi` only where the `sahf` before `fucomi` left a different PF, which separates it from `pfnever`. **Rejections.** Translation rejects 10 sequences with the expected diagnostic: - `fcmovu` before any flag writer; - `fcmovnu` after `test eax, eax`, `fcmovu` after `fucomi` and `inc eax`, and `jp` after `fucomi` and `add eax, 1`; - `fcmovu` after a call; - a join of a `fucomi` path and a `cmp` path; - `sahf` on `fnstsw ax` without a comparison, then `fcmovu` (the status-word diagnostic); - `fcmovu st, st(2)` with two values and `fcmovnu st, st(1)` with one value; - `setp` after `fucomi`, which the integer lowering does not implement. Five twins must still translate: - `fcmovu` after `fucomi`; - `fcmovnu` after `fucomi` and `mov eax, [rcx]`; - `jp` after `fucomip` and `fstp st(0)`; - a join of a `fucomi` path and a `sahf` path; - `fcmovu` after `fucomi` and `lea`. **Changes to existing tests.** Rejection cases that the profile now accepts are replaced by sequences that stay outside it, and each list keeps its length: | Test | Replaced | Now | |---|---|---| | `cpu_aot_x87_compare` | `jp` and `jnp` after `fucomi` | `jp` and `jnp` after `fucomi` and `inc eax` | | `cpu_aot_x87_status` | `jp` after `fucomi` | `jp` after `fucomi` and `inc eax` | | `cpu_aot_x87_fcmov` | `fcmovu` and `fcmovnu` (unsupported operation) | the same after `cmp`, now rejected by the PF rule | | `cpu_aot_x87_compare` | `jo` after `fucomi` | `cmovs` after `fucomi` | `jo` was rejected only because the integer lowering has no `jo`. Since #534 the comparisons clear OF, which `seto` and `adox` read. `cmovs` reads SF, which the comparisons do not model, so the list again names a reader of an unmodelled flag. The PF diagnostic now names the four comparisons, and the needle in `cpu_aot_x87_status` follows it. Case and control counts of these tests are unchanged. The matching sentences in `docs/experiments.md` are updated. ## Verification Tested at `9332372` on `main` at `075413d`. The machine runs Windows 11 build 26200, MSVC 19.50.35720, Windows SDK 10.0.26100.0 and Python 3.13 on an AMD Ryzen 7 7700 (`AMD64 Family 25 Model 97 Stepping 2, AuthenticAMD`). ```powershell cmake --build --preset dev --target cpu_x87_parity_lifted ctest --preset dev --no-tests=error -R '^cpu_aot_x87_parity$' -V ``` ```text 17672 cases over 16 exports (output block and guards) matched the reference in native and translated DLLs; each export returned with control 0x027f and an empty stack FCOMI-family records: JP taken in 2124 of 6624 cases; FCMOVU and FCMOVNU read PF set in 2361 and clear in 4675 moves processor: AMD64 Family 25 Model 97 Stepping 2, AuthenticAMD x87_call kept a live caller ST(0) value and control word around all 16 native and translated exports control pfnever: 4959/17672 mismatches, 531 in equal_branch control pfequal: 6244/17672 mismatches, 660 in classify control pfkeep: 4904/17672 mismatches, 127 in sahf_fucomi control uinvert: 4050/17672 mismatches, 1522 in fucomi_fcmovu control nuinvert: 2122/17672 mismatches, 1522 in fcomi_fcmovnu rejected 10 sequences: FCMOVU before any flag writer; FCMOVNU after TEST EAX, EAX; FCMOVU after FUCOMI and INC EAX; JP after FUCOMI and ADD EAX, 1; FCMOVU after FUCOMIP and a call; a join of a FUCOMI path and a CMP path; SAHF on FNSTSW AX with no comparison, then FCMOVU; FCMOVU ST(2) with two values; FCMOVNU ST(1) with one value; SETP after FUCOMI translated 5 sequences whose PF the integer lowering models: FCMOVU after FUCOMI; FCMOVNU after FUCOMI and MOV EAX, [RCX]; JP after FUCOMIP and FSTP ST(0); a join of a FUCOMI path and a SAHF path; FCMOVU after FUCOMI and LEA ``` - **x87 regressions:** all eighteen x87 tests pass at `9332372` (`ctest --preset dev --no-tests=error -R '^cpu_aot_x87_'`), including the three with replaced rejection cases. - **Full suite:** `ctest --preset dev --no-tests=error -j 8 --output-junit test-results.xml` gives 251 passed, 2 failed and 2 capability skips (`bvh_wave64_warp`, `wave_shuffle_warp`) out of 255 at `9332372`. 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 `075413d` gives 250 passed, with the same two failures and skips, out of 254 in the same session. 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 `9332372`, 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 `9332372` in the fork's `Windows checks` run [45013424622](https://github.com/denn0x/Xbox2Windows/actions/runs/45013424622). Both Windows jobs ran and passed all eighteen x87 tests (208 passed and 2 capability skips, `bvh_wave64_warp` and `wave_shuffle_warp`, out of 210 in each). **Coverage.** `python tools/form_coverage.py out/build/dev/test-results.xml` gives x64 **460 of 948** forms, against 458 on `main` in the same session. The added forms are `fcmovu` and `fcmovnu`. **Not measured:** - Intel CPUs. All measurements are on one AMD Zen 4 part. - `setp`, `setnp`, `cmovp` and `cmovnp`, which the integer lowering does not implement. ## Overlap I trial-merged this branch with the 56 open PRs. Each one conflicts in the same files against `main` and against `main` with this branch merged: the 8 that merge cleanly into `main` also merge cleanly with it, and this branch adds no conflict to the others. ## Evidence The fixture is synthetic and original to this repository (MIT). `docs/experiments.md` documents the 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:38 +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!542
No description provided.