fix(libc): compute tanh with FreeBSD msun's algorithm, as titles' libc.prx does #10316

Merged
JJenkx merged 2 commits from upstream-pr/10316 into main 2026-10-26 08:48:44 +00:00
JJenkx commented 2026-10-23 17:29:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-23T17:29:53Z.


What

tanh returned the host's tanh, so its last bit depended on the host. For example, tanh(0x3f927ed0db8370b1) is 0x3f927e4d1092a580 in the bundled libc.prx and 0x3f927e4d1092a57f on Windows (UCRT). On 20 million random arguments, UCRT's tanh differs from the titles' copy on 1,428,804.

The titles' copy (NID JM4EBvWT9rc) is FreeBSD msun's s_tanh.c, computed through msun's expm1. tanh now computes it over the static Expm1 from #10306, which is now merged on main:

  • the infinities and NaN;
  • the 2^-28 shortcut;
  • the |x| >= 1 form 1 - 2 / (expm1(2|x|) + 2);
  • the |x| < 1 form -t / (t + 2) with t = expm1(-2|x|);
  • 1 - tiny from 22 up, with tiny read at run time.

It calls Expm1 directly, not the exported expm1, so no errno can come from the inner call. Its arguments never exceed 44 in magnitude anyway. The titles' copy sets no errno for any argument, and neither does this port.

Tested

Current Linux refresh: head a5b452fa493d91a14fbb3e708f618171a9b3dcb4, directly on main 50a4f824fc3ee23177d8e1effbe306e50150187f. The merged #10306 commit is no longer duplicated in this stack. Production added/removed lines are byte-identical to the previous reviewed head. Fresh independent libc/math build; guest_math and guest_dinkumware_internals each pass three consecutive runs. With only Math.cpp reverted to the direct parent's production code and the new fixture retained, guest_math aborts; restoration returns both groups to passing and reproduces the original libc library hash. Conventions/whitespace pass.

Read-only merge-tree against fetched main dc15ed65375228a0f69c41d7770c4c118adb38f2 is clean; the checks above are on the explicitly stated tested base, not a build of that merge tree. Windows remains user-paused and has not been rerun on this head.

Exact-head Linux build, JUnit, negative/restoration controls and rebase proof, SHA-256 1087c9bf4d5bd4e0a9c5e54b5d18e710c1e576d7de607f0adc30d41d23af67a2.

Historical previous-head evidence (not rerun on the current head):

Independent Windows regression report on the refreshed head (ycbuzi): PR head e2c058c1ff4eea220f3b1aba3f3d35d339ba82b7 was cleanly integrated onto main fa515def85 as b3c835692c on Windows 11 Home 10.0.26200, MinGW-w64 GCC 15.2.0 UCRT r7, Release with BUILD_TESTING=ON. The reporter built libc, guest_math_tests and guest_dinkumware_internals_tests without reported errors or warnings; both focused groups passed twice using ctest --repeat until-fail:2. Their negative control replaced Math.cpp with main's version: guest_math aborted while the Dinkumware group still passed. This is attributed independent integration/regression evidence; original-module matrices and titles were not rerun.

