fix(libc): hypot returns +inf when one operand is infinite #3429

Merged
CouchRiv merged 1 commit from upstream-pr/3429 into main 2026-10-11 10:35:52 +00:00
CouchRiv commented 2026-10-11 10:25:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by CouchRiv on 2026-10-11T10:25:00Z.


What

hypot(inf, NaN) returns a NaN on main:

hypot(0x7ff0000000000000, 0x7ff0000000000001) = 0x7ff8000000000001

C Annex F says hypot(±∞, y) returns +∞ even if y is a NaN, and FreeBSD msun checks the infinite operand first (e_hypot.c#L75-L79), so it also returns +∞ for a signalling NaN. The host std::hypot returns a NaN for the signalling NaN.

hypot now returns +∞ when either operand is infinite; every other input still goes to std::hypot. hypotf and the other functions do not change.

Tested

CachyOS Linux, RTX 4070 Ti (NVIDIA) and Radeon 610M (RADV), lavapipe for CI parity.

  • guest_math: four new hypot_nid_postfix checks (±inf with a NaN, both orders) fail on main and pass with the change; 1/1 on NVIDIA, RADV and lavapipe.
  • tools/check_conventions.py --base origin/main and git diff --check: clean.
  • Windows and macOS not run.

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

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4818375158 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3429). Originally posted by [CouchRiv](https://github.com/CouchRiv) on 2026-10-11T10:25:00Z. --- ### What `hypot(inf, NaN)` returns a NaN on `main`: ``` hypot(0x7ff0000000000000, 0x7ff0000000000001) = 0x7ff8000000000001 ``` C Annex F says `hypot(±∞, y)` returns +∞ even if `y` is a NaN, and FreeBSD msun checks the infinite operand first ([e_hypot.c#L75-L79](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/lib/msun/src/e_hypot.c#L75-L79)), so it also returns +∞ for a signalling NaN. The host `std::hypot` returns a NaN for the signalling NaN. `hypot` now returns +∞ when either operand is infinite; every other input still goes to `std::hypot`. `hypotf` and the other functions do not change. ### Tested CachyOS Linux, RTX 4070 Ti (NVIDIA) and Radeon 610M (RADV), lavapipe for CI parity. - guest_math: four new `hypot_nid_postfix` checks (±inf with a NaN, both orders) fail on `main` and pass with the change; 1/1 on NVIDIA, RADV and lavapipe. - `tools/check_conventions.py --base origin/main` and `git diff --check`: clean. - Windows and macOS not run. ### 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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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 AI-assisted: yes
adriadam10 commented 2026-10-11 10:35:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T10:35:49Z.


Checked against FreeBSD e_hypot.c (Inf/NaN branch returns the infinite operand, also for a signalling NaN) and C Annex F; guest_math passes with the change and fails without it (negative check), full scoped ctest green. Merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6108133246 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3429#issuecomment-6108133246). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-11T10:35:49Z. --- Checked against FreeBSD `e_hypot.c` (Inf/NaN branch returns the infinite operand, also for a signalling NaN) and C Annex F; `guest_math` passes with the change and fails without it (negative check), full scoped ctest green. Merging.
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!3429
No description provided.