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

Merged
JJenkx merged 7 commits from upstream-pr/10315 into main 2026-11-05 16:13:56 +00:00
JJenkx commented 2026-10-23 17:29:50 +00:00 (Migrated from github.com)

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


What

atanh returned the host's atanh, so its last bit and its errno depended on the host. For example, atanh(0x3f03a67d5567227b) is 0x3f03a67d558ea76e in the bundled libc.prx and 0x3f03a67d558ea76f on Windows (UCRT). On 20 million random arguments, UCRT's atanh differs from the titles' copy on 1,841,966.

The titles' copy (NID YjbpxXpi6Zk) is FreeBSD msun's e_atanh.c, computed through msun's log1p. atanh now computes it over log1p_nid_postfix, the msun port supplied by the already merged #10314:

  • the domain checks;
  • the 2^-28 shortcut;
  • the |x| < 0.5 form 0.5 * log1p(2x + 2x^2 / (1 - x));
  • the 0.5 * log1p(2x / (1 - x)) form above it.

errno: msun sets none. The titles' copy sets it at msun's two error returns:

  • EDOM at (x - x) / (x - x), for |x| > 1, the infinities and every NaN, keeping NaN payloads;
  • ERANGE at x / 0, for ±1.

This is the same rule as the bundled library's atanhf already in the debt file. The internal log1p calls always get arguments ≥ 0, so log1p's own EDOM never fires from here.

Tested

Current-main refresh — 2026-11-04. Head 9811e07c2f421d3ea034cb7f3388a006dc354c11 merges main 9ab6a281ea61c9e62a7eb07eb4cef8f19ede1c5c while retaining the prior public head as an ancestor. Main’s newer code, regression fixtures and debt entries are preserved alongside this topic.

Linux x86-64, GCC16.2.1, Ninja Release: actual libc, libkernel and libSceAmpr CMake owners rebuilt coherently. guest_math, guest_math_exact, guest_dinkumware_internals each pass three consecutive runs (9 passes, zero skips). The same groups pass three times on pinned main. Public-header audit and conventions pass. Original licence-notice trailing whitespace is retained where present. The scoped recipe reuses frozen ZArchive/zstd providers; this is not a whole-project build. No new Windows, bundled-SDK, physical-console, title/GPU or FPS measurement is claimed; earlier reports retain their original pins.

Exact source/parent proofs, per-branch patches/input manifests, build and CTest logs/JUnit, library/executable hashes and scoped CMake recipe, SHA-256 97a2377bc996be1690e8853e1ea090a98b5ed808bcc37a15a5cdada5c2e00845 (download verified). AI-assisted: yes (Codex).

Current-main integration — 2026-11-03. Head 7c767c439fb25a28415020465d9f727ecf964fe5 includes main 4f41f4c375f5abb487dbd184fbc150f31994d762 and retains the prior public history and attribution/licence notices. Main’s newer math implementations and fixtures remain alongside this topic.

Linux x86-64 Release, GCC 16.2.1, Ninja, BUILD_TESTING=ON: rebuilt actual libc and math executables. guest_math, guest_math_exact, and guest_dinkumware_internals each pass 3/3 (9 passes, zero skips); public_headers and conventions pass. The same three groups pass 3/3 plus headers on pinned main. All 34 recursive dependency trees and 31,551 tracked blobs verify with zero errors; the update=none QuickJS test262 corpus is excluded from these library inputs. git diff --check reports the preserved verbatim licence trailing-space line; no other whitespace warnings occur.

Source/parent/gitlink proofs, exact library/test hashes, raw logs/JUnit, and head-leased push receipts: qualification archive, SHA-256 e6a50522bc28e30871fdc2c12a88d2537515914239e8166f6237f48895126a06 (download verified). That Linux archive makes no Windows/macOS, SDK-module, console, title/GPU or performance claim; older platform/reference reports retain their original pins. AI-assisted: yes (Codex). The prior Windows/negative-control head remains 0a95fd8f78c6b9c57c5a16212b48b3b01cbe0b0b; relevant atanh/log1p routines, edge fixtures, Dinkumware source/test and VABI header are byte-identical, that continuity proof remains separately scoped. Fresh final-head Linux and Windows controls are recorded below.

