fix(libc): raise round's inexact for large values and pass signaling NaNs through fdim #23410

Merged
DerKarussellbremser merged 1 commit from upstream-pr/23410 into main 2026-11-19 02:16:12 +00:00
DerKarussellbremser commented 2026-11-19 00:47:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by DerKarussellbremser on 2026-11-19T00:47:01Z.


What

The double and float siblings of #23401. Measured against the libc.prx bundled with PPSA10122 01.101.000 (SDK 5.00), round, roundf, fdim and fdimf gave the module's value everywhere but not always its exception flags:

  • round, roundf (0x4eec0, 0x4ef50). The module takes t = floor(|x|) without raising anything, always forms t + 1, and then returns t or t + 1. From 2^53 (2^24 for float) upwards t + 1 is inexact, so FE_INEXACT is raised although the result is x. This libc used the host round, which raises nothing there. They now follow the module's sequence: the fraction is cleared by a bit mask, so nothing is raised for a non-integer, and t + 1 is computed whether it is used or not.
  • fdim, fdimf (0x47c00, 0x47c60). A signaling NaN is returned unchanged with no flag by the module; std::isnan raised FE_INVALID on it. NaNs are now recognized from the bits and returned as they are. The selection also compares y < x, as the module does, instead of comparing the difference with zero, which set the denormal-operand bit on 2 of 500,000 float pairs.

The value that is not selected is held in a volatile so the compiler keeps the operation; GCC otherwise moves it into the branch that uses it (I saw exactly that for fdim during this work: 198,722 of 500,000 pairs lost the flag).

No constant or table from the module is involved.

Tested

Windows 11 Home 10.0.26200, WinLibs GCC 15.2.0 r7 UCRT, Release, on main 9ffa95e7d9.

  • Red on the base: with the new cases, guest_math aborts (exit code 0xc0000409) on the base Math.cpp and passes with the change.
  • ctest -R "^guest_(math|dinkumware|bessel|sce_v2|floating|complex|icu)": 124 of 124 pass, with the 90 test targets behind them brought up to date at this source.
  • Against the module, with a harness that is not in the repository and runs the module's code natively. Arguments are a quarter raw bit patterns (so NaNs, infinities and subnormals are in), a quarter uniform in ±4, a quarter uniform in ±40 and a quarter with exponents from 2^-20 to 2^20. Compared are the result bits (two NaNs count as equal), the union of the x87 and MXCSR exception flags and errno.
    • Before, on 200,000 arguments: round 23,891 flag differences, roundf 20,383, fdim 22, fdimf 195; no difference in bits.
    • After, on 500,000 arguments with each of three seeds: 0 differences for all four.
  • NaN payloads are not part of that comparison, so the new test pins them: a signaling NaN comes back with the same bits from all four functions. The expected values of the new cases are the module's results.
  • tools/check_conventions.py --base origin/main passes. tools/pr_overlap.py --branch fix/round-fdim-flags: one report, #19434, which adds libSceLibcInternal forwarders that call this round and roundf; it does not implement them. It could not compare #6295, #7544 and #17026 because they conflict with main; none of their diffs touches these four functions.
  • Not tested: the Linux and macOS builds, where the volatile has to do the same job for GCC 13 and Clang; a console; other SDK versions; MXCSR rounding modes other than to-nearest.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt and the licence notice of code adapted from upstream (see AGENTS.md)
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

