fix(libc): compute asinh with FreeBSD msun's algorithm #11737
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/11737"
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 xXPBSXx on 2026-10-27T06:24:44Z.
What
asinhreturned the hostlibm's result. It now follows the branches and thresholds of FreeBSD 11.4'ss_asinh.c: below 2^-28 it returnsx; up to|x| = 2it uses this file's msunlog1p; from there to 2^28 it useslog(2|x| + 1/(sqrt(x*x+1)+|x|))and above 2^28log(|x|) + ln2, with this file'slog, which is still the host's. It sets noerrno, as msun does not.The host differs from msun in the last bit for many arguments:
asinh(0x3e2fffffffffffff)is0x3e2fffffffffffffin a local build of msun and0x3e2ffffffffffffewith the Windows UCRT, andasinh(0x3fc858aceb437920)is0x3fc833b16e1536cfin msun and0x3fc833b16e1536cewith glibc 2.39. Of 2,000,000 random arguments in [0, 2] (both signs) the UCRT'sasinhdiffers from msun on 319,633 and glibc's on 1,212, and this implementation on none (0 as well in [3.8e-9, 2] log-uniformly, 0 of 2,000,000 below 2^-28 on both hosts).Bits are identical to msun up to
|x| = 2. Above 2 they depend on the hostloguntil it follows msun: of 2,000,000 log-uniform random arguments in (2, 2^28], 38,719 differ with the UCRT'slogand 38,926 with glibc's (for the host'sasinhthe numbers are 131,924 and 38,926), and none differ above 2^28 on either host, which is how those twologimplementations happen to round, not a property of this code. The entry indocs/dev/debt/libc.mdsays so.The test
guest_mathgetsCheckAsinhMsunBits(32 cases, both signs, with msun's bits as the expected values: signed zero, the smallest denormal, both sides of the 2^-28 cutoff, 0.5, 1, both sides of 2, 12 arguments below 2 where both the UCRT and glibc differ from msun, two just above 2 and three just above 2^28 where the neighbouring branch's formula would change the last bit; the latter three depend on the hostlogand match msun with the UCRT's and with glibc 2.39's) andCheckAsinhLargeArguments(14 arguments above 2 in both signs, accepted within 4 ulps of msun's bits because they depend on the hostlog, plus the infinities and a NaN). Fix and test are separate commits; the test fails onmain.Tested
mainb7105f7285(ninja exit code 0):guest_mathpasses 3 of 3 runs;guest_math_exactandguest_dinkumware_internalspass.Math.cppofmain(the production change reverted) and the new test,guest_mathfails (exit code 0xc0000409) with "Guest asinh failed for 3e2fffffffffffff: got 3e2ffffffffffffe, expected 3e2fffffffffffff".asinh_nid_postfix, each makingguest_mathfail on Windows with the failing argument in the message: the tiny-argument return removed, the 2^-28 cutoff moved down by one, the cutoff at 2 moved down by one, the cutoff at 2^28 moved down by one, the+ ln2dropped from the large branch, the factor 2 dropped from the middle branch, the1 +dropped from thelog1pbranch, and the sign dropped.-O3; the CMake files set no-ffp-contractor-mfma), same base: the same arms give the same results,guest_mathpasses 3 of 3 runs, andguest_math_exactandguest_dinkumware_internalspass.main'sMath.cppand the eight deliberate defects were measured on an earlier rebase of these two commits (main9e2e90dc00;git range-diffshows=for both commits, andMath.cppandGuestMath.cppare byte-identical to this head's); the three passing runs and the other two tests were repeated on this head, on both systems.lib/msun(BSD licence; not in the repository) with all its symbols renamed, compared on random arguments with theasinhcode taken fromMath.cpp; that measures these hosts against msun. A differential run by others against thelibc.prxof a title (300,000 arguments, comment, head4902c38569) finds every argument up to 2 in magnitude bit-identical and 2,313 differences of one ulp above 2, which goes through this file's hostlog; itserrnoagrees on all 300,000 arguments. The systemlibSceLibcInternalis unmeasured.errno(compared with nothing), builds with FMA contraction (-mfma,-march=native), other glibc versions.Overlap
No open PR changes
asinh_nid_postfix, andasinhhas no other open or merged PR (#1718 was closed; #3190 only exported it). 140 open PRs touchMath.cpp,GuestMath.cppordocs/dev/debt/libc.md(or list 100 or more files);git merge-treeof this head with each (againstmainb7105f7285): 113 merge without a conflict and 27 conflict, 25 of which already conflict withmainon their own. The other one is #10002 (asinhf), whose changed line is directly belowasinh_nid_postfix; #11556 (sinh) has been merged, and this branch mergedmainafter it. From 2 up,asinhcalls this file'slog_nid_postfix, which is still the host'slog; #10245 (log) changes only that function's body, and results there should become bit-identical to msun once it lands (not measured).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)AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-27T06:29:30Z.
Review: DISMISSED
The numerical work is unusually well documented, and the branch structure and thresholds match FreeBSD 11.4
s_asinh.c(including its small-input return,2and2^28boundaries, formulas, and sign handling). I found two repository/process blockers before approval:s_asinh.c, whose Sun Microsystems permission explicitly requires retaining its notice: https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/msun/src/s_asinh.c . The changed code does not preserve that notice or identify how it is retained. Please add the required attribution/license text in a way compatible with this repository's conventions, or clarify that the code was independently written from the mathematical description and not adapted from that implementation.asinhfadjacent to this implementation) and #11556 (the adjacentGuestMath.cpptest insertion), but the checklist saysDepends on: noneand the report says only that whichever lands second will rebase.CONTRIBUTING.mdrequires the authors to agree which goes first and list it as a dependency. Please coordinate with both authors and update the checklist accordingly.I compared the implementation against the cited FreeBSD source and reviewed the tests and debt entry. I did not run
guest_mathlocally: this workspace's Linux configure stops because the installed system Vulkan-Headers lack the requiredVK_KHR_maintenance8; the product CI jobs are skipped on this PR. Current hosted conventions and Linux relinker checks pass; the Windows and macOS relinker jobs were still pending at the latest queue refresh.