Skip to content

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

Merged
2111 commits merged into
boykopovar:mainfrom
JJenkx:windows/libc-tanhf-msun
Nov 1, 2026
Merged

2111 commits merged into
boykopovar:mainfrom
JJenkx:windows/libc-tanhf-msun

Conversation

@JJenkx

@JJenkx JJenkx commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

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 d6e7ab4.

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

AI-assisted (Claude Code).

@github-actions

github-actions Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 6565 docs/dev/debt/libc.md L263
PR 10989 docs/dev/debt/libc.md L263
PR 11831 docs/dev/debt/libc.md L263
PR 12586 docs/dev/debt/libc.md L263

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 5021 docs/dev/debt/libc.md
PR 5303 docs/dev/debt/libc.md
PR 6085 core/libs/prx/libc/src/Math.cpp
PR 6295 core/libs/prx/libc/src/Math.cpp
core/libs/tests/GuestMath.cpp
PR 7081 docs/dev/debt/libc.md
PR 7386 docs/dev/debt/libc.md
PR 7447 docs/dev/debt/libc.md
PR 7715 docs/dev/debt/libc.md
PR 8150 docs/dev/debt/libc.md
PR 8965 docs/dev/debt/libc.md
PR 9340 docs/dev/debt/libc.md
PR 9371 docs/dev/debt/libc.md
PR 9447 docs/dev/debt/libc.md
PR 9526 docs/dev/debt/libc.md
PR 9863 docs/dev/debt/libc.md
PR 9929 docs/dev/debt/libc.md
PR 10324 docs/dev/debt/libc.md
PR 10405 docs/dev/debt/libc.md
PR 10729 docs/dev/debt/libc.md
PR 11332 docs/dev/debt/libc.md
PR 12682 docs/dev/debt/libc.md
PR 12688 docs/dev/debt/libc.md
PR 12700 docs/dev/debt/libc.md
PR 12965 docs/dev/debt/libc.md
PR 13793 docs/dev/debt/libc.md
PR 13809 docs/dev/debt/libc.md
PR 14212 docs/dev/debt/libc.md

@MarcZX10

Copy link
Copy Markdown
Contributor

The implementation and exhaustive comparison report are detailed, and the focused Windows guest_math/libc runs are reported on the stated base. Current upstream main is now a0900b9d; GitHub compare reports head 3a7beb54 is 26 commits behind, with merge base d6e7ab42. Please rebase onto current main and rerun the focused math checks/conventions on the final combined head when practical. Since this PR edits the same libc math source as the other in-flight math changes, please preserve their implementations and use the rebase to verify the combined result; this is not a request to change your stated PR order or ask maintainers to merge anything.

@JJenkx

JJenkx commented Oct 22, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto current main 3bbd232; head is now 5da9d16, with no conflicts. Windows (GCC 15.2.0 UCRT r7, Release) build of that exact head: guest_math and the libc tests (8) pass in three runs out of three, and tools/check_conventions.py --base origin/main passes. The other math PRs' implementations are untouched; this branch still only changes its own functions.

@JJenkx
JJenkx force-pushed the windows/libc-tanhf-msun branch from 3a7beb5 to 5da9d16 Compare October 22, 2026 20:23
@MarcZX10

Copy link
Copy Markdown
Contributor

Thanks for the exact-head Windows guest_math and eight libc-test reruns. I also checked the current comparison: upstream main has advanced from the tested base 3bbd2321 to 7c900bbc, and this head is now 14 commits behind. Please refresh the branch and rerun the focused math checks/conventions on the final combined head. Hosted conventions and the relinker/register-context checks pass; Linux/Windows product jobs are skipped, and macOS write-watch is still running. Your exhaustive title comparisons remain author-reported; I did not run them locally.

@Dom3005

Dom3005 commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

Linux run (the description lists Linux as not tested). Docker on Windows: Ubuntu 26.04, GCC 15, glibc 2.43, WSL2 kernel 6.6, Release, BUILD_TESTING=ON. Head 5da9d16 merged onto main fcd4558 (clean).

  • guest_math pass 5 of 5 (--repeat until-fail:5).
  • Baseline: with this PR's test kept and main's versions of the changed sources, the test fails on Linux too, so it covers the change against glibc as well as UCRT.

No findings.

AI-assisted: yes

@harisibrar1 harisibrar1 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.

Checked against main 3875f8c 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 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 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.

@JJenkx

JJenkx commented Oct 23, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto current main 4997726 with no conflicts; the new head is 82668d5. Exact-head Windows guest_math (GCC 15.2.0 UCRT r7, Release, fresh build directory) passes in three runs out of three, and check_conventions.py passes. #10222 is restacked on this head.

AI-assisted: yes (Claude Code).

@JJenkx

JJenkx commented Oct 23, 2026

Copy link
Copy Markdown
Contributor Author

Linux run at this PR's exact head 82668d5961 (82668d59613fa51f72510f61e4a66a16d1128b27), not merged onto anything else; its base on main is 4997726a5f.

Arch Linux, kernel 7.2.9, Ryzen 9 7950X, g++ (GCC) 16.2.1 20260810, glibc 2.44, x86-64. Release, BUILD_TESTING=ON.

  • guest_math: passed in each of 3 runs, exit code 0 each time, with no failed, skipped or not-run tests.

This is the focused test only; no title, console or floating-point exception flags were checked.

AI-assisted: yes (Claude Code).

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 24, 2026
JJenkx pushed a commit to JJenkx/AnyPS5 that referenced this pull request Oct 24, 2026
Additive conflicts with main's newer libc changes: the debt list keeps both
sides' entries (per entry) and GuestMath.cpp keeps both sides' test
functions and calls.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@JJenkx

JJenkx commented Oct 24, 2026

Copy link
Copy Markdown
Contributor Author

Merged current main fe204e1a8d into this branch as 776a58906a (was 82668d5961), a merge commit rather than a rebase. Done on Linux while the Windows session is paused.

  • The conflicts were additive: the docs/dev/debt/libc.md list keeps both sides' entries (checked per entry), and GuestMath.cpp keeps both sides' test functions and calls.
  • New head, Linux only: guest_math and guest_math_exact pass (Arch Linux, kernel 7.2.9, GCC 16.2.1, glibc 2.44, Release, BUILD_TESTING=ON). tools/check_conventions.py --base origin/main reports no errors.
  • Earlier Windows and comparison evidence in this thread stays pinned to the heads it was measured on.

AI-assisted: yes (Claude Code)

@github-actions github-actions Bot added needs rebase Conflicts with main, rebase needed and removed needs rebase Conflicts with main, rebase needed labels Oct 24, 2026
@JJenkx
JJenkx force-pushed the windows/libc-tanhf-msun branch from 8af97c6 to 3c22f70 Compare October 25, 2026 09:04
@JJenkx

JJenkx commented Oct 25, 2026

Copy link
Copy Markdown
Contributor Author

Refreshed onto main da6c581a02db as 3c22f70a81fd: one topic commit, no merge commits. The reviewed helper, tanhf implementation and regression function are unchanged from the previous public head.

Fresh Linux: all four focused groups pass 3/3 before and after the expected main-Math negative failure; exact-math/Dinkumware controls pass, restoration is byte-identical, and conventions/whitespace pass. The description links exact-head logs and preserves historical independent reports with their measured pins. No new Windows/module/title rerun is claimed. AI-assisted: yes.

adriadam10 and others added 25 commits October 30, 2026 11:56
…entory-exports-20261030

feat(audiodec): add missing inventory exports
…ry-exports-20261030

feat(font): add missing font inventory exports
…ngengine-inventory-exports-20261030

feat(videostreamingengine): add missing named inventory exports
…dation-20261029

fix(ngs2): validate grain size in 64-sample units
…el-input-20261029

fix(tools): validate PR records before labeling
…ranges-20261029

fix(tools): bound ELF annotation table reads
…ring-bounds-20261029

fix(tools): bound sampler COFF string reads
…shell-inventory-exports-20261030

feat(usbstorage): add missing shell inventory exports
…ngp-inventory-exports-20261030

feat(videorecordingp): add remaining inventory exports
…de-parameter

feat(libSceGameLiveStreaming): implement sceGameLiveStreamingScreenInitializeSeparateModeParameter
…exports

feat(libScePSNowGkp): declare 5 callable exports
feat(libSceMbus): declare 4 callable exports
feat(libSceHmd): declare 19 callable exports
…inventory-exports-20261030

feat(keyboard): add missing debug inventory export
…command-buffer-release

fix(agc): free a completed batch's command buffer instead of keeping it
…nventory-exports-20261030

feat(mbus): add missing debug inventory export
…parate-mode-parameter

feat(libSceSocialScreen): implement sceSocialScreenInitializeSeparateModeParameter
feat(libSceCesCs): add the ISO-2022-JP profiles, context and string conversions
…ext-320-exports

feat(libSceNpSpeechToText): declare 7 callable exports
…t-dialog-320-exports

feat(libSceConnectAccountDialog): declare 7 callable exports
…ument-codes

fix(libSceNpUniversalDataSystem): return the console's codes for bad event and property arguments
…king-memory-size

feat(libSceMoveTracker): implement sceMoveTrackerGetWorkingMemorySize
…blocking-count

docs(libSceAudioOut): count the blocking values passed to sceAudioOut2ContextPush
…mory

feat(libSceVrHand): implement sceVrHandQueryMemory
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 30, 2026
@JJenkx

JJenkx commented Oct 30, 2026

Copy link
Copy Markdown
Contributor Author

Linux run at this PR's exact head a06ed844e3 (a06ed844e352d33f89f6ed015da3203745330acd), not merged onto anything else; its base on main is e471817c68.

Arch Linux, kernel 7.2.9, Ryzen 9 7950X, g++ (GCC) 16.2.1 20260810, glibc 2.44, x86-64. Release, BUILD_TESTING=ON, Ninja -j4, private worktree.

  • ctest -R '^guest_math$': passes in each of 3 runs (3 passes).

@JJenkx

JJenkx commented Oct 30, 2026

Copy link
Copy Markdown
Contributor Author

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).

@oneandonlydean oneandonlydean closed this pull request by merging all changes into boykopovar:main in 88b7503 Nov 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.