Skip to content

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

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
Krille2004zzz:feat/libsce-libcinternal-hypot
Nov 26, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
Krille2004zzz:feat/libsce-libcinternal-hypot

Conversation

@Krille2004zzz

Copy link
Copy Markdown
Contributor

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 feat(libc): implement _Hypot, _FHypot and _LHypot Dinkumware math helpers #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.

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

@PrivacyOnion PrivacyOnion left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@github-actions

github-actions Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Progress report (86bf1f0 → d3773c6)

📈 System libraries: 45.74% (+0.01%, +3 implemented functions)

✅ 3 implemented
Library Name
libSceLibcInternal _FHypot_nid_postfix
libSceLibcInternal _Hypot_nid_postfix
libSceLibcInternal _LHypot_nid_postfix

Changed libraries (implemented exports)

Library Before After Change
libSceLibcInternal 688/1126 (61.10%) 691/1126 (61.37%) +0.27%

@github-actions

github-actions Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 13778 core/libs/prx/libSceLibcInternal/CMakeLists.txt
PR 17746 core/libs/prx/libSceLibcInternal/CMakeLists.txt
PR 18135 core/libs/prx/libSceLibcInternal/CMakeLists.txt
core/libs/prx/libSceLibcInternal/Export.cpp
PR 19434 core/libs/prx/libSceLibcInternal/CMakeLists.txt
PR 19602 core/libs/prx/libSceLibcInternal/CMakeLists.txt
PR 24583 core/libs/prx/libSceLibcInternal/CMakeLists.txt
core/libs/prx/libSceLibcInternal/Export.cpp
core/libs/prx/libSceLibcInternal/include/MathInternals.hpp
PR 24907 core/libs/prx/libSceLibcInternal/CMakeLists.txt
PR 25573 core/libs/prx/libSceLibcInternal/CMakeLists.txt
core/libs/prx/libSceLibcInternal/Export.cpp
PR 26588 core/libs/prx/libSceLibcInternal/CMakeLists.txt
core/libs/prx/libSceLibcInternal/Export.cpp

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Nov 21, 2026
@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Nov 22, 2026
@Krille2004zzz
Krille2004zzz force-pushed the feat/libsce-libcinternal-hypot branch from 2440525 to d3773c6 Compare November 22, 2026 20:40
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Nov 22, 2026

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Helldiverr

Copy link
Copy Markdown
Contributor

Works on Windows, with two non-blocking points: a test gap for _LHypot and _FHypot, and an undocumented errno change. The merge passes the libSceLibcInternal and Dinkumware tests 89/89 in 3 of 3 runs, against 88/88 on main. The new guest_libc_internal_scaled_hypot passes in all 3 runs. A routing mutation turns _LHypot(1e4000L, 1e4000L) into infinity and still passes it.

Existing runs: the author, Krille2004zzz, tested on Linux x86-64 (Debian, GCC 14.2) and lists Windows as not tested. PrivacyOnion approved at d3e41cb996 after building and running the new test on Linux. MarcZX10 approved at d3e41cb996 from reading the code, then left comment reviews at 24405258b9 and at the head d3773c67a0; neither review ran a build or tests. The project's CI windows job passed at the head and ran guest_libc_internal_scaled_hypot, which passed. No community Windows run at the head.

What this run adds

  • A Windows build of the merge, with every libSceLibcInternal and Dinkumware test: 1 run on main and 3 on the merge.
  • A negative control and seven mutations of ScaledHypot.cpp, each run against the new test.
  • A probe of about 2 million random finite pairs for each width. It checks that result * 2^exponent equals the guest hypot, hypotf and hypotl, and that the exponent is even and nonzero. It also checks guest errno and a few long double and subnormal cases.

