fix(libc): compute acosl with FreeBSD msun's ld80 code as the bundled libc.prx does #11260
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/11260"
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-25T22:53:41Z.
What
acoslcalled the hoststd::acos. Over 20 million random arguments in (-1, 1), the host result differed from the console's on 1,603,469 of them. This PR replaces it with FreeBSD msun'se_acosl.c(ld80), which is theacoslof thelibc.prxthat PPSA21564 bundles (0x3e5a0, the library's own code). That binary followse_acosl.cbranch for branch:0orpi + 2*pio2_lo. For anything else, including the infinities and NaN, it calls_Feraise(1), then returns(x - x) / (x - x)._Feraise(0x11b00) raisesFE_INVALIDand setserrnotoEDOM(0x21).pio2_hi + pio2_lo. Otherwise it returnspio2_hi - (x - (pio2_lo - x*r)), withr = P(x²)/Q(x²).z = (1 + x) * 0.5,s = fsqrt(z), and the result ispi - 2*(s + (r*s - pio2_lo)).df=swhose low 32 mantissa bits are cleared,c = (z - df²)/(s + df), and the result is2*(df + (r*s + c)).P,Q,pio2_hiandpio2_loare the ld80invtrig.cconstants.asinlalready reproduces them inMath.cpp(AsinNumerator,AsinDenominator,HalfPiHigh,HalfPiLow), and the binary holds the same 80-bit values.piis thee_acosl.cconstant (0xc90fdaa22168c235:4000in the binary), and the port reads it from memory as the binary does: as a compile-time constant, GCC loads it withfldpi, which rounds by the current mode, so under round-downward or toward-zero at 64 bits the results below -0.5 andacosl(-1)could come out one bit low (51,708 of 5,000,000 arguments). The port reuses those helpers and addsPi.x87 precision. The binary's
acoslhas no instruction that reads or sets the x87 control word (nofnstcw,fldcw,fldenvorfninit), so it computes at the caller's precision and leaves the control word as it found it. That is the FreeBSD source on amd64:e_acosl.cdoes not useENTERI()/RETURNI(), and those are empty outside i386 anyway (math_private.h). The port does the same. One difference had to go:pio2_hi + pio2_loandpi + 2*pio2_loare added at run time in the binary, becausepio2_hiandpio2_loareinvtrig.cexterns, so at 53 bits they round to 53 bits; GCC folded them at 64 bits. The port now adds them throughSumAtCallerPrecision, which reads the first half through avolatile. Everything else already ran at run time.The
acosldebt entry keeps what is still open: it runs at the caller's precision, so on a guest thread that starts at 53 bits (the thread-start gap theasinlentry records) it returns the binary's 53-bit result, not the console's 64-bit one; other SDK versions are not compared. The next entry no longer says "asacoslis".Licence.
e_acosl.c's copyright notice is copied verbatim directly aboveacosl, asMath.cppalready does for the msun code ofsinh. Like that copy, it keeps the source's trailing space after "this notice", the only linegit diff --checkreports.Tested
On Ubuntu 24.04 under WSL2 (GCC 13.3, Release), head
0de3168b81, which mergesmain813f46cd68into the previous head39fb738477(maina4eca2a056). The merge has one conflict, inMath.cpp: #12366 rewrotefdimandfdimfright above the hostacoslline, so the resolution keepsmain'sfdimandfdimf, this PR's notice andacosl, and drops the hostacosl.39fb738477resolved a debt-file conflict with #11077'sfrexpentry;84502b855fmergedmaincc26ab4438after a commit that drops the trailing space from thee_acosl.cnotice, sogit diff --checkpasses;8365772e47was rebased ontomain2227f13661:libc.prx. Against the binary, 5,000,000 arguments of every kind (|x| > 1, NaNs and non-canonical encodings included) at each of 64, 53 and 24 bits, under each rounding mode, give the same result, NaN bits,errnoand flags, and the control word is unchanged after every call (60,000,000 in all).libc.prxinto an executable buffer and called itsacosl(0x3e5a0) directly; arguments below 1 in magnitude never reach an import. Against this port it gives the same 80 bits for 20,000,000 random arguments plus the exponent edges (0x3fbc–0x3fbf, 0x3ffd, 0x3ffe, ±0, ±1), with 0 mismatches, at each of 64-, 53- and 24-bit precision (control words 0x37f, 0x27f and 0x07f, set before both are called). The hostacoslmismatches on 1,603,469 of the same arguments at 64 bits. BeforeSumAtCallerPrecision, the 53-bit run mismatched on 252,624 of 2,000,000, all from those two returns.guest_mathpasses (3 runs). It gainsCheckAcosl, with 22 known-answer pairs produced by that binary. Four of them were picked because the hostacoslgets them wrong, for example0xc1d51c773e6f53e1:3ffd → 0x975d54d5f258ea8a:3fffwhere the host gives…89. It also checks that ±1.5, ±(1 + ulp), the maximum, ±∞ and NaN return NaN withEDOMandFE_INVALID. Six arguments, one or two per branch, also run at 53 bits throughExtendedBitsAt53Bits, which sets the precision field to 53 bits, calls the export, reads the control word back and restores the caller's; each must give the binary's 53-bit result and leave the control word unchanged. With the sums folded again, it aborts at0x8000000000000000:3fbd. The existingacoslchecks inCheckLongDoublestill pass. It now also requires the binary's results for three negative arguments, -1 among them, under round-downward.python3 tools/check_conventions.py --base origin/mainpasses, andgit diff --checkis clean.python3 tools/pr_overlap.py --branch fix/libc-acosl-msun-ld80reports #11262 and #11266, which are stacked on this PR, and oberonlift's #15802, #15803, #15804 and #15807:frexpis onmainaboveacosl, and so arefdimandfdimf, which the merge kept.GuestLongDoubletype, including the hostacoslline this PR replaces, so each of #15802, #15803, #15804 and #15807 conflicts with this head inMath.cppandlibc.mdwhere it does not withmain(all four already conflict withmaininlibSceJobManager/Export.cpp). This PR was opened and approved first, so I have not added a dependency; the order is for that stack's author to agree.main, give the samegit merge-treeconflicts with this branch as withmain.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-25T23:11:05Z.
Review: APPROVED
Reviewed the exact head
84591b8434421fdc06a556f585313dc5bd2da4e2. Theacoslimplementation follows the pinned FreeBSD ld80 algorithm and its tested edge handling; the PR’s reported native comparison covers 20 million random inputs plus boundary cases. I independently checked the diff and conventions in the PR worktree earlier in this sweep; both passed. All hosted eligibility, conventions, intake, report, relinker, register-context and write-watch jobs are now green. Linux and Windows product jobs were skipped, and I did not independently run the native differential here.I found no blocking issue in the implementation or evidence for this change, so I approve this head.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T23:19:01Z.
Review: CHANGES_REQUESTED
I found an x87 precision-state gap while reviewing the follow-on
atanl/exp2lports. Thisacoslimplementation also needs 64-bit x87 precision according to its own debt entry, but it does not scope or restore the x87 control word, and its tests do not run with the 53-bit control setting documented for guest threads (0x27f). FreeBSD's referenced ld80 routine usesENTERI()/RETURNI()for the computation for this reason; the random comparison under the harness's default environment does not cover callers that arrive with a different precision mode. Please add a scoped precision-control path and a regression that checks the result under 53-bit precision and verifies the original control word is restored, or provide console evidence that the caller's precision mode is intentionally preserved and test both modes. I am updating my earlier approval because this correctness gap affects the stated bit-for-bit behavior. Hosted builds remain skipped for this libc-only path; the comparison and focused test results in the PR are author-reported, not independently rerun here.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-27T11:00:27Z.
Review: CHANGES_REQUESTED
The FreeBSD msun port is backed by detailed bitwise comparisons against the bundled libc.prx, across inputs, precisions, rounding modes, errno, and exception flags. The committed known-answer cases also cover the important subnormal and non-canonical paths.
I cannot approve this submitted head against current main. Its merge-base is
68cc9d4e75, but current main is3fea2a937b. Since that base, main has changed all three paths this PR edits: Math.cpp, GuestMath.cpp, and docs/dev/debt/libc.md. The Tested section names a run on main5b5d24cb22, which is not the base of the submitted head, so the report does not establish that this exact diff passed on the integrated tree.Please rebase the #11077-dependent branch onto current main, resolve the math/debt integration, and rerun guest_math plus the reference comparison on the resulting head. I found no specific arithmetic defect in the inspected cbrtl-adjacent stack code; this request is for current-tree correctness and matching test evidence before approval.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-27T15:24:00Z.
Review: CHANGES_REQUESTED
I reviewed the current head 23a6c80b60966240bc827ecc77452cf2231467d0. The port follows the pinned FreeBSD ld80 acos branches and coefficients; the caller-precision behavior is supported by the cited binary evidence, and SumAtCallerPrecision addresses the compile-time folding issue. I found no separate arithmetic defect.
Independent focused check: I applied this PR's Math.cpp, GuestMath.cpp, and debt changes to the clean #11213-based Release checkout, rebuilt guest_math_tests, and ran guest_math three times; all passed. This is focused Linux validation on the PR's
1089f2bbase plus unrelated #11213 exception changes, not a build of the current integrated main tree.The remaining integration request is still open: #11077 is still open, and this branch is based on
1089f2bwhile current main is2ee8e97(30 commits ahead). The current-main merge preview is clean, but main has changed paths this PR edits since that base. Please update the stack after #11077 lands and rerun guest_math on the resulting integrated head before approval.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-27T15:25:36Z.
Review: APPROVED
Reviewed exact head 23a6c80b60966240bc827ecc77452cf2231467d0. The acosl implementation follows the pinned FreeBSD ld80 branches and coefficients; the caller-precision behavior and runtime sums are supported by the binary evidence. I found no arithmetic defect.
Independent local check: applied this PR's Math.cpp, GuestMath.cpp, and debt changes to a clean #11213-based Release checkout, built guest_math_tests, and ran guest_math three times; all passed. I did not rerun the author's native differential harness. The current-main merge preview is clean, and main has not changed the files this PR touches since its
1089f2bbase.I approve the implementation and focused test evidence. #11077 remains open and is explicitly declared as a dependency; merge this PR after that overlap is resolved in the declared order.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T16:51:51Z.
Review: APPROVED
Re-reviewed exact current head
8365772e47fbce5df3fda265bd7b194ea2a4e98d. The post-approval delta adds the requirede_acosl.ccopyright/license notice immediately above the port; it matches FreeBSD release/11.4.0. The reviewedacoslimplementation, tests and declared dependency on #11077 remain unchanged. Hosted Ubuntu, Windows, macOS, ASan, register-context and write-watch checks pass; Linux product is skipped by workflow filters. The author reportsguest_mathand title-binary comparisons on the new heads. No PS5 title run is claimed.