Fresh current-head Windows qualification — 2026-11-03: exact 7c767 Windows receipt records 12/12 initial math/exact/Dinkumware/header passes, a frozen-test body-only std::atanh negative at CheckAtanhConsoleBits, then 12/12 restored passes, zero skips. Actual module binding, 37,044 source/provider blobs and raw hashes are verified. Raw PE hashes differ; only timestamp/checksum fields are zeroed in a separate in-memory comparison, with all other bytes identical. This is not a fresh Linux negative or a new SDK-module/console/title/performance/full-suite result. AI-assisted: yes (Sol6.1medium).

Fresh current-head Linux negative/restoration — 2026-11-03: On the same exact 7c767c439fb25a28415020465d9f727ecf964fe5 head, the three math/exact/Dinkumware groups pass three times each (9). Replacing only the double atanh_nid_postfix body with return std::atanh(x); fails the unchanged CheckAtanhConsoleBits bit assertion at GuestMath.cpp:1417. Restoring the exact source returns all three groups to three passes each (9), plus public_headers, zero skips. The library and all three test executables have identical raw SHA-256s before and after restoration. Both 31,551-provider-blob audits match; source is clean and the actual private libc.prx loader path is recorded. Verified Linux control/restoration logs, JUnit and fingerprints, SHA-256 4cf51299cc068b43477322860d635451ba2450a478ea6b865af3e85b18c0ca5c. This supplies a fresh Linux final-tree negative separately from the historical controls; no new SDK-module, console, title or performance comparison. AI-assisted: yes (Codex).

Earlier dated qualifications below retain their recorded source/platform pins.

Historical head 0a95fd8f78c6b9c57c5a16212b48b3b01cbe0b0b merged main 92d2fdc3438f83e39d624eb65f3195bf2701568c into the existing branch, without rebasing or force-pushing. The conflict resolution retains both this PR's atanh test calls/debt entry and main's log10/log2 calls/debt entries. The atanh implementation is byte-identical to prior public head 72a6683e64531e4fc948e6115af96037b367ab5e.

Linux x86-64, Arch 7.2.11-arch1-1, Ryzen 9 7950X, GCC 16.2.1, Ninja Release, BUILD_TESTING=ON, pinned dependency worktrees. Rebuilt actual libc and the math/exact-math/Dinkumware targets. guest_math, guest_math_exact, guest_dinkumware_internals, public_headers: 4/4 pass, zero skips.

Final-tree negative control: the same frozen math executable aborts under GDB at CheckAtanhConsoleBits when only atanh_nid_postfix is replaced with the pre-topic return std::atanh(x) wrapper. All other production functions and every test are retained. Exact source restoration rebuilds libc byte-for-byte identically; all four checks pass again. Conventions and whitespace checks pass; worktree clean.

Fresh build/control logs, JUnit, hashes and exact diff, SHA-256 abd0345d33231e5b624db479c7e01b85cca84b5329598e643a61595b4f78f85f.

