Skip to content

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

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
JJenkx:windows/libc-cbrtf-msun
Oct 24, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
JJenkx:windows/libc-cbrtf-msun

Conversation

@JJenkx

@JJenkx JJenkx commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

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

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 10129 docs/dev/debt/libc.md L71-72
PR 10134 docs/dev/debt/libc.md L71-72

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 3965 docs/dev/debt/libc.md
PR 5021 docs/dev/debt/libc.md
PR 5303 docs/dev/debt/libc.md
PR 6044 core/libs/tests/GuestMath.cpp
PR 7201 core/libs/prx/libc/src/Math.cpp
PR 7544 core/libs/prx/libc/src/Math.cpp
core/libs/tests/GuestMath.cpp
docs/dev/debt/libc.md
PR 7648 docs/dev/debt/libc.md
PR 7715 docs/dev/debt/libc.md
PR 8150 docs/dev/debt/libc.md
PR 8306 docs/dev/debt/libc.md
PR 8417 core/libs/prx/libc/src/Math.cpp
core/libs/tests/GuestMath.cpp
docs/dev/debt/libc.md
PR 8863 core/libs/prx/libc/src/Math.cpp
docs/dev/debt/libc.md
PR 8965 docs/dev/debt/libc.md
PR 9133 docs/dev/debt/libc.md
PR 9232 core/libs/prx/libc/src/Math.cpp
docs/dev/debt/libc.md
PR 9342 docs/dev/debt/libc.md
PR 9408 docs/dev/debt/libc.md
PR 9447 docs/dev/debt/libc.md
PR 9526 docs/dev/debt/libc.md
PR 9527 docs/dev/debt/libc.md
PR 9552 docs/dev/debt/libc.md
PR 9669 docs/dev/debt/libc.md
PR 9833 core/libs/tests/GuestMath.cpp
docs/dev/debt/libc.md
PR 9929 docs/dev/debt/libc.md
PR 9945 docs/dev/debt/libc.md
PR 10002 core/libs/prx/libc/src/Math.cpp
core/libs/tests/GuestMath.cpp
docs/dev/debt/libc.md
PR 10003 core/libs/prx/libc/src/Math.cpp
core/libs/tests/GuestMath.cpp
docs/dev/debt/libc.md
PR 10007 core/libs/prx/libc/src/Math.cpp
core/libs/tests/GuestMath.cpp
docs/dev/debt/libc.md

@MarcZX10

Copy link
Copy Markdown
Contributor

The exhaustive cbrtf comparison and focused Windows test report are detailed. Current upstream main is a0900b9d; GitHub compare reports head 8778a1a8 is 26 commits behind, with merge base d6e7ab42. Please rebase onto current main and rerun the focused math checks/conventions on the final head when practical. This change shares Math.cpp, GuestMath.cpp and the debt index with other math PRs; please preserve the other implementations during the rebase and verify the combined result. This does not ask you to change your stated order or ask maintainers to merge anything.

…bc.prx does

cbrtf returned the host cbrt, so its result depended on the host: on
Windows it differs from the libc.prx that titles ship on 1,303,357,444 of
the 2^32 floats. That libc.prx is msun's s_cbrtf.c, which gives the same
bits as PPSA07923's, PPSA05916's and PPSA13934's copies for every argument.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@JJenkx
JJenkx force-pushed the windows/libc-cbrtf-msun branch from 8778a1a to ac92cc6 Compare October 22, 2026 20:23
@JJenkx

JJenkx commented Oct 22, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto current main 3bbd232; head is now ac92cc6, 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.

@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 ac92cc6 merged onto main fcd4558 (clean).

  • guest_math passes 5 of 5 (--repeat until-fail:5).
  • Baseline: with main's Math.cpp the new CheckCbrtfConsoleBits also passes on Linux, so glibc's cbrtf already gives the console's bits on all 21 cases. The change still removes the host dependency. It just can't be told apart from main on Linux with these cases. If you know an input where glibc and the console differ, a row for it would let the test cover both hosts.

No findings.

AI-assisted: yes

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

@oneandonlydean oneandonlydean self-assigned this Oct 23, 2026
@oneandonlydean
oneandonlydean merged commit d7f0c00 into boykopovar:main Oct 24, 2026
19 checks passed
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.

4 participants