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

Merged
JJenkx merged 1 commit from upstream-pr/9862 into main 2026-10-24 02:23:06 +00:00
JJenkx commented 2026-10-22 19:29:26 +00:00 (Migrated from github.com)

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


What

cbrtf returned the host's cbrt, so its result depended on the host. On Windows (UCRT) it differs from the libc.prx that titles ship on 1,303,357,444 of the 2^32 floats (30 %). For example, the console gives cbrtf(0x3f000555) = 0x3f4b32c7 and cbrtf(0x7f7fffff) = 0x54cb2ff5.

The titles' cbrtf (NID GlelR9EEeck) is FreeBSD msun's s_cbrtf.c, and cbrtf now computes it the same way:

  • a 5-bit first guess from the exponent bits, with B1/B2, and subnormals scaled by 2^24 first;
  • two Newton steps in double precision, then rounding to float.

The titles' copies set no errno, and neither does this.

cbrtf is imported by 4 of the 22 titles whose relinker registries I audited: PPSA02630, PPSA19591, PPSA25872 and PPSA27616. The core is in double precision, so the FMA-contracted copy of PPSA13934 gives the same bits too.

Tested

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. Their cbrtf and this build's cbrtf_nid_postfix give the same bits for all 2^32 arguments, and none of them sets errno.
  • guest_math: the new CheckCbrtfConsoleBits has 21 cases with the console's results, each also checking that errno is untouched: subnormals and normals where UCRT differs, of both signs, ±0, the smallest subnormal, the largest subnormal and smallest normal, 1, ±8, 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. tools/pr_overlap.py --branch finds no conflicting pull request, and the branch merges cleanly with my other math PRs (#9771, #9777, #9784, #9839, #9841, #9844, #9850, #9857).
  • Independent Linux review: Dom3005's report tested head ac92cc628 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 guest_math passing five runs (--repeat until-fail:5). All 21 new cases also pass with main's Math.cpp on this Linux setup, so these cases demonstrate a Windows regression fix and Linux compatibility; they do not establish a Linux result change. An input that distinguishes glibc from the console remains unestablished. This 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.

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:4972970371 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9862). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-22T19:29:26Z. --- ### What `cbrtf` returned the host's `cbrt`, so its result depended on the host. On Windows (UCRT) it differs from the `libc.prx` that titles ship on 1,303,357,444 of the 2^32 floats (30 %). For example, the console gives `cbrtf(0x3f000555)` = `0x3f4b32c7` and `cbrtf(0x7f7fffff)` = `0x54cb2ff5`. The titles' `cbrtf` (NID `GlelR9EEeck`) is FreeBSD msun's [`s_cbrtf.c`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/msun/src/s_cbrtf.c), and `cbrtf` now computes it the same way: - a 5-bit first guess from the exponent bits, with `B1`/`B2`, and subnormals scaled by `2^24` first; - two Newton steps in double precision, then rounding to float. The titles' copies set no `errno`, and neither does this. `cbrtf` is imported by 4 of the 22 titles whose relinker registries I audited: PPSA02630, PPSA19591, PPSA25872 and PPSA27616. The core is in double precision, so the FMA-contracted copy of PPSA13934 gives the same bits too. ### Tested 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`. Their `cbrtf` and this build's `cbrtf_nid_postfix` give the same bits for all 2^32 arguments, and none of them sets `errno`. - **`guest_math`:** the new `CheckCbrtfConsoleBits` has 21 cases with the console's results, each also checking that `errno` is untouched: subnormals and normals where UCRT differs, of both signs, ±0, the smallest subnormal, the largest subnormal and smallest normal, 1, ±8, `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. `tools/pr_overlap.py --branch` finds no conflicting pull request, and the branch merges cleanly with my other math PRs (#9771, #9777, #9784, #9839, #9841, #9844, #9850, #9857). - **Independent Linux review:** [Dom3005's report](https://github.com/boykopovar/AnyPS5/pull/9862#issuecomment-6353487195) tested head `ac92cc628` 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 `guest_math` passing five runs (`--repeat until-fail:5`). All 21 new cases also pass with main's `Math.cpp` on this Linux setup, so these cases demonstrate a Windows regression fix and Linux compatibility; they do not establish a Linux result change. An input that distinguishes glibc from the console remains unestablished. This 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. ### 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 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes AI-assisted (Claude Code).
MarcZX10 commented 2026-10-23 03:44:32 +00:00 (Migrated from github.com)

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

Review: APPROVED


Reviewed exact head ac92cc6281131d8499a23c2d1f6ae47296027230. The implementation follows FreeBSD 11.4 msun's cbrtf bit-level seed, subnormal scaling and two double-precision Newton steps; special values and signed zero are covered. The production and test files are unchanged since the tested base; the only current-main change among the PR paths is the adjacent debt index.

I locally built the exact PR Math.cpp and test source and ran the isolated 21-case CheckCbrtfConsoleBits on Linux; it passed. The available full guest_math CTest uses an older source snapshot and aborts first at its strtol check, so this is not a full-suite pass. The author reports exhaustive Windows comparisons and repeated focused tests; independent Linux testing reports five passing guest_math runs and notes the 21 cases also pass with Linux main's implementation. Hosted policy checks pass; product jobs are skipped.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5623635383 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9862#pullrequestreview-5623635383). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-23T03:44:32Z. **Review:** APPROVED --- Reviewed exact head `ac92cc6281131d8499a23c2d1f6ae47296027230`. The implementation follows FreeBSD 11.4 msun's `cbrtf` bit-level seed, subnormal scaling and two double-precision Newton steps; special values and signed zero are covered. The production and test files are unchanged since the tested base; the only current-main change among the PR paths is the adjacent debt index. I locally built the exact PR `Math.cpp` and test source and ran the isolated 21-case `CheckCbrtfConsoleBits` on Linux; it passed. The available full `guest_math` CTest uses an older source snapshot and aborts first at its `strtol` check, so this is not a full-suite pass. The author reports exhaustive Windows comparisons and repeated focused tests; independent Linux testing reports five passing `guest_math` runs and notes the 21 cases also pass with Linux main's implementation. Hosted policy checks pass; product jobs are 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!9862
No description provided.