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

Merged
denn0x merged 1 commit from upstream-pr/893 into main 2026-11-23 16:30:47 +00:00
denn0x commented 2026-11-23 16:17:00 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by denn0x on 2026-11-23T16:17:00Z.


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).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6563639989 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/893). Originally posted by [denn0x](https://github.com/denn0x) on 2026-11-23T16:17:00Z. --- Refs #406 Adds `fprem` and `fprem1` to the checked x87 entry, following the scope proposed in [this #406 comment](https://github.com/R0Y00/Xbox2Windows/issues/406#issuecomment-7078010414) 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).
upstream-sync closed this pull request 2026-11-24 06:45:16 +00:00
R0Y00 commented 2026-11-24 06:49:17 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-23T16:27:01Z.


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7107151536 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/893#issuecomment-7107151536). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-23T16:27:01Z. --- 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.
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!886
No description provided.