fix(libc): keep frexp from raising FE_INVALID for a signaling NaN #4946

Closed
ErenYeager1836 wants to merge 1 commit from upstream-pr/4946 into main
ErenYeager1836 commented 2026-10-13 05:57:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ErenYeager1836 on 2026-10-13T05:57:37Z.


What

Under IEEE 754 and FreeBSD's s_frexp.c and s_frexpf.c (libm), frexp and frexpf store 0 in *exp for zero, infinity, and NaN and return the argument unchanged without raising the FE_INVALID floating-point exception for a signaling NaN.

In core/libs/prx/libc/src/Math.cpp, frexp and frexpf forwarded directly to host std::frexp. On hosts where std::frexp evaluates floating-point arguments via FPU operations, passing a signaling NaN can raise FE_INVALID, and *exp is left unspecified by C17 7.12.6.4 for infinity and NaN.

This change:

  • Uses bitwise checks (std::memcpy into integer words) to identify zero, infinity, and NaN without tripping host floating-point exception traps on signaling NaNs.
  • Stores 0 in *exponent and returns the argument unchanged for these special values as FreeBSD libm does.
  • Adds test coverage for special values, zeros, infinities, quiet NaNs, and signaling NaNs in GuestMath.cpp (CheckFrexpSpecialValues).
  • Documents technical debt in docs/dev/debt/libc.md.

Tested

  • Windows 11, GuestMath.cpp unit tests.
  • Verified python tools/check_conventions.py --base upstream/main passes with no violations.

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
  • Unblocks: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4839040328 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4946). Originally posted by [ErenYeager1836](https://github.com/ErenYeager1836) on 2026-10-13T05:57:37Z. --- ### What Under IEEE 754 and FreeBSD's [`s_frexp.c`](https://github.com/freebsd/freebsd-src/blob/releng/11.4/lib/msun/src/s_frexp.c) and [`s_frexpf.c`](https://github.com/freebsd/freebsd-src/blob/releng/11.4/lib/msun/src/s_frexpf.c) (libm), `frexp` and `frexpf` store 0 in `*exp` for zero, infinity, and NaN and return the argument unchanged without raising the `FE_INVALID` floating-point exception for a signaling NaN. In `core/libs/prx/libc/src/Math.cpp`, `frexp` and `frexpf` forwarded directly to host `std::frexp`. On hosts where `std::frexp` evaluates floating-point arguments via FPU operations, passing a signaling NaN can raise `FE_INVALID`, and `*exp` is left unspecified by C17 7.12.6.4 for infinity and NaN. This change: - Uses bitwise checks (`std::memcpy` into integer words) to identify zero, infinity, and NaN without tripping host floating-point exception traps on signaling NaNs. - Stores 0 in `*exponent` and returns the argument unchanged for these special values as FreeBSD libm does. - Adds test coverage for special values, zeros, infinities, quiet NaNs, and signaling NaNs in `GuestMath.cpp` (`CheckFrexpSpecialValues`). - Documents technical debt in `docs/dev/debt/libc.md`. ### Tested - Windows 11, `GuestMath.cpp` unit tests. - Verified `python tools/check_conventions.py --base upstream/main` passes with no violations. ### Checklist - [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] Unblocks: none - [x] AI-assisted: yes
xXPBSXx commented 2026-10-13 07:05:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-13T07:05:50Z.


Windows test of this PR.

Environment: Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, BUILD_TESTING=ON, -j2, libusb-cmake from a local checkout. Built from main ea2bbefb2 with this PR and six other open PRs (#4958, #4956, #4954, #4953, #4946, #4942, #4927) merged together; the merges had no conflict.

Result: guest_math (1 of 1; the test file is shared with #4942, both merged together) pass.

Not checked: whether the test fails without the change in the library code; the PR was not tested alone, only in the combined merge (the tests of the other PRs in the batch also pass); no title run; Linux not tested.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6140228898 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4946#issuecomment-6140228898). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-13T07:05:50Z. --- Windows test of this PR. Environment: Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, `BUILD_TESTING=ON`, `-j2`, `libusb-cmake` from a local checkout. Built from `main` `ea2bbefb2` with this PR and six other open PRs (#4958, #4956, #4954, #4953, #4946, #4942, #4927) merged together; the merges had no conflict. Result: guest_math (1 of 1; the test file is shared with #4942, both merged together) pass. Not checked: whether the test fails without the change in the library code; the PR was not tested alone, only in the combined merge (the tests of the other PRs in the batch also pass); no title run; Linux not tested. AI-assisted (Claude Code).
nicolasfalesy commented 2026-10-13 07:22:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by nicolasfalesy on 2026-10-13T07:22:30Z.


Linux test (Arch, GCC 16.2.1, glibc, Release), this PR merged onto main 7e181fcc3 together with #4963, #4953, #4946 and #4942 (no conflicts): guest_math passes. With this PR's Math.cpp change reverted it fails on Linux too, so the check guards the change on both systems.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6140476404 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4946#issuecomment-6140476404). Originally posted by [nicolasfalesy](https://github.com/nicolasfalesy) on 2026-10-13T07:22:30Z. --- Linux test (Arch, GCC 16.2.1, glibc, Release), this PR merged onto `main` 7e181fcc3 together with #4963, #4953, #4946 and #4942 (no conflicts): `guest_math` passes. With this PR's `Math.cpp` change reverted it fails on Linux too, so the check guards the change on both systems.
github-actions[bot] commented 2026-10-13 21:25:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-13T21:25:33Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
#5495 docs/dev/debt/libc.md L36-37
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6155530773 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4946#issuecomment-6155530773). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-13T21:25:33Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #5495 | `docs/dev/debt/libc.md` [L36-37](https://github.com/boykopovar/AnyPS5/blob/0df6e295c52e7cd6afa394ed35513bd252045c74/docs/dev/debt/libc.md#L36-L37) | | |
oneandonlydean commented 2026-10-14 02:33:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T02:33:07Z.


Closing: no title is known to reach this. With the queue at 700+ open PRs, please hold off on new PRs that don't name a title needing the change until it's down. If a title stops on this on current main, reopen with the title ID and the log line.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6159589684 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4946#issuecomment-6159589684). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T02:33:07Z. --- Closing: no title is known to reach this. With the queue at 700+ open PRs, please hold off on new PRs that don't name a title needing the change until it's down. If a title stops on this on current main, reopen with the title ID and the log line.

Pull request closed

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!4946
No description provided.