cpu: clear OF in the x87 FCOMI lowering #528

Merged
denn0x merged 1 commit from upstream-pr/534 into main 2026-11-22 10:13:40 +00:00
denn0x commented 2026-11-22 10:02:59 +00:00 (Migrated from github.com)

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


Change

Refs #406. fcomi, fcomip, fucomi and fucomip clear OF. The x87 profile lowered them before the integer lowering modelled OF, and #335 then added OF to the integer lowering. Since then the comparison statement set only ZF and CF and left overflow as the previous instruction set it. On main, this function translates and returns 1 where the processor returns 0 whenever the add overflows:

add  eax, ecx          ; OF = 1 for 0x7fffffff + 1
fld  tbyte ptr [rcx]
fld  st(0)
fcomi st, st(1)        ; OF = 0
fstp st(0)
fstp st(0)
seto al                ; native 0, translated 1
ret

x87_lower.statement now emits overflow = false; with ZF and CF for the four comparisons. The docstring and docs/experiments.md say so. Before, docs/experiments.md said that readers of OF stay outside the profile, which stopped being true when #335 added seto and adox.

Generated code. I compared every generated source against main, built in the same build directory. 206 of the 222 are byte-identical, including 100 of the 116 cpu_x87_*.cpp files. The nine sources of cpu_aot_x87_fcmov differ only in their two fcomi statements, which gain overflow = false;. The seven sources of cpu_aot_x87_compare gain it in every comparison statement and contain the new export, and cpu_x87_compare_keepof.cpp is new.

Fixture

cpu_aot_x87_compare gains one export, add_fcomi_of. It adds two input dwords, stores seto, loads two m80 values, runs fcomi st, st(1), and then stores seto and adox of a zeroed register, both before the record, whose rcl changes OF. The 317 cases pair every seventh comparison pair of the test with dword pairs that overflow (7fffffff + 1, 80000000 + 80000000, 40000000 + 40000000, 80000000 + ffffffff) or do not (1 + 2, ffffffff + 1). The add set OF in 211 of them, and the native seto before the comparison agrees with the model in all 317.

A seventh control, keepof, removes the new overflow = false;, which gives the lowering before this change. It must mismatch, and it does in 211/12230 cases, all in add_fcomi_of. The six earlier controls keep their mutations; their counts grow with the new cases (3023, 7260, 13, 745, 33 and 812).

Verification

Tested at 41b7415 on main at 119bdd6. 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_compare_lifted
ctest --preset dev --no-tests=error -R '^cpu_aot_x87_compare$' -V
12230 cases over 12 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 3590, equal 2055, greater 3614, unordered 2971
add_fcomi_of: ADD set OF before the comparison in 211 of 317 cases
processor: AMD64 Family 25 Model 97 Stepping 2, AuthenticAMD
x87_call kept a live caller ST(0) value and control word around all 12 native and translated exports
control unordeq: 3023/12230 mismatches
control swap: 7260/12230 mismatches
control zeroorder: 13/12230 mismatches
control low11: 745/12230 mismatches
control rawexp: 33/12230 mismatches
control unnormal: 812/12230 mismatches
control keepof: 211/12230 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 sixteen x87 tests pass at 41b7415 (ctest --preset dev --no-tests=error -R '^cpu_aot_x87_').
  • Full suite: ctest --preset dev --no-tests=error -j 8 --output-junit test-results.xml gives 241 passed, 2 failed and 2 capability skips (bvh_wave64_warp, wave_shuffle_warp) out of 245 at 41b7415. 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 119bdd6 gives the same result 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 41b7415, 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 41b7415 in the fork's Windows checks run 45009547556. Both Windows jobs ran and passed all sixteen x87 tests (201 passed and 2 capability skips, bvh_wave64_warp and wave_shuffle_warp, out of 203 in each).

Coverage. python tools/form_coverage.py out/build/dev/test-results.xml gives x64 424 of 948 forms, as on main in the same session. No form is added.

Not measured: SF and AF, which the comparisons also clear. The integer lowering does not model them and rejects their readers.

Overlap

I trial-merged this branch with the 55 open PRs. Each one conflicts in the same files against main and against main with this branch merged: the 6 that merge cleanly into main also merge cleanly with it, and this branch adds no conflict to the others. The change is one line of tools/x87_lower.py and its docstring, one export of tests/cpu/x87_compare.py, one control in CMakeLists.txt and the cpu_aot_x87_compare paragraph of docs/experiments.md.

Evidence

