Skip to content

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

Merged
oneandonlydean merged 3 commits into
boykopovar:mainfrom
JJenkx:windows/libc-cbrt-msun
Oct 28, 2026
Merged

oneandonlydean merged 3 commits into
boykopovar:mainfrom
JJenkx:windows/libc-cbrt-msun

Conversation

@JJenkx

@JJenkx JJenkx commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

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 15e71db.

  • 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

AI-assisted (Claude Code; Codex for the Linux rebase and validation).

@github-actions

github-actions Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

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 5021 docs/dev/debt/libc.md
PR 6085 core/libs/prx/libc/src/Math.cpp
PR 6295 core/libs/prx/libc/src/Math.cpp
core/libs/tests/GuestMath.cpp
PR 6713 docs/dev/debt/libc.md
PR 7081 docs/dev/debt/libc.md
PR 7386 docs/dev/debt/libc.md
PR 7447 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 8937 core/libs/prx/libc/src/Math.cpp
docs/dev/debt/libc.md
PR 9371 docs/dev/debt/libc.md
PR 9447 docs/dev/debt/libc.md
PR 9526 docs/dev/debt/libc.md
PR 9929 docs/dev/debt/libc.md
PR 10324 docs/dev/debt/libc.md
PR 10433 docs/dev/debt/libc.md
PR 10620 docs/dev/debt/libc.md
PR 11332 docs/dev/debt/libc.md
PR 11848 docs/dev/debt/libc.md
PR 11850 docs/dev/debt/libc.md
PR 11870 docs/dev/debt/libc.md

@JJenkx

JJenkx commented Oct 23, 2026

Copy link
Copy Markdown
Contributor Author

Linux run at this PR's exact head be09291d48 (be09291d4861889dbe8d312c25605eef9ea70128), not merged onto anything else; its base on main is 15e71dbe11.

Arch Linux, kernel 7.2.9, Ryzen 9 7950X, g++ (GCC) 16.2.1 20260810, glibc 2.44, x86-64. Release, BUILD_TESTING=ON.

  • guest_math: passed in each of 3 runs, exit code 0 each time, with no failed, skipped or not-run tests.

This is the focused test only; no title, console or floating-point exception flags were checked.

AI-assisted: yes (Claude Code).

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 24, 2026

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

Checked against main 7c2400d 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 15e71db 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.

@JJenkx
JJenkx force-pushed the windows/libc-cbrt-msun branch from be09291 to 4641dad Compare October 24, 2026 06:24
@JJenkx

JJenkx commented Oct 24, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto main b0f57be3c7 as 4641dad5a5012a4fcd495f28628b13ebe636feb0, retaining both the main coshf and this cbrt debt entries. The cbrt source/test patch is unchanged. It applies directly to main without #10274, so I removed that series-order dependency and updated the description.

The independent Linux Release libc/math build passed; guest_math and guest_dinkumware_internals each passed 3/3 on this exact head, along with conventions and diff checks. Logs and patch. The original Windows/module measurements remain explicitly historical; they were not rerun.

@github-actions github-actions Bot added needs rebase Conflicts with main, rebase needed and removed needs rebase Conflicts with main, rebase needed labels Oct 24, 2026
JJenkx pushed a commit to JJenkx/AnyPS5 that referenced this pull request Oct 24, 2026
Additive conflicts with main's newer libc math changes: the debt list keeps
both sides' entries (per entry), the GuestMath.cpp test functions and calls
from both sides are kept, and Math.cpp keeps both sides' independent changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@JJenkx

JJenkx commented Oct 24, 2026

Copy link
Copy Markdown
Contributor Author

Merged current main 1e20650e84 into this branch as 09bd784583 (was 4641dad5a5), a merge commit rather than a rebase. Done on Linux while the Windows session is paused.

  • The conflicts were additive, next to main's newer libc math changes.
  • The docs/dev/debt/libc.md entry list keeps both sides' entries, checked per entry: this PR's entries keep their exact text, and so do main's.
  • GuestMath.cpp keeps both sides' test functions and calls.
  • New head, Linux only: guest_math and guest_math_exact pass (Arch Linux, kernel 7.2.9, GCC 16.2.1, glibc 2.44, Release, BUILD_TESTING=ON). tools/check_conventions.py --base origin/main reports no errors.
  • Earlier Windows and comparison evidence in this thread stays pinned to the heads it was measured on.

AI-assisted: yes (Claude Code)

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 24, 2026
@ycbuzi

ycbuzi commented Oct 24, 2026

Copy link
Copy Markdown
Contributor

Windows run of 09bd784 merged onto main fa515de (91ba4d5371, clean), since Windows was not run on this head. Windows 11 Home 10.0.26200, MinGW-w64 GCC 15.2.0 (ucrt-posix-seh r7), Release, BUILD_TESTING=ON.

Built libc, guest_math_tests and guest_dinkumware_internals_tests with no errors or warnings. ctest --repeat until-fail:2 on guest_math and guest_dinkumware_internals: both pass on both repeats. Negative control: with src/Math.cpp taken from main and the rest of this head, guest_math aborts (exit code 0xc0000409) and guest_dinkumware_internals still passes. Not run: a title.

@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 the current head 09bd784. 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.

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 24, 2026
@JJenkx
JJenkx force-pushed the windows/libc-cbrt-msun branch from 09bd784 to 7e0150e Compare October 25, 2026 08:55
@JJenkx

JJenkx commented Oct 25, 2026

Copy link
Copy Markdown
Contributor Author

Refreshed onto main da6c581a02db as 7e0150e9b091: one topic commit, no merge commits. The reviewed cbrt implementation and regression function are unchanged from previous public head 09bd784583; current main math/tests and canonical declarations are retained. Depends on remains none.

Fresh Linux: all four focused groups pass 3/3 before and after the expected main-Math negative failure; exact-math and Dinkumware controls pass, restored library bytes match, and conventions/whitespace pass. The description links complete evidence and keeps the independent Windows report attributed to its previous measured head. No Windows/title/module rerun is claimed. AI-assisted: yes.

@github-actions github-actions Bot added needs rebase Conflicts with main, rebase needed and removed needs rebase Conflicts with main, rebase needed labels Oct 25, 2026
@JJenkx
JJenkx force-pushed the windows/libc-cbrt-msun branch from 7e0150e to 26e8bc5 Compare October 25, 2026 20:37
@JJenkx

JJenkx commented Oct 25, 2026

Copy link
Copy Markdown
Contributor Author

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.

@github-actions github-actions Bot added needs rebase Conflicts with main, rebase needed and removed needs rebase Conflicts with main, rebase needed labels Oct 25, 2026
…c.prx does

cbrt returned the host's cbrt, whose last bit differs from the titles' copy
on Windows on 99,933,841 of 320 million random arguments, about 31%. The
titles' copy is FreeBSD msun's s_cbrt.c, and cbrt now computes it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@JJenkx
JJenkx force-pushed the windows/libc-cbrt-msun branch from 26e8bc5 to 035d9c0 Compare October 26, 2026 02:29
@github-actions github-actions Bot added needs rebase Conflicts with main, rebase needed and removed needs rebase Conflicts with main, rebase needed labels Oct 26, 2026
Conflicts with main's newer libc changes resolved without changing either
side: the debt list keeps each side's entry changes (per entry, deletions
included), GuestMath.cpp keeps both sides' test functions and calls, and
Math.cpp differs from main by exactly this branch's own changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@JJenkx

JJenkx commented Oct 27, 2026

Copy link
Copy Markdown
Contributor Author

Merged main 5bc84038c4 into this branch as 8f39bcc549 (was 035d9c0821), a merge commit rather than a rebase, after today's main changes conflicted again.

  • The conflicts were additive. docs/dev/debt/libc.md keeps each side's entry changes, checked per entry including deletions. GuestMath.cpp keeps both sides' test functions and calls. Where Math.cpp conflicted, the merged file differs from main by exactly this branch's own changes.
  • New head, Linux only: guest_math and guest_math_exact pass (Arch Linux, GCC 16.2.1, Release, BUILD_TESTING=ON). tools/check_conventions.py --base origin/main adds no new findings, and the branch still merges cleanly with main 801d5b9bc1.
  • Earlier Windows and comparison evidence in this thread stays pinned to the heads it was measured on.

AI-assisted: yes (Claude Code)

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 27, 2026
@oneandonlydean

Copy link
Copy Markdown
Collaborator

@JJenkx heads-up: the last "merge main" here also moved submodule pointers back (3rdparty/SPIRV-* / 3rdparty/vorbis), undoing recent main updates. I pushed one commit on top that resets them to main's (build: restore submodule pointers to main); nothing else changed. Please pull before your next push.

@JJenkx

JJenkx commented Oct 27, 2026

Copy link
Copy Markdown
Contributor Author

@oneandonlydean thanks, and sorry for the noise. The cause was on my side: I merged main in a worktree whose submodules were still checked out at older commits, then staged everything, so the merge recorded those older checkouts over main's SPIRV-Headers, SPIRV-Tools and vorbis pointers. Your commit is right; this branch's own changes touch no submodules.

The same refresh hit five more branches (#10345, #10229, #10222, #10226, #10113). Each now has the same one-commit pointer restore on top, and none differs from main's gitlinks. I've changed my merge script to stage only the resolved files, and it now rejects any merge whose submodule pointers differ from what a three-way merge gives. No earlier merges of mine were affected.

AI-assisted: yes (Claude Code)

@JJenkx

JJenkx commented Oct 27, 2026

Copy link
Copy Markdown
Contributor Author

Linux run at this PR's exact head f54ec181a1 (f54ec181a11395659a14445e36160039307550e0), not merged onto anything else; its base on main is 5bc84038c4.

Arch Linux, kernel 7.2.9, Ryzen 9 7950X, g++ (GCC) 16.2.1 20260810, glibc 2.44, x86-64. Release, BUILD_TESTING=ON.

Passed in each of 3 runs, exit code 0 each time, with no failed, skipped or not-run tests:

  • guest_math

These are the focused tests only; no title or console was used.

AI-assisted: yes (Claude Code).

@oneandonlydean
oneandonlydean merged commit 9f605c9 into boykopovar:main Oct 28, 2026
17 checks passed
@JJenkx

JJenkx commented Oct 28, 2026

Copy link
Copy Markdown
Contributor Author

Re-checked at the current head f54ec181a1, after the recent restacks, on Windows (WinLibs GCC 15.2.0 UCRT r7, Release). guest_math passes.

A scratch loader runs the decrypted libc.prx of PPSA07923 and PPSA05916 natively and calls cbrt by NID next to this build, with 300,000 arguments: half random bit patterns, half spread over ±100. Each call compares the result bits and errno. 0 value and 0 errno differences on both titles. Current main (d13b3d7c55) differs on 93,106 values against PPSA07923, on the same inputs.

AI-assisted: yes (Claude Code).

@JJenkx

JJenkx commented Oct 28, 2026

Copy link
Copy Markdown
Contributor Author

For prioritisation: in a Windows import audit of the 74 extracted titles on our shared library (tools/audit_dump.py with #11622's import_audit.py, 2026-10-27), cbrt is imported by 7 of 74 titles.

AI-assisted: yes (Claude Code).

@JJenkx

JJenkx commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Supplemental Linux floating-environment qualification of the merged cube-root implementations: 49,152 sampled cbrt calls and 49,152 cbrtf calls per provider, zero differences versus the owned bundled PPSA21564 libc. No SDK change was required.

The actual frozen SDK is main 1cd6df7381c6537f7efb2e90913c1a708e5ab140. Both selected function bodies are unchanged at clean main d3a34e400184a7b413440a5df1f7629fa73dbe8f; that is source-body equality, not a build at the newer pin. Linux x86-64 / Ryzen 9 7950X / GCC 16.2.1.

Each export: 128 distinct encodings (special values, signed subnormals, finite/cube boundaries and deterministic random patterns), all three x87 precisions, independent four-way x87 and SSE rounding modes, four DAZ/FTZ states and two pending-flag states. Results match bit-for-bit, with equal errno, raw x87/MXCSR flags, TOP and controls. A separate expected-state check confirms both providers retain caller controls and seeded flags, with TOP zero. Both actual function owners were checked; mapped providers, the frozen bank, reference and original asset hashes remain unchanged.

Probe, comparison rows, expected-state analysis and provenance — SHA-256 dff496205e037a724d35f25f53d290c1f2e182ac71564ae37c9bb3065a52bccb; anonymous download and all 16 payload hashes verified. Original modules, proprietary disassembly and SDK binaries are excluded.

This is sampled, masked-exception coverage on one bundled SDK version and one Linux CPU. It does not qualify other FMA-contracted variants, unmasked traps, physical-console execution, game performance or exhaustive floating-point behavior. The original module still uses SDK dependencies. The cbrtf half supplements merged #9862.

AI-assisted: yes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants