fix(libc): compute cosh with fdlibm's algorithm, as titles' libc.prx does #12122

Merged
JJenkx merged 13 commits from upstream-pr/12122 into main 2026-11-05 03:59:27 +00:00
JJenkx commented 2026-10-27 22:02:27 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-27T22:02:27Z.


Fulfilled prerequisite: #10234 merged; its head 2c8a5d59f81da182b1ec734ea0914c587eb3f784 is an ancestor of pinned main and refreshed head 598429f215aae6bde10e0eb3703bb6bd24fe8157.

What

cosh returned the host's std::cosh. On 300,000 random arguments, that is one unit in the last place away from the libc.prx that titles ship on 20,045 of them (about 7%). It now follows fdlibm's e_cosh.c, using this file's expm1 and exp:

abs(x) result
NaN, inf x * x
below 0.5·ln 2 1 + t²/(2(1 + t)), where t = expm1(abs(x)); 1 + t below 2^-55
below 22 (exp(abs(x)) + 1/exp(abs(x)))/2
below log(DBL_MAX) exp(abs(x))/2
up to the overflow threshold w/2 · w, where w = exp(abs(x)/2)
above overflow; ERANGE

In the second-to-last range, FreeBSD 11.4's msun uses __ldexp_exp. The titles don't: with __ldexp_exp, 111,042 of 400,000 arguments around the thresholds came out 2 ulp away from them. fdlibm's original exp(abs(x)/2) product, with its exact low-word threshold, matches.

Tested

Current-main integration — 2026-11-03. Head 598429f215aae6bde10e0eb3703bb6bd24fe8157 includes main 4f41f4c375f5abb487dbd184fbc150f31994d762 and retains the prior public history and attribution/licence notices. Main’s newer math implementations and fixtures remain alongside this topic. #10234 is merged; its actual head is already in main. One duplicate, byte-identical exp fixture definition was removed; its unchanged body and single call remain.

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). Older platform/reference reports retain their original pins; no new Windows/macOS, SDK-module, console, title/GPU or performance claim. AI-assisted: yes (Codex).

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

Current-main refresh — 2026-11-02. Published head e02f1f640e09a52019066824002eb976db1c8bf4 includes pinned main a4eca2a056bf3bbbec98ab42e9d28163e4ea9cb8. The actual c988f73c exp prerequisite ancestry and full retained Sun/FreeBSD notice remain. The exp and cosh bodies are byte-identical to the previous public head; exact cosh(+1)/cosh(-1) bits and errno fixtures remain. Main’s atan/log10/log2 functions and fixtures are retained.

Rebuilt actual libc/Ampr and selected tests from the final input tree. guest_math, guest_math_exact, guest_dinkumware_internals each pass three consecutive runs: 9 runtime passes, zero skips, plus public_headers. Conventions and whitespace pass. Source/input manifests, exact dependencies, library/test hashes, logs/JUnit and head-leased push receipts are attached. The same pinned main passes 18 selected groups three times (54 runtime passes) plus headers. All 34 recursive dependency trees/31,551 tracked blobs verify with zero errors; QuickJS’s update=none test262 corpus is explicitly excluded because it is not a library build input.

Linux-7.2.11-arch1-1-x86_64-with-glibc2.44, Ryzen 9 7950X, GCC 16.2.1, Ninja Release, BUILD_TESTING=ON. Focused Linux CPU qualification only. Older Windows/module/reference reports retain their original pins; no fresh Windows/macOS, SDK-module, console, title/GPU or speed claim. AI-assisted: yes (Codex).

Evidence: https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/math-six-main-a4eca-linux-extra10321-20261102.tar.gz; SHA-256 525ccb31a5d3d0f5adced5fda58818d8432f06c0afa7290139adb60ae6664b54.

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

Current Linux review follow-up (2026-10-31), head 5abea905c268ab1e44f60c15da751fdfbfd976e9: test-only fast-forward of e0415047dc; production Math.cpp is unchanged. Clean merge-tree against checked main 32bf44ca7c6.

CPU-only native calls to the original PPSA07923 bundled libc module, with default floating controls and FE_TONEAREST, produce 0x3ff8b07551d9f550 for both cosh(+1) and cosh(-1) and preserve seeded errno=7. The unpatched converted function matches all 261 bytes in the unwrapped original. These are bundled-module results on Linux, not physical-console measurements. CheckHyperbolic now checks those exact result bits and errno for both signs.

Negative control: substituting msun's alternate exp(1) bit inside cosh's ±1 calculation produces the one-ULP-higher 0x3ff8b07551d9f551. The old tolerance fixture passes; the new exact fixture fails at its new assertion. After restoring production code, guest_math, guest_math_exact and guest_dinkumware_internals pass (3/3, no skips), with matching direct HLE/reference results. Conventions and whitespace checks pass. Arch Linux 7.2.11, Ryzen9 7950X, GCC16.2.1, Release/BUILD_TESTING=ON.

Reference hashes/results, probe, negative-control logs, final JUnit, linkage and test patch, download-verified SHA-256 3b464a76ae9be24bf07b54c1b1f4c023144987e14951236216bb165f541c5fd1. No SDK/game code or binaries are attached. AI-assisted: yes (Codex).

Original Windows qualification (original pins):

Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, BUILD_TESTING=ON, on main f759e165d9 with #10234's head 138f4397d7.

Comparison: a scratch loader runs the decrypted libc.prx of three titles natively. Each call compares the result bits and errno:

  • Random: 300,000 arguments, half random bit patterns and half spread over ±100.
  • Thresholds: 400,000 consecutive doubles around 2^-54, 0.5·ln 2, 22, log(DBL_MAX) and the overflow threshold, both signs.
random thresholds
main vs PPSA07923 20,045 values 78,006
this PR vs PPSA07923 0 values, 0 errno 0
this PR vs PPSA05916 0, 0 0
this PR vs PPSA13934 (FMA build) 78 values, 0 errno 162

Tests:

  • New checks: guest_math pins titles' bits for one argument in each finite range. It also checks that these leave errno alone, that an overflowing argument sets ERANGE, and that -inf and NaN leave errno alone.
  • It passes in each of 3 runs on the head.
  • Negative control: with the old std::cosh line and these tests, guest_math fails.
  • python tools/check_conventions.py --base origin/main and git diff --check pass.
  • Debt: a new cosh entry records what was measured.

