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

Merged
JJenkx merged 3 commits from upstream-pr/10283 into main 2026-10-28 00:01:02 +00:00
JJenkx commented 2026-10-23 16:32:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-23T16:32:00Z.


What

cbrt returned the host's cbrt, so its last bit depended on the host. On Windows (UCRT) it differs from the libc.prx that titles ship on 99,933,841 of 320 million random arguments, about 31%. For example, cbrt(0x3ffa0ccfe147c94d) (about 1.628) is 0x3ff2d29f09b6d6a3 on the console and 0x3ff2d29f09b6d6a4 here.

The titles' copy (NID 5ZkEP3Rq7As) is FreeBSD msun's s_cbrt.c. cbrt now computes it:

  • the B1/B2 exponent estimate, with the 2^54 scaling for subnormals;
  • the P0–P4 polynomial step;
  • rounding t to 23 bits;
  • the final Newton step.

It sets no errno, as the titles' copy does not.

Tested

Refreshed onto main 5a2744c55efcedf4c5f1b190af46fccbe5df1310 as 26e8bc5f43e7fa8b4082ff1c06f771c53c34c3e7, one topic commit with no merge commits. The only conflict was an additive libc debt append: all current-main entries and this PR's distinct cbrt entry are preserved. The implementation and CheckCbrtConsoleBits bodies are byte-identical to previous public head 7e0150e9b0911d5af3dbb2ae859ae8bc5104e93c.

Fresh Linux libc, guest_math_tests and guest_math_exact_tests builds pass. Both focused CTest groups pass three times (6 passes); conventions, whitespace and merge-tree checks pass. This is rebase qualification. The original large Windows and bundled-SDK comparisons were not rerun and retain their prior head attribution, including the recorded FMA variant and exception-flag limitations.

Refresh patch, preservation hashes and build/test logs — SHA256 c2816446e25860f967e9f77a750958911893cbd89189fb54c014e250e0999e56; uploaded archive downloaded and hash-verified.

Independent Windows regression report on previous public head (ycbuzi): PR head 09bd784583aca366f165ef3b91245d121fcf3405 was cleanly integrated onto main fa515def85 as 91ba4d5371 on Windows 11 Home 10.0.26200, MinGW-w64 GCC 15.2.0 UCRT r7, Release with BUILD_TESTING=ON. The reporter built libc, guest_math_tests and guest_dinkumware_internals_tests without reported errors or warnings; both focused groups passed twice using ctest --repeat until-fail:2. Their negative control replaced Math.cpp with main's version: guest_math aborted while the Dinkumware group still passed. This is attributed independent integration/regression evidence; original-module matrices and titles were not rerun.

Current Linux refresh: head 7e0150e9b0911d5af3dbb2ae859ae8bc5104e93c, one topic commit directly on main da6c581a02dbd3983ccc70fff92169acce802f77, without merge commits. Both cbrt_nid_postfix and CheckCbrtConsoleBits are unchanged from previous public head 09bd784583aca366f165ef3b91245d121fcf3405. Newer main math implementations, regression calls, canonical public declarations and debt entries are retained. No pending math port is required: 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. The independent Windows report above and original-module comparisons below remain pinned to their measured heads; no new Windows, title, console, exception-flag or performance claim.

Exact-head Linux build/tests, negative control and preservation proof (SHA-256 2f58ea7138bff280a0c9f58642a45b20b056dbd82d785d6dabcdc2145b21ddfc).

Historical Windows/module measurements (previous head): the following results are retained from the original submission; “console” below refers to the bundled SDK modules executed on Windows, not a physical console measurement.

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

  • Comparison: a scratch loader runs the decrypted libc.prx of PPSA07923, PPSA05916 and PPSA13934 natively and supplies __error. It compared their cbrt with this PR's code, checking result bits (NaN payloads included) and errno, on:

    • 320 million random arguments per title: a third random bit patterns, two thirds random significands with exponents 2^-63 to 2^9;
    • 10,305 structured arguments: the cubes of −1000 to 1000 and the reciprocals of the same integers; every power of two from 2^-1074 to 2^1023 of both signs, and its upper neighbour; the special values.

    Results:

    • PPSA07923 and PPSA05916: identical bits on all of them, and errno is never set.
    • PPSA13934: identical errno. The bits differ on 32 random arguments, because that copy is an FMA-contracted build.
  • guest_math: the new CheckCbrtConsoleBits has 25 cases with the console's results:

    • ±0, ± the smallest subnormal, the largest subnormal and the smallest normal;
    • ±1, 8, −27 and 1000;
    • 2 and 0.5;
    • ±DBL_MAX, the infinities and NaN payloads of both signs;
    • six arguments where UCRT differs.

    Each case checks that errno stays unchanged.

    guest_math and guest_dinkumware_internals pass in three runs out of three on the exact head.

  • Negative control: with main's Math.cpp and these tests, guest_math aborts in a Require, while guest_dinkumware_internals still passes.

  • python tools/check_conventions.py --base origin/main and git diff --check pass.

  • Overlap: other math ports can share source, test and debt insertion anchors. This cbrt implementation does not call any pending math port; textual overlap does not require the previous series order.

  • Not tested: macOS, a physical console, floating-point exception flags, and cbrtl. Windows/module sampling above remains historical; current Linux validation is listed first.

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; Codex for the Linux rebase and validation).

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4986021355 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10283). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-23T16:32:00Z. --- ### What `cbrt` returned the host's `cbrt`, so its last bit depended on the host. On Windows (UCRT) it differs from the `libc.prx` that titles ship on 99,933,841 of 320 million random arguments, about 31%. For example, `cbrt(0x3ffa0ccfe147c94d)` (about 1.628) is `0x3ff2d29f09b6d6a3` on the console and `0x3ff2d29f09b6d6a4` here. The titles' copy (NID `5ZkEP3Rq7As`) is FreeBSD msun's [`s_cbrt.c`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/msun/src/s_cbrt.c). `cbrt` now computes it: - the `B1`/`B2` exponent estimate, with the 2^54 scaling for subnormals; - the `P0`–`P4` polynomial step; - rounding `t` to 23 bits; - the final Newton step. It sets no `errno`, as the titles' copy does not. ### Tested Refreshed onto main `5a2744c55efcedf4c5f1b190af46fccbe5df1310` as `26e8bc5f43e7fa8b4082ff1c06f771c53c34c3e7`, one topic commit with no merge commits. The only conflict was an additive libc debt append: all current-main entries and this PR's distinct cbrt entry are preserved. The implementation and `CheckCbrtConsoleBits` bodies are byte-identical to previous public head `7e0150e9b0911d5af3dbb2ae859ae8bc5104e93c`. Fresh Linux `libc`, `guest_math_tests` and `guest_math_exact_tests` builds pass. Both focused CTest groups pass three times (6 passes); conventions, whitespace and merge-tree checks pass. This is rebase qualification. The original large Windows and bundled-SDK comparisons were not rerun and retain their prior head attribution, including the recorded FMA variant and exception-flag limitations. [Refresh patch, preservation hashes and build/test logs](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/pr10283-head26e8bc5f-main5a2744-linux-refresh.tar.gz) — SHA256 `c2816446e25860f967e9f77a750958911893cbd89189fb54c014e250e0999e56`; uploaded archive downloaded and hash-verified. **Independent Windows regression report on previous public head ([ycbuzi](https://github.com/boykopovar/AnyPS5/pull/10283#issuecomment-6386338927)):** PR head `09bd784583aca366f165ef3b91245d121fcf3405` was cleanly integrated onto main `fa515def85` as `91ba4d5371` on Windows 11 Home 10.0.26200, MinGW-w64 GCC 15.2.0 UCRT r7, Release with `BUILD_TESTING=ON`. The reporter built `libc`, `guest_math_tests` and `guest_dinkumware_internals_tests` without reported errors or warnings; both focused groups passed twice using `ctest --repeat until-fail:2`. Their negative control replaced `Math.cpp` with main's version: `guest_math` aborted while the Dinkumware group still passed. This is attributed independent integration/regression evidence; original-module matrices and titles were not rerun. **Current Linux refresh:** head `7e0150e9b0911d5af3dbb2ae859ae8bc5104e93c`, one topic commit directly on main `da6c581a02dbd3983ccc70fff92169acce802f77`, without merge commits. Both `cbrt_nid_postfix` and `CheckCbrtConsoleBits` are unchanged from previous public head `09bd784583aca366f165ef3b91245d121fcf3405`. Newer main math implementations, regression calls, canonical public declarations and debt entries are retained. No pending math port is required: 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. The independent Windows report above and original-module comparisons below remain pinned to their measured heads; no new Windows, title, console, exception-flag or performance claim. [Exact-head Linux build/tests, negative control and preservation proof](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/pr10283-head7e0150e9-mainda6c-linux-validation.tar.gz) (SHA-256 `2f58ea7138bff280a0c9f58642a45b20b056dbd82d785d6dabcdc2145b21ddfc`). **Historical Windows/module measurements (previous head):** the following results are retained from the original submission; “console” below refers to the bundled SDK modules executed on Windows, not a physical console measurement. Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, `BUILD_TESTING=ON`, `main` 15e71dbe11. - **Comparison:** a scratch loader runs the decrypted `libc.prx` of PPSA07923, PPSA05916 and PPSA13934 natively and supplies `__error`. It compared their `cbrt` with this PR's code, checking result bits (NaN payloads included) and `errno`, on: - 320 million random arguments per title: a third random bit patterns, two thirds random significands with exponents 2^-63 to 2^9; - 10,305 structured arguments: the cubes of −1000 to 1000 and the reciprocals of the same integers; every power of two from 2^-1074 to 2^1023 of both signs, and its upper neighbour; the special values. Results: - PPSA07923 and PPSA05916: identical bits on all of them, and `errno` is never set. - PPSA13934: identical `errno`. The bits differ on 32 random arguments, because that copy is an FMA-contracted build. - **`guest_math`:** the new `CheckCbrtConsoleBits` has 25 cases with the console's results: - ±0, ± the smallest subnormal, the largest subnormal and the smallest normal; - ±1, 8, −27 and 1000; - 2 and 0.5; - `±DBL_MAX`, the infinities and NaN payloads of both signs; - six arguments where UCRT differs. Each case checks that `errno` stays unchanged. `guest_math` and `guest_dinkumware_internals` pass in three runs out of three on the exact head. - **Negative control:** with `main`'s `Math.cpp` and these tests, `guest_math` aborts in a `Require`, while `guest_dinkumware_internals` still passes. - `python tools/check_conventions.py --base origin/main` and `git diff --check` pass. - **Overlap:** other math ports can share source, test and debt insertion anchors. This cbrt implementation does not call any pending math port; textual overlap does not require the previous series order. - **Not tested:** macOS, a physical console, floating-point exception flags, and `cbrtl`. Windows/module sampling above remains historical; current Linux validation is listed first. ### 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; Codex for the Linux rebase and validation).
harisibrar1 commented 2026-10-24 05:36:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-24T05:36:47Z.

Review: COMMENTED


Checked against main 7c2400d29b and FreeBSD msun s_cbrt.c at release/9.0.0, 10.0.0, 11.4.0 and 12.0.0:

  • s_cbrt.c is the same code in all four releases (only a comment typo fix in between), and the port matches it exactly: B1/B2, the 2^54 subnormal path, the polynomial grouping, the 23-bit rounding and the Newton step. All five P constants round to the hex values in msun's comments.
  • I re-ran all 25 CheckCbrtConsoleBits vectors through a Python emulation of the same operations in plain IEEE double, and every one matches. 0xc050f6b81592ae3f is a nice case: msun gives ...43fa where the correctly rounded result is ...43f9, so the test really pins msun.
  • The branch is on 15e71dbe11 and no longer merges into main: docs/dev/debt/libc.md conflicts because main's coshf entry sits where the cbrt entry goes, and keeping both lines resolves it. Math.cpp and the tests merge cleanly. Could you rebase and update the "Based on current main" and "Depends on: #10274" lines? The branch doesn't seem to need #10274, which is still open.

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

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5637029882 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10283#pullrequestreview-5637029882). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-24T05:36:47Z. **Review:** COMMENTED --- Checked against main 7c2400d29b and FreeBSD msun `s_cbrt.c` at release/9.0.0, 10.0.0, 11.4.0 and 12.0.0: - `s_cbrt.c` is the same code in all four releases (only a comment typo fix in between), and the port matches it exactly: B1/B2, the 2^54 subnormal path, the polynomial grouping, the 23-bit rounding and the Newton step. All five P constants round to the hex values in msun's comments. - I re-ran all 25 `CheckCbrtConsoleBits` vectors through a Python emulation of the same operations in plain IEEE double, and every one matches. `0xc050f6b81592ae3f` is a nice case: msun gives `...43fa` where the correctly rounded result is `...43f9`, so the test really pins msun. - The branch is on 15e71dbe11 and no longer merges into main: docs/dev/debt/libc.md conflicts because main's `coshf` entry sits where the `cbrt` entry goes, and keeping both lines resolves it. Math.cpp and the tests merge cleanly. Could you rebase and update the "Based on current main" and "Depends on: #10274" lines? The branch doesn't seem to need #10274, which is still open. Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
MarcZX10 commented 2026-10-24 21:28:20 +00:00 (Migrated from github.com)

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

Review: APPROVED


Reviewed the current head 09bd784583aca366f165ef3b91245d121fcf3405. The cbrt translation follows the cited FreeBSD msun operation order, including subnormal scaling, bit-based seed, polynomial grouping, 23-bit truncation and Newton correction. The added 25 bit-pattern vectors cover signed zero, subnormals, normal boundaries, finite extremes, infinities and NaN payloads, and explicitly assert that errno is unchanged. I verified the current diff against upstream/main, a clean merge-tree result, and passing convention/diff checks. The exact head also has reported focused Linux and Windows runs (guest_math; Windows additionally guest_dinkumware_internals) with a negative control showing guest_math fails when the implementation is replaced by main host cbrt. Hosted product jobs remain skipped, as documented. I found no outstanding blocking issue.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5640097145 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10283#pullrequestreview-5640097145). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-24T21:28:20Z. **Review:** APPROVED --- Reviewed the current head 09bd784583aca366f165ef3b91245d121fcf3405. The cbrt translation follows the cited FreeBSD msun operation order, including subnormal scaling, bit-based seed, polynomial grouping, 23-bit truncation and Newton correction. The added 25 bit-pattern vectors cover signed zero, subnormals, normal boundaries, finite extremes, infinities and NaN payloads, and explicitly assert that errno is unchanged. I verified the current diff against upstream/main, a clean merge-tree result, and passing convention/diff checks. The exact head also has reported focused Linux and Windows runs (guest_math; Windows additionally guest_dinkumware_internals) with a negative control showing guest_math fails when the implementation is replaced by main host cbrt. Hosted product jobs remain skipped, as documented. I found no outstanding blocking issue.
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!10283
No description provided.