The fixture is synthetic and original to this repository (MIT). docs/experiments.md documents the export, the control 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:6541067143 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/534). Originally posted by [denn0x](https://github.com/denn0x) on 2026-11-22T10:02:59Z. --- ## Change Refs #406. `fcomi`, `fcomip`, `fucomi` and `fucomip` clear OF. The x87 profile lowered them before the integer lowering modelled OF, and #335 then added OF to the integer lowering. Since then the comparison statement set only ZF and CF and left `overflow` as the previous instruction set it. On `main`, this function translates and returns 1 where the processor returns 0 whenever the `add` overflows: ```asm add eax, ecx ; OF = 1 for 0x7fffffff + 1 fld tbyte ptr [rcx] fld st(0) fcomi st, st(1) ; OF = 0 fstp st(0) fstp st(0) seto al ; native 0, translated 1 ret ``` `x87_lower.statement` now emits `overflow = false;` with ZF and CF for the four comparisons. The docstring and `docs/experiments.md` say so. Before, `docs/experiments.md` said that readers of OF stay outside the profile, which stopped being true when #335 added `seto` and `adox`. **Generated code.** I compared every generated source against `main`, built in the same build directory. 206 of the 222 are byte-identical, including 100 of the 116 `cpu_x87_*.cpp` files. The nine sources of `cpu_aot_x87_fcmov` differ only in their two `fcomi` statements, which gain `overflow = false;`. The seven sources of `cpu_aot_x87_compare` gain it in every comparison statement and contain the new export, and `cpu_x87_compare_keepof.cpp` is new. ## Fixture `cpu_aot_x87_compare` gains one export, `add_fcomi_of`. It adds two input dwords, stores `seto`, loads two m80 values, runs `fcomi st, st(1)`, and then stores `seto` and `adox` of a zeroed register, both before the record, whose `rcl` changes OF. The 317 cases pair every seventh comparison pair of the test with dword pairs that overflow (`7fffffff + 1`, `80000000 + 80000000`, `40000000 + 40000000`, `80000000 + ffffffff`) or do not (`1 + 2`, `ffffffff + 1`). The `add` set OF in 211 of them, and the native `seto` before the comparison agrees with the model in all 317. A seventh control, `keepof`, removes the new `overflow = false;`, which gives the lowering before this change. It must mismatch, and it does in 211/12230 cases, all in `add_fcomi_of`. The six earlier controls keep their mutations; their counts grow with the new cases (3023, 7260, 13, 745, 33 and 812). ## Verification Tested at `41b7415` on `main` at `119bdd6`. 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_compare_lifted ctest --preset dev --no-tests=error -R '^cpu_aot_x87_compare$' -V ``` ```text 12230 cases over 12 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 3590, equal 2055, greater 3614, unordered 2971 add_fcomi_of: ADD set OF before the comparison in 211 of 317 cases processor: AMD64 Family 25 Model 97 Stepping 2, AuthenticAMD x87_call kept a live caller ST(0) value and control word around all 12 native and translated exports control unordeq: 3023/12230 mismatches control swap: 7260/12230 mismatches control zeroorder: 13/12230 mismatches control low11: 745/12230 mismatches control rawexp: 33/12230 mismatches control unnormal: 812/12230 mismatches control keepof: 211/12230 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 sixteen x87 tests pass at `41b7415` (`ctest --preset dev --no-tests=error -R '^cpu_aot_x87_'`). - **Full suite:** `ctest --preset dev --no-tests=error -j 8 --output-junit test-results.xml` gives 241 passed, 2 failed and 2 capability skips (`bvh_wave64_warp`, `wave_shuffle_warp`) out of 245 at `41b7415`. 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 `119bdd6` gives the same result 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 `41b7415`, 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 `41b7415` in the fork's `Windows checks` run [45009547556](https://github.com/denn0x/Xbox2Windows/actions/runs/45009547556). Both Windows jobs ran and passed all sixteen x87 tests (201 passed and 2 capability skips, `bvh_wave64_warp` and `wave_shuffle_warp`, out of 203 in each). **Coverage.** `python tools/form_coverage.py out/build/dev/test-results.xml` gives x64 **424 of 948** forms, as on `main` in the same session. No form is added. **Not measured:** SF and AF, which the comparisons also clear. The integer lowering does not model them and rejects their readers. ## Overlap I trial-merged this branch with the 55 open PRs. Each one conflicts in the same files against `main` and against `main` with this branch merged: the 6 that merge cleanly into `main` also merge cleanly with it, and this branch adds no conflict to the others. The change is one line of `tools/x87_lower.py` and its docstring, one export of `tests/cpu/x87_compare.py`, one control in `CMakeLists.txt` and the `cpu_aot_x87_compare` paragraph of `docs/experiments.md`. ## Evidence The fixture is synthetic and original to this repository (MIT). `docs/experiments.md` documents the export, the control 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:33 +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!528
No description provided.