cpu: lower FBSTP and FBLD in the x87 profile #916

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

@⁠denn0x on GitHub · 2026-11-23 17:03 UTC · original
denn0x:cpu/lower-x87-bcd → main · +712 −16 · 12 files · 1 commits · merged 2026-11-23 17:18 UTC

Refs #406

Adds fbstp to both x87 profiles and fbld to the checked entry, as the follow-up to #904 agreed in #406.

Behavior

  • fbstp m80 rounds ST(0) to an integer by the rounding control and stores 18 packed BCD digits in bytes 0 to 8, two digits a byte with the lower digit in the low nibble, and the sign of ST(0) in bit 7 of byte 9. A negative value that rounds to zero keeps its sign. The precision control does not apply.
  • A NaN, an infinity, an unsupported encoding or a rounded magnitude of 10^18 or more stores the packed BCD indefinite (bytes 7 to 9 are 0xC0, 0xFF, 0xFF), the masked invalid-operation response.
  • fbld m80 pushes the digits exactly, with the sign from bit 7 of byte 9. The other bits of that byte are ignored, and a zero keeps its sign. Only the checked entry accepts it. The SDM leaves the result undefined for a digit above 9, so that case leaves through reject with the new status 6 (bcddigit) instead of a guessed value. x87_from_bcd checks all 18 digits before it writes the register, and the function leaves before any later store. Statuses 1 to 5 are unchanged.
  • x87_to_bcd and x87_from_bcd in tools/x87_soft.h do the conversions. x87_to_bcd reuses x87_to_integer for the rounding.

Measurements

Commit 86c5ae7 on f19bcfc. 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_bcd$' -V

cpu_aot_x87_bcd has 9 exports (3 in the fixed profile) and 21930 cases. The model uses Fraction arithmetic and shares no code with tools/x87_soft.h. It agrees with 14 conversions worked by hand.

  • The exports cover fbstp from m80 values and from fild integers, fbstp at depth 8 and under each rounding control, plus fbld alone, both round trips, the sum of two fbld values, and fbld as the eighth value.

  • Every case matched the model in the translation: status, output block and guards, and *result. The statuses were 21342 ok and 588 bcddigit, and 4391 cases stored the packed BCD indefinite.

  • The 21342 native cases matched the model, with control word 0x027F and an empty stack after each export.

  • Hardware observation, not part of the pass condition: native fbld with a digit above 9 gave the sum of the digits times their powers of ten in all 588 cases.

  • Nine controls each mismatch:

    Control What it changes Mismatches
    rcnearest rounds with 0x027F instead of the loaded word 3402
    rangebefore checks the range on the truncated value, before rounding 114
    threshold stores 10^18 itself as digits 314
    zerosign stores a negative value that rounds to zero as +0 2279
    indefinite indefinite top byte 0x80, as for integers, instead of 0xC0 4391
    bstpnibbles stores the two digits of a byte swapped 11037
    bldnibbles loads them swapped 2208
    signbits reads the sign from the whole of byte 9 342
    nodigitcheck accepts a digit above 9 588
  • Rejected: fbld in the fixed profile, fbld at depth 8, fbstp on an empty stack, a store of the value fbstp popped, and fbld with a value left at the return.

  • Translated: fbstp in the fixed profile, fbld and fbstp in the checked entry, and fbld as the eighth value.

Five tests used fbstp as their example of a form outside the profile. Each now uses a form that stays outside it:

Test Replacement
cpu_aot_x87_moves fnstenv
cpu_aot_x87_integers fnsave
cpu_aot_x87_compare frstor
cpu_aot_x87_iarith fsincos
cpu_aot_x87_status fldenv

Wider checks:

  • ctest --preset dev --no-tests=error -R '^cpu_aot_x87': 21/21 pass. The same with --preset release ran at 11a6549, the same change before the rebase onto f19bcfc, whose merges do not touch the x87 files: 21/21 pass. Both configurations reject fbld in the fixed profile, and both run the five replacement rejection cases.
  • Full suite: 472 passed (20 of them capability skips) and 4 failed, out of 476.
    • 3 failures are the RX 9070 XT hardware results listed in #893, from code this change does not touch.
    • pe_package_import_closure fails the same way on main here: creating a symbolic link needs a privilege this account does not hold (WinError 1314).
  • Generated cpu_*.cpp versus main at f19bcfc: 181 byte-identical, and 150 more after removing the aot_source_sha256 line and the 38 added lines of x87_soft.h that each embeds. The remaining 2 are the build-path-dependent cpu_exe_image_selector and cpu_image_data_lifted.

docs/experiments.md adds fbstp to the profile paragraph, fbld and status 6 to the checked entry, a cpu_aot_x87_bcd paragraph, and the new rejection forms.

AI-assisted (Claude Code).

**@⁠denn0x** on GitHub · 2026-11-23 17:03 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/916) `denn0x:cpu/lower-x87-bcd` → `main` · +712 −16 · 12 files · 1 commits · merged 2026-11-23 17:18 UTC Refs #406 Adds `fbstp` to both x87 profiles and `fbld` to the checked entry, as the follow-up to #904 agreed in #406. ## Behavior - `fbstp m80` rounds ST(0) to an integer by the rounding control and stores 18 packed BCD digits in bytes 0 to 8, two digits a byte with the lower digit in the low nibble, and the sign of ST(0) in bit 7 of byte 9. A negative value that rounds to zero keeps its sign. The precision control does not apply. - A NaN, an infinity, an unsupported encoding or a rounded magnitude of 10^18 or more stores the packed BCD indefinite (bytes 7 to 9 are `0xC0`, `0xFF`, `0xFF`), the masked invalid-operation response. - `fbld m80` pushes the digits exactly, with the sign from bit 7 of byte 9. The other bits of that byte are ignored, and a zero keeps its sign. Only the checked entry accepts it. The SDM leaves the result undefined for a digit above 9, so that case leaves through `reject` with the new status 6 (`bcddigit`) instead of a guessed value. `x87_from_bcd` checks all 18 digits before it writes the register, and the function leaves before any later store. Statuses 1 to 5 are unchanged. - `x87_to_bcd` and `x87_from_bcd` in `tools/x87_soft.h` do the conversions. `x87_to_bcd` reuses `x87_to_integer` for the rounding. ## Measurements Commit `86c5ae7` on `f19bcfc`. 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_bcd$' -V ``` `cpu_aot_x87_bcd` has 9 exports (3 in the fixed profile) and 21930 cases. The model uses `Fraction` arithmetic and shares no code with `tools/x87_soft.h`. It agrees with 14 conversions worked by hand. - The exports cover `fbstp` from m80 values and from `fild` integers, `fbstp` at depth 8 and under each rounding control, plus `fbld` alone, both round trips, the sum of two `fbld` values, and `fbld` as the eighth value. - Every case matched the model in the translation: status, output block and guards, and `*result`. The statuses were 21342 ok and 588 `bcddigit`, and 4391 cases stored the packed BCD indefinite. - The 21342 native cases matched the model, with control word `0x027F` and an empty stack after each export. - Hardware observation, not part of the pass condition: native `fbld` with a digit above 9 gave the sum of the digits times their powers of ten in all 588 cases. - Nine controls each mismatch: | Control | What it changes | Mismatches | | --- | --- | --- | | `rcnearest` | rounds with `0x027F` instead of the loaded word | 3402 | | `rangebefore` | checks the range on the truncated value, before rounding | 114 | | `threshold` | stores 10^18 itself as digits | 314 | | `zerosign` | stores a negative value that rounds to zero as +0 | 2279 | | `indefinite` | indefinite top byte `0x80`, as for integers, instead of `0xC0` | 4391 | | `bstpnibbles` | stores the two digits of a byte swapped | 11037 | | `bldnibbles` | loads them swapped | 2208 | | `signbits` | reads the sign from the whole of byte 9 | 342 | | `nodigitcheck` | accepts a digit above 9 | 588 | - Rejected: `fbld` in the fixed profile, `fbld` at depth 8, `fbstp` on an empty stack, a store of the value `fbstp` popped, and `fbld` with a value left at the return. - Translated: `fbstp` in the fixed profile, `fbld` and `fbstp` in the checked entry, and `fbld` as the eighth value. Five tests used `fbstp` as their example of a form outside the profile. Each now uses a form that stays outside it: | Test | Replacement | | --- | --- | | `cpu_aot_x87_moves` | `fnstenv` | | `cpu_aot_x87_integers` | `fnsave` | | `cpu_aot_x87_compare` | `frstor` | | `cpu_aot_x87_iarith` | `fsincos` | | `cpu_aot_x87_status` | `fldenv` | Wider checks: - `ctest --preset dev --no-tests=error -R '^cpu_aot_x87'`: 21/21 pass. The same with `--preset release` ran at `11a6549`, the same change before the rebase onto `f19bcfc`, whose merges do not touch the x87 files: 21/21 pass. Both configurations reject `fbld` in the fixed profile, and both run the five replacement rejection cases. - Full suite: 472 passed (20 of them capability skips) and 4 failed, out of 476. - 3 failures are the RX 9070 XT hardware results listed in #893, from code this change does not touch. - `pe_package_import_closure` fails the same way on `main` here: creating a symbolic link needs a privilege this account does not hold (`WinError 1314`). - Generated `cpu_*.cpp` versus `main` at `f19bcfc`: 181 byte-identical, and 150 more after removing the `aot_source_sha256` line and the 38 added lines of `x87_soft.h` that each embeds. The remaining 2 are the build-path-dependent `cpu_exe_image_selector` and `cpu_image_data_lifted`. `docs/experiments.md` adds `fbstp` to the profile paragraph, `fbld` and status 6 to the checked entry, a `cpu_aot_x87_bcd` paragraph, and the new rejection forms. AI-assisted (Claude Code).
FBSTP rounds ST(0) to an integer by the rounding control and stores 18
packed BCD digits with the sign of ST(0), or the packed BCD indefinite
for a NaN, an infinity, an unsupported encoding or a rounded magnitude
of 10^18 or more. Both profiles accept it. The checked entry also
accepts FBLD, which loads the digits exactly with the sign from bit 7 of
byte 9; a digit above 9, whose result the SDM leaves undefined, leaves
with the new status 6 ("bcddigit"). The fixed profile rejects FBLD.

cpu_aot_x87_bcd has 9 exports (3 in the fixed profile), 21930 cases and
nine controls. Native FBLD with a digit above 9 is an observation only.
The fbstp rejection cases of cpu_aot_x87_moves, cpu_aot_x87_integers,
cpu_aot_x87_compare, cpu_aot_x87_iarith and cpu_aot_x87_status become
fnstenv, fnsave, frstor, fsincos and fldenv, which stay outside the
profile.

AI-assisted (Claude Code).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 22:27:58 +00:00
github manually merged commit 86c5ae79a4 into gh-pr/916/base 2026-11-26 22:27:59 +00:00
Author
Owner

@⁠R0Y00 on GitHub · 2026-11-23 17:12 UTC · original

Independent check of 86c5ae7 on Intel Family 6 Model 183 Stepping 1: cpu_aot_x87_bcd passed in Debug and Release. All 21,930 translated cases and 21,342 specified native comparisons matched, including signed zero and the packed indefinite; all nine mutations fired. Intel's 588 native invalid-digit observations are kept separate from the checked profile's required status 6.

**@⁠R0Y00** on GitHub · 2026-11-23 17:12 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/916#issuecomment-7108320979) Independent check of 86c5ae7 on Intel Family 6 Model 183 Stepping 1: cpu_aot_x87_bcd passed in Debug and Release. All 21,930 translated cases and 21,342 specified native comparisons matched, including signed zero and the packed indefinite; all nine mutations fired. Intel's 588 native invalid-digit observations are kept separate from the checked profile's required status 6.
This discussion has been locked. Commenting is limited to contributors.
No description provided.