https://claude.ai/code/session_011TXsSW8zeAgKgHLCFsYwM3

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5386676463 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23410). Originally posted by [DerKarussellbremser](https://github.com/DerKarussellbremser) on 2026-11-19T00:47:01Z. --- ### What The `double` and `float` siblings of #23401. Measured against the `libc.prx` bundled with PPSA10122 01.101.000 (SDK 5.00), `round`, `roundf`, `fdim` and `fdimf` gave the module's value everywhere but not always its exception flags: - **`round`, `roundf`** (0x4eec0, 0x4ef50). The module takes `t = floor(|x|)` without raising anything, always forms `t + 1`, and then returns `t` or `t + 1`. From 2^53 (2^24 for `float`) upwards `t + 1` is inexact, so `FE_INEXACT` is raised although the result is `x`. This libc used the host `round`, which raises nothing there. They now follow the module's sequence: the fraction is cleared by a bit mask, so nothing is raised for a non-integer, and `t + 1` is computed whether it is used or not. - **`fdim`, `fdimf`** (0x47c00, 0x47c60). A signaling NaN is returned unchanged with no flag by the module; `std::isnan` raised `FE_INVALID` on it. NaNs are now recognized from the bits and returned as they are. The selection also compares `y < x`, as the module does, instead of comparing the difference with zero, which set the denormal-operand bit on 2 of 500,000 `float` pairs. The value that is not selected is held in a `volatile` so the compiler keeps the operation; GCC otherwise moves it into the branch that uses it (I saw exactly that for `fdim` during this work: 198,722 of 500,000 pairs lost the flag). No constant or table from the module is involved. ### Tested Windows 11 Home 10.0.26200, WinLibs GCC 15.2.0 r7 UCRT, Release, on `main` 9ffa95e7d9. - **Red on the base:** with the new cases, `guest_math` aborts (exit code 0xc0000409) on the base `Math.cpp` and passes with the change. - `ctest -R "^guest_(math|dinkumware|bessel|sce_v2|floating|complex|icu)"`: 124 of 124 pass, with the 90 test targets behind them brought up to date at this source. - **Against the module**, with a harness that is not in the repository and runs the module's code natively. Arguments are a quarter raw bit patterns (so NaNs, infinities and subnormals are in), a quarter uniform in ±4, a quarter uniform in ±40 and a quarter with exponents from 2^-20 to 2^20. Compared are the result bits (two NaNs count as equal), the union of the x87 and MXCSR exception flags and `errno`. - Before, on 200,000 arguments: `round` 23,891 flag differences, `roundf` 20,383, `fdim` 22, `fdimf` 195; no difference in bits. - After, on 500,000 arguments with each of three seeds: 0 differences for all four. - NaN payloads are not part of that comparison, so the new test pins them: a signaling NaN comes back with the same bits from all four functions. The expected values of the new cases are the module's results. - `tools/check_conventions.py --base origin/main` passes. `tools/pr_overlap.py --branch fix/round-fdim-flags`: one report, #19434, which adds `libSceLibcInternal` forwarders that call this `round` and `roundf`; it does not implement them. It could not compare #6295, #7544 and #17026 because they conflict with `main`; none of their diffs touches these four functions. - Not tested: the Linux and macOS builds, where the `volatile` has to do the same job for GCC 13 and Clang; a console; other SDK versions; MXCSR rounding modes other than to-nearest. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) and the licence notice of code adapted from upstream (see AGENTS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_011TXsSW8zeAgKgHLCFsYwM3
PrivacyOnion commented 2026-11-19 00:48:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-19T00:48:55Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 053c0ff7da:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_math_tests built cleanly.
  • CTest (guest_math):
    1/1 Test #1156: guest_math .......................   Passed    0.01 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libc/src/Math.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #1156: guest_math .......................Subprocess aborted***Exception:   0.13 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified std::bit_cast bit mask checks in fdim_nid_postfix and fdimf_nid_postfix correctly identify NaN values and preserve sNaNs without triggering FE_INVALID.
    • Verified RoundHalfAway template bit-manipulation logic, exponent bias handling, fraction bit clearing, and rounding direction for both positive and negative values.
    • Verified inexact flag behavior on values >= 2^53 (double) and >= 2^24 (float) where whole + 1 produces FE_INEXACT via volatile store.
    • Verified subnormal inputs, infinities, quiet/signaling NaNs, and exact power-of-two boundaries.
    • Verified AGENTS.md compliance regarding comments, conventions, and external debt tracking.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5967600807 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23410#pullrequestreview-5967600807). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-19T00:48:55Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `053c0ff7da`: - **Conventions**: `tools/check_conventions.py` passed. - **Build**: `guest_math_tests` built cleanly. - **CTest** (`guest_math`): ``` 1/1 Test #1156: guest_math ....................... Passed 0.01 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/libs/prx/libc/src/Math.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/1 Test #1156: guest_math .......................Subprocess aborted***Exception: 0.13 sec 0% tests passed, 1 tests failed out of 1 ``` Restoring the change makes it pass again. - **Code reading**: - Verified std::bit_cast bit mask checks in fdim_nid_postfix and fdimf_nid_postfix correctly identify NaN values and preserve sNaNs without triggering FE_INVALID. - Verified RoundHalfAway template bit-manipulation logic, exponent bias handling, fraction bit clearing, and rounding direction for both positive and negative values. - Verified inexact flag behavior on values >= 2^53 (double) and >= 2^24 (float) where whole + 1 produces FE_INEXACT via volatile store. - Verified subnormal inputs, infinities, quiet/signaling NaNs, and exact power-of-two boundaries. - Verified AGENTS.md compliance regarding comments, conventions, and external debt tracking. Looks Good To Me
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
boykopovar/AnyPS5!23410
No description provided.