fix(libc): compute expl, sinhl, coshl and tanhl with FreeBSD msun's ld80 code #18525

Merged
AlfaRojo merged 5 commits from upstream-pr/18525 into main 2026-11-13 18:23:17 +00:00
AlfaRojo commented 2026-11-11 20:08:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by AlfaRojo on 2026-11-11T20:08:53Z.


What

expl, sinhl, coshl and tanhl returned the host's std::exp, std::sinh, std::cosh and std::tanh, so their low bits depended on the host math library (WinLibs differs from msun on about 22%, 32%, 32% and 4% of random arguments; e.g. expl(19.1947635596975747091) is off by one ulp, and msun's sinhl is within 0.5 ulp where WinLibs' is up to 0.74). They now port FreeBSD release/11.4.0 lib/msun/ld80/s_expl.c with its k_expl.h kernel (128-entry Tang table, shared), lib/msun/src/e_sinhl.c, e_coshl.c and s_tanhl.c: https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/msun/ld80/s_expl.c. Both licence notices (Kargl BSD and Sun) are kept. expm1l from the same file is not ported. The debt entry in docs/dev/debt/libc.md is split out of the host-computed list and records that console equivalence is unverified, that errno is untouched, and the x87 precision behaviour.

Tested

  • Windows 11 Pro 10.0.26200, WinLibs GCC 15.2.0 r7 UCRT, Ninja, Release, based on upstream/main 65351c2931.
  • guest_math 3/3 runs pass: C99 Annex F specials (expl(-inf) = +0, expl(+inf), NaN, expl(0) = 1, signed-zero sinhl/tanhl, tanhl(+-inf) = +-1, coshl(+-inf)), the exact overflow and underflow thresholds of expl (the threshold value itself is finite, the next value is +inf with FE_OVERFLOW; the underflow threshold gives the smallest denormal, the next value +0 with FE_UNDERFLOW), sinhl/coshl overflow past the threshold, tanhl(+-100) = +-1 with FE_INEXACT, errno left unchanged, and 82 regular values bit for bit (small, near 1, 40 and 64, near overflow and underflow, denormal results, the host-differing ones) plus one value per function at 53-bit x87 precision. The expected bits come from the port and were cross-checked against mpmath at 300 bits: every case is within 0.51 ulp.
  • Port vs the original msun sources built with the host gcc: 30,000,000 inputs at 64-bit and 5,000,000 at 53-bit precision (uniform and wide-range arguments, tiny arguments, the overflow and underflow thresholds, and random bit patterns including denormals, infinities and NaNs), 0 differences for all four functions.
  • Negative control: with the four std:: calls restored the test aborts at the expl underflow-threshold check (host gives 0, msun the smallest denormal); pinned values of all four functions differ from the host.
  • tools/check_conventions.py --base upstream/main passes; tools/check_test_registration.py passes.
  • Merge conflicts with my other open libm branches, trivially resolved: #18481 (powl) only in docs/dev/debt/libc.md; #18513 (sinl, cosl, tanl) in the adjacent four Math.cpp export lines, the test call list and the same debt line.
  • Overlap: tools/pr_overlap.py failed closed (output attached; 1981 open PRs, 118 share files, 31 not compared because they conflict with main). No open PR implements these four functions. #18122 forwards expl, sinhl, coshl and tanhl of libSceLibcInternal to *_nid_no_patch, which call *_nid_postfix, so it composes with this change. #11262, #11266, #18110 and #18481 edit the same docs/dev/debt/libc.md list entry this PR splits, and #11266 and #18513 the neighbouring Math.cpp lines; whichever merges second needs a trivial rebase. #11266 (exp2l) has its own tables and does not add k_expl.h. I grepped the diffs of the uncompared PRs for the four names: the only hits are the word explicit in unrelated libkernel and relinker PRs, and a reformat of the debt files in #14212; #6295, #7544 and #11109 (not compared, touch Math.cpp) do not mention them.
  • Head f74693e6f7 (merged with upstream/main including #11266's exp2l): guest_math, guest_math_elementary and guest_math_exact, all three test executables rebuilt, pass in 3/3 runs; check_conventions.py --base upstream/main reports no errors.

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 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:5266099784 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18525). Originally posted by [AlfaRojo](https://github.com/AlfaRojo) on 2026-11-11T20:08:53Z. --- ### What `expl`, `sinhl`, `coshl` and `tanhl` returned the host's `std::exp`, `std::sinh`, `std::cosh` and `std::tanh`, so their low bits depended on the host math library (WinLibs differs from msun on about 22%, 32%, 32% and 4% of random arguments; e.g. `expl(19.1947635596975747091)` is off by one ulp, and msun's `sinhl` is within 0.5 ulp where WinLibs' is up to 0.74). They now port FreeBSD release/11.4.0 `lib/msun/ld80/s_expl.c` with its `k_expl.h` kernel (128-entry Tang table, shared), `lib/msun/src/e_sinhl.c`, `e_coshl.c` and `s_tanhl.c`: https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/msun/ld80/s_expl.c. Both licence notices (Kargl BSD and Sun) are kept. `expm1l` from the same file is not ported. The debt entry in `docs/dev/debt/libc.md` is split out of the host-computed list and records that console equivalence is unverified, that errno is untouched, and the x87 precision behaviour. ### Tested - Windows 11 Pro 10.0.26200, WinLibs GCC 15.2.0 r7 UCRT, Ninja, Release, based on upstream/main 65351c2931. - `guest_math` 3/3 runs pass: C99 Annex F specials (`expl(-inf)` = +0, `expl(+inf)`, NaN, `expl(0)` = 1, signed-zero `sinhl`/`tanhl`, `tanhl(+-inf)` = +-1, `coshl(+-inf)`), the exact overflow and underflow thresholds of `expl` (the threshold value itself is finite, the next value is +inf with FE_OVERFLOW; the underflow threshold gives the smallest denormal, the next value +0 with FE_UNDERFLOW), `sinhl`/`coshl` overflow past the threshold, `tanhl(+-100)` = +-1 with FE_INEXACT, errno left unchanged, and 82 regular values bit for bit (small, near 1, 40 and 64, near overflow and underflow, denormal results, the host-differing ones) plus one value per function at 53-bit x87 precision. The expected bits come from the port and were cross-checked against mpmath at 300 bits: every case is within 0.51 ulp. - Port vs the original msun sources built with the host gcc: 30,000,000 inputs at 64-bit and 5,000,000 at 53-bit precision (uniform and wide-range arguments, tiny arguments, the overflow and underflow thresholds, and random bit patterns including denormals, infinities and NaNs), 0 differences for all four functions. - Negative control: with the four `std::` calls restored the test aborts at the `expl` underflow-threshold check (host gives 0, msun the smallest denormal); pinned values of all four functions differ from the host. - `tools/check_conventions.py --base upstream/main` passes; `tools/check_test_registration.py` passes. - Merge conflicts with my other open libm branches, trivially resolved: #18481 (`powl`) only in `docs/dev/debt/libc.md`; #18513 (`sinl`, `cosl`, `tanl`) in the adjacent four `Math.cpp` export lines, the test call list and the same debt line. - Overlap: `tools/pr_overlap.py` failed closed (output attached; 1981 open PRs, 118 share files, 31 not compared because they conflict with main). No open PR implements these four functions. #18122 forwards `expl`, `sinhl`, `coshl` and `tanhl` of libSceLibcInternal to `*_nid_no_patch`, which call `*_nid_postfix`, so it composes with this change. #11262, #11266, #18110 and #18481 edit the same `docs/dev/debt/libc.md` list entry this PR splits, and #11266 and #18513 the neighbouring `Math.cpp` lines; whichever merges second needs a trivial rebase. #11266 (`exp2l`) has its own tables and does not add `k_expl.h`. I grepped the diffs of the uncompared PRs for the four names: the only hits are the word `explicit` in unrelated libkernel and relinker PRs, and a reformat of the debt files in #14212; #6295, #7544 and #11109 (not compared, touch `Math.cpp`) do not mention them. - Head `f74693e6f7` (merged with upstream/main including #11266's `exp2l`): `guest_math`, `guest_math_elementary` and `guest_math_exact`, all three test executables rebuilt, pass in 3/3 runs; `check_conventions.py --base upstream/main` reports no errors. ### 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 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-12 06:08:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-12T06:08:54Z.

Review: APPROVED


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

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: guest_math_tests built cleanly.
  • CTest (guest_math, guest_math_elementary, guest_math_exact):
    1/3 Test #1067: guest_math_exact .................   Passed    0.02 sec
    2/3 Test  #993: guest_math .......................   Passed    0.04 sec
    3/3 Test  #994: guest_math_elementary ............   Passed    0.02 sec
    100% tests passed, 0 tests failed out of 3
    
  • Negative control: with core/libs/prx/libc/src/Math.cpp reverted to main and the new tests kept, the suite fails:
    1/3 Test #1067: guest_math_exact .................   Passed    0.02 sec
    2/3 Test  #994: guest_math_elementary ............   Passed    0.02 sec
    3/3 Test  #993: guest_math .......................Subprocess aborted***Exception:   0.39 sec
    67% tests passed, 1 tests failed out of 3
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified FreeBSD msun ld80 s_expl.c, k_expl.h, e_sinhl.c, e_coshl.c, and s_tanhl.c ports in core/libs/prx/libc/src/Math.cpp, confirming table coefficients, polynomial terms, and argument reductions match upstream release/11.4.0.
    • Verified Annex F special value handling across expl_nid_postfix, sinhl_nid_postfix, coshl_nid_postfix, and tanhl_nid_postfix for signed zero (+-0.0), positive/negative infinities, quiet and signaling NaNs, and exact threshold boundaries.
    • Verified floating-point exceptions and errno preservation in core/libs/prx/libc/src/Math.cpp and core/libs/tests/GuestMath.cpp, ensuring FE_OVERFLOW, FE_UNDERFLOW, and FE_INEXACT are raised appropriately while errno is left unmodified per msun ld80 semantics.
    • Verified array indexing and bounds safety for ExplTable[n2] where n2 is mapped modulo ExplIntervals (128), ensuring non-negative in-bounds lookups for all integer ranges of n.
    • Audited x87 extended precision (80-bit) arithmetic, signed arithmetic right shifts (C++20), and bitwise float reconstructions in ExplHalfKernel and ExplPowerOfTwo for cross-platform Linux (LP64) and Windows/MinGW (LLP64) correctness.
    • Verified docs/dev/debt/libc.md properly splits the technical debt entry and accurately documents console unverified equivalence, errno behavior, and caller x87 precision semantics.
  • Note: the AI-assisted checkbox can stay.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5869887807 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18525#pullrequestreview-5869887807). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-12T06:08:54Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `331e064441`: - **Conventions**: `tools/check_conventions.py` passed; `git diff --check` clean. - **Build**: `guest_math_tests` built cleanly. - **CTest** (`guest_math`, `guest_math_elementary`, `guest_math_exact`): ``` 1/3 Test #1067: guest_math_exact ................. Passed 0.02 sec 2/3 Test #993: guest_math ....................... Passed 0.04 sec 3/3 Test #994: guest_math_elementary ............ Passed 0.02 sec 100% tests passed, 0 tests failed out of 3 ``` - **Negative control**: with `core/libs/prx/libc/src/Math.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/3 Test #1067: guest_math_exact ................. Passed 0.02 sec 2/3 Test #994: guest_math_elementary ............ Passed 0.02 sec 3/3 Test #993: guest_math .......................Subprocess aborted***Exception: 0.39 sec 67% tests passed, 1 tests failed out of 3 ``` Restoring the change makes it pass again. - **Code reading**: - Verified FreeBSD msun ld80 s_expl.c, k_expl.h, e_sinhl.c, e_coshl.c, and s_tanhl.c ports in core/libs/prx/libc/src/Math.cpp, confirming table coefficients, polynomial terms, and argument reductions match upstream release/11.4.0. - Verified Annex F special value handling across expl_nid_postfix, sinhl_nid_postfix, coshl_nid_postfix, and tanhl_nid_postfix for signed zero (+-0.0), positive/negative infinities, quiet and signaling NaNs, and exact threshold boundaries. - Verified floating-point exceptions and errno preservation in core/libs/prx/libc/src/Math.cpp and core/libs/tests/GuestMath.cpp, ensuring FE_OVERFLOW, FE_UNDERFLOW, and FE_INEXACT are raised appropriately while errno is left unmodified per msun ld80 semantics. - Verified array indexing and bounds safety for ExplTable[n2] where n2 is mapped modulo ExplIntervals (128), ensuring non-negative in-bounds lookups for all integer ranges of n. - Audited x87 extended precision (80-bit) arithmetic, signed arithmetic right shifts (C++20), and bitwise float reconstructions in ExplHalfKernel and ExplPowerOfTwo for cross-platform Linux (LP64) and Windows/MinGW (LLP64) correctness. - Verified docs/dev/debt/libc.md properly splits the technical debt entry and accurately documents console unverified equivalence, errno behavior, and caller x87 precision semantics. - Note: the AI-assisted checkbox can stay. Looks Good To Me
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!18525
No description provided.