Repository navigation
fix(libc): compute atanl with FreeBSD msun's ld80 code as the bundled libc.prx does - #11262
Conversation
|
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.
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.
|
MarcZX10
left a comment
There was a problem hiding this comment.
The atanl port and its test data are otherwise well-supported, but the implementation has the x87 precision-state gap already recorded in docs/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.
|
Windows run of head
AI-assisted: yes (Claude Code); results above were run on this machine. |
b3bd0e6 to
7f5f340
Compare
|
@MarcZX10 Same answer as on #11260 (#11260 (comment)). The bundled At 53 bits the port differed from it only for |x| ≥ 2^65, where the binary adds |
|
Windows run of this head
AI-assisted: yes (Claude Code) |
MarcZX10
left a comment
There was a problem hiding this comment.
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 68cc9d4, while current main is 3fea2a9. 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 is 5b5d24c, 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.
|
Windows result for head Host: Windows 11 26300, Intel i7-12700K, WinLibs GCC 15.2.0 (x86_64-ucrt-posix-seh, r6), Release, The tree also carried the other open libc PRs #11260, #11266, #11632, #11634, #11278, #11270, #11283, #10929, all merged cleanly, so the run is a joint one; the tests of this PR are AI-assisted: yes (Claude Code), the runs and the numbers above are from this machine. |
7f5f340 to
c7362dc
Compare
|
@MarcZX10 Rebased onto #11260's 23a6c80 on That run found one difference, now fixed. The small-argument test added the long double The dependencies are unchanged (#11260 and #9867); I'll rebase again when #9867 lands. |
MarcZX10
left a comment
There was a problem hiding this comment.
I reviewed the current head c7362dc. 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 1089f2b base 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 1089f2b while current main is 2ee8e97 (30 commits ahead). Please rebase after those dependencies land and rerun guest_math on the integrated head before approval.
MarcZX10
left a comment
There was a problem hiding this comment.
Reviewed exact head c7362dc. 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 1089f2b base.
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.
|
A runtime differential of this head against the Setup:
Results:
Only finite arguments in that range were covered: no NaNs, infinities, subnormals or arguments of large magnitude. AI-assisted: yes (Claude Code). |
|
@MarcZX10 Head ab63a05 merges #11260's refreshed head 84502b8 ( |
|
@MarcZX10 #9867 has merged, which left this PR conflicting with |
|
@MarcZX10 Head 0ecffe5 merges #11260's refreshed head 39fb738 (current |
✅ AMD GPU test: passedPR head
Unstable on main (1, these also failed in recent main runs on this machine; not counted against this PR)Failing on main too (13, not counted against this PR)Validation IDs already present on main (4 IDs in 765 tests)Skipped (61)ReproduceAutomated build and test on a contributor's AMD machine; it does not replace upstream CI. A new comment is posted for each new push. Full logs. |
|
@MarcZX10 Head 1dfac78 merges #11260's refreshed head 0de3168 (current |
|
Retested head Host: Windows 11 26300, Intel i7-12700K, WinLibs GCC 15.2.0 (x86_64-ucrt-posix-seh, r6), Release,
AI-assisted: yes (Claude Code), the runs and the numbers above are from this machine. |
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 1dfac78, which merges #11260's head 0de3168 (with
main813f46c) into the previous head 0ecffe5, without a conflict. 0ecffe5 merged #11260's head 39fb738 (withmaina4eca2a) into 6479c72, without a conflict either. That head merged #11260's c6faef7 (withmaine0caf13) into ab63a05, 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 fix(libc): compute acosl with FreeBSD msun's ld80 code as the bundled libc.prx does #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. fix(libc): compute erff and erfcf with fdlibm's algorithm, as titles' libc.prx does #10226 and fix(libc): compute atan and atan2 with FreeBSD msun's algorithm, as titles' libc.prx does #10229, which carried the same hunk, and fix(libc): compute _FSinh as titles do #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)