The Windows module comparisons and negative control below are historical evidence from original head 171b98173f536676ab8c8c3aba0a443ff060fb16; Windows was not rerun on the refreshed commit. Results described as console results mean the supplied titles' bundled libraries executed on the Windows host, not measurements on a physical console.

Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, BUILD_TESTING=ON, on #10306's head.

  • Comparison: a scratch loader runs the decrypted libc.prx of PPSA07923, PPSA05916 and PPSA13934 natively and supplies __error. It compared their tanh with this PR's code, checking result bits (NaN payloads included) and errno, on 16,068,211 arguments per title:

    • 16 million random arguments, half random bit patterns and half with exponents 2^-63 to 2^8;
    • the 8,001 floats around ±1, ±22, ±2^-28, 0.5 and 10;
    • every power of two from 2^-1074 to 2^1023, both signs;
    • the special values.

    Results:

    • PPSA07923 and PPSA05916: identical bits on all of them, and errno is never set.
    • PPSA13934: no errno either. The bits differ on 1,054 arguments, because that copy is an FMA-contracted build.
  • guest_math: the new CheckTanhConsoleBits has 24 cases with the bundled library's results:

    • ±0 and both sides of 2^-28;
    • 0.5, the float below 1, and ±1;
    • both sides of 22, −22 and 50;
    • DBL_MAX, ±inf and NaN payloads of both signs;
    • six arguments where UCRT differs.

    Each case also checks that errno stays unchanged.

    guest_math and guest_dinkumware_internals pass in three runs out of three on the exact head.

  • Negative control: with #10306's Math.cpp and these tests, guest_math aborts in a Require, while guest_dinkumware_internals still passes.

  • python tools/check_conventions.py --base #10306's head, and git diff --check, pass.

  • Order: Standalone tanh change on main, which now contains the required internal Expm1 implementation from merged #10306.

  • Not tested: macOS, a physical console, floating-point exception flags, and tanhl. The original module comparison and negative control were Windows-only; the new Linux build and regression checks are scoped separately above.

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: none; the msun function is rewritten, as #9668, #5036 and the sinhf entry did
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4986744683 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10316). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-23T17:29:53Z. --- ### What `tanh` returned the host's `tanh`, so its last bit depended on the host. For example, `tanh(0x3f927ed0db8370b1)` is `0x3f927e4d1092a580` in the bundled `libc.prx` and `0x3f927e4d1092a57f` on Windows (UCRT). On 20 million random arguments, UCRT's `tanh` differs from the titles' copy on 1,428,804. The titles' copy (NID `JM4EBvWT9rc`) is FreeBSD msun's [`s_tanh.c`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/msun/src/s_tanh.c), computed through msun's `expm1`. `tanh` now computes it over the static `Expm1` from #10306, which is now merged on main: - the infinities and NaN; - the `2^-28` shortcut; - the `|x| >= 1` form `1 - 2 / (expm1(2|x|) + 2)`; - the `|x| < 1` form `-t / (t + 2)` with `t = expm1(-2|x|)`; - `1 - tiny` from 22 up, with `tiny` read at run time. It calls `Expm1` directly, not the exported `expm1`, so no `errno` can come from the inner call. Its arguments never exceed 44 in magnitude anyway. The titles' copy sets no `errno` for any argument, and neither does this port. ### Tested Current Linux refresh: head `a5b452fa493d91a14fbb3e708f618171a9b3dcb4`, directly on main `50a4f824fc3ee23177d8e1effbe306e50150187f`. The merged #10306 commit is no longer duplicated in this stack. Production added/removed lines are byte-identical to the previous reviewed head. Fresh independent libc/math build; guest_math and guest_dinkumware_internals each pass three consecutive runs. With only Math.cpp reverted to the direct parent's production code and the new fixture retained, guest_math aborts; restoration returns both groups to passing and reproduces the original libc library hash. Conventions/whitespace pass. Read-only merge-tree against fetched main `dc15ed65375228a0f69c41d7770c4c118adb38f2` is clean; the checks above are on the explicitly stated tested base, not a build of that merge tree. Windows remains user-paused and has not been rerun on this head. [Exact-head Linux build, JUnit, negative/restoration controls and rebase proof](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/pr10316-pr10506-linear-main50-linux-validation.tar.gz), SHA-256 `1087c9bf4d5bd4e0a9c5e54b5d18e710c1e576d7de607f0adc30d41d23af67a2`. Historical previous-head evidence (not rerun on the current head): **Independent Windows regression report on the refreshed head ([ycbuzi](https://github.com/boykopovar/AnyPS5/pull/10316#issuecomment-6386339588)):** PR head `e2c058c1ff4eea220f3b1aba3f3d35d339ba82b7` was cleanly integrated onto main `fa515def85` as `b3c835692c` on Windows 11 Home 10.0.26200, MinGW-w64 GCC 15.2.0 UCRT r7, Release with `BUILD_TESTING=ON`. The reporter built `libc`, `guest_math_tests` and `guest_dinkumware_internals_tests` without reported errors or warnings; both focused groups passed twice using `ctest --repeat until-fail:2`. Their negative control replaced `Math.cpp` with main's version: `guest_math` aborted while the Dinkumware group still passed. This is attributed independent integration/regression evidence; original-module matrices and titles were not rerun. The Windows module comparisons and negative control below are historical evidence from original head `171b98173f536676ab8c8c3aba0a443ff060fb16`; Windows was not rerun on the refreshed commit. Results described as console results mean the supplied titles' bundled libraries executed on the Windows host, not measurements on a physical console. Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, `BUILD_TESTING=ON`, on #10306's head. - **Comparison:** a scratch loader runs the decrypted `libc.prx` of PPSA07923, PPSA05916 and PPSA13934 natively and supplies `__error`. It compared their `tanh` with this PR's code, checking result bits (NaN payloads included) and `errno`, on 16,068,211 arguments per title: - 16 million random arguments, half random bit patterns and half with exponents 2^-63 to 2^8; - the 8,001 floats around ±1, ±22, ±2^-28, 0.5 and 10; - every power of two from 2^-1074 to 2^1023, both signs; - the special values. Results: - PPSA07923 and PPSA05916: identical bits on all of them, and `errno` is never set. - PPSA13934: no `errno` either. The bits differ on 1,054 arguments, because that copy is an FMA-contracted build. - **`guest_math`:** the new `CheckTanhConsoleBits` has 24 cases with the bundled library's results: - ±0 and both sides of 2^-28; - 0.5, the float below 1, and ±1; - both sides of 22, −22 and 50; - `DBL_MAX`, ±`inf` and NaN payloads of both signs; - six arguments where UCRT differs. Each case also checks that `errno` stays unchanged. `guest_math` and `guest_dinkumware_internals` pass in three runs out of three on the exact head. - **Negative control:** with #10306's `Math.cpp` and these tests, `guest_math` aborts in a `Require`, while `guest_dinkumware_internals` still passes. - `python tools/check_conventions.py --base` #10306's head, and `git diff --check`, pass. - **Order:** Standalone tanh change on main, which now contains the required internal Expm1 implementation from merged #10306. - **Not tested:** macOS, a physical console, floating-point exception flags, and `tanhl`. The original module comparison and negative control were Windows-only; the new Linux build and regression checks are scoped separately above. ### 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: none; the msun function is rewritten, as #9668, #5036 and the `sinhf` entry did - [x] Generic behaviour, not specific to one title - [x] Depends on: none <!-- #PR, or none --> - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes AI-assisted (Claude Code).
harisibrar1 commented 2026-10-24 12:47:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-24T12:47:41Z.

