fix(libc): compute cbrtl with FreeBSD msun's ld80 code as the bundled libc.prx does #11632
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/11632"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by AlexFigas on 2026-10-27T00:09:55Z.
What
cbrtlreturned the host'scbrtl. On 20,000,000 random 80-bit encodings, glibc's result differs from the console's on 2,038,367 of them. For example,cbrtl(0x8659809ec6db32bc:401d)is0x82151d1b393fd44d:4009on the console and…44eon the host.The evidence is the
libc.prxbundled with PPSA21564, buildJ03093718, the library's own code. Itscbrtl(0x42360, no calls) is FreeBSD msun'ss_cbrtl.cfor ld80, instruction for instruction:x + x; ±0 returnsx.fmulby0x6010000000000000) before the exponent is read.0x3fff. It is doubled or quadrupled byk % 3, with C's truncating%and/(16-bitimul 0x5556in the binary). The scalevkeeps the argument's sign and gets exponent0x3fff + k / 3.floatconversion (fst dword) gives the 5-bit estimate(hx & 0x7fffffff) / 3 + B1, withB1 = 709958130.dt * (dx + dx + dr) / (dx + dr + dr), in SSE doubles with no fused operations.t = dt + (2^32 + 2^-31) - 2^32on the x87. The binary adds2^32 + 2^-31at run time from two stack doubles, msun's volatilevd2andvd1, so this port reads them throughvolatileas well.t + t * ((x/t² - t) / (2t + x/t²)), thent * v.The new
JoinExtendedbuilds an 80-bit value from its mantissa and sign-exponent, the counterpart ofSplitExtended.The binary has no control-word instruction, since msun's
ENTERIchanges the precision only on i386 (math_private.h). It runs at the caller's x87 precision, and so does this port. The precision a guest thread starts with is #11849.The debt list of host-computed long double functions drops
cbrtl. A new entry records what is left: other SDK versions and the systemlibSceLibcInternalare not compared.Licence.
s_cbrtl.c's notice is copied verbatim at the top ofMath.cpp, after the notices #12541 added and before the first#include, behind aDerived from FreeBSD lib/msun/src/s_cbrtl.c (release/11.4.0)line in #12541's form.Tested
On Ubuntu 24.04 under WSL2 (GCC 13.3, Release). Head
b2a99b137fmergesmaina4eca2a056into the previous head5ab9b2926a(maine0caf13600). The merge has one conflict, indocs/dev/debt/libc.md:mainhas #11849's shorterasinlentry and still listscbrtlamong the host-computed functions, so the resolution keepsmain'sasinl, dropscbrtlfrom the list and keeps this PR'scbrtlentry. Before that,5ab9b2926amerged into02c58684e5(main8a4fc62318), which merged into77b2461f11, which had mergedmainf1e340be4a, and9d91493c28before itmainda026c7065and with it #12541. The native comparison and precision runs below were first made on the earlier head a180cc03b0, whosecbrtlis the same; the first bullet repeats the comparison with the binary on this head.This head.
guest_mathandguest_math_exactpass 3 runs each. Itslibc.prxgives the binary's result,errnoand flags, with the control word unchanged, for 20,000,000 encodings at each of 64, 53 and 24 bits under each rounding mode (240,000,000 in all).Native comparison. I used the harness from #11260, with that
libc.prx's segments mapped into a Linux process. It ran the binary'scbrtlnext to this one on 20,000,000 encodings:This port matches the binary's 80-bit result, NaN bits included, on every argument, and matches its
errnoand floating-point exception flags too. The host'scbrtldiffers on 2,038,367 results and 1,000,487 flag sets.Precision. The same 20,000,000 encodings at 64-, 53- and 24-bit precision, each in all four rounding modes: no difference in result,
errnoor flags, and the control word is unchanged after every call. Beforevdwas read at run time, 105 of 2,000,000 results differed at 53 bits.guest_mathpasses. It gainsCheckCbrtlConsoleBits, 17 known-answer pairs from that binary at control word0x037f:It also runs 8 at
0x027f(53 bits), with the binary's results there: 2, -27, two subnormals, a NaN, and three arguments whose 53-bit result depends on readingvdat run time.Each check sets the control word and requires the binary's result and x87 and SSE exception flags, the same control word afterwards, and
errnounchanged. With2^32 + 2^-31folded at compile time, it fails at 53 bits on0xd82f8566779298d8:b21c.Whole suite. All 2536 tests passed with #11634 on top of the earlier head a180cc03b0.
Conventions.
python3 tools/check_conventions.py --base origin/mainpasses with 0 errors. Its one warning is the length of the host-computed list entry, which is 1446 characters onmainand 1439 here.Overlap.
python3 tools/pr_overlap.py --branch fix/libc-cbrtl-msun-ld80reports #11634, which is stacked on this one, and #8417 indocs/dev/debt/libc.md.asinlline below the entries this PR changes ismain's, and the merge kept it.tgammato the host-computed list line that this PR takescbrtlout of. It has had changes requested since October 26, so this PR does not wait for it; #8417 will need a one-line merge.mainat the top ofMath.cpp, landed first as agreed there. Merging it kept all of its blocks unchanged and put this PR's block after them.main. Each gives the samegit merge-treeconflicts with this head as withmaina4eca2a056.Math.cppandGuestMath.cpphunks stay clear of #11260, #11262 and #11266.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-27T00:13:16Z.
Review: CHANGES_REQUESTED
Reviewed exact head
490bd42d523538b1796a60156b3064d30ba00044. Changes requested for a runtime precision-control mismatch.[P1]
cbrtl_nid_postfix: the console-bit match does not hold under the guest thread’s default x87 precision. The implementation performs its final rounding and Newton step with x87long doublearithmetic, so its result depends on the x87 precision-control field. The PR’s own debt entry says it matches the bundled console code only at 64-bit precision, while Windows guest threads started by_beginthreadexuse 53-bit precision (0x027f). This leaves the newly implemented export returning different bits on the normal Windows guest-thread state; the current known-answer tests only exercise the 64-bit state and therefore miss that case. Please make the result match the console independently of the caller’s precision mode (while preserving the caller’s floating-point environment as required), and add a regression that starts from 53-bit precision and verifies both the expected result and the restored control word/status. If this limitation is intentional, it should not be presented as a completed console-compatible implementation without an explicit scope decision.The PR’s reported 20-million-input native comparison is useful, but it is explicitly at 64-bit precision; the hosted product jobs are skipped by path policy, and the relinker/ASan checks are still running at review time.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-27T11:01:17Z.
Review: COMMENTED
I reviewed the exact head a180cc03b072a0db8d4dbafaca55ef313d447a80. The port follows the stated FreeBSD msun sequence, and the committed known-answer cases exercise positive/negative values, subnormals, non-canonical encodings, infinities, NaNs, and both 64-bit and 53-bit precision. I found no specific arithmetic defect in this pass.
Independent local check: I built the exact
guest_math_teststarget on Linux with the repository-pinned dependencies and ran it natively; it exited 0. The host cannot run the full product suite because it lacks AVX2/FMA. The hosted Windows check and Ubuntu/Windows relinker plus ASan checks pass; macOS compiler, register-context Debug, and one write-watch job are still queued, while the full Linux product job is skipped by path policy. I am leaving this as a comment review until those macOS jobs finish and the required platform coverage is complete.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-27T15:12:31Z.
Review: CHANGES_REQUESTED
I reviewed the exact head a180cc03b072a0db8d4dbafaca55ef313d447a80. The port follows the stated FreeBSD msun sequence, and the committed known-answer cases exercise positive/negative values, subnormals, non-canonical encodings, infinities, NaNs, and both 64-bit and 53-bit precision. I found no specific arithmetic defect in this pass.
Independent local check: I built the exact
guest_math_teststarget on Linux with the repository-pinned dependencies and ran it natively; it exited 0. The host cannot run the full product suite because it lacks AVX2/FMA. The hosted Windows check and Ubuntu/Windows relinker plus ASan checks pass; macOS compiler, register-context Debug, and one write-watch job are still queued, while the full Linux product job is skipped by path policy. I am leaving this as a comment review until those macOS jobs finish and the required platform coverage is complete.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-27T15:12:44Z.
Review: CHANGES_REQUESTED
The arithmetic review did not find a defect in the
cbrtlport. I also built and ranguest_mathandguest_math_exactthree times each using the PR's changedMath.cppandGuestMath.cppapplied to a clean build based on this PR's original base; the tests passed. The combined build also contained unrelated #11213 exception changes, so this is focused local evidence rather than a build of this exact commit.I cannot approve the current branch because its contribution/landing coordination is incomplete. A merge preview of the current #11632 and #11849 heads conflicts in
docs/dev/debt/libc.md. #11849 narrows theasinldebt entry; this PR reorganizes the same long-double math section to documentcbrtl. The PR overlap workflow currently reported only #11634, and this PR's checklist says “Depends on: none,” so the #11849 overlap and landing order are not declared.CONTRIBUTING.mdasks authors to agree which overlapping PR goes first and declare that dependency.Please coordinate the order with #11849 and update the dependency/overlap information, then rebase or otherwise show a clean merge preview against the agreed order. This request concerns the documented debt-file conflict and landing sequence; it is not a request to change the reviewed arithmetic.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-27T19:10:33Z.
Review: APPROVED
I re-reviewed exact head 77d43c5de51bb0416e7a596e401507df9549a85e after the overlap follow-up. The landing order is now explicit: #11632, then #11634, then #11849; #11849 declares both math PRs as dependencies, and the author documented the one debt-file conflict and how to preserve both entries. This satisfies the repository's overlap-order requirement. The current-main merge preview for this PR is clean.
The focused guest_math build and repeated tests passed in my earlier review. The hosted checks on this head are green apart from the Linux product job being skipped; the author also supplied Windows and binary-comparison evidence. I found no implementation defect in the reviewed cbrtl path. Approved; retain the declared #11077 dependency and the stated landing order.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T16:51:47Z.
Review: APPROVED
Re-reviewed exact current head
d842922824753d84a30f90241ca1c10f3fa7068d. The post-approval change adds thes_cbrtl.ccopyright and permissive license notice directly above the port; I compared it with FreeBSD release/11.4.0 and it matches verbatim. The implementation/test review and the declared #11632 → #11634 → #11849 landing sequence remain applicable. Hosted Ubuntu, Windows, macOS, ASan, register-context and write-watch checks pass; the Linux product job is skipped by workflow filters. The author reportsguest_mathand title-binary comparisons rerun on the new heads. No PS5 title run is claimed.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T08:35:08Z.
Review: CHANGES_REQUESTED
Re-reviewed exact head
d842922824753d84a30f90241ca1c10f3fa7068dafter the new maintainer comment. The FreeBSDs_cbrtl.ccopyright/license notice is currently placed immediately abovecbrtl_nid_postfixaround line 567. Please move that intact block to the top ofMath.cpp, as required for the ported source, without changing its text. Current-main merge preview is clean andgit diff --checkpasses. No arithmetic or test changes are requested.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T11:07:53Z.
Review: CHANGES_REQUESTED
Re-checked — changes requested
The source notice is now placed in the requested form, and the update reports three
guest_mathruns plus the bundled-libc comparison. That resolves the prior notice-placement finding. However, the branch is based on1b580f13cf, while currentmainisba5b5330b3; current main has changedMath.cpp,GuestMath.cppand the libc debt file touched by this PR. Please integrate current main and rerun the focused math tests andcbrtlcomparison on the resulting exact head. The reported earlier-head results do not cover these intervening code/test changes.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-30T10:19:58Z.
Review: CHANGES_REQUESTED
Re-checked — changes requested
Reviewed exact head
554c60b29f5bd03b9b4cee33a653f9960bcd0b91; the remaining blocker is the maintainer-directed landing order with #12541, which is not reflected in this PR's checklist.What I ran
The PR base is
98ddd9c1792bbab016ceed5db267a93d1d61f00a; currentmainis07253ec3794121657a8d9a2229a6224651b0c8f5. The head merges cleanly into currentmain. Local no-build checks ran on Linux (Debian, Python 3.11.2, GCC 12.2.0); I did not configure or build.Results
docs/dev/debt/libc.md.mainchange todocs/dev/debt/libc.md.guest_mathand bundled-libc comparisoncbrtlcomparisons across precisions/rounding modes; I did not independently run them.Selected unedited output
Findings
Blocking
The current checklist says
Depends on: none, but maintainer direction on #12541 is to land #12541 first and declareDepends on: #12541on both #11632 and #11634. This PR's FreeBSD license notice shares the top-of-file insertion point with #12541, so the order needs to be recorded before approval. Please update this checklist now; after #12541 merges, rebase this PR onto currentmain, preserve both notice blocks, and obtain fresh CI.Non-blocking
The conventions check warns that the modified libc debt entry is 1,439 characters, above its 1,000-character guidance. It is not a conventions error and does not block this review request.
Sources
Not verified
I did not build or run
guest_mathor the bundled-library comparison locally. The author-reported math runs are on the exact PR head; the physical console was not tested. CI statuses above are from the exact-head snapshot and may change after this review.To get this to a good state
Update the checklist to
Depends on: #12541. Once #12541 merges, rebase this PR onto currentmain, keep both license blocks at the top ofMath.cpp, and rerun fresh CI plus the focused math validation. I will rerun the same available no-build checks and inspect the new head before reviewing again.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-30T14:59:25Z.
Review: CHANGES_REQUESTED
Re-checked — new head, changes requested
The #12541 landing-order finding is resolved and the current-main merge preview is clean, but this branch still does not include current
mainas required by the repository rules.What I ran
Reviewed head
9d91493c28768bfaa7e63d5eb6c92263835139f7against PR baseda026c70655a132031d45e2008c24f993870d801on Debian 12 x86_64, Python 3.11.2, GCC 12.2.0. Currentmainised1e0ed08fc2452192df91793a33dd0cc7ad5212; merging the head into it previews cleanly.Results
main; preview tree680dae1c5824ce047dab5e6d1300bec736d969e6.Math.cpp:61; the hosted conventions check passes.guest_mathruns on this head and 240 million comparisons across precision/rounding combinations; I did not independently run them.Selected unedited output
Negative control
Not independently exercised. The author reports
CheckCbrtlConsoleBitsdetects the prior folded-constant regression; I did not build or run the exact-head binary.Findings
Blocking
The head contains
mainonly throughda026c70655a132031d45e2008c24f993870d801, while currentmainised1e0ed08fc2452192df91793a33dd0cc7ad5212.CONTRIBUTING.mdrequires keeping branches up to date. The changed paths have no later-main edits and the merge preview is clean, but this branch remains behind. Please merge currentmaininto this PR and rerun the focused math tests on the resulting head.Non-blocking
The copied upstream license notice has one trailing space; the hosted conventions check passes. The precheck's stack-cycle result is inconsistent with the actual checklist and clean dependency preview. MacOS hosted jobs are still pending, and the author-reported comparison runs are not independent verification.
Sources
Not verified
No local build or CTest run; no PS5-console run. The hosted macOS jobs remain queued.
To get this to a good state
Merge current
maininto #11632, preserve the #11632 → #11634 → #11849 landing order, and rerun the focused math tests on the exact updated head. I will repeat the no-build checks and re-review that head.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-30T15:00:07Z.
Review: CHANGES_REQUESTED
Re-checked — new head, changes requested
Correction to my preceding review:
git diff --checkpasses for the PR diff against its base; the trailing-space line I reported was from comparing the older reviewed head to the new head. The current-main synchronization finding remains.What I ran
Reviewed head
9d91493c28768bfaa7e63d5eb6c92263835139f7against PR baseda026c70655a132031d45e2008c24f993870d801on Debian 12 x86_64, Python 3.11.2, GCC 12.2.0. Currentmainised1e0ed08fc2452192df91793a33dd0cc7ad5212; the merge preview is clean.Results
main; preview tree680dae1c5824ce047dab5e6d1300bec736d969e6.guest_mathruns on this head and 240 million comparisons across precision/rounding combinations; I did not independently run them.Selected unedited output
Negative control
Not independently exercised. The author reports
CheckCbrtlConsoleBitsdetects the prior folded-constant regression; I did not build or run the exact-head binary.Findings
Blocking
The head contains
mainonly throughda026c70655a132031d45e2008c24f993870d801, while currentmainised1e0ed08fc2452192df91793a33dd0cc7ad5212.CONTRIBUTING.mdrequires keeping branches up to date. The changed paths have no later-main edits and the merge preview is clean, but this branch remains behind. Please merge currentmaininto this PR and rerun the focused math tests on the resulting head.Non-blocking
The precheck's stack-cycle result is inconsistent with the actual checklist and clean dependency preview. MacOS hosted jobs are still pending, and the author-reported comparison runs are not independent verification.
Sources
Not verified
No local build or CTest run; no PS5-console run. The hosted macOS jobs remain queued.
To get this to a good state
Merge current
maininto #11632, preserve the #11632 → #11634 → #11849 landing order, and rerun the focused math tests on the exact updated head. I will repeat the no-build checks and re-review that head.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-01T01:11:19Z.
Review: APPROVED
Re-checked — new head, looks good
I found no new blocking issue at head
02c58684e5413be99fbbd9d635c86927db187b9c; the current-main merge preview is clean and the Windows workflow passed.What I ran
Reviewed head
02c58684e5413be99fbbd9d635c86927db187b9cagainst base8a4fc62318a4b73b6895bf20a024c625efeb7c3bon Debian 12 x86_64, Python 3.11.2 and GCC 12.2.0. The preview onto currentmain(7c4dcf0ad84de7a473ce97bb21644c614f1c3bd3) merged cleanly.Results
8713d650d8b51e7fa0af9b0f204a4588a45db36f. No newermaincommits touch the changed math, test, or debt paths.write-watchjobs failed during CMake configuration because that test project registers duplicate names; the failure is outside this PR's changed paths.guest_mathandguest_math_exactpassing three runs each, plus 12 console-differentialcbrtlsweeps of 20 million encodings per sweep across x87 precisions and rounding modes. I did not run these locally.Selected unedited output
Sources
s_cbrtl.cNot verified
No local C++ build or CTest run, no Linux product test, and no physical-console run. The extensive console-differential results above are author-reported, not independently reproduced.