Not tested: macOS and floating-point exception flags. Linux qualification is recorded 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
  • Generic behaviour, not specific to one title
  • Depends on: none (#10234 merged; head 2c8a5d59f81da182b1ec734ea0914c587eb3f784 retained in ancestry)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5041785657 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12122). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-27T22:02:27Z. --- **Fulfilled prerequisite:** #10234 merged; its head `2c8a5d59f81da182b1ec734ea0914c587eb3f784` is an ancestor of pinned main and refreshed head `598429f215aae6bde10e0eb3703bb6bd24fe8157`. ### What `cosh` returned the host's `std::cosh`. On 300,000 random arguments, that is one unit in the last place away from the `libc.prx` that titles ship on 20,045 of them (about 7%). It now follows fdlibm's [`e_cosh.c`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/msun/src/e_cosh.c), using this file's `expm1` and `exp`: | `abs(x)` | result | |---|---| | NaN, inf | `x * x` | | below 0.5·ln 2 | `1 + t²/(2(1 + t))`, where `t = expm1(abs(x))`; `1 + t` below 2^-55 | | below 22 | `(exp(abs(x)) + 1/exp(abs(x)))/2` | | below log(DBL_MAX) | `exp(abs(x))/2` | | up to the overflow threshold | `w/2 · w`, where `w = exp(abs(x)/2)` | | above | overflow; `ERANGE` | In the second-to-last range, FreeBSD 11.4's msun uses `__ldexp_exp`. The titles don't: with `__ldexp_exp`, 111,042 of 400,000 arguments around the thresholds came out 2 ulp away from them. fdlibm's original `exp(abs(x)/2)` product, with its exact low-word threshold, matches. ### Tested **Current-main integration — 2026-11-03.** Head `598429f215aae6bde10e0eb3703bb6bd24fe8157` includes main `4f41f4c375f5abb487dbd184fbc150f31994d762` and retains the prior public history and attribution/licence notices. Main’s newer math implementations and fixtures remain alongside this topic. #10234 is merged; its actual head is already in main. One duplicate, byte-identical exp fixture definition was removed; its unchanged body and single call remain. 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). Older platform/reference reports retain their original pins; no new Windows/macOS, SDK-module, console, title/GPU or performance claim. AI-assisted: yes (Codex). Earlier dated qualifications below retain their recorded source/platform pins. **Current-main refresh — 2026-11-02.** Published head `e02f1f640e09a52019066824002eb976db1c8bf4` includes pinned main `a4eca2a056bf3bbbec98ab42e9d28163e4ea9cb8`. The actual c988f73c exp prerequisite ancestry and full retained Sun/FreeBSD notice remain. The exp and cosh bodies are byte-identical to the previous public head; exact cosh(+1)/cosh(-1) bits and errno fixtures remain. Main’s atan/log10/log2 functions and fixtures are retained. Rebuilt actual libc/Ampr and selected tests from the final input tree. `guest_math`, `guest_math_exact`, `guest_dinkumware_internals` each pass three consecutive runs: **9 runtime passes, zero skips**, plus public_headers. Conventions and whitespace pass. Source/input manifests, exact dependencies, library/test hashes, logs/JUnit and head-leased push receipts are attached. The same pinned main passes 18 selected groups three times (54 runtime passes) plus headers. All 34 recursive dependency trees/31,551 tracked blobs verify with zero errors; QuickJS’s update=none test262 corpus is explicitly excluded because it is not a library build input. Linux-7.2.11-arch1-1-x86_64-with-glibc2.44, Ryzen 9 7950X, GCC 16.2.1, Ninja Release, BUILD_TESTING=ON. Focused Linux CPU qualification only. Older Windows/module/reference reports retain their original pins; no fresh Windows/macOS, SDK-module, console, title/GPU or speed claim. AI-assisted: yes (Codex). Evidence: https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/math-six-main-a4eca-linux-extra10321-20261102.tar.gz; SHA-256 525ccb31a5d3d0f5adced5fda58818d8432f06c0afa7290139adb60ae6664b54. Earlier dated qualifications below retain their recorded source/platform pins. **Current Linux review follow-up (2026-10-31), head `5abea905c268ab1e44f60c15da751fdfbfd976e9`:** test-only fast-forward of `e0415047dc`; production `Math.cpp` is unchanged. Clean merge-tree against checked main `32bf44ca7c6`. CPU-only native calls to the original PPSA07923 bundled libc module, with default floating controls and `FE_TONEAREST`, produce `0x3ff8b07551d9f550` for both `cosh(+1)` and `cosh(-1)` and preserve seeded `errno=7`. The unpatched converted function matches all 261 bytes in the unwrapped original. These are bundled-module results on Linux, not physical-console measurements. `CheckHyperbolic` now checks those exact result bits and errno for both signs. Negative control: substituting msun's alternate `exp(1)` bit inside cosh's ±1 calculation produces the one-ULP-higher `0x3ff8b07551d9f551`. The old tolerance fixture passes; the new exact fixture fails at its new assertion. After restoring production code, `guest_math`, `guest_math_exact` and `guest_dinkumware_internals` pass (3/3, no skips), with matching direct HLE/reference results. Conventions and whitespace checks pass. Arch Linux 7.2.11, Ryzen9 7950X, GCC16.2.1, Release/BUILD_TESTING=ON. [Reference hashes/results, probe, negative-control logs, final JUnit, linkage and test patch](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/pr12122-cosh-one-linux-20261031.tar.gz), download-verified SHA-256 `3b464a76ae9be24bf07b54c1b1f4c023144987e14951236216bb165f541c5fd1`. No SDK/game code or binaries are attached. AI-assisted: yes (Codex). **Original Windows qualification (original pins):** Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, `BUILD_TESTING=ON`, on `main` `f759e165d9` with #10234's head `138f4397d7`. **Comparison:** a scratch loader runs the decrypted `libc.prx` of three titles natively. Each call compares the result bits and `errno`: - **Random:** 300,000 arguments, half random bit patterns and half spread over ±100. - **Thresholds:** 400,000 consecutive doubles around 2^-54, 0.5·ln 2, 22, log(DBL_MAX) and the overflow threshold, both signs. | | random | thresholds | |---|---|---| | `main` vs PPSA07923 | 20,045 values | 78,006 | | this PR vs PPSA07923 | **0** values, **0** `errno` | **0** | | this PR vs PPSA05916 | **0**, **0** | **0** | | this PR vs PPSA13934 (FMA build) | 78 values, 0 `errno` | 162 | **Tests:** - **New checks:** `guest_math` pins titles' bits for one argument in each finite range. It also checks that these leave `errno` alone, that an overflowing argument sets `ERANGE`, and that `-inf` and NaN leave `errno` alone. - It passes in each of 3 runs on the head. - **Negative control:** with the old `std::cosh` line and these tests, `guest_math` fails. - `python tools/check_conventions.py --base origin/main` and `git diff --check` pass. - **Debt:** a new `cosh` entry records what was measured. **Not tested:** macOS and floating-point exception flags. Linux qualification is recorded 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 - [x] Generic behaviour, not specific to one title - [x] Depends on: none (#10234 merged; head `2c8a5d59f81da182b1ec734ea0914c587eb3f784` retained in ancestry) - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes AI-assisted (Claude Code).
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!12122
No description provided.