Code

  1. The exponent rule matches the measurements. largerExponent / 4 * 2 truncates toward zero (core/libs/prx/libSceLibcInternal/ScaledHypot.cpp:25 at d3773c67a0), which gives the measured -514 for (1e-310, 1e-310); floor division would give -516, and the floordiv mutation fails at that case. The 0 stored first (:18), the fallback to 2 (:26) and the positive zero (:22) are each pinned by a mutation. Across 1,998,013 random double pairs, every exponent was even and nonzero.
  2. Test gap: _LHypot and _FHypot are checked only at values where double precision gives the same answer: (3, 4), (0, 0) and (inf, NaN) for _LHypot (core/libs/tests/GuestLibcInternalScaledHypot.cpp:70-74 at d3773c67a0), plus (3e38, 3e38) for _FHypot (:58-65). Passing hypot_nid_postfix instead of hypotl_nid_postfix at ScaledHypot.cpp:44 passes the suite. With that mutation, (1e4000L, 1e4000L) returns infinity instead of 0x9.44cece11eba5bd2p+6641 with exponent 6644, and (1/3, 2/3) differs in its last 11 bits. The same swap for _FHypot at :36 also passes. A case like _LHypot(1e4000L, 1e4000L) writing 6644, with ldexp(result, 6644) == hypotl_nid_postfix(1e4000L, 1e4000L), would catch the first. It passes on the merge in the probe.
  3. errno: _Hypot can now change guest errno, and the debt entry does not say so. When one argument scales below the double range, the host std::ldexp at ScaledHypot.cpp:28 sets errno to 34 (ERANGE). On this build, (1e308, 5e-324) and (1e300, 1e-300) both leave errno at 34, while hypot_nid_postfix on the same pairs leaves it at 0. Guest errno is the host errno: __error_nid_postfix returns &errno (core/libs/prx/libc/src/Process.cpp:251-252 at 86dceb9157). The feat(libc): implement _Hypot, _FHypot and _LHypot Dinkumware math helpers #12363 measurements do not cover errno, so the module's behaviour is unknown. The debt fragment (docs/dev/debt/changes/feat-libsce-libcinternal-hypot.md:5 at d3773c67a0) lists what was not compared; adding errno to that list would record the gap.
  4. Last-bit differences are rare and already covered by the debt entry. In the probe, result * 2^exponent differed from the guest hypot by 1 ulp in 3 of 1,998,013 double pairs, and from hypotf in 80 float pairs. Each printed case has one argument in the subnormal range, where hypot of the unscaled pair takes its subnormal path. long double had no mismatches on the pairs of the same run, rescaled over ±2^15000.
  5. The NaN-before-infinity order (:19-20) is not observable from the results. Swapping the two checks still passes, because fabs(inf) + fabs(NaN) is NaN and both paths have already stored 0. Nothing needs to change.
  6. The edits to two earlier fragments fold cleanly. Those fragments are feat-libc-internal-wide-table-getters.md and libcinternal-erfc-tail-kernels.md. tools/debt_fold.py --check passes, and --show libSceLibcInternal no longer lists _Hypot, _FHypot or _LHypot under Additional imported names. tools/check_conventions.py --base upstream/main passes.
  7. The pr-overlap bot reports textual conflicts in Export.cpp with feat(libSceLibcInternal): implement the _Quad and _Quadph families #24583 (the _Quad family), on neighbouring stub lines. Whichever merges second needs a small rebase; the two PRs share no exports.

Negative control and mutation

Each row was built on the merge and run against guest_libc_internal_scaled_hypot.

Change Where (d3773c67a0) Result
Negative control: all three exports call NotImplemented_nid_no_patch ScaledHypot.cpp:36,40,44 fails, _Hypot_nid_postfix not implemented
Floor instead of truncating division :25 fails at _Hypot 1e-310 1e-310
No 0 stored first :18 fails at _Hypot 0 0
Infinity checked before NaN :19-20 passes (equivalent, point 5)
No fallback to 2 :26 fails at _Hypot 3 4
Zero returns x + y :22 fails at _Hypot -0 -0
_LHypot through double hypot :44 passes (point 2)
_FHypot through double hypot :36 passes (point 2)

Tests

The tests matching ^guest_libc_internal|^libc_internal|^dinkumware, with ctest -j 8.

main merge run 1 merge run 2 merge run 3
Passed 88/88 89/89 89/89 89/89
guest_libc_internal_scaled_hypot absent 0.06 0.03 0.04
Total time 4.90 2.06 1.18 1.23

Times are in seconds.

Host

  • CPU: Ryzen 9 9950X3D
  • OS: Windows 11 Pro 26300
  • Toolchain: WinLibs GCC 15.2, Release
  • Commits: head d3773c67a0, merged onto main 86dceb9157 as 297bc15beb

AI-assisted: yes

@oneandonlydean
oneandonlydean merged commit ac449d9 into boykopovar:main Nov 26, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants