cpu: lower FPREM and FPREM1 in the checked x87 entry #893

Manually merged
github merged 1 commit from gh-pr/893/head into gh-pr/893/base 2026-11-26 22:26:25 +00:00
Owner

@⁠denn0x on GitHub · 2026-11-23 16:17 UTC · original
denn0x:cpu/lower-x87-fprem → main · +942 −20 · 8 files · 1 commits · merged 2026-11-23 16:30 UTC

Refs #406

Adds fprem and fprem1 to the checked x87 entry, following the scope proposed in this #406 comment and the fldcw precedent. The fixed profile still rejects both instructions.

Behavior

  • The checked entry computes the exact remainder st(0) - q * st(1). q is the quotient of the magnitudes, truncated by fprem and rounded to nearest-even by fprem1. C0, C3 and C1 become bits 2, 1 and 0 of q, and C2 becomes 0. Neither instruction reads the control word. The helper is x87_prem in tools/x87_soft.h.
  • C1 is known only after fprem and fprem1, until the next x87 instruction. It stays unknown after fxam and every other instruction, as #627 set it.
  • The SDM leaves two outcomes to the implementation, so the checked entry leaves through reject(status) for both:
    • status 4 when the normalized exponent difference is 64 or more, which gives a partial reduction;
    • status 5 for a NaN result in a function that contains fnstsw, because the status bits after it are not given. Without fnstsw the NaN value is kept.
  • The fmod loop fprem; fnstsw ax; sahf; jp back is accepted. In an accepted run C2 is always 0, so the back edge is translated but never taken.

This branch was rebased onto #627. That PR covered ftst, ficom and fxam before my stacked branches for those opened, so this branch does not include them.

Measurements

Commit bcc1068 on ca0cc0e. AMD Ryzen 7 7700 (AMD64 Family 25 Model 97 Stepping 2), Windows 11 build 26300, MSVC x64, dev preset.

ctest --preset dev --no-tests=error -R '^cpu_aot_x87_fprem$' -V
  • 36952 cases over 11 exports matched the independent Fraction model in the checked translation. The check covers the status, the output block and its guards, and *result.
  • Translated statuses: 27623 ok, 4104 partial and 5225 NaN.
  • Native: C2 was 1 in exactly the 2119 single-step records rejected as partial, and the result was a NaN in exactly the 4598 single-step cases whose model result is a NaN. The native loops reduced partially and iterated in 794 cases. Their final value matched the full remainder. The 1254 loop cases with a NaN result ran only translated.
  • Eight mutated controls each mismatch: nopartial 4104, nonan 5225, d63 431, truncate1 3374, tieup 867, halfnear 472, qswap 5498 and zerosign 1211.
  • Translation rejects 8 sequences: fprem and fprem1 in the fixed profile; fprem with one value; fprem1 on an empty stack; test ah, 80h (the busy bit) after fprem; fadd between fprem and fnstsw; a push inside the fmod loop; and a return with the status word in AX. It translates 4: test ah, 02h (C1) after fprem1; sahf; je (C3) after fprem; the fmod loop; and fprem without fnstsw.

Wider checks on the same commit:

  • ctest --preset dev --no-tests=error -R '^cpu_aot_x87': 20/20 pass.
  • Full suite (ctest --preset dev --no-tests=error -j 8): 442 passed, 19 capability skips and 4 failures out of 465.
    • Three failures are RX 9070 XT hardware results from offscreen_check and pixel_aot, which this change does not touch: dxr_opacity_hardware, gpu_alias_lookup_writes_hardware and pixel_aot_comparisons_hardware. They failed the same way in a full run without this change at 1e8c6a6. dxr_opacity_hardware is the result already recorded for this adapter.
    • The fourth, bvh_differential_warp, failed under -j 8. It passed 3 of 3 times when run alone on this branch, and also passes alone on ca0cc0e.
  • Generated sources versus ca0cc0e: the 318 cpu_*.cpp files generated from fixtures that exist on main were compared. 177 are byte-identical. 139 differ only in the embedded x87_soft.h text, which gains x87_prem, and in the aot_source_sha256 line. The remaining 2, cpu_exe_image_selector and cpu_image_data_lifted, embed hashes of natively built images, which vary with the build directory.
  • form_coverage_check, docs_links_check and bidi_controls_check pass.

docs/experiments.md records the fixture and the two new checked-entry statuses.

Two observations on this processor are not modelled, and the checked entry rejects both cases. A partial step leaves an exponent difference of 32 + (D mod 32) and clears C0, C1 and C3. After a NaN result, C0 and C3 keep their previous values and C1 and C2 are cleared. Intel measurements from the probe in the #406 comment would show whether a follow-up can model them.

AI-assisted (Claude Code).

**@⁠denn0x** on GitHub · 2026-11-23 16:17 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/893) `denn0x:cpu/lower-x87-fprem` → `main` · +942 −20 · 8 files · 1 commits · merged 2026-11-23 16:30 UTC Refs #406 Adds `fprem` and `fprem1` to the checked x87 entry, following the scope proposed in [this #406 comment](https://git.gtahub.st/github/R0Y00--Xbox2Windows/issues/406) and the `fldcw` precedent. The fixed profile still rejects both instructions. ## Behavior - The checked entry computes the exact remainder `st(0) - q * st(1)`. q is the quotient of the magnitudes, truncated by `fprem` and rounded to nearest-even by `fprem1`. C0, C3 and C1 become bits 2, 1 and 0 of q, and C2 becomes 0. Neither instruction reads the control word. The helper is `x87_prem` in `tools/x87_soft.h`. - C1 is known only after `fprem` and `fprem1`, until the next x87 instruction. It stays unknown after `fxam` and every other instruction, as #627 set it. - The SDM leaves two outcomes to the implementation, so the checked entry leaves through `reject(status)` for both: - status 4 when the normalized exponent difference is 64 or more, which gives a partial reduction; - status 5 for a NaN result in a function that contains `fnstsw`, because the status bits after it are not given. Without `fnstsw` the NaN value is kept. - The fmod loop `fprem; fnstsw ax; sahf; jp back` is accepted. In an accepted run C2 is always 0, so the back edge is translated but never taken. This branch was rebased onto #627. That PR covered `ftst`, `ficom` and `fxam` before my stacked branches for those opened, so this branch does not include them. ## Measurements Commit `bcc1068` on `ca0cc0e`. AMD Ryzen 7 7700 (`AMD64 Family 25 Model 97 Stepping 2`), Windows 11 build 26300, MSVC x64, `dev` preset. ```powershell ctest --preset dev --no-tests=error -R '^cpu_aot_x87_fprem$' -V ``` - 36952 cases over 11 exports matched the independent `Fraction` model in the checked translation. The check covers the status, the output block and its guards, and `*result`. - Translated statuses: 27623 ok, 4104 partial and 5225 NaN. - Native: C2 was 1 in exactly the 2119 single-step records rejected as partial, and the result was a NaN in exactly the 4598 single-step cases whose model result is a NaN. The native loops reduced partially and iterated in 794 cases. Their final value matched the full remainder. The 1254 loop cases with a NaN result ran only translated. - Eight mutated controls each mismatch: `nopartial` 4104, `nonan` 5225, `d63` 431, `truncate1` 3374, `tieup` 867, `halfnear` 472, `qswap` 5498 and `zerosign` 1211. - Translation rejects 8 sequences: `fprem` and `fprem1` in the fixed profile; `fprem` with one value; `fprem1` on an empty stack; `test ah, 80h` (the busy bit) after `fprem`; `fadd` between `fprem` and `fnstsw`; a push inside the fmod loop; and a return with the status word in AX. It translates 4: `test ah, 02h` (C1) after `fprem1`; `sahf; je` (C3) after `fprem`; the fmod loop; and `fprem` without `fnstsw`. Wider checks on the same commit: - `ctest --preset dev --no-tests=error -R '^cpu_aot_x87'`: 20/20 pass. - Full suite (`ctest --preset dev --no-tests=error -j 8`): 442 passed, 19 capability skips and 4 failures out of 465. - Three failures are RX 9070 XT hardware results from `offscreen_check` and `pixel_aot`, which this change does not touch: `dxr_opacity_hardware`, `gpu_alias_lookup_writes_hardware` and `pixel_aot_comparisons_hardware`. They failed the same way in a full run without this change at `1e8c6a6`. `dxr_opacity_hardware` is the result already recorded for this adapter. - The fourth, `bvh_differential_warp`, failed under `-j 8`. It passed 3 of 3 times when run alone on this branch, and also passes alone on `ca0cc0e`. - Generated sources versus `ca0cc0e`: the 318 `cpu_*.cpp` files generated from fixtures that exist on `main` were compared. 177 are byte-identical. 139 differ only in the embedded `x87_soft.h` text, which gains `x87_prem`, and in the `aot_source_sha256` line. The remaining 2, `cpu_exe_image_selector` and `cpu_image_data_lifted`, embed hashes of natively built images, which vary with the build directory. - `form_coverage_check`, `docs_links_check` and `bidi_controls_check` pass. `docs/experiments.md` records the fixture and the two new checked-entry statuses. Two observations on this processor are not modelled, and the checked entry rejects both cases. A partial step leaves an exponent difference of 32 + (D mod 32) and clears C0, C1 and C3. After a NaN result, C0 and C3 keep their previous values and C1 and C2 are cleared. Intel measurements from the probe in the #406 comment would show whether a follow-up can model them. AI-assisted (Claude Code).
The checked entry computes the exact partial remainder with the quotient
truncated (FPREM) or rounded to nearest-even (FPREM1), and sets C0, C3
and C1 to the low quotient bits and C2 to 0. C1 becomes known after these
two instructions only; it stays unknown after FXAM and everything else.
Two outcomes the SDM leaves to the implementation leave through
reject(status): a partial reduction (exponent difference 64 or more,
status 4), and a NaN result in a function that reads the status word
(status 5). The fixed profile still rejects both instructions. The fmod
loop fprem; fnstsw ax; sahf; jp is accepted.

cpu_aot_x87_fprem compares 36952 cases over 11 exports against an
independent Fraction model, with eight mutated controls.

AI-assisted (Claude Code).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 22:26:24 +00:00
github manually merged commit bcc1068bcc into gh-pr/893/base 2026-11-26 22:26:25 +00:00
Author
Owner

@⁠R0Y00 on GitHub · 2026-11-23 16:27 UTC · original

Independent Intel result at bcc1068 (GenuineIntel Family 6 Model 183 Stepping 1, Windows x64, MSVC 19.51). Debug and Release each passed the cpu_aot_x87_fprem check. The 35,698 comparable native cases matched the model, with C2 set in the 2,119 single-step partial cases. All eight mutations were detected. This run covers the checked profile and does not identify the native partial-reduction remainder or the unspecified NaN status bits.

**@⁠R0Y00** on GitHub · 2026-11-23 16:27 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/893#issuecomment-7107151536) Independent Intel result at `bcc1068` (GenuineIntel Family 6 Model 183 Stepping 1, Windows x64, MSVC 19.51). Debug and Release each passed the `cpu_aot_x87_fprem` check. The 35,698 comparable native cases matched the model, with C2 set in the 2,119 single-step partial cases. All eight mutations were detected. This run covers the checked profile and does not identify the native partial-reduction remainder or the unspecified NaN status bits.
This discussion has been locked. Commenting is limited to contributors.
No description provided.