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

Merged
JJenkx merged 2111 commits from upstream-pr/9867 into main 2026-11-01 01:57:51 +00:00
JJenkx commented 2026-10-22 19:44:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-22T19:44:58Z.


What

tanhf returned the host's tanh, so its last bit depended on the host. On Windows (UCRT) it differs from the libc.prx that titles ship on 69,671,910 of the 2^32 floats. For example, the console gives tanhf(0x3f000555) = 0x3eeca303 and tanhf(0xbf400ddd) = 0xbf22a163.

The titles' tanhf (NID SAd0Z3wKwLA) is FreeBSD msun's s_tanhf.c, which calls msun's s_expm1f.c. This PR adds:

  • tanhf: 1 - 2/(expm1f(2|x|) + 2) for |x| >= 1, -t/(t + 2) with t = expm1f(-2|x|) below 1, ±1 from 9 up, x for tiny arguments, and 1/x ± 1 for the infinities and NaN;
  • TanhfExpm1: a full port of expm1f for it, with huge read at run time. The existing SinhfExpm1 only covers the positive arguments sinhf passes, and tanhf also needs negative ones.

The exported expm1f is unchanged. None of the copies checked sets errno here, and neither does this.

tanhf is imported by 3 of the 22 titles whose relinker registries I audited: PPSA02630, PPSA16064 and PPSA25872.

Tested

Current Linux refresh: head 3c22f70a81fdc116e8a22df82791b3295e9011ef, one topic commit directly on main da6c581a02dbd3983ccc70fff92169acce802f77, without merge commits. The reviewed TanhfExpm1, tanhf_nid_postfix and CheckTanhfConsoleBits are unchanged from previous public head 8af97c6fb0309fa6c583c1a4d1f618c93ebb7a76. Current main math, test calls, canonical public declarations and debt entries are retained. Depends on remains none.

Fresh Linux x86-64 Release validation: guest_math, guest_math_exact, guest_dinkumware_internals and public_headers each pass three consecutive runs, then all four pass three more after negative-control restoration. With only production Math.cpp replaced by main, guest_math aborts as expected while exact-math and Dinkumware controls pass. Restoring the candidate produces a byte-identical libc.prx. Conventions and whitespace checks pass.

Exact-head Linux logs, negative control and preservation proof (SHA-256 c72326ce62ebac6d06a6e4a35c66122ec79c48fdf1b24413e5553765182039a5). The other stack head in the preservation/config receipt is local preparation for #10222, not a claim that its separately requested #10226 integration qualification is complete.

Historical Windows/module measurements and independent review reports: retained below with their original measured revisions. No new Windows, title, console, exception-flag or performance claim.

Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, BUILD_TESTING=ON, main d6e7ab42df.

  • Exhaustive comparison: a scratch loader runs the decrypted libc.prx of PPSA07923, PPSA05916 and PPSA13934 natively and supplies __error. It compared their tanhf with this build's tanhf_nid_postfix for all 2^32 arguments.
    • PPSA07923 and PPSA05916: identical bits everywhere.
    • PPSA13934: its FMA-contracted copy differs by up to 3 ulp on 150,846 arguments, so no single port matches every title's last bit (as with #9771, #9839 and #9841).
    • No copy sets errno.
  • guest_math: the new CheckTanhfConsoleBits has 22 cases with the console's results, each also checking that errno is untouched: values in each expm1f range where UCRT differs, of both signs, the tiny shortcut, ±0, the smallest subnormal, ±1, the edges at 9, FLT_MAX, the infinities and NaN payloads of both signs. guest_math and the libc tests (8) pass in three runs out of three. With main's Math.cpp and this test, guest_math aborts in a Require.
  • python tools/check_conventions.py --base origin/main and git diff --check pass. The branch merges cleanly with my other math PRs (#9771, #9777, #9784, #9839, #9841, #9844, #9850, #9857, #9862). tools/pr_overlap.py finds one adjacency in docs/dev/debt/libc.md with #9364 (next to the rename entry); keeping both entries resolves it.
  • Independent Linux review: Dom3005's report tested head 5da9d1614 merged cleanly onto main fcd455870: Ubuntu 26.04 in Docker on Windows, GCC 15, glibc 2.43, WSL2 kernel 6.6, Release, BUILD_TESTING=ON. The reviewer reports five passing guest_math runs (--repeat until-fail:5). Keeping this PR's test while restoring main's changed production sources makes the test fail on Linux as well. This supplies a Linux regression comparison in addition to the Windows evidence above; it is reviewer-supplied evidence for those revisions, not a local rerun or a latest-main claim.
  • Not tested: macOS, a console, the floating-point exception flags.

Current Linux qualification (2026-10-28)

Rebased onto main 2227f1366188cda5699dd20e2e756a18006a5d4b. Linux CPU validation on 2026-10-28 at 56b82ac96e6136cd4d10648f8174dda62cdfd9cb (Arch Linux 7.2.9, AMD Ryzen 9 7950X, GCC 16.2.1, Ninja Release, BUILD_TESTING=ON).

libc, guest_math_tests and guest_math_exact_tests rebuilt from this exact source. guest_math and guest_math_exact each passed three consecutive runs (6/6, no skips); the same two suites each passed three runs on the pinned main (6/6). Conventions and whitespace checks pass.

Exact source pins, preservation proofs, dependency checks, build logs and JUnit, SHA-256 1176269c9e6948b5252a5db9e660978d7f978fb2d9f4ed6c68bc050b10b5ae68. This is focused Linux CPU qualification; earlier Windows/title/reference measurements retain their original pins. No new title or performance result is claimed.

Qualification update — 2026-10-30, repeat 2

Integrated pinned main e471817c68d4e1b1e44b0ce369ed88bafe4ae8ed at a06ed844e352d33f89f6ed015da3203745330acd, retaining original topic commits as ancestors.

Actual Linux libc/focused binaries rebuilt from this final head; guest_math, guest_math_exact, guest_dinkumware_internals each pass three consecutive runs (9 runtime passes), plus public_headers. Conventions against current origin/main and whitespace pass. Pinned main passed all seven runtime groups plus the header audit three times (24 runs). Full source/function/test/debt/header and 31 recursive dependency / 17,742 tracked-blob proofs are attached. Later main 1c6b316cf762 has 17 off-topic commits, no relevant libc/numeric/qsort/gitlink changes, and all 9 merge previews are clean; those preview trees were not executed.

Focused Linux CPU scope only: prior Windows/SDK/reference reports keep original pins. No fresh Windows, console, SDK-module, title/GPU or speed measurement is claimed by this report. AI-assisted: yes. Evidence: https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/owned-math9-maine471-linux-20261030-repeat2.tar.gz (SHA256 5da4ce1732b45bb3fc566c31078c8c9ae8bf0c403654dfc60d15ee28490e20bf).

AI-assisted: yes (Codex).

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 functions are 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:4973155474 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9867). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-22T19:44:58Z. --- ### What `tanhf` returned the host's `tanh`, so its last bit depended on the host. On Windows (UCRT) it differs from the `libc.prx` that titles ship on 69,671,910 of the 2^32 floats. For example, the console gives `tanhf(0x3f000555)` = `0x3eeca303` and `tanhf(0xbf400ddd)` = `0xbf22a163`. The titles' `tanhf` (NID `SAd0Z3wKwLA`) is FreeBSD msun's [`s_tanhf.c`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/msun/src/s_tanhf.c), which calls msun's [`s_expm1f.c`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/msun/src/s_expm1f.c). This PR adds: - **`tanhf`:** `1 - 2/(expm1f(2|x|) + 2)` for `|x| >= 1`, `-t/(t + 2)` with `t = expm1f(-2|x|)` below 1, `±1` from 9 up, `x` for tiny arguments, and `1/x ± 1` for the infinities and NaN; - **`TanhfExpm1`:** a full port of `expm1f` for it, with `huge` read at run time. The existing `SinhfExpm1` only covers the positive arguments `sinhf` passes, and `tanhf` also needs negative ones. The exported `expm1f` is unchanged. None of the copies checked sets `errno` here, and neither does this. `tanhf` is imported by 3 of the 22 titles whose relinker registries I audited: PPSA02630, PPSA16064 and PPSA25872. ### Tested **Current Linux refresh:** head `3c22f70a81fdc116e8a22df82791b3295e9011ef`, one topic commit directly on main `da6c581a02dbd3983ccc70fff92169acce802f77`, without merge commits. The reviewed `TanhfExpm1`, `tanhf_nid_postfix` and `CheckTanhfConsoleBits` are unchanged from previous public head `8af97c6fb0309fa6c583c1a4d1f618c93ebb7a76`. Current main math, test calls, canonical public declarations and debt entries are retained. Depends on remains none. Fresh Linux x86-64 Release validation: `guest_math`, `guest_math_exact`, `guest_dinkumware_internals` and `public_headers` each pass three consecutive runs, then all four pass three more after negative-control restoration. With only production `Math.cpp` replaced by main, `guest_math` aborts as expected while exact-math and Dinkumware controls pass. Restoring the candidate produces a byte-identical `libc.prx`. Conventions and whitespace checks pass. [Exact-head Linux logs, negative control and preservation proof](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/pr9867-head3c22f70a-mainda6c-linux-validation.tar.gz) (SHA-256 `c72326ce62ebac6d06a6e4a35c66122ec79c48fdf1b24413e5553765182039a5`). The other stack head in the preservation/config receipt is local preparation for #10222, not a claim that its separately requested #10226 integration qualification is complete. **Historical Windows/module measurements and independent review reports:** retained below with their original measured revisions. No new Windows, title, console, exception-flag or performance claim. Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, `BUILD_TESTING=ON`, `main` d6e7ab42df. - **Exhaustive comparison:** a scratch loader runs the decrypted `libc.prx` of PPSA07923, PPSA05916 and PPSA13934 natively and supplies `__error`. It compared their `tanhf` with this build's `tanhf_nid_postfix` for all 2^32 arguments. - PPSA07923 and PPSA05916: identical bits everywhere. - PPSA13934: its FMA-contracted copy differs by up to 3 ulp on 150,846 arguments, so no single port matches every title's last bit (as with #9771, #9839 and #9841). - No copy sets `errno`. - **`guest_math`:** the new `CheckTanhfConsoleBits` has 22 cases with the console's results, each also checking that `errno` is untouched: values in each `expm1f` range where UCRT differs, of both signs, the tiny shortcut, ±0, the smallest subnormal, ±1, the edges at 9, `FLT_MAX`, the infinities and NaN payloads of both signs. `guest_math` and the libc tests (8) pass in three runs out of three. With `main`'s `Math.cpp` and this test, `guest_math` aborts in a `Require`. - `python tools/check_conventions.py --base origin/main` and `git diff --check` pass. The branch merges cleanly with my other math PRs (#9771, #9777, #9784, #9839, #9841, #9844, #9850, #9857, #9862). `tools/pr_overlap.py` finds one adjacency in `docs/dev/debt/libc.md` with #9364 (next to the `rename` entry); keeping both entries resolves it. - **Independent Linux review:** [Dom3005's report](https://github.com/boykopovar/AnyPS5/pull/9867#issuecomment-6353484483) tested head `5da9d1614` merged cleanly onto `main` `fcd455870`: Ubuntu 26.04 in Docker on Windows, GCC 15, glibc 2.43, WSL2 kernel 6.6, Release, `BUILD_TESTING=ON`. The reviewer reports five passing `guest_math` runs (`--repeat until-fail:5`). Keeping this PR's test while restoring main's changed production sources makes the test fail on Linux as well. This supplies a Linux regression comparison in addition to the Windows evidence above; it is reviewer-supplied evidence for those revisions, not a local rerun or a latest-main claim. - Not tested: macOS, a console, the floating-point exception flags. <!-- codex-math-refresh-20261028-repeat-pr9867 --> ### Current Linux qualification (2026-10-28) Rebased onto `main` `2227f1366188cda5699dd20e2e756a18006a5d4b`. Linux CPU validation on 2026-10-28 at `56b82ac96e6136cd4d10648f8174dda62cdfd9cb` (Arch Linux 7.2.9, AMD Ryzen 9 7950X, GCC 16.2.1, Ninja Release, BUILD_TESTING=ON). `libc`, `guest_math_tests` and `guest_math_exact_tests` rebuilt from this exact source. `guest_math` and `guest_math_exact` each passed three consecutive runs (6/6, no skips); the same two suites each passed three runs on the pinned main (6/6). Conventions and whitespace checks pass. [Exact source pins, preservation proofs, dependency checks, build logs and JUnit](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/owned-math25-main2227-linux-20261028-repeat.tar.gz), SHA-256 `1176269c9e6948b5252a5db9e660978d7f978fb2d9f4ed6c68bc050b10b5ae68`. This is focused Linux CPU qualification; earlier Windows/title/reference measurements retain their original pins. No new title or performance result is claimed. <!-- codex-maintenance-20261030-repeat2-9867 --> **Qualification update — 2026-10-30, repeat 2** Integrated pinned main `e471817c68d4e1b1e44b0ce369ed88bafe4ae8ed` at `a06ed844e352d33f89f6ed015da3203745330acd`, retaining original topic commits as ancestors. Actual Linux libc/focused binaries rebuilt from this final head; `guest_math`, `guest_math_exact`, `guest_dinkumware_internals` each pass three consecutive runs (9 runtime passes), plus public_headers. Conventions against current origin/main and whitespace pass. Pinned main passed all seven runtime groups plus the header audit three times (24 runs). Full source/function/test/debt/header and 31 recursive dependency / 17,742 tracked-blob proofs are attached. Later main `1c6b316cf762` has 17 off-topic commits, no relevant libc/numeric/qsort/gitlink changes, and all 9 merge previews are clean; those preview trees were not executed. Focused Linux CPU scope only: prior Windows/SDK/reference reports keep original pins. No fresh Windows, console, SDK-module, title/GPU or speed measurement is claimed by this report. AI-assisted: yes. Evidence: https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/owned-math9-maine471-linux-20261030-repeat2.tar.gz (SHA256 5da4ce1732b45bb3fc566c31078c8c9ae8bf0c403654dfc60d15ee28490e20bf). AI-assisted: yes (Codex). ### 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 functions are rewritten, as #9668, #5036 and the `sinhf` entry did - [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 AI-assisted (Claude Code).
harisibrar1 commented 2026-10-23 03:19:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-23T03:19:54Z.

Review: APPROVED


Checked against main 3875f8c8ff and FreeBSD msun s_tanhf.c/s_expm1f.c at release/9.0.0, 10.0.0, 11.4.0, 12.0.0 and 13.0.0:

  • The port matches release/11.4.0 line by line: volatile tiny in tanhf, run-time huge and the (u_int32_t)(0x7f+k)<<23 twopk in expm1f. 9.0.0 and 10.0.0 differ only in qualifiers and that twopk encoding, which give the same bits over tanhf's argument range, so the 11.4.0 links are right.
  • A float32 emulation of msun's tanhf reproduces all 22 expected values in CheckTanhfConsoleBits, and a correctly-rounded tanh misses 6 of the 7 non-trivial ones, so the test separates the two.
  • It still merges cleanly with current main.

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

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5623505919 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9867#pullrequestreview-5623505919). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-23T03:19:54Z. **Review:** APPROVED --- Checked against main 3875f8c8ff and FreeBSD msun `s_tanhf.c`/`s_expm1f.c` at release/9.0.0, 10.0.0, 11.4.0, 12.0.0 and 13.0.0: - The port matches release/11.4.0 line by line: volatile `tiny` in tanhf, run-time `huge` and the `(u_int32_t)(0x7f+k)<<23` twopk in expm1f. 9.0.0 and 10.0.0 differ only in qualifiers and that twopk encoding, which give the same bits over tanhf's argument range, so the 11.4.0 links are right. - A float32 emulation of msun's tanhf reproduces all 22 expected values in `CheckTanhfConsoleBits`, and a correctly-rounded tanh misses 6 of the 7 non-trivial ones, so the test separates the two. - It still merges cleanly with current main. Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
MarcZX10 commented 2026-10-23 03:44:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-23T03:44:01Z.

Review: APPROVED


Reviewed exact head 5da9d16147a3bc1324ddf5ba22a210c9ed57b002. The tanhf/expm1f port matches FreeBSD 11.4 msun's control flow, and the edge tests cover signs, small values, infinities, NaNs, errno preservation, and key boundaries. The source and test files are unchanged between the tested base and current upstream main; only the adjacent debt document changed.

I independently ran the PR's 22-case CheckTanhfConsoleBits on Linux using the exact PR Math.cpp and test source. The focused check passed. The available local full guest_math CTest build uses an older source snapshot and aborts first at its strtol check, so I am not counting that as a full-suite pass. Independent reviewer evidence on this exact PR head reports five passing Linux guest_math runs; the author reports repeated Windows runs and exhaustive bit comparison. Product CI jobs remain skipped.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5623632682 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9867#pullrequestreview-5623632682). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-23T03:44:01Z. **Review:** APPROVED --- Reviewed exact head `5da9d16147a3bc1324ddf5ba22a210c9ed57b002`. The `tanhf`/`expm1f` port matches FreeBSD 11.4 msun's control flow, and the edge tests cover signs, small values, infinities, NaNs, errno preservation, and key boundaries. The source and test files are unchanged between the tested base and current upstream `main`; only the adjacent debt document changed. I independently ran the PR's 22-case `CheckTanhfConsoleBits` on Linux using the exact PR `Math.cpp` and test source. The focused check passed. The available local full `guest_math` CTest build uses an older source snapshot and aborts first at its `strtol` check, so I am not counting that as a full-suite pass. Independent reviewer evidence on this exact PR head reports five passing Linux `guest_math` runs; the author reports repeated Windows runs and exhaustive bit comparison. Product CI jobs remain skipped.
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!9867
No description provided.