feat(libSceLibcInternal): implement _Hypot, _FHypot and _LHypot #21704

Merged
Krille2004zzz merged 1 commit from upstream-pr/21704 into main 2026-11-26 16:54:10 +00:00
Krille2004zzz commented 2026-11-16 16:20:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Krille2004zzz on 2026-11-16T16:20:11Z.


What

Implements _Hypot, _FHypot and _LHypot in libSceLibcInternal (new ScaledHypot.cpp), replacing the parameterless stubs.

  • Prototypes (x, y, int* exponent) come from Dinkumware's public xmath.h declarations. No Dinkumware code is ported; the body is written from the measured behaviour.
  • Behaviour follows the firmware 13.42 libSceLibcInternal.sprx measurements that @BunnyFl posted in #12363 (module run natively on a PC, not a console). Thanks for those:
    • 0 is stored through exponent before anything else
    • a NaN argument returns NaN before an infinite one returns infinity, both with exponent 0
    • two zeros return 0 with exponent 0
    • otherwise the exponent is frexp exponent of the larger magnitude, divided by 4 (truncated), times 2, or 2 when that is 0, and the result is hypot/hypotf/hypotl of the arguments scaled by 2^-exponent
  • Known gap, recorded in the debt fragment: the module's own scaled algorithm is not reproduced, so for (0.1, 0.2) the module is 1 ulp higher than this, and other last-bit differences are possible. An unwritable exponent throws std::invalid_argument where the module faults.

Tested

Linux x86-64 (Debian, GCC 14.2.0), Release, BUILD_TESTING=ON, on this branch rebased on current main. No title tested.

  • New guest_libc_internal_scaled_hypot passes. It checks every #12363 measured case: (3, 4), (-5, 12), (0, 0), (1e300, 1e300) giving 1.7281329749791539e150 and 498, (1e-310, 1e-310) giving 7.584601526487318e-156 and -514, (0.1, 0.2), (inf, NaN), (NaN, 1), plus (inf, 1), the float/long double (3, 4) and (inf, NaN) cases, result * 2^exponent == hypot on extra pairs, and the null exponent. Removing the NaN check makes it fail at _Hypot nan 1.
  • ctest -R "libc_internal|dinkumware|public_headers": 159/159 pass.
  • Full ctest -j8 (before the rebase): 5846/5847 pass. The failure is guest_http2_transport, which also fails on unmodified main on this machine. GPU tests were skipped (no Vulkan device).
  • tools/check_conventions.py --base origin/main: all checks pass. tools/pr_overlap.py --branch: no shared exports. Six PRs that already conflict with main (#13778, #17300, #18135, #19602, #20059, #21255) could not be compared automatically. I compared their hunks by hand: none touches these functions or the changed lines.
  • Not tested on Windows or macOS.

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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5340686687 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/21704). Originally posted by [Krille2004zzz](https://github.com/Krille2004zzz) on 2026-11-16T16:20:11Z. --- ### What Implements `_Hypot`, `_FHypot` and `_LHypot` in libSceLibcInternal (new `ScaledHypot.cpp`), replacing the parameterless stubs. - Prototypes `(x, y, int* exponent)` come from Dinkumware's public [xmath.h](https://github.com/ganboing/crt.vc12/blob/f1d47126bf13d1c9dccaa6c53b72217fd0474968/src/xmath.h) declarations. No Dinkumware code is ported; the body is written from the measured behaviour. - Behaviour follows the firmware 13.42 `libSceLibcInternal.sprx` measurements that @BunnyFl posted in #12363 (module run natively on a PC, not a console). Thanks for those: - 0 is stored through `exponent` before anything else - a NaN argument returns NaN before an infinite one returns infinity, both with exponent 0 - two zeros return 0 with exponent 0 - otherwise the exponent is `frexp` exponent of the larger magnitude, divided by 4 (truncated), times 2, or 2 when that is 0, and the result is `hypot`/`hypotf`/`hypotl` of the arguments scaled by 2^-exponent - Known gap, recorded in the debt fragment: the module's own scaled algorithm is not reproduced, so for (0.1, 0.2) the module is 1 ulp higher than this, and other last-bit differences are possible. An unwritable exponent throws `std::invalid_argument` where the module faults. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Linux x86-64 (Debian, GCC 14.2.0), Release, `BUILD_TESTING=ON`, on this branch rebased on current `main`. No title tested. - New `guest_libc_internal_scaled_hypot` passes. It checks every #12363 measured case: (3, 4), (-5, 12), (0, 0), (1e300, 1e300) giving `1.7281329749791539e150` and 498, (1e-310, 1e-310) giving `7.584601526487318e-156` and -514, (0.1, 0.2), (inf, NaN), (NaN, 1), plus (inf, 1), the float/long double (3, 4) and (inf, NaN) cases, `result * 2^exponent == hypot` on extra pairs, and the null exponent. Removing the NaN check makes it fail at `_Hypot nan 1`. - `ctest -R "libc_internal|dinkumware|public_headers"`: 159/159 pass. - Full `ctest -j8` (before the rebase): 5846/5847 pass. The failure is `guest_http2_transport`, which also fails on unmodified `main` on this machine. GPU tests were skipped (no Vulkan device). - `tools/check_conventions.py --base origin/main`: all checks pass. `tools/pr_overlap.py --branch`: no shared exports. Six PRs that already conflict with `main` (#13778, #17300, #18135, #19602, #20059, #21255) could not be compared automatically. I compared their hunks by hand: none touches these functions or the changed lines. - Not tested on Windows or macOS. ### 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
PrivacyOnion commented 2026-11-16 16:22:54 +00:00 (Migrated from github.com)

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

Review: APPROVED


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

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: guest_libc_internal_scaled_hypot_tests built cleanly.
  • CTest (guest_libc_internal_scaled_hypot):
    1/1 Test #4072: guest_libc_internal_scaled_hypot ...   Passed    0.02 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libSceLibcInternal/Export.cpp, core/libs/prx/libSceLibcInternal/include/MathInternals.hpp reverted to main, the new tests do not build/link without the change; restoring it makes them build and pass.
  • Code reading:
    • Verified pointer safety in ScaledHypot.cpp:11-14: StoreExponent validates the guest pointer via GuestCopyOut_nid_no_patch before writing 0 initially and before writing the computed scale, throwing std::invalid_argument on failure.
    • Verified IEEE-754 special-value handling in ScaledHypot.cpp:19-22: NaN arguments are checked before infinity (so _Hypot(inf, NaN) returns NaN with exponent 0), infinities return +infinity with exponent 0, and two signed/unsigned zeros return +0.0 with exponent 0.
    • Verified exponent scaling formula in ScaledHypot.cpp:23-28: std::frexp on the larger magnitude divided by 4 (truncated) times 2 (or 2 when 0) matches the firmware 13.42 measurements and avoids overflow/underflow before calling hypotf_nid_postfix / hypot_nid_postfix / hypotl_nid_postfix.
    • Verified cross-platform LP64/LLP64 portability and ABI signatures in MathInternals.hpp and ScaledHypot.cpp: APS5_VABI is applied to _FHypot_nid_postfix, _Hypot_nid_postfix, and _LHypot_nid_postfix, and CMakeLists.txt configures Windows unwind and PATH properly.
    • Verified AGENTS.md compliance and technical debt tracking in docs/dev/debt/changes/feat-libsce-libcinternal-hypot.md: stubs removed from Export.cpp are removed from the debt inventory and known last-ulp/fault differences are documented under Functional debt.
    • Verified unit test coverage in GuestLibcInternalScaledHypot.cpp: tests cover _Hypot, _FHypot, and _LHypot across normal values, subnormals/near-maxima (1e308, 5e-324, 3e38f), signed zeros, NaN vs infinity precedence, and null exponent pointer handling.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5923432608 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/21704#pullrequestreview-5923432608). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-16T16:22:54Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `d3e41cb996`: - **Conventions**: `tools/check_conventions.py` passed; `git diff --check` clean. - **Build**: `guest_libc_internal_scaled_hypot_tests` built cleanly. - **CTest** (`guest_libc_internal_scaled_hypot`): ``` 1/1 Test #4072: guest_libc_internal_scaled_hypot ... Passed 0.02 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/libs/prx/libSceLibcInternal/Export.cpp`, `core/libs/prx/libSceLibcInternal/include/MathInternals.hpp` reverted to `main`, the new tests do not build/link without the change; restoring it makes them build and pass. - **Code reading**: - Verified pointer safety in ScaledHypot.cpp:11-14: StoreExponent validates the guest pointer via GuestCopyOut_nid_no_patch before writing 0 initially and before writing the computed scale, throwing std::invalid_argument on failure. - Verified IEEE-754 special-value handling in ScaledHypot.cpp:19-22: NaN arguments are checked before infinity (so _Hypot(inf, NaN) returns NaN with exponent 0), infinities return +infinity with exponent 0, and two signed/unsigned zeros return +0.0 with exponent 0. - Verified exponent scaling formula in ScaledHypot.cpp:23-28: std::frexp on the larger magnitude divided by 4 (truncated) times 2 (or 2 when 0) matches the firmware 13.42 measurements and avoids overflow/underflow before calling hypotf_nid_postfix / hypot_nid_postfix / hypotl_nid_postfix. - Verified cross-platform LP64/LLP64 portability and ABI signatures in MathInternals.hpp and ScaledHypot.cpp: APS5_VABI is applied to _FHypot_nid_postfix, _Hypot_nid_postfix, and _LHypot_nid_postfix, and CMakeLists.txt configures Windows unwind and PATH properly. - Verified AGENTS.md compliance and technical debt tracking in docs/dev/debt/changes/feat-libsce-libcinternal-hypot.md: stubs removed from Export.cpp are removed from the debt inventory and known last-ulp/fault differences are documented under Functional debt. - Verified unit test coverage in GuestLibcInternalScaledHypot.cpp: tests cover _Hypot, _FHypot, and _LHypot across normal values, subnormals/near-maxima (1e308, 5e-324, 3e38f), signed zeros, NaN vs infinity precedence, and null exponent pointer handling. Looks Good To Me
MarcZX10 commented 2026-11-20 11:34:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-20T11:34:44Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this change; its scaled result logic and exponent handling are consistent with the documented behavior, with remaining firmware-specific differences recorded as technical debt.

What I ran

  • Read the prepared diff and all changed files at head d3e41cb996e228e19d9125fb7fad67ac8c081282 using repo_read.py.
  • No build or tests were run for this review.

Results

Check Result Note
Changed code review passed All six changed files read
Build and tests not run Author-reported results are not independently verified

Findings

Blocking

None.

Non-blocking

None.

Not verified

The reported test runs and platform behavior were not independently verified. The firmware comparison was measured by the author on a PC, not a console; firmware-specific bit-level behavior, signalling NaNs, directed rounding, and exception flags remain unconfirmed. No excluded changed files were listed in the manifest.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5992031410 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/21704#pullrequestreview-5992031410). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-20T11:34:44Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change; its scaled result logic and exponent handling are consistent with the documented behavior, with remaining firmware-specific differences recorded as technical debt. ## What I ran - Read the prepared diff and all changed files at head `d3e41cb996e228e19d9125fb7fad67ac8c081282` using `repo_read.py`. - No build or tests were run for this review. ## Results | Check | Result | Note | |---|---|---| | Changed code review | passed | All six changed files read | | Build and tests | not run | Author-reported results are not independently verified | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified The reported test runs and platform behavior were not independently verified. The firmware comparison was measured by the author on a PC, not a console; firmware-specific bit-level behavior, signalling NaNs, directed rounding, and exception flags remain unconfirmed. No excluded changed files were listed in the manifest. ## To get this to a good state Nothing is required.
MarcZX10 commented 2026-11-21 09:54:06 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-21T09:54:06Z.

Review: COMMENTED


Reviewed — no blocking findings

The implementation follows the documented measured exponent and special-value rules, while the firmware comparison remains incomplete and the reported tests were not run by this review.

What I ran

  • Read the prepared description, commits, diff, manifest and all eight changed files at head 24405258b9d014f4339a8034ff60bf214d8ee1aa.
  • Inspected GuestCopyOut_nid_no_patch declarations and the existing hypot_nid_postfix implementation for targeted context.
  • No commands or tests were run in this review.

Results

Check Result Note
Changed-file review passed All eight manifest files read
Targeted context passed Memory-write API and host hypot inspected
Tests in this review not run No test output available

Findings

Blocking

None.

Non-blocking

  • core/libs/dev/debt/changes/feat-libsce-libcinternal-hypot.md:5 documents that _Hypot, _FHypot and _LHypot may differ from firmware in last-bit results, with only limited float and long-double measurements. The implementation’s generic use of the host hypot routines is consistent with that stated limitation. Consider adding more firmware comparisons when available, especially for float and long double.

Not verified

  • No tests or build were run on this head. The test results in the PR description are author-reported, including the full suite result from before rebase.
  • Windows, macOS, console behavior, signaling NaNs, directed rounding, and floating-point exception flags were not verified.
  • files.json was not available through the reader; the manifest lists eight changed files, and all eight were read. No files were marked excluded in the manifest.

To get this to a good state

Nothing is required. Optional: expand the firmware comparisons for float and long double when measurements are available.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6004485453 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/21704#pullrequestreview-6004485453). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-21T09:54:06Z. **Review:** COMMENTED --- # Reviewed — no blocking findings The implementation follows the documented measured exponent and special-value rules, while the firmware comparison remains incomplete and the reported tests were not run by this review. ## What I ran - Read the prepared description, commits, diff, manifest and all eight changed files at head `24405258b9d014f4339a8034ff60bf214d8ee1aa`. - Inspected `GuestCopyOut_nid_no_patch` declarations and the existing `hypot_nid_postfix` implementation for targeted context. - No commands or tests were run in this review. ## Results | Check | Result | Note | |---|---|---| | Changed-file review | passed | All eight manifest files read | | Targeted context | passed | Memory-write API and host hypot inspected | | Tests in this review | not run | No test output available | ## Findings ### Blocking None. ### Non-blocking - `core/libs/dev/debt/changes/feat-libsce-libcinternal-hypot.md:5` documents that `_Hypot`, `_FHypot` and `_LHypot` may differ from firmware in last-bit results, with only limited float and long-double measurements. The implementation’s generic use of the host hypot routines is consistent with that stated limitation. Consider adding more firmware comparisons when available, especially for float and long double. ## Not verified - No tests or build were run on this head. The test results in the PR description are author-reported, including the full suite result from before rebase. - Windows, macOS, console behavior, signaling NaNs, directed rounding, and floating-point exception flags were not verified. - `files.json` was not available through the reader; the manifest lists eight changed files, and all eight were read. No files were marked excluded in the manifest. ## To get this to a good state Nothing is required. Optional: expand the firmware comparisons for float and long double when measurements are available.
MarcZX10 commented 2026-11-22 21:55:06 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-22T21:55:06Z.

Review: COMMENTED


Reviewed — comments

The implementation follows the stated scaling and special-value rules, but I could not run the reported checks in this review, so a formal approval is not justified.

What I ran

Reviewed head d3773c67a0872c44cf21487e1549de74e02f56a4. I read the prepared description, commits, manifest, and diff; used repo_read.py to inspect the changed implementation, declarations, build wiring, tests, and debt entries. No test or build command was run here. The author reports Linux x86-64 Debian/GCC 14.2 results; those were not independently verified.

Results

Check Result Note
Source review passed Changed files and relevant hunks inspected
Reported test suite not run Results are author reported
Negative control not run Not independently checked

Negative control

Could not run the reported mutation check that removes the NaN check.

Findings

Blocking

None.

Non-blocking

  • The compatibility evidence is limited: the implementation intentionally differs by one ULP for (0.1, 0.2), while _FHypot and _LHypot were measured only for (3, 4) and (inf, NaN). The debt entry records these limits. Additional firmware comparisons would strengthen confidence in the edge cases and rounding behavior.

Sources

Not verified

No build, test, negative control, Windows or macOS run, title test, or physical-console behavior was verified here. I read the changed files listed in the manifest: core/libs/prx/libSceLibcInternal/CMakeLists.txt, core/libs/prx/libSceLibcInternal/Export.cpp (relevant changed hunks), core/libs/prx/libSceLibcInternal/ScaledHypot.cpp, core/libs/prx/libSceLibcInternal/include/MathInternals.hpp, core/libs/tests/GuestLibcInternalScaledHypot.cpp, and the three changed debt fragments. No changed file was excluded by the manifest.

To get this to a good state

Nothing is required. Optional: add broader firmware comparisons for float and long-double edge cases and rounding behavior.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6014732286 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/21704#pullrequestreview-6014732286). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-22T21:55:06Z. **Review:** COMMENTED --- # Reviewed — comments The implementation follows the stated scaling and special-value rules, but I could not run the reported checks in this review, so a formal approval is not justified. ## What I ran Reviewed head `d3773c67a0872c44cf21487e1549de74e02f56a4`. I read the prepared description, commits, manifest, and diff; used `repo_read.py` to inspect the changed implementation, declarations, build wiring, tests, and debt entries. No test or build command was run here. The author reports Linux x86-64 Debian/GCC 14.2 results; those were not independently verified. ## Results | Check | Result | Note | |---|---|---| | Source review | passed | Changed files and relevant hunks inspected | | Reported test suite | not run | Results are author reported | | Negative control | not run | Not independently checked | ## Negative control Could not run the reported mutation check that removes the NaN check. ## Findings ### Blocking None. ### Non-blocking - The compatibility evidence is limited: the implementation intentionally differs by one ULP for `(0.1, 0.2)`, while `_FHypot` and `_LHypot` were measured only for `(3, 4)` and `(inf, NaN)`. The debt entry records these limits. Additional firmware comparisons would strengthen confidence in the edge cases and rounding behavior. ## Sources - [`ScaledHypot.cpp` at the reviewed head](https://github.com/boykopovar/AnyPS5/blob/d3773c67a0872c44cf21487e1549de74e02f56a4/core/libs/prx/libSceLibcInternal/ScaledHypot.cpp) - [Scaled hypot debt entry at the reviewed head](https://github.com/boykopovar/AnyPS5/blob/d3773c67a0872c44cf21487e1549de74e02f56a4/docs/dev/debt/changes/feat-libsce-libcinternal-hypot.md) ## Not verified No build, test, negative control, Windows or macOS run, title test, or physical-console behavior was verified here. I read the changed files listed in the manifest: `core/libs/prx/libSceLibcInternal/CMakeLists.txt`, `core/libs/prx/libSceLibcInternal/Export.cpp` (relevant changed hunks), `core/libs/prx/libSceLibcInternal/ScaledHypot.cpp`, `core/libs/prx/libSceLibcInternal/include/MathInternals.hpp`, `core/libs/tests/GuestLibcInternalScaledHypot.cpp`, and the three changed debt fragments. No changed file was excluded by the manifest. ## To get this to a good state Nothing is required. Optional: add broader firmware comparisons for float and long-double edge cases and rounding behavior.
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!21704
No description provided.