fix(libc): compute atanl with FreeBSD msun's ld80 code as the bundled libc.prx does #11262
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/11262"
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:58:47Z.
What
atanlcalled the hoststd::atan, which differed from the console's result on 734,205 of 20 million random arguments. This PR replaces it with FreeBSD msun'ss_atanl.c(ld80), theatanlof thelibc.prxthat PPSA21564 bundles (0x417f0, the library's own code).The binary follows
s_atanl.cstep by step:0x4040(ATAN_CONST), a NaN returnsx + xand anything else returns±(atanhi[3] + atanlo[3]).expman = exponent << 8 | mantissa bits 55–62. Below0x3ffdc0(|x| < 0.4375), it evaluates1e300 + x > 1(thedouble1e300, before the exponent test) and returnsxwhen the exponent is below0x3fdf. Otherwise it returnsx - x*(s1 + s2).0x3ffe60,0x3fff30and0x400038. They select(2x - 1)/(2 + x),(x - 1)/(x + 1),(x - 1.5)/(1 + 1.5x)or-1/xon|x|. The result isatanhi[id] - ((x*(s1 + s2) - atanlo[id]) - x), negated for negative arguments.s1 = z*T_even(w)ands2 = w*T_odd(w)over the 13aTcoefficients, withz = x²andw = z².The three tables are taken from the binary as exact 80-bit hex literals (
aTat 0x15e9b0,atanhiat 0x15e930,atanloat 0x15e970). These are the ld80invtrig.cvalues;atanhi[3]/atanlo[3]are thepio2_hi/pio2_lothatasinlandacoslalready use.x87 precision. As with
acoslin #11260, the binary'satanlhas no instruction that reads or sets the x87 control word ands_atanl.chas noENTERI(), so it computes at the caller's precision. It addsatanhi[3] + atanlo[3]at run time, since the tables areinvtrig.cexterns, so at 53 bits the large-argument result rounds to 53 bits; GCC had folded that sum at 64 bits. The port now adds it through #11260'sSumAtCallerPrecision.atanlgets its own debt entry likeacosl's (it runs at the caller's precision), and is removed from the list of long-double functions the host computes.Licence. The notices of
ld80/invtrig.cands_atanl.care copied verbatim directly above the coefficient tables and aboveatanl. The tables moved next toatanlfor that, asMath.cppalready does for the msun code ofsinh.Tested
On Ubuntu 24.04 under WSL2 (GCC 13.3, Release), head
1dfac78f90, which merges #11260's head0de3168b81(withmain813f46cd68) into the previous head0ecffe5ca5, without a conflict.0ecffe5ca5merged #11260's head39fb738477(withmaina4eca2a056) into6479c72ac1, without a conflict either. That head merged #11260'sc6faef7cb0(withmaine0caf13600) intoab63a051c5, where the merge conflicted wheremain'stanhffrom #9867 met this stack'satanland the oldtanhfstub; it keepsmain'stanhfand this PR'satanl:libc.prx. Against the binary, 5,000,000 arguments of every kind (±0, ±∞, 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.prxtext and read-only segments mapped into an executable buffer, with itsatanlcalled directly. It covers 20,000,000 random arguments of either sign, with magnitudes from 2^-127 to 2^67 and every branch weighted, plus the exponent edges0x3fde–0x3fe0,0x3ffc–0x4000,0x403f–0x4041and0x7ffe. The port matches all of them bit for bit (0 mismatches) at each of 64-, 53- and 24-bit precision (control words 0x37f, 0x27f and 0x07f), while the hostatanlmismatches on 734,205 at 64 bits. BeforeSumAtCallerPrecision, the 53-bit run mismatched on 12,475 of 2,000,000, all from the large-argument return.guest_mathpasses (3 runs). It gainsCheckAtanl, with 27 known-answer pairs from that binary covering every branch, ±0, ±∞, NaN and2^64. Four were chosen because the hostatanlgets them wrong, for example0xef160152f75d188c:3fff → 0x8a24f59a3321c1ee:3fff, where the host returns…ed. Nine arguments, covering each reduction, ±2^65 and -∞, also run at 53 bits through #11260'sExtendedBitsAt53Bits; each must give the binary's 53-bit result and leave the control word unchanged. With the sum folded again, it aborts at0x8000000000000000:4040. The existingatanl(∞)andatanl(1)checks still pass. It now also requires thatatanl(+0)at 53 bits raises noFE_INEXACT: the port had added the long double1e300L, which is not exact at 53 bits, where the binary adds the double1e300.python3 tools/check_conventions.py --base origin/mainpasses, andgit diff --checkis clean.python3 tools/pr_overlap.py --branch fix/libc-atanl-msun-ld80reports adjacent-line overlaps only:tanhf, the line aboveatanlinMath.cpp, has merged, and this head contains it. #10226 and #10229, which carried the same hunk, and #12053 (_FSinh), which rewrites the lines aftertanh, now merge cleanly with this head.fdimlines aboveacoslaremain's.GuestLongDoubletype, including the hostatanlline this PR replaces, so each 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 head 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:19:03Z.
Review: CHANGES_REQUESTED
The
atanlport and its test data are otherwise well-supported, but the implementation has the x87 precision-state gap already recorded indocs/dev/debt/libc.md: the function performs long-double arithmetic without forcing the 64-bit x87 precision used by the reference routine. Guest threads can arrive with 53-bit precision (0x27f), so the reported bit-for-bit comparison under the harness's default control word does not establish the same result for actual callers. The tests do not set a non-default control word or check restoration. Please add a scoped 64-bit precision mode with a regression under 53-bit precision that verifies both result bits and restoration, or provide evidence that the console contract preserves the caller's mode and test both modes. The hosted check suite now passes, including macOS Intel Release register-context; product builds are skipped, and I have not independently run the author's comparison.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-27T11:00:43Z.
Review: CHANGES_REQUESTED
The PR describes bitwise comparisons against the bundled libc.prx across 20 million arguments, all three x87 precision settings, and the key special/large-argument paths. I am not approving this submitted head yet: its merge-base with main is
68cc9d4e75, while current main is3fea2a937b. Main has since changed Math.cpp, GuestMath.cpp, and docs/dev/debt/libc.md, all of which this PR edits. The tested tree named in the description is5b5d24cb22, not the submitted head's base.This PR also depends on #11260 and #9867, and its branch is stacked on #11260. Please coordinate the rebase after those dependencies are integrated, then rerun guest_math and confirm the updated checklist still names the required dependencies. I found no independently reproduced arithmetic failure; I cannot validate the integrated result until the stack is current.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-27T15:24:01Z.
Review: CHANGES_REQUESTED
I reviewed the current head c7362dc28e2c400414090ded8690f031741a9a52. The reduction thresholds, rational approximation, and caller-precision large-argument sum follow the pinned FreeBSD ld80 implementation. The PR now tests the bundled binary's results at 64-, 53-, and 24-bit precision and leaves the control word unchanged, addressing the prior precision-state concern. I found no separate arithmetic defect.
Independent focused check: I applied the #11260 and #11262 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 verifies the stack on its
1089f2bbase plus unrelated #11213 exception changes, not the current integrated main tree.The integration request remains: #11260 and #9867 are still open, and the stack is based on
1089f2bwhile current main is2ee8e97(30 commits ahead). Please rebase after those dependencies land and rerun guest_math on the integrated head before approval.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-27T15:25:37Z.
Review: APPROVED
Reviewed exact head c7362dc28e2c400414090ded8690f031741a9a52. The atanl range reductions, coefficient tables, and caller-precision large-argument sum follow the pinned FreeBSD ld80 implementation. The new tests and reported comparisons cover 64-, 53-, and 24-bit precision and control-word preservation. I found no arithmetic defect.
Independent local check: applied the #11260 and #11262 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 these files since the stack's
1089f2bbase.I approve the implementation and focused test evidence. #11260 and #9867 remain open and are explicitly declared dependencies; preserve their stated landing order before merging this stack.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T16:51:52Z.
Review: APPROVED
Re-reviewed exact current head
aaa0f5c0fe687bf6bde4cb2fb96969f78abe18f7. The rebase moves the existing atan coefficient tables beside their use and adds theinvtrig.c/s_atanl.ccopyright/license notices; those notices match FreeBSD release/11.4.0, and the coefficient values and reviewed algorithm are unchanged. The #11260 and #9867 dependencies remain declared. Hosted Ubuntu, Windows, macOS, ASan, register-context and write-watch checks pass; Linux product is skipped by workflow filters. The author reports exact-head guest math and comparison runs. No PS5 title run is claimed.