Review: APPROVED


Checked against main 39a36bbec8 and FreeBSD release/11.4.0 lib/msun/src/s_tanh.c and s_expm1.c:

  • It merges cleanly into current main. Main still returns std::tanh, and its static Expm1 (from #10306) never touches errno, so the "no errno" claim holds.
  • The port matches s_tanh.c exactly: the thresholds, the huge+x>one shortcut, both expm1 forms, and 1-tiny at |x| >= 22.
  • My own Python transcription of 11.4 s_tanh.c and s_expm1.c reproduces all 24 expected values in CheckTanhConsoleBits, so the table agrees with the reference algorithm.
  • Since #10506 changes this same saturation branch, it may be simpler to land the two together, or in order.

Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5638288389 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10316#pullrequestreview-5638288389). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-24T12:47:41Z. **Review:** APPROVED --- Checked against main 39a36bbec8 and FreeBSD release/11.4.0 lib/msun/src/s_tanh.c and s_expm1.c: - It merges cleanly into current main. Main still returns `std::tanh`, and its static `Expm1` (from #10306) never touches errno, so the "no errno" claim holds. - The port matches `s_tanh.c` exactly: the thresholds, the `huge+x>one` shortcut, both expm1 forms, and `1-tiny` at |x| >= 22. - My own Python transcription of 11.4 `s_tanh.c` and `s_expm1.c` reproduces all 24 expected values in `CheckTanhConsoleBits`, so the table agrees with the reference algorithm. - Since #10506 changes this same saturation branch, it may be simpler to land the two together, or in order. Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
MarcZX10 commented 2026-10-24 21:12:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T21:12:36Z.

Review: CHANGES_REQUESTED


The earlier source-level review and the reported Linux/Windows exact-head tests provide good coverage of the algorithm. The remaining integration evidence is stale: this branch is based on 6c52b64, 6,887 commits behind current main, while the hosted Linux and Windows product jobs are skipped. Please refresh the branch onto current main as required by CONTRIBUTING.md, then rerun guest_math and guest_dinkumware_internals on the final head on Linux and Windows. Keep the negative control clearly tied to the final head.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5640047828 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10316#pullrequestreview-5640047828). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-24T21:12:36Z. **Review:** CHANGES_REQUESTED --- The earlier source-level review and the reported Linux/Windows exact-head tests provide good coverage of the algorithm. The remaining integration evidence is stale: this branch is based on `6c52b64`, 6,887 commits behind current `main`, while the hosted Linux and Windows product jobs are skipped. Please refresh the branch onto current `main` as required by CONTRIBUTING.md, then rerun `guest_math` and `guest_dinkumware_internals` on the final head on Linux and Windows. Keep the negative control clearly tied to the final head.
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!10316
No description provided.