cpu: lower FCOMI, FCOMIP, FUCOMI and FUCOMIP in the x87 profile #450

Merged
denn0x merged 1 commit from upstream-pr/456 into main 2026-11-22 04:51:48 +00:00
denn0x commented 2026-11-22 04:46:07 +00:00 (Migrated from github.com)

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


Change

Refs #406. This follows #452 (integer conversions). It adds the comparisons into EFLAGS to the opt-in x87 profile in lower(..., x87=True): fcomi, fcomip, fucomi and fucomip with st, st(i). The p forms pop after comparing.

Flags. A comparison sets the two flags the integer lowering models:

Result ZF CF
st(0) < st(i) 0 1
equal 1 0
st(0) > st(i) 0 0
unordered (a NaN or an unsupported encoding) 1 1

With invalid operations masked, fcomi and fucomi give the same flags. All 64 significand bits count, +0 equals -0, and a pseudo-denormal equals the normal value with the same significand at biased exponent 1.

PF is not modelled. The integer lowering already rejects every instruction that reads PF, SF, OF or the whole flags register, so nothing in the profile can observe the flags it does not model. As a consequence, the usual compiler test for floating-point equality (fucomip; jp; jne) stays outside the profile. Supporting it needs a parity flag in the integer lowering, which is a separate change.

Out of scope: the comparisons into the status word (fcom, fucom, fucompp, ftst, fxam, ficom) and fnstsw, which nothing in the profile could read. They remain rejected.

Lowering (tools/x87_lower.py).

  • Statement: { const int r = x87_compare(x87_slot[top], x87_slot[k]); zero = r == 1 || r == 3; carry = r == 0 || r == 3; }. It writes the same zero and carry variables as the integer compares.
  • Depth: an st(i) operand at or beyond the current depth is an underflow. fcomip and fucomip lower the depth.

Helper (tools/x87_soft.h). x87_compare(a, b) returns 0 for less, 1 for equal, 2 for greater and 3 for unordered. It handles unordered operands first, then zeros of either sign, then opposite signs, and then compares the unpacked exponent and significand. Infinities unpack above every finite value.

Fixture

tests/cpu/x87_compare.asm and .def are generated by tests/cpu/x87_compare.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_compare). The 11 exports cover:

  • all four mnemonics with st(1);
  • fcomi st, st(2) and fucomip st, st(2);
  • a value against itself through fucomi st, st(0) and fcomip st, st(0);
  • m64 and m32 operands loaded before the comparison;
  • two comparisons in a row.

Every export stores the values left on the stack.

Flag record. After each comparison, a 20-byte record reads the flags through the integer lowering's own flag readers, with only flag-neutral movs in between:

  • je, jne, ja, jae, jb and jbe, each as a taken byte;
  • sete;
  • cmove, cmovne and cmovbe, each as a dword;
  • last, rcl of a zeroed register.

rcl rotates CF in and changes it, so it comes last and CF is also observed on its own. loope and loopne, which also read ZF, are not used.

Model. The model compares with Fraction arithmetic and 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. 11913 cases. The first comparison of each case gives 3489 less, 2008 equal, 3520 greater and 2896 unordered. They cover:

  • every pair of the 34 special encodings;
  • values against themselves, against their neighbours in the lowest significand bit and in bit 11, against their negation and against the next exponent;
  • pseudo-denormal and normal twins, and denormals one bit apart;
  • random values;
  • the m64 and m32 special encodings.

Negative controls. Six mutated translations must each mismatch:

Control Mutation Mismatches
unordeq unordered reported as equal 2948/11913
swap the operands exchanged 7065/11913
zeroorder -0 compares below +0 13/11913
low11 the low 11 significand bits ignored 723/11913
rawexp the raw biased exponent used for pseudo-denormals 32/11913
unnormal unnormals compared by their bits instead of as unordered 789/11913

