fix(libc): classify signaling NaNs without raising exceptions #21161

Merged
AlexWhitehouse merged 1 commit from upstream-pr/21161 into main 2026-11-17 01:30:31 +00:00
AlexWhitehouse commented 2026-11-15 21:53:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by AlexWhitehouse on 2026-11-15T21:53:36Z.


What

libc's classification exports were implemented with std::isfinite, std::isnan, std::isnormal and std::isinf. GCC compiles those to ucomisd/ucomiss compares, which raise FE_INVALID when the argument is a signaling NaN. The result was right, but the call set the invalid-operation flag. FreeBSD 11.4, which the PS5 libc follows, classifies by reading the exponent and fraction bits, so it raises nothing. This PR does the same for:

Export NID
__isfinite dhK16CKwhQg
__isfinitef Q8pvJimUWis
__isnan GfxAp9Xyiqs
__isnanf lA94ZgT+vMM
__isnormal fGPRa6T+Cu8
__isnormalf WkYnBHFsmW4
__isinf V02oFv+-JzA
__isinff rDMyAf1Jhug

The existing isnan, isnanf and isinf wrappers forward to these, so they're fixed too.

Code reaches these exports through math.h: in FreeBSD 11.4's math.h lines 111–115, isfinite, isinf and isnormal compile to calls to __isfinite(f), __isinf(f) and __isnormal(f). Only isnan is inline.

  • Sources: FreeBSD releng/11.4 (d1060029):

    The code is written in the repository's style with std::bit_cast and masks, not copied, so it carries no BSD notice.

  • Behaviour: results, signatures and return values are unchanged for every input. Only the exception flags differ: none is raised, and flags already set stay set. errno is untouched.

  • Debt: a debt fragment records that the classifiers follow FreeBSD and that console exception-flag behaviour is unmeasured. No inventory count changes.

Tested

  • Test: the existing guest_math test now has two tables, binary64 and binary32. Each covers signaling and quiet NaNs, ±0, the smallest and largest subnormals, the smallest normal, the largest finite value and ±∞, with both signs. For every export and wrapper, the test:

    • starts with the flags clear, and again with FE_DIVBYZERO already raised;
    • checks the classification, that fetestexcept(FE_ALL_EXCEPT) is unchanged and that errno is unchanged.

    Inputs are built from bits through volatile, so the test itself raises nothing. Existing assertions are unchanged.

  • Linux x86-64 (Ubuntu 26.04, GCC 15, Release):

    • on main, guest_math fails at the flag assertion: __isnan executes ucomisd %xmm0,%xmm0;
    • with this PR, guest_math passes.
  • Windows 11 (build 26300, WinLibs MinGW-w64 GCC 15.2.0 UCRT, Release): guest_math passes at 2ea572c68a, so the classifiers raise no flags under MinGW's UCRT fenv either, with no compiler warnings in the changed files. The branch was then rebased onto current main, and the focused tests pass again on Linux.

  • Mutation checks: putting back std::isnan in __isnan, or std::isinf in __isinf, fails the flag assertion.

  • python3 tools/check_conventions.py --base origin/main: all checks passed. python3 tools/debt_fold.py --check: passed.

  • python3 tools/pr_overlap.py: no open PR changes these function bodies or their NIDs. It couldn't compare some Math.cpp PRs automatically, so I read their diffs:

    • #19434 adds libc-internal forwarding wrappers with these names but doesn't change the libc bodies;
    • #6295 changes nextafter/nexttoward;
    • #7544 and #17026 change lgamma/signgam.

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
  • 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

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5327593342 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/21161). Originally posted by [AlexWhitehouse](https://github.com/AlexWhitehouse) on 2026-11-15T21:53:36Z. --- ### What libc's classification exports were implemented with `std::isfinite`, `std::isnan`, `std::isnormal` and `std::isinf`. GCC compiles those to `ucomisd`/`ucomiss` compares, which raise `FE_INVALID` when the argument is a signaling NaN. The result was right, but the call set the invalid-operation flag. FreeBSD 11.4, which the PS5 libc follows, classifies by reading the exponent and fraction bits, so it raises nothing. This PR does the same for: | Export | NID | |---|---| | `__isfinite` | `dhK16CKwhQg` | | `__isfinitef` | `Q8pvJimUWis` | | `__isnan` | `GfxAp9Xyiqs` | | `__isnanf` | `lA94ZgT+vMM` | | `__isnormal` | `fGPRa6T+Cu8` | | `__isnormalf` | `WkYnBHFsmW4` | | `__isinf` | `V02oFv+-JzA` | | `__isinff` | `rDMyAf1Jhug` | The existing `isnan`, `isnanf` and `isinf` wrappers forward to these, so they're fixed too. Code reaches these exports through `math.h`: in FreeBSD 11.4's [`math.h` lines 111–115](https://github.com/freebsd/freebsd-src/blob/d106002967eff712894ab8b5337bbbff106ce5e0/lib/msun/src/math.h#L111-L115), `isfinite`, `isinf` and `isnormal` compile to calls to `__isfinite(f)`, `__isinf(f)` and `__isnormal(f)`. Only `isnan` is inline. - **Sources:** FreeBSD releng/11.4 (d1060029): - msun [`s_isfinite.c` lines 33–49](https://github.com/freebsd/freebsd-src/blob/d106002967eff712894ab8b5337bbbff106ce5e0/lib/msun/src/s_isfinite.c#L33-L49), [`s_isnan.c` lines 33–59](https://github.com/freebsd/freebsd-src/blob/d106002967eff712894ab8b5337bbbff106ce5e0/lib/msun/src/s_isnan.c#L33-L59) and [`s_isnormal.c` lines 33–49](https://github.com/freebsd/freebsd-src/blob/d106002967eff712894ab8b5337bbbff106ce5e0/lib/msun/src/s_isnormal.c#L33-L49); - libc [`gen/isinf.c` lines 42–58](https://github.com/freebsd/freebsd-src/blob/d106002967eff712894ab8b5337bbbff106ce5e0/lib/libc/gen/isinf.c#L42-L58). The code is written in the repository's style with `std::bit_cast` and masks, not copied, so it carries no BSD notice. - **Behaviour:** results, signatures and return values are unchanged for every input. Only the exception flags differ: none is raised, and flags already set stay set. errno is untouched. - **Debt:** a debt fragment records that the classifiers follow FreeBSD and that console exception-flag behaviour is unmeasured. No inventory count changes. ### Tested - **Test:** the existing `guest_math` test now has two tables, binary64 and binary32. Each covers signaling and quiet NaNs, ±0, the smallest and largest subnormals, the smallest normal, the largest finite value and ±∞, with both signs. For every export and wrapper, the test: - starts with the flags clear, and again with `FE_DIVBYZERO` already raised; - checks the classification, that `fetestexcept(FE_ALL_EXCEPT)` is unchanged and that errno is unchanged. Inputs are built from bits through `volatile`, so the test itself raises nothing. Existing assertions are unchanged. - **Linux x86-64** (Ubuntu 26.04, GCC 15, Release): - on `main`, `guest_math` fails at the flag assertion: `__isnan` executes `ucomisd %xmm0,%xmm0`; - with this PR, `guest_math` passes. - **Windows 11** (build 26300, WinLibs MinGW-w64 GCC 15.2.0 UCRT, Release): `guest_math` passes at 2ea572c68a, so the classifiers raise no flags under MinGW's UCRT `fenv` either, with no compiler warnings in the changed files. The branch was then rebased onto current `main`, and the focused tests pass again on Linux. - **Mutation checks:** putting back `std::isnan` in `__isnan`, or `std::isinf` in `__isinf`, fails the flag assertion. - `python3 tools/check_conventions.py --base origin/main`: all checks passed. `python3 tools/debt_fold.py --check`: passed. - **`python3 tools/pr_overlap.py`:** no open PR changes these function bodies or their NIDs. It couldn't compare some `Math.cpp` PRs automatically, so I read their diffs: - #19434 adds libc-internal forwarding wrappers with these names but doesn't change the libc bodies; - #6295 changes `nextafter`/`nexttoward`; - #7544 and #17026 change `lgamma`/`signgam`. ### 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) - [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)
PrivacyOnion commented 2026-11-16 00:22:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-16T00:22:31Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 93934ef9f1:

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: guest_math_tests built cleanly.
  • CTest (guest_math):
    1/1 Test #1082: guest_math .......................   Passed    0.04 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 #1082: guest_math .......................Subprocess aborted***Exception:   0.42 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified IEEE 754 binary32 bit masks in core/libs/prx/libc/src/Math.cpp: __isfinitef_nid_postfix (exponent != 0x7f800000u), __isnormalf_nid_postfix (exponent != 0 && exponent != 0x7f800000u), __isinff_nid_postfix ((bits & 0x7fffffffu) == 0x7f800000u), and __isnanf_nid_postfix ((bits & 0x7f800000u) == 0x7f800000u && (bits & 0x007fffffu) != 0).
    • Verified IEEE 754 binary64 bit masks in core/libs/prx/libc/src/Math.cpp: __isfinite_nid_postfix (exponent != 0x7ff0000000000000ull), __isnormal_nid_postfix (exponent != 0 && exponent != 0x7ff0000000000000ull), __isinf_nid_postfix ((bits & 0x7fffffffffffffffull) == 0x7ff0000000000000ull), and __isnan_nid_postfix ((bits & 0x7ff0000000000000ull) == 0x7ff0000000000000ull && (bits & 0x000fffffffffffffull) != 0).
    • Verified forwarding wrappers isnanf_nid_postfix, isnan_nid_postfix, and isinf_nid_postfix in core/libs/prx/libc/src/Math.cpp:5201-5203 delegate directly to the bit-cast implementations without floating-point conversions that would quiet sNaNs on x86_64.
    • Checked LP64/LLP64 portability and C++20 type safety of std::bit_caststd::uint32_t(float) and std::bit_caststd::uint64_t(double) across Linux and Windows targets.
    • Verified unit tests in core/libs/tests/GuestMath.cpp:4716-4780 cover both positive and negative sNaN, qNaN, zero, min/max subnormal, min/max normal, and infinity inputs while asserting preservation of FE_ALL_EXCEPT flags and errno.
    • Confirmed docs/dev/debt/changes/fix-libc-classify-signaling-nan.md properly documents the functional addition and upstream FreeBSD 11.4 sources in compliance with AGENTS.md.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5913727477 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/21161#pullrequestreview-5913727477). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-16T00:22:31Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `93934ef9f1`: - **Conventions**: `tools/check_conventions.py` passed; `git diff --check` clean. - **Build**: `guest_math_tests` built cleanly. - **CTest** (`guest_math`): ``` 1/1 Test #1082: guest_math ....................... Passed 0.04 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 #1082: guest_math .......................Subprocess aborted***Exception: 0.42 sec 0% tests passed, 1 tests failed out of 1 ``` Restoring the change makes it pass again. - **Code reading**: - Verified IEEE 754 binary32 bit masks in core/libs/prx/libc/src/Math.cpp: __isfinitef_nid_postfix (exponent != 0x7f800000u), __isnormalf_nid_postfix (exponent != 0 && exponent != 0x7f800000u), __isinff_nid_postfix ((bits & 0x7fffffffu) == 0x7f800000u), and __isnanf_nid_postfix ((bits & 0x7f800000u) == 0x7f800000u && (bits & 0x007fffffu) != 0). - Verified IEEE 754 binary64 bit masks in core/libs/prx/libc/src/Math.cpp: __isfinite_nid_postfix (exponent != 0x7ff0000000000000ull), __isnormal_nid_postfix (exponent != 0 && exponent != 0x7ff0000000000000ull), __isinf_nid_postfix ((bits & 0x7fffffffffffffffull) == 0x7ff0000000000000ull), and __isnan_nid_postfix ((bits & 0x7ff0000000000000ull) == 0x7ff0000000000000ull && (bits & 0x000fffffffffffffull) != 0). - Verified forwarding wrappers isnanf_nid_postfix, isnan_nid_postfix, and isinf_nid_postfix in core/libs/prx/libc/src/Math.cpp:5201-5203 delegate directly to the bit-cast implementations without floating-point conversions that would quiet sNaNs on x86_64. - Checked LP64/LLP64 portability and C++20 type safety of std::bit_cast<std::uint32_t>(float) and std::bit_cast<std::uint64_t>(double) across Linux and Windows targets. - Verified unit tests in core/libs/tests/GuestMath.cpp:4716-4780 cover both positive and negative sNaN, qNaN, zero, min/max subnormal, min/max normal, and infinity inputs while asserting preservation of FE_ALL_EXCEPT flags and errno. - Confirmed docs/dev/debt/changes/fix-libc-classify-signaling-nan.md properly documents the functional addition and upstream FreeBSD 11.4 sources in compliance with AGENTS.md. Looks Good To Me
MarcZX10 commented 2026-11-16 05:59:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-16T05:59:24Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this change; the reviewed code and tests support merging.

What I ran

Reviewed head 93934ef9f14dd94c9b197f69a8b2ac640f5966d4. No commands or tests were run; repo_read.py was unavailable as an executable tool in this session. Read the prepared description, commit summary, manifest and diff, plus the full text of all three changed files: core/libs/prx/libc/src/Math.cpp, core/libs/tests/GuestMath.cpp, and docs/dev/debt/changes/fix-libc-classify-signaling-nan.md. No changed files were excluded by the manifest.

Results

Check Result Note
Diff and changed-file review passed Three changed files read
Programmatic tests not run Author reports focused tests passed
Console behavior not run Debt fragment records it as unmeasured

Findings

Blocking

None.

Non-blocking

None.

Not verified

I did not execute tests, inspect CI output, or measure behavior on a PS5 console. The author reports Linux and Windows test passes and mutation checks in the PR description; these were not run in this review.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5915639536 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/21161#pullrequestreview-5915639536). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-16T05:59:24Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change; the reviewed code and tests support merging. ## What I ran Reviewed head `93934ef9f14dd94c9b197f69a8b2ac640f5966d4`. No commands or tests were run; `repo_read.py` was unavailable as an executable tool in this session. Read the prepared description, commit summary, manifest and diff, plus the full text of all three changed files: `core/libs/prx/libc/src/Math.cpp`, `core/libs/tests/GuestMath.cpp`, and `docs/dev/debt/changes/fix-libc-classify-signaling-nan.md`. No changed files were excluded by the manifest. ## Results | Check | Result | Note | |---|---|---| | Diff and changed-file review | passed | Three changed files read | | Programmatic tests | not run | Author reports focused tests passed | | Console behavior | not run | Debt fragment records it as unmeasured | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified I did not execute tests, inspect CI output, or measure behavior on a PS5 console. The author reports Linux and Windows test passes and mutation checks in the PR description; these were not run in this review. ## To get this to a good state Nothing is required.
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!21161
No description provided.