fix(libc): raise the flags of the unselected value in roundl and fdiml #23401

Merged
DerKarussellbremser merged 1 commit from upstream-pr/23401 into main 2026-11-19 14:32:33 +00:00
DerKarussellbremser commented 2026-11-19 00:25:54 +00:00 (Migrated from github.com)

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


What

The two flag-only rows of #22838. In the libc.prx bundled with PPSA10122 01.101.000 (SDK 5.00), roundl (0x4cd80) and fdiml (0x47cc0) have the shape of FreeBSD msun's s_roundl.c and s_fdim.c, compiled so that both candidate results are computed and one is selected afterwards. The arithmetic of the candidate that is not returned still raises its flags:

  • roundl takes t = floorl(|x|) and always forms t + 1, then returns t or t + 1. For |x| of 2^64 and more, t + 1 is inexact, so FE_INEXACT is raised although the result is x. This libc used the host round, which raises nothing there.
  • fdiml always forms x - y and returns it or +0. When x <= y, that subtraction can still raise FE_INEXACT, and fdiml(inf, inf) returns +0 with FE_INVALID. This libc only subtracted when x > y.

Both now compute the unselected value as well. It is held in a volatile so the compiler keeps the operation; GCC otherwise moves it into the branch that uses it.

Two smaller things the measurement showed, both fixed here:

  • A signaling NaN is returned unchanged with no flag by the module. std::isfinite and std::isnan raised FE_INVALID on it, so both functions now classify from the bits.
  • At 53-bit x87 precision roundl differed from the module in the result bits for 21 of 200,000 arguments; with the module's sequence it no longer does.

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_exact aborts (exit code 0xc0000409) on the base MathExact.cpp and passes with the change.
  • ctest -R "^guest_(math|dinkumware|bessel|sce_v2|floating)": 61 of 61 pass, with the 33 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, on the 200,000 arguments (pairs for fdiml) of the #22838 table at each of the control words 0x37f, 0x27f, 0x7f, 0x77f, 0xb7f and 0xf7f, comparing result bits, the union of the x87 and MXCSR exception flags and errno:
    • roundl: 0 differences in all six modes. Before: 49,686 flag differences at 0x37f, and 21 in bits plus 49,700 in flags at 0x27f.
    • fdiml: 0 differences in all six modes. Before: 97,898 flag differences at 0x37f and 100,224 at 0x27f.
  • roundl on 500,000 more arguments at 0x37f, 0x27f and 0x77f: 0 differences for arguments in an encoding the x87 produces. A fifth of those arguments are raw 80-bit patterns; 59,612 of the ones that are not a valid encoding differ, which I have not looked into (floorl here is the host's).
  • The expected values of the new test cases are the module's results for those arguments.
  • tools/check_conventions.py --base origin/main passes. tools/pr_overlap.py --branch fix/roundl-fdiml-flags: no overlap with the 2,002 open pull requests.
  • 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.

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 (the roundl and fdiml rows of #22838)
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

https://claude.ai/code/session_011TXsSW8zeAgKgHLCFsYwM3

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5386467929 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23401). Originally posted by [DerKarussellbremser](https://github.com/DerKarussellbremser) on 2026-11-19T00:25:54Z. --- ### What The two flag-only rows of #22838. In the `libc.prx` bundled with PPSA10122 01.101.000 (SDK 5.00), `roundl` (0x4cd80) and `fdiml` (0x47cc0) have the shape of FreeBSD msun's `s_roundl.c` and `s_fdim.c`, compiled so that both candidate results are computed and one is selected afterwards. The arithmetic of the candidate that is not returned still raises its flags: - `roundl` takes `t = floorl(|x|)` and always forms `t + 1`, then returns `t` or `t + 1`. For `|x|` of 2^64 and more, `t + 1` is inexact, so `FE_INEXACT` is raised although the result is `x`. This libc used the host `round`, which raises nothing there. - `fdiml` always forms `x - y` and returns it or `+0`. When `x <= y`, that subtraction can still raise `FE_INEXACT`, and `fdiml(inf, inf)` returns `+0` with `FE_INVALID`. This libc only subtracted when `x > y`. Both now compute the unselected value as well. It is held in a `volatile` so the compiler keeps the operation; GCC otherwise moves it into the branch that uses it. Two smaller things the measurement showed, both fixed here: - A signaling NaN is returned unchanged with no flag by the module. `std::isfinite` and `std::isnan` raised `FE_INVALID` on it, so both functions now classify from the bits. - At 53-bit x87 precision `roundl` differed from the module in the result bits for 21 of 200,000 arguments; with the module's sequence it no longer does. 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_exact` aborts (exit code 0xc0000409) on the base `MathExact.cpp` and passes with the change. - `ctest -R "^guest_(math|dinkumware|bessel|sce_v2|floating)"`: 61 of 61 pass, with the 33 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, on the 200,000 arguments (pairs for `fdiml`) of the #22838 table at each of the control words 0x37f, 0x27f, 0x7f, 0x77f, 0xb7f and 0xf7f, comparing result bits, the union of the x87 and MXCSR exception flags and `errno`: - `roundl`: 0 differences in all six modes. Before: 49,686 flag differences at 0x37f, and 21 in bits plus 49,700 in flags at 0x27f. - `fdiml`: 0 differences in all six modes. Before: 97,898 flag differences at 0x37f and 100,224 at 0x27f. - `roundl` on 500,000 more arguments at 0x37f, 0x27f and 0x77f: 0 differences for arguments in an encoding the x87 produces. A fifth of those arguments are raw 80-bit patterns; 59,612 of the ones that are not a valid encoding differ, which I have not looked into (`floorl` here is the host's). - The expected values of the new test cases are the module's results for those arguments. - `tools/check_conventions.py --base origin/main` passes. `tools/pr_overlap.py --branch fix/roundl-fdiml-flags`: no overlap with the 2,002 open pull requests. - 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. ### 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 (the `roundl` and `fdiml` rows of #22838) - [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:27:08 +00:00 (Migrated from github.com)

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

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 430dba2c90:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_math_exact_tests built cleanly.
  • CTest (guest_math_exact):
    1/1 Test #1230: guest_math_exact .................   Passed    0.00 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libc/src/MathExact.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #1230: guest_math_exact .................Subprocess aborted***Exception:   0.12 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • core/libs/prx/libc/src/MathExact.cpp: fdiml_nid_postfix correctly classifies sNaN and qNaN via ExtendedNan(Bits(...)) without triggering FE_INVALID, computes x - y in a volatile long double to ensure exception flags (such as FE_INEXACT and FE_INVALID on inf - inf) are raised per FreeBSD msun semantics, and returns the expected result.
    • core/libs/prx/libc/src/MathExact.cpp: roundl_nid_postfix checks exponent 0x7fff directly via bit operations to preserve sNaN/qNaN/inf without premature host exceptions, uses negative check !(x >= 0.0L) to correctly preserve -0.0L, computes t + 1 in a volatile long double to raise FE_INEXACT on large finite values (|x| >= 2^64), and selects correctly based on -0.5L < difference.
    • core/libs/prx/libc/src/MathExact.cpp: AGENTS.md rules checked - no comments added, conventional commit format preserved, no fake stubs or hardcoded title IDs, and no unapproved file modifications.
    • core/libs/tests/GuestMathExact.cpp: CheckRoundlAndFdimlFlags thoroughly verifies inexact flags, sNaN preservation without exceptions, zero/sign handling, infinity subtraction invalid exception, and boundary exponent values.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5967415037 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23401#pullrequestreview-5967415037). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-19T00:27:08Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `430dba2c90`: - **Conventions**: `tools/check_conventions.py` passed. - **Build**: `guest_math_exact_tests` built cleanly. - **CTest** (`guest_math_exact`): ``` 1/1 Test #1230: guest_math_exact ................. Passed 0.00 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/libs/prx/libc/src/MathExact.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/1 Test #1230: guest_math_exact .................Subprocess aborted***Exception: 0.12 sec 0% tests passed, 1 tests failed out of 1 ``` Restoring the change makes it pass again. - **Code reading**: - core/libs/prx/libc/src/MathExact.cpp: fdiml_nid_postfix correctly classifies sNaN and qNaN via ExtendedNan(Bits(...)) without triggering FE_INVALID, computes `x - y` in a volatile long double to ensure exception flags (such as FE_INEXACT and FE_INVALID on inf - inf) are raised per FreeBSD msun semantics, and returns the expected result. - core/libs/prx/libc/src/MathExact.cpp: roundl_nid_postfix checks exponent 0x7fff directly via bit operations to preserve sNaN/qNaN/inf without premature host exceptions, uses negative check `!(x >= 0.0L)` to correctly preserve -0.0L, computes `t + 1` in a volatile long double to raise FE_INEXACT on large finite values (|x| >= 2^64), and selects correctly based on `-0.5L < difference`. - core/libs/prx/libc/src/MathExact.cpp: AGENTS.md rules checked - no comments added, conventional commit format preserved, no fake stubs or hardcoded title IDs, and no unapproved file modifications. - core/libs/tests/GuestMathExact.cpp: CheckRoundlAndFdimlFlags thoroughly verifies inexact flags, sNaN preservation without exceptions, zero/sign handling, infinity subtraction invalid exception, and boundary exponent values. 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!23401
No description provided.