fix(libc): keep __isnan and __isnanf integer bit tests under Apple clang #22081

Merged
kurexw merged 1 commit from upstream-pr/22081 into main 2026-11-17 06:39:05 +00:00
kurexw commented 2026-11-17 04:39:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by kurexw on 2026-11-17T04:39:30Z.


What

#21161 rewrote __isnan and __isnanf (core/libs/prx/libc/src/Math.cpp) as bit tests, so that classifying a value never raises floating-point exceptions. Apple clang 21.0.0 (clang-2100.4.1.1) at -O3 sees that the two-part test (exponent all ones, mantissa nonzero) means "is NaN" and compiles it back into a floating-point compare. In libc.prx (otool -tvV):

___isnan_nid_postfix:   ucomisd %xmm0, %xmm0 ; setp %al
___isnanf_nid_postfix:  ucomiss %xmm0, %xmm0 ; setp %al

ucomisd raises FE_INVALID for a signaling NaN, and the x87/SSE denormal flag for a subnormal input. So guest_math fails at GuestMath.cpp:4971 (actualFlags == flags) on macOS, for __isnan and for isnan, which forwards to it. __isinf, __isfinite and __isnormal were already compiled to integer code.

This PR writes both tests as one unsigned compare of the magnitude bits against infinity, (bits & 0x7fff...) > 0x7ff0..., which is the same test musl uses. For every input this is equivalent to the old two-part test: the magnitude is above infinity's bit pattern exactly when the exponent is all ones and the mantissa is nonzero. Apple clang keeps it as integer code:

___isnan_nid_postfix:   movq %xmm0,%rax ; and abs-mask ; cmpq 0x7ff0000000000000 ; seta %al
___isnanf_nid_postfix:  movd %xmm0,%ecx ; andl 0x7fffffff ; cmpl 0x7f800001 ; setae %al

isnan and isnanf forward to these and compile the same way. No ucomis* is left in any of the four.

Tested

macOS 27.2 on an Apple M4, with Apple clang 21.0.0 for an x86-64 build under Rosetta.

  • guest_math: main at a72186d3cf fails at GuestMath.cpp:4971. With this PR it passed in three runs out of three.
  • guest_math_elementary passes with this PR.
  • guest_math_exact fails at GuestMathExact.cpp:687 (expf(89) errno) on both main and this PR. That's a separate issue.
  • otool -tvV on the rebuilt libc.prx shows the integer code above for __isnan, __isnanf, isnan and isnanf.
  • I didn't build this on Linux or Windows, or with GCC or MSVC. Another compiler could still turn this form into a floating-point compare too; guest_math would catch that.

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

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5350812073 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22081). Originally posted by [kurexw](https://github.com/kurexw) on 2026-11-17T04:39:30Z. --- ### What #21161 rewrote `__isnan` and `__isnanf` (`core/libs/prx/libc/src/Math.cpp`) as bit tests, so that classifying a value never raises floating-point exceptions. Apple clang 21.0.0 (clang-2100.4.1.1) at `-O3` sees that the two-part test (exponent all ones, mantissa nonzero) means "is NaN" and compiles it back into a floating-point compare. In `libc.prx` (`otool -tvV`): ``` ___isnan_nid_postfix: ucomisd %xmm0, %xmm0 ; setp %al ___isnanf_nid_postfix: ucomiss %xmm0, %xmm0 ; setp %al ``` `ucomisd` raises `FE_INVALID` for a signaling NaN, and the x87/SSE denormal flag for a subnormal input. So `guest_math` fails at `GuestMath.cpp:4971` (`actualFlags == flags`) on macOS, for `__isnan` and for `isnan`, which forwards to it. `__isinf`, `__isfinite` and `__isnormal` were already compiled to integer code. This PR writes both tests as one unsigned compare of the magnitude bits against infinity, `(bits & 0x7fff...) > 0x7ff0...`, which is the same test musl uses. For every input this is equivalent to the old two-part test: the magnitude is above infinity's bit pattern exactly when the exponent is all ones and the mantissa is nonzero. Apple clang keeps it as integer code: ``` ___isnan_nid_postfix: movq %xmm0,%rax ; and abs-mask ; cmpq 0x7ff0000000000000 ; seta %al ___isnanf_nid_postfix: movd %xmm0,%ecx ; andl 0x7fffffff ; cmpl 0x7f800001 ; setae %al ``` `isnan` and `isnanf` forward to these and compile the same way. No `ucomis*` is left in any of the four. ### Tested macOS 27.2 on an Apple M4, with Apple clang 21.0.0 for an x86-64 build under Rosetta. - `guest_math`: main at a72186d3cf fails at `GuestMath.cpp:4971`. With this PR it passed in three runs out of three. - `guest_math_elementary` passes with this PR. - `guest_math_exact` fails at `GuestMathExact.cpp:687` (`expf(89)` errno) on both main and this PR. That's a separate issue. - `otool -tvV` on the rebuilt `libc.prx` shows the integer code above for `__isnan`, `__isnanf`, `isnan` and `isnanf`. - I didn't build this on Linux or Windows, or with GCC or MSVC. Another compiler could still turn this form into a floating-point compare too; `guest_math` would catch that. ### 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)
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!22081
No description provided.