Rejections. Translation rejects 19 sequences with the expected diagnostic:

  • jp, jnp, setp, cmovp, js, jl, lahf, sahf and pushfq after fucomi, with the integer lowering's "unsupported query operation";
  • comparisons without enough stack values: fcomi st, st(1) with one value, fucomip st, st(2) with two, and fcomip on an empty stack;
  • fcom, fucom, fucompp, ftst, fxam, ficom and fnstsw.

Changes to existing tests: none. The fcom and fcomp rejection cases of the earlier x87 tests stay valid.

Verification

Tested at 6804d0d on 7cca3d1. main has since moved to 3a806d3 (#455), which does not touch the x87 files or tools/ffx_version_query.py. This branch merges cleanly into 3a806d3. 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_compare_lifted
ctest --preset dev --no-tests=error -R '^cpu_aot_x87_compare$' -V
11913 cases over 11 exports (output block and guards) matched the reference in native and translated DLLs; each export returned with control 0x027f and an empty stack
first-record outcomes: less 3489, equal 2008, greater 3520, unordered 2896
processor: AMD64 Family 25 Model 97 Stepping 2, AuthenticAMD
x87_call kept a live caller ST(0) value and control word around all 11 native and translated exports
control unordeq: 2948/11913 mismatches
control swap: 7065/11913 mismatches
control zeroorder: 13/11913 mismatches
control low11: 723/11913 mismatches
control rawexp: 32/11913 mismatches
control unnormal: 789/11913 mismatches
rejected 19 sequences outside the profile: fucomi then jp; fucomi then jnp; fucomi then setp; fucomi then cmovp; fucomi then js; fucomi then jl; fucomi then lahf; fucomi then sahf; fucomi then pushfq; fcomi st, st(1) with one value; fucomip st, st(2) with two values; fcomip on an empty stack; fcom st(1); fucom st(1); fucompp; ftst; fxam; ficom m16; fnstsw ax
  • x87 regressions: all eight x87 tests pass (x87_add, x87_mul, x87_div, x87_sub, x87_moves, x87_constants, x87_integers, x87_compare).
  • Full suite: ctest --preset dev --no-tests=error -j 8 --output-junit test-results.xml gives 182 passed, 1 failed and 2 capability skips out of 185. 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 6804d0d in the fork's Windows checks run 44986105043. Both Windows jobs ran and passed all eight x87 tests (157 of 157 tests in each).

Coverage. python tools/form_coverage.py out/build/dev/test-results.xml gives x64 157 of 948 forms, and 153 without cpu_aot_x87_compare. The added forms are fcomi, fcomip, fucomi and fucomip.

Not measured:

  • Intel CPUs. All measurements are on one AMD Zen 4 part.
  • PF, and the OF, SF and AF that fcomi clears, because the integer lowering does not model them.
  • The status word, including the invalid-operation flag that fcomi raises for a QNaN and fucomi does not.

Overlap

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

Two open PRs change the flag model that this PR writes into:

  • #335 (ADCX, ADOX and SETO) adds an overflow flag to the integer lowering, with seto as a reader. fcomi clears OF, so if #335 lands first, the comparison statement here must also set overflow = false. Otherwise seto after fcomi would read a stale value. Whichever PR lands second should add that assignment.
  • #328 (unsigned and equality CMOVcc and SETcc) adds readers of zero and carry. They read the flags this PR writes in the same way as the readers that already exist, so the two PRs compose.

Evidence

The fixture is synthetic and original to this repository (MIT). docs/experiments.md documents the comparisons, the flag mapping, the PF limitation, 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:6538575511 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/456). Originally posted by [denn0x](https://github.com/denn0x) on 2026-11-22T04:46:07Z. --- ## Change Refs #406. This follows #452 (integer conversions). It adds the comparisons into EFLAGS to the opt-in x87 profile in `lower(..., x87=True)`: `fcomi`, `fcomip`, `fucomi` and `fucomip` with `st, st(i)`. The `p` forms pop after comparing. **Flags.** A comparison sets the two flags the integer lowering models: | Result | ZF | CF | |---|---|---| | `st(0)` < `st(i)` | 0 | 1 | | equal | 1 | 0 | | `st(0)` > `st(i)` | 0 | 0 | | unordered (a NaN or an unsupported encoding) | 1 | 1 | With invalid operations masked, `fcomi` and `fucomi` give the same flags. All 64 significand bits count, +0 equals -0, and a pseudo-denormal equals the normal value with the same significand at biased exponent 1. **PF is not modelled.** The integer lowering already rejects every instruction that reads PF, SF, OF or the whole flags register, so nothing in the profile can observe the flags it does not model. As a consequence, the usual compiler test for floating-point equality (`fucomip; jp; jne`) stays outside the profile. Supporting it needs a parity flag in the integer lowering, which is a separate change. **Out of scope:** the comparisons into the status word (`fcom`, `fucom`, `fucompp`, `ftst`, `fxam`, `ficom`) and `fnstsw`, which nothing in the profile could read. They remain rejected. **Lowering (`tools/x87_lower.py`).** - **Statement:** `{ const int r = x87_compare(x87_slot[top], x87_slot[k]); zero = r == 1 || r == 3; carry = r == 0 || r == 3; }`. It writes the same `zero` and `carry` variables as the integer compares. - **Depth:** an `st(i)` operand at or beyond the current depth is an underflow. `fcomip` and `fucomip` lower the depth. **Helper (`tools/x87_soft.h`).** `x87_compare(a, b)` returns 0 for less, 1 for equal, 2 for greater and 3 for unordered. It handles unordered operands first, then zeros of either sign, then opposite signs, and then compares the unpacked exponent and significand. Infinities unpack above every finite value. ## Fixture `tests/cpu/x87_compare.asm` and `.def` are generated by `tests/cpu/x87_compare.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_compare`). The 11 exports cover: - all four mnemonics with `st(1)`; - `fcomi st, st(2)` and `fucomip st, st(2)`; - a value against itself through `fucomi st, st(0)` and `fcomip st, st(0)`; - m64 and m32 operands loaded before the comparison; - two comparisons in a row. Every export stores the values left on the stack. **Flag record.** After each comparison, a 20-byte record reads the flags through the integer lowering's own flag readers, with only flag-neutral `mov`s in between: - `je`, `jne`, `ja`, `jae`, `jb` and `jbe`, each as a taken byte; - `sete`; - `cmove`, `cmovne` and `cmovbe`, each as a dword; - last, `rcl` of a zeroed register. `rcl` rotates CF in and changes it, so it comes last and CF is also observed on its own. `loope` and `loopne`, which also read ZF, are not used. **Model.** The model compares with `Fraction` arithmetic and 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.** 11913 cases. The first comparison of each case gives 3489 less, 2008 equal, 3520 greater and 2896 unordered. They cover: - every pair of the 34 special encodings; - values against themselves, against their neighbours in the lowest significand bit and in bit 11, against their negation and against the next exponent; - pseudo-denormal and normal twins, and denormals one bit apart; - random values; - the m64 and m32 special encodings. **Negative controls.** Six mutated translations must each mismatch: | Control | Mutation | Mismatches | |---|---|---| | `unordeq` | unordered reported as equal | 2948/11913 | | `swap` | the operands exchanged | 7065/11913 | | `zeroorder` | -0 compares below +0 | 13/11913 | | `low11` | the low 11 significand bits ignored | 723/11913 | | `rawexp` | the raw biased exponent used for pseudo-denormals | 32/11913 | | `unnormal` | unnormals compared by their bits instead of as unordered | 789/11913 | **Rejections.** Translation rejects 19 sequences with the expected diagnostic: - `jp`, `jnp`, `setp`, `cmovp`, `js`, `jl`, `lahf`, `sahf` and `pushfq` after `fucomi`, with the integer lowering's "unsupported query operation"; - comparisons without enough stack values: `fcomi st, st(1)` with one value, `fucomip st, st(2)` with two, and `fcomip` on an empty stack; - `fcom`, `fucom`, `fucompp`, `ftst`, `fxam`, `ficom` and `fnstsw`. **Changes to existing tests:** none. The `fcom` and `fcomp` rejection cases of the earlier x87 tests stay valid. ## Verification Tested at `6804d0d` on `7cca3d1`. `main` has since moved to `3a806d3` (#455), which does not touch the x87 files or `tools/ffx_version_query.py`. This branch merges cleanly into `3a806d3`. 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_compare_lifted ctest --preset dev --no-tests=error -R '^cpu_aot_x87_compare$' -V ``` ```text 11913 cases over 11 exports (output block and guards) matched the reference in native and translated DLLs; each export returned with control 0x027f and an empty stack first-record outcomes: less 3489, equal 2008, greater 3520, unordered 2896 processor: AMD64 Family 25 Model 97 Stepping 2, AuthenticAMD x87_call kept a live caller ST(0) value and control word around all 11 native and translated exports control unordeq: 2948/11913 mismatches control swap: 7065/11913 mismatches control zeroorder: 13/11913 mismatches control low11: 723/11913 mismatches control rawexp: 32/11913 mismatches control unnormal: 789/11913 mismatches rejected 19 sequences outside the profile: fucomi then jp; fucomi then jnp; fucomi then setp; fucomi then cmovp; fucomi then js; fucomi then jl; fucomi then lahf; fucomi then sahf; fucomi then pushfq; fcomi st, st(1) with one value; fucomip st, st(2) with two values; fcomip on an empty stack; fcom st(1); fucom st(1); fucompp; ftst; fxam; ficom m16; fnstsw ax ``` - **x87 regressions:** all eight x87 tests pass (`x87_add`, `x87_mul`, `x87_div`, `x87_sub`, `x87_moves`, `x87_constants`, `x87_integers`, `x87_compare`). - **Full suite:** `ctest --preset dev --no-tests=error -j 8 --output-junit test-results.xml` gives 182 passed, 1 failed and 2 capability skips out of 185. 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 `6804d0d` in the fork's `Windows checks` run [44986105043](https://github.com/denn0x/Xbox2Windows/actions/runs/44986105043). Both Windows jobs ran and passed all eight x87 tests (157 of 157 tests in each). **Coverage.** `python tools/form_coverage.py out/build/dev/test-results.xml` gives x64 **157 of 948** forms, and 153 without `cpu_aot_x87_compare`. The added forms are `fcomi`, `fcomip`, `fucomi` and `fucomip`. **Not measured:** - Intel CPUs. All measurements are on one AMD Zen 4 part. - PF, and the OF, SF and AF that `fcomi` clears, because the integer lowering does not model them. - The status word, including the invalid-operation flag that `fcomi` raises for a QNaN and `fucomi` does not. ## Overlap I trial-merged this branch with all 89 open PRs. Each one conflicts in the same files against `main` and against this branch: the 17 that merge cleanly into `main` also merge cleanly here, and this branch adds no conflict to the others. Two open PRs change the flag model that this PR writes into: - **#335** (ADCX, ADOX and SETO) adds an `overflow` flag to the integer lowering, with `seto` as a reader. `fcomi` clears OF, so if #335 lands first, the comparison statement here must also set `overflow = false`. Otherwise `seto` after `fcomi` would read a stale value. Whichever PR lands second should add that assignment. - **#328** (unsigned and equality CMOVcc and SETcc) adds readers of `zero` and `carry`. They read the flags this PR writes in the same way as the readers that already exist, so the two PRs compose. ## Evidence The fixture is synthetic and original to this repository (MIT). `docs/experiments.md` documents the comparisons, the flag mapping, the PF limitation, 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:07 +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!450
No description provided.