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

Merged
denn0x merged 1 commit from upstream-pr/916 into main 2026-11-23 17:18:16 +00:00
denn0x commented 2026-11-23 17:03:43 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by denn0x on 2026-11-23T17:03:43Z.


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

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6564516295 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/916). Originally posted by [denn0x](https://github.com/denn0x) on 2026-11-23T17:03:43Z. --- 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).
upstream-sync closed this pull request 2026-11-24 06:45:23 +00:00
R0Y00 commented 2026-11-24 06:49:22 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-23T17:12:51Z.


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7108320979 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/916#issuecomment-7108320979). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-23T17:12:51Z. --- 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.
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!909
No description provided.