feat(libc): implement hypot and hypotf #1196

Merged
MathiasDevelopes merged 2 commits from upstream-pr/1196 into main 2026-10-08 04:21:40 +00:00
MathiasDevelopes commented 2026-10-07 17:12:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MathiasDevelopes on 2026-10-07T17:12:07Z.


What

hypot and hypotf were not exported, so a title importing them from libSceLibcInternal stopped at startup with an unresolved import (iz2shAGFIxc).

Both now forward to std::hypot, next to the other math functions in Math.cpp. That gives the C99 behaviour: no intermediate overflow or underflow, and infinity when either argument is infinite, even if the other is NaN.

GuestMath checks the plain case, both signs, arguments whose squares overflow (1e308 and 2e38f), infinity with NaN in either order, and NaN propagation.

Tested

  • Windows 11, MinGW-w64 GCC (winlibs), Release: guest_math passes
  • tools/check_conventions.py --base main passes
  • PPSA14632 (Windows): imports hypotf; without it, it stops at startup with the unresolved import, and with it, it gets past that
  • Linux: not built locally

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:4775950603 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1196). Originally posted by [MathiasDevelopes](https://github.com/MathiasDevelopes) on 2026-10-07T17:12:07Z. --- ### What `hypot` and `hypotf` were not exported, so a title importing them from libSceLibcInternal stopped at startup with an unresolved import (`iz2shAGFIxc`). Both now forward to `std::hypot`, next to the other math functions in `Math.cpp`. That gives the C99 behaviour: no intermediate overflow or underflow, and infinity when either argument is infinite, even if the other is NaN. `GuestMath` checks the plain case, both signs, arguments whose squares overflow (`1e308` and `2e38f`), infinity with NaN in either order, and NaN propagation. ### Tested - Windows 11, MinGW-w64 GCC (winlibs), Release: `guest_math` passes - `tools/check_conventions.py --base main` passes - PPSA14632 (Windows): imports `hypotf`; without it, it stops at startup with the unresolved import, and with it, it gets past that - Linux: not built locally ### 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 - [x] AI-assisted: yes
copilot-pull-request-reviewer[bot] commented 2026-10-07 17:12:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by copilot-pull-request-reviewer[bot] on 2026-10-07T17:12:14Z.

Review: COMMENTED


Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5445733754 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1196#pullrequestreview-5445733754). Originally posted by [copilot-pull-request-reviewer[bot]](https://github.com/apps/copilot-pull-request-reviewer) on 2026-10-07T17:12:14Z. **Review:** COMMENTED --- Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.
github-actions[bot] commented 2026-10-07 17:13:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-07T17:13:31Z.


Progress report (9619edf → 2a253b3)

📈 System libraries: 85.58% (+0.01%, +2 implemented functions)

✅ 2 implemented
Library Name
libc hypot_nid_postfix
libc hypotf_nid_postfix
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6042938612 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1196#issuecomment-6042938612). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-07T17:13:31Z. --- <!-- progress-report --> ### Progress report (9619edf → 2a253b3) 📈 **System libraries**: 85.58% (+0.01%, +2 implemented functions) <details> <summary>✅ 2 implemented</summary> | Library | Name | | - | - | | libc | `hypot_nid_postfix` | | libc | `hypotf_nid_postfix` | </details>
tv7 commented 2026-10-07 19:25:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by tv7 on 2026-10-07T19:25:22Z.


Data point: PPSA03306 (Silt, Unity) imports hypotf (iz2shAGFIxc). On main (cea1ef13) plus #1141, startup stops at FAIL: unresolved ELF import iz2shAGFIxc from libSceLibcInternal.prx. With this PR merged as well, every startup import resolves and the title starts running (IL2CPP opens Media/Metadata and Media/Resources). Windows 11, GCC 15.2.0, RTX 3070.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6045207508 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1196#issuecomment-6045207508). Originally posted by [tv7](https://github.com/tv7) on 2026-10-07T19:25:22Z. --- Data point: PPSA03306 (Silt, Unity) imports `hypotf` (`iz2shAGFIxc`). On `main` (`cea1ef13`) plus #1141, startup stops at `FAIL: unresolved ELF import iz2shAGFIxc from libSceLibcInternal.prx`. With this PR merged as well, every startup import resolves and the title starts running (IL2CPP opens `Media/Metadata` and `Media/Resources`). Windows 11, GCC 15.2.0, RTX 3070.
eliasbruno124-dev commented 2026-10-07 19:48:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by eliasbruno124-dev on 2026-10-07T19:48:49Z.

Review: COMMENTED


I found a reproducible hypotf exception difference at e84f43aceb8e32eb43b00349c47331695c1c66dc on Windows 11, WinLibs GCC 15.2.0 UCRT, Release. The existing GuestMath test passes, but the float overload loses FE_INVALID for a signaling NaN paired with infinity.

Minimal check inside GuestMath.cpp (add <bit> and <cfenv>, using the existing hypotf_nid_postfix declaration):

std::fesetround(FE_TONEAREST);
std::feclearexcept(FE_ALL_EXCEPT);
const float result = hypotf_nid_postfix(
    std::bit_cast<float>(std::uint32_t{0x7f800001}),
    std::bit_cast<float>(std::uint32_t{0x7f800000}));
const bool invalid = (std::fetestexcept(FE_INVALID) & FE_INVALID) != 0;

Both versions return positive infinity. This PR's DLL produces invalid == false; a retained original PS5 title's bundled libc routine sets it. Swapping the inputs reproduces the difference. Quiet NaN plus infinity leaves invalid clear, and (3, 4) returns 5 without invalid.

This local one-line correction restores the observed exception on this toolchain:

float APS5_VABI hypotf_nid_postfix(float x, float y) { return static_cast<float>(std::hypot(static_cast<double>(x), static_cast<double>(y))); }

An extended full GuestMath fixture fails against the unchanged PR DLL and passes with that correction. It covers signaling/quiet NaNs, both argument orders, all four rounding modes and retention of an existing division-by-zero flag. A separate small DLL-loading probe reproduced the two signaling failures and both controls again before this comment.

The broader private comparison executes 4,457 input pairs under 16 rounding/FTZ/DAZ combinations. The correction removes the 52 invalid-flag discrepancies per mode and preserves the PR's tested numeric results apart from NaN payload bits. Other exception-status differences remain; this is not a bit-exact claim.

The original reference is an isolated unchanged routine from PPSA03524/01.003.000, run on the Windows CPU without title initialization. Component builds reuse audited dependencies from main 3c054008. No console hardware, native Linux or gameplay result is claimed, and no proprietary code/data is included here. The existing implementation remains your contribution; this is a narrow follow-up finding. Tests and this report were prepared with AI assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5447519171 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1196#pullrequestreview-5447519171). Originally posted by [eliasbruno124-dev](https://github.com/eliasbruno124-dev) on 2026-10-07T19:48:49Z. **Review:** COMMENTED --- I found a reproducible `hypotf` exception difference at `e84f43aceb8e32eb43b00349c47331695c1c66dc` on Windows 11, WinLibs GCC 15.2.0 UCRT, Release. The existing GuestMath test passes, but the float overload loses `FE_INVALID` for a signaling NaN paired with infinity. Minimal check inside `GuestMath.cpp` (add `<bit>` and `<cfenv>`, using the existing `hypotf_nid_postfix` declaration): ```cpp std::fesetround(FE_TONEAREST); std::feclearexcept(FE_ALL_EXCEPT); const float result = hypotf_nid_postfix( std::bit_cast<float>(std::uint32_t{0x7f800001}), std::bit_cast<float>(std::uint32_t{0x7f800000})); const bool invalid = (std::fetestexcept(FE_INVALID) & FE_INVALID) != 0; ``` Both versions return positive infinity. This PR's DLL produces `invalid == false`; a retained original PS5 title's bundled libc routine sets it. Swapping the inputs reproduces the difference. Quiet NaN plus infinity leaves invalid clear, and `(3, 4)` returns 5 without invalid. This local one-line correction restores the observed exception on this toolchain: ```cpp float APS5_VABI hypotf_nid_postfix(float x, float y) { return static_cast<float>(std::hypot(static_cast<double>(x), static_cast<double>(y))); } ``` An extended full GuestMath fixture fails against the unchanged PR DLL and passes with that correction. It covers signaling/quiet NaNs, both argument orders, all four rounding modes and retention of an existing division-by-zero flag. A separate small DLL-loading probe reproduced the two signaling failures and both controls again before this comment. The broader private comparison executes 4,457 input pairs under 16 rounding/FTZ/DAZ combinations. The correction removes the 52 invalid-flag discrepancies per mode and preserves the PR's tested numeric results apart from NaN payload bits. Other exception-status differences remain; this is not a bit-exact claim. The original reference is an isolated unchanged routine from PPSA03524/01.003.000, run on the Windows CPU without title initialization. Component builds reuse audited dependencies from main `3c054008`. No console hardware, native Linux or gameplay result is claimed, and no proprietary code/data is included here. The existing implementation remains your contribution; this is a narrow follow-up finding. Tests and this report were prepared with AI assistance.
MathiasDevelopes commented 2026-10-08 02:33:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MathiasDevelopes on 2026-10-08T02:33:54Z.


Thanks @eliasbruno124-dev for the detailed report, it checks out. std::hypot(float, float) goes to the CRT's hypotf, which returns +inf as soon as it sees an infinity and never touches the signaling NaN, so FE_INVALID was lost.

Fixed in 2a253b37 with your suggested correction: hypotf now widens both arguments to double before calling hypot. The float-to-double conversion raises FE_INVALID and quiets the sNaN, so hypotf(sNaN, inf) returns +inf with the flag set in either argument order. I also added a case to GuestMath.cpp that checks both orders.

I verified it with GCC and Clang on Linux, where the old code even returned NaN for this case. Quiet NaN plus infinity still leaves the flag clear, (3, 4) still gives 5, and 20M random float pairs return bit-identical results before and after. I haven't rerun it on Windows/UCRT myself, so a confirmation against your fixture would be welcome.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6050992357 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1196#issuecomment-6050992357). Originally posted by [MathiasDevelopes](https://github.com/MathiasDevelopes) on 2026-10-08T02:33:54Z. --- Thanks @eliasbruno124-dev for the detailed report, it checks out. `std::hypot(float, float)` goes to the CRT's `hypotf`, which returns +inf as soon as it sees an infinity and never touches the signaling NaN, so FE_INVALID was lost. Fixed in 2a253b37 with your suggested correction: `hypotf` now widens both arguments to double before calling `hypot`. The float-to-double conversion raises FE_INVALID and quiets the sNaN, so `hypotf(sNaN, inf)` returns +inf with the flag set in either argument order. I also added a case to `GuestMath.cpp` that checks both orders. I verified it with GCC and Clang on Linux, where the old code even returned NaN for this case. Quiet NaN plus infinity still leaves the flag clear, `(3, 4)` still gives 5, and 20M random float pairs return bit-identical results before and after. I haven't rerun it on Windows/UCRT myself, so a confirmation against your fixture would be welcome.
TheAggie commented 2026-10-08 02:57:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by TheAggie on 2026-10-08T02:57:31Z.


Another title this unblocks: Homebrew UI Lab (PPSA99050, blackbearreloaded/ps5-homebrew-ui) calls hypotf when switching pages with L1/R1. Without it AnyPS5 aborts every time with hypotf iz2shAGFIxc not implemented. With this PR merged locally, page switching works and guest_math passes. Windows 11, MinGW GCC 15.2.0 (WinLibs r7), RTX 5070 Ti.

To be clear about the setup: unmodified main does not get UI Lab far enough to call hypotf. I tested on a local branch that carries several other pending fixes (rendering and pixel shader input changes I'm preparing as separate PRs), with this PR merged on top.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6051243875 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1196#issuecomment-6051243875). Originally posted by [TheAggie](https://github.com/TheAggie) on 2026-10-08T02:57:31Z. --- Another title this unblocks: Homebrew UI Lab (PPSA99050, [blackbearreloaded/ps5-homebrew-ui](https://github.com/blackbearreloaded/ps5-homebrew-ui)) calls `hypotf` when switching pages with L1/R1. Without it AnyPS5 aborts every time with `hypotf iz2shAGFIxc not implemented`. With this PR merged locally, page switching works and `guest_math` passes. Windows 11, MinGW GCC 15.2.0 (WinLibs r7), RTX 5070 Ti. To be clear about the setup: unmodified `main` does not get UI Lab far enough to call `hypotf`. I tested on a local branch that carries several other pending fixes (rendering and pixel shader input changes I'm preparing as separate PRs), with this PR merged on top.
eliasbruno124-dev commented 2026-10-08 07:09:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by eliasbruno124-dev on 2026-10-08T07:09:15Z.


Confirmed on Windows 11 with WinLibs MinGW-w64 GCC 15.2.0 UCRT, Release, using a fresh complete library and relinker build of upstream main dcbf089d28eddca63a95faaf33b9848454b632f5, which includes your correction in 2a253b37.

The current full guest_math fixture passes. I also rebuilt and ran the unchanged four-case DLL-loading reproducer from my earlier review against the new libc.prx:

Inputs Result FE_INVALID
signaling NaN, +infinity +infinity set
+infinity, signaling NaN +infinity set
quiet NaN, +infinity +infinity clear
3, 4 5 clear

The input bit patterns are unchanged (0x7f800001 for the signaling NaN and 0x7fc00001 for the quiet NaN). All four outputs match the corrected Windows results from the original report. The tested library hashes remain unchanged afterward.

This confirms the requested Windows/UCRT exception behavior. I did not rerun the larger private numeric corpus, native Linux or console tests, and this is not a gameplay result. Tests and this follow-up were prepared with AI assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6054566056 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1196#issuecomment-6054566056). Originally posted by [eliasbruno124-dev](https://github.com/eliasbruno124-dev) on 2026-10-08T07:09:15Z. --- Confirmed on Windows 11 with WinLibs MinGW-w64 GCC 15.2.0 UCRT, Release, using a fresh complete library and relinker build of upstream main `dcbf089d28eddca63a95faaf33b9848454b632f5`, which includes your correction in `2a253b37`. The current full `guest_math` fixture passes. I also rebuilt and ran the unchanged four-case DLL-loading reproducer from my earlier review against the new `libc.prx`: | Inputs | Result | FE_INVALID | | --- | --- | --- | | signaling NaN, +infinity | +infinity | set | | +infinity, signaling NaN | +infinity | set | | quiet NaN, +infinity | +infinity | clear | | 3, 4 | 5 | clear | The input bit patterns are unchanged (`0x7f800001` for the signaling NaN and `0x7fc00001` for the quiet NaN). All four outputs match the corrected Windows results from the original report. The tested library hashes remain unchanged afterward. This confirms the requested Windows/UCRT exception behavior. I did not rerun the larger private numeric corpus, native Linux or console tests, and this is not a gameplay result. Tests and this follow-up were prepared with AI assistance.
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!1196
No description provided.