Earlier reference measurements retain their original source pins and scope; see prior exact-head qualification. At historical head 0a95fd8, Windows qualification and the supported-behavior host-wrapper negative control were complete; see that Windows follow-up for the exact pins, 12/12 initial and 12/12 restored passes, loaded-module path and verified artifact. No new bundled-library reference, console, title/GPU or performance execution is claimed. The separate #10565 follow-up addresses matching x87 exception flags; that behavior is not added here.

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 (#10314 is merged)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a (CPU-only libc change)
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4986744292 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10315). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-23T17:29:50Z. --- ### What `atanh` returned the host's `atanh`, so its last bit and its `errno` depended on the host. For example, `atanh(0x3f03a67d5567227b)` is `0x3f03a67d558ea76e` in the bundled `libc.prx` and `0x3f03a67d558ea76f` on Windows (UCRT). On 20 million random arguments, UCRT's `atanh` differs from the titles' copy on 1,841,966. The titles' copy (NID `YjbpxXpi6Zk`) is FreeBSD msun's [`e_atanh.c`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/msun/src/e_atanh.c), computed through msun's `log1p`. `atanh` now computes it over `log1p_nid_postfix`, the msun port supplied by the already merged #10314: - the domain checks; - the `2^-28` shortcut; - the `|x| < 0.5` form `0.5 * log1p(2x + 2x^2 / (1 - x))`; - the `0.5 * log1p(2x / (1 - x))` form above it. **`errno`:** msun sets none. The titles' copy sets it at msun's two error returns: - `EDOM` at `(x - x) / (x - x)`, for |x| > 1, the infinities and every NaN, keeping NaN payloads; - `ERANGE` at `x / 0`, for ±1. This is the same rule as the bundled library's `atanhf` already in the debt file. The internal `log1p` calls always get arguments ≥ 0, so `log1p`'s own `EDOM` never fires from here. ### Tested <!-- libc-main9ab-refresh-20261104-10315 --> **Current-main refresh — 2026-11-04.** Head `9811e07c2f421d3ea034cb7f3388a006dc354c11` merges main `9ab6a281ea61c9e62a7eb07eb4cef8f19ede1c5c` while retaining the prior public head as an ancestor. Main’s newer code, regression fixtures and debt entries are preserved alongside this topic. Linux x86-64, GCC16.2.1, Ninja Release: actual libc, libkernel and libSceAmpr CMake owners rebuilt coherently. `guest_math`, `guest_math_exact`, `guest_dinkumware_internals` each pass three consecutive runs (**9 passes, zero skips**). The same groups pass three times on pinned main. Public-header audit and conventions pass. Original licence-notice trailing whitespace is retained where present. The scoped recipe reuses frozen ZArchive/zstd providers; this is not a whole-project build. No new Windows, bundled-SDK, physical-console, title/GPU or FPS measurement is claimed; earlier reports retain their original pins. [Exact source/parent proofs, per-branch patches/input manifests, build and CTest logs/JUnit, library/executable hashes and scoped CMake recipe](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/owned-libc11-main9ab-linux-20261104.tar.gz), SHA-256 `97a2377bc996be1690e8853e1ea090a98b5ed808bcc37a15a5cdada5c2e00845` (download verified). AI-assisted: yes (Codex). **Current-main integration — 2026-11-03.** Head `7c767c439fb25a28415020465d9f727ecf964fe5` includes main `4f41f4c375f5abb487dbd184fbc150f31994d762` and retains the prior public history and attribution/licence notices. Main’s newer math implementations and fixtures remain alongside this topic. Linux x86-64 Release, GCC 16.2.1, Ninja, BUILD_TESTING=ON: rebuilt actual libc and math executables. `guest_math`, `guest_math_exact`, and `guest_dinkumware_internals` each pass 3/3 (**9 passes, zero skips**); `public_headers` and conventions pass. The same three groups pass 3/3 plus headers on pinned main. All 34 recursive dependency trees and 31,551 tracked blobs verify with zero errors; the update=none QuickJS test262 corpus is excluded from these library inputs. `git diff --check` reports the preserved verbatim licence trailing-space line; no other whitespace warnings occur. Source/parent/gitlink proofs, exact library/test hashes, raw logs/JUnit, and head-leased push receipts: [qualification archive](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/math15-main4f41-linux-20261103.tar.gz), SHA-256 `e6a50522bc28e30871fdc2c12a88d2537515914239e8166f6237f48895126a06` (download verified). That Linux archive makes no Windows/macOS, SDK-module, console, title/GPU or performance claim; older platform/reference reports retain their original pins. AI-assisted: yes (Codex). The prior Windows/negative-control head remains `0a95fd8f78c6b9c57c5a16212b48b3b01cbe0b0b`; relevant atanh/log1p routines, edge fixtures, Dinkumware source/test and VABI header are byte-identical, that continuity proof remains separately scoped. Fresh final-head Linux and Windows controls are recorded below. **Fresh current-head Windows qualification — 2026-11-03:** [exact 7c767 Windows receipt](https://github.com/boykopovar/AnyPS5/pull/10315#issuecomment-6605247501) records 12/12 initial math/exact/Dinkumware/header passes, a frozen-test body-only `std::atanh` negative at `CheckAtanhConsoleBits`, then 12/12 restored passes, zero skips. Actual module binding, 37,044 source/provider blobs and raw hashes are verified. Raw PE hashes differ; only timestamp/checksum fields are zeroed in a separate in-memory comparison, with all other bytes identical. This is not a fresh Linux negative or a new SDK-module/console/title/performance/full-suite result. AI-assisted: yes (Sol6.1medium). **Fresh current-head Linux negative/restoration — 2026-11-03:** On the same exact `7c767c439fb25a28415020465d9f727ecf964fe5` head, the three math/exact/Dinkumware groups pass three times each (9). Replacing only the double `atanh_nid_postfix` body with `return std::atanh(x);` fails the unchanged `CheckAtanhConsoleBits` bit assertion at `GuestMath.cpp:1417`. Restoring the exact source returns all three groups to three passes each (9), plus `public_headers`, zero skips. The library and all three test executables have identical raw SHA-256s before and after restoration. Both 31,551-provider-blob audits match; source is clean and the actual private `libc.prx` loader path is recorded. [Verified Linux control/restoration logs, JUnit and fingerprints](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/pr10315-7c767-linux-negative-restored-20261103.tar.gz), SHA-256 `4cf51299cc068b43477322860d635451ba2450a478ea6b865af3e85b18c0ca5c`. This supplies a fresh Linux final-tree negative separately from the historical controls; no new SDK-module, console, title or performance comparison. AI-assisted: yes (Codex). Earlier dated qualifications below retain their recorded source/platform pins. <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Historical head `0a95fd8f78c6b9c57c5a16212b48b3b01cbe0b0b` merged main `92d2fdc3438f83e39d624eb65f3195bf2701568c` into the existing branch, without rebasing or force-pushing. The conflict resolution retains both this PR's atanh test calls/debt entry and main's log10/log2 calls/debt entries. The atanh implementation is byte-identical to prior public head `72a6683e64531e4fc948e6115af96037b367ab5e`. Linux x86-64, Arch 7.2.11-arch1-1, Ryzen 9 7950X, GCC 16.2.1, Ninja Release, `BUILD_TESTING=ON`, pinned dependency worktrees. Rebuilt actual libc and the math/exact-math/Dinkumware targets. **`guest_math`, `guest_math_exact`, `guest_dinkumware_internals`, `public_headers`: 4/4 pass, zero skips.** Final-tree negative control: the same frozen math executable aborts under GDB at `CheckAtanhConsoleBits` when only `atanh_nid_postfix` is replaced with the pre-topic `return std::atanh(x)` wrapper. All other production functions and every test are retained. Exact source restoration rebuilds libc byte-for-byte identically; all four checks pass again. Conventions and whitespace checks pass; worktree clean. [Fresh build/control logs, JUnit, hashes and exact diff](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/pr10315-main92d2-0a95fd8f78-linux-20261102.tar.gz), SHA-256 `abd0345d33231e5b624db479c7e01b85cca84b5329598e643a61595b4f78f85f`. Earlier reference measurements retain their original source pins and scope; see [prior exact-head qualification](https://github.com/boykopovar/AnyPS5/pull/10315#issuecomment-6469148275). At historical head `0a95fd8`, Windows qualification and the supported-behavior host-wrapper negative control were complete; see [that Windows follow-up](https://github.com/boykopovar/AnyPS5/pull/10315#issuecomment-6586016517) for the exact pins, 12/12 initial and 12/12 restored passes, loaded-module path and verified artifact. No new bundled-library reference, console, title/GPU or performance execution is claimed. The separate #10565 follow-up addresses matching x87 exception flags; that behavior is not added here. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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 (#10314 is merged) - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a (CPU-only libc change) - [x] AI-assisted: yes
harisibrar1 commented 2026-10-24 12:46:33 +00:00 (Migrated from github.com)

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

Review: COMMENTED


Checked against main 39a36bbec8 and FreeBSD release/11.4.0 lib/msun/src/e_atanh.c (over s_log1p.c):

  • The port matches e_atanh.c: the (ix|((lx|-lx)>>31)) domain test, the x/zero pole, the 2^-28 shortcut and both log1p forms. EDOM and ERANGE are added only at msun's two error returns.
  • I wrote my own Python port of e_atanh.c over s_log1p.c (plain doubles), and it reproduces all 24 CheckAtanhConsoleBits expectations, including NaN payloads and errno.
  • Merge order with #10314: this branch carries the pre-rebase log1p commit 0c8ca62c7a, not #10314's current head f4cb18645c, so git does not recognise it as the same change. I simulated main, then #10314, then this PR with git merge-file; the reverse order gives the same result.
    • All three files merge without conflict markers. Math.cpp ends up with a single log1p.
    • GuestMath.cpp gets two static void CheckLog1pConsoleBits() definitions, because this PR inserts it after MakeExtended and #10314 after CheckExpm1ConsoleBits. main() also gets two calls to it. That would fail to compile as a redefinition. I didn't build it.
    • docs/dev/debt/libc.md gets two identical log1p bullets, one before expm1 and one after.
    • Could you rebase onto #10314 once it lands, dropping 0c8ca62c7a? Until then, could you put the log1p test and debt entry exactly where #10314 has them?
  • Otherwise this still merges cleanly onto current main, and main's atanh is still the host forwarder.

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

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5638285283 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10315#pullrequestreview-5638285283). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-24T12:46:33Z. **Review:** COMMENTED --- Checked against main 39a36bbec8 and FreeBSD release/11.4.0 lib/msun/src/e_atanh.c (over s_log1p.c): - The port matches e_atanh.c: the `(ix|((lx|-lx)>>31))` domain test, the `x/zero` pole, the 2^-28 shortcut and both log1p forms. `EDOM` and `ERANGE` are added only at msun's two error returns. - I wrote my own Python port of e_atanh.c over s_log1p.c (plain doubles), and it reproduces all 24 `CheckAtanhConsoleBits` expectations, including NaN payloads and errno. - Merge order with #10314: this branch carries the pre-rebase log1p commit 0c8ca62c7a, not #10314's current head f4cb18645c, so git does not recognise it as the same change. I simulated main, then #10314, then this PR with `git merge-file`; the reverse order gives the same result. - All three files merge without conflict markers. `Math.cpp` ends up with a single `log1p`. - `GuestMath.cpp` gets two `static void CheckLog1pConsoleBits()` definitions, because this PR inserts it after `MakeExtended` and #10314 after `CheckExpm1ConsoleBits`. `main()` also gets two calls to it. That would fail to compile as a redefinition. I didn't build it. - `docs/dev/debt/libc.md` gets two identical `log1p` bullets, one before `expm1` and one after. - Could you rebase onto #10314 once it lands, dropping 0c8ca62c7a? Until then, could you put the log1p test and debt entry exactly where #10314 has them? - Otherwise this still merges cleanly onto current main, and main's `atanh` is still the host forwarder. Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
MarcZX10 commented 2026-10-24 21:12:54 +00:00 (Migrated from github.com)

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

Review: DISMISSED


The earlier review identified a real stacked-test collision with #10314; the updated description says the inherited log1p test and debt entry now align with that branch, which addresses that concern. The integration evidence is still based on 6c52b64, 6,822 commits behind current main, and the hosted Linux/Windows product jobs are skipped. Please refresh #10314 and this dependent branch onto current main, then rerun guest_math and guest_dinkumware_internals on the final head on Linux and Windows. Keep the negative control tied to that final tree.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5640048774 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10315#pullrequestreview-5640048774). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-24T21:12:54Z. **Review:** DISMISSED --- The earlier review identified a real stacked-test collision with #10314; the updated description says the inherited `log1p` test and debt entry now align with that branch, which addresses that concern. The integration evidence is still based on `6c52b64`, 6,822 commits behind current `main`, and the hosted Linux/Windows product jobs are skipped. Please refresh #10314 and this dependent branch onto current `main`, then rerun `guest_math` and `guest_dinkumware_internals` on the final head on Linux and Windows. Keep the negative control tied to that final tree.
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!10315
No description provided.