fix(libc): compute sinh with FreeBSD msun's algorithm #11556
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/11556"
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-26T19:21:50Z.
What
sinhreturned the hostlibm's result. It now follows the thresholds and branches of FreeBSD 11.4'se_sinh.c: below 22 in magnitude through this file'sExpm1(the FreeBSDexpm1port already here), from 22 to ln(DBL_MAX) throughexp, up to the overflow threshold throughexpscaled ask_exp.c's__ldexp_expdoes, andx * 1e307beyond it. On overflow it setserrnotoERANGE, as the host'ssinhdid andsinhfdoes; msun itself sets none, and the console'ssinhon overflow is not measured (the differential described under Tested may contain no overflowing argument).The host differs from msun in the last bit for many arguments:
sinh(0x3ff401fffa851ce0)is0x3ff9a53cef355b40in a local build of msun and0x3ff9a53cef355b3fwith the Windows UCRT; of 2,000,000 random arguments in [-20, 20] the UCRT'ssinhdiffers from msun on 561,202 and glibc's on 1,795, and this implementation on none (0 of 2,000,000 as well in [-22, 22] and in [-0.001, 0.001]).The thresholds and branches follow
e_sinh.c; results are bit-identical to msun below 22 in magnitude. From 22 on they depend on this file'sexp, which is still the host's, so they can differ from msun in the last bit untilexpfollows msun. In [22, 709.78], 190,199 of 2,000,000 random arguments differ with the UCRT'sexp, against 189,717 for the host'ssinh, so that range is marginally worse than before; between 709.78 and the overflow threshold (710.4758) it is much better, 189,439 against 1,140,352. The entry indocs/dev/debt/libc.mdsays that the results from 22 on can differ.The test
guest_mathgetsCheckSinhMsunBits: 13 structured inputs (signed zeros, the 2^-28 and 1.0 boundaries, 21.999..., the smallest denormal, the infinities) and 20 arguments where both the Windows UCRT and glibc differ from msun, with msun's bits as the expected values. Fix and test are separate commits; the test fails onmain.Tested
mainafc780e681(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 sinh failed for bfd6a0128184d680: got bfd719794d320781, expected bfd719794d320780".sinh_nid_postfix, each makingguest_mathfail on Windows: the threshold 22 changed to 21 (fails at403527ff77fdabb1: got41c6f9a671b8ec61, expected41c6f9a671b8ec60), the sign ofhdropped (fails atbe30000000000000: got3e30000000000000), and the|x| < 2^-28branch removed (fails at3e2fffffffffffff: got3e30000000000000).-O3; the CMake files set no-ffp-contractor-mfma): the same arms give the same results, andguest_math,guest_math_exactandguest_dinkumware_internalspass. This ran on an earlier head of these commits (cc83507285, on an oldermain) whose added and removed lines inMath.cppandGuestMath.cppare the same as this head's; only the surrounding lines frommainand the place of the debt entry changed since.lib/msun(BSD licence; not in the repository) with all its symbols renamed, compared on the same random arguments with thesinhcode taken fromMath.cpp; that measures this host against msun. A differential run by others against thelibc.prxof a title (300,000 arguments, comment, head1f8adf12f5) finds every argument below 22 in magnitude bit-identical and 8,585 differences of one ulp in theexprange, which goes through this file's hostexp; itserrnoagrees on all 300,000 arguments, a set that, as the commenter notes, may contain no overflowing one. The systemlibSceLibcInternalis unmeasured.errnotoERANGEin the overflow path, which the host'ssinhdid and the first version of this port did not;mainis merged in (main81267b2204). The new check puts a sentinel inerrnoand requiresERANGEwith an infinity of the right sign for0x408633cf00000000andDBL_MAXin both signs, and the sentinel unchanged for a finite argument, the infinities, 1 and 0. Without theerrnolineguest_mathfails with "Guest sinh overflow failed for 408633cf00000000: got 7ff0000000000000, errno 1234", and witherrnoset on every call it fails with "Guest sinh changed errno for 408633ce8fb9e87d: errno 34"; both on Windows (UCRT) and Ubuntu 24.04 under WSL2 (a virtualised kernel, not bare-metal), whereguest_mathpasses 3 of 3 runs on this head, as doguest_math_exactandguest_dinkumware_internals.errnobeyond the overflow case (compared with nothing), builds with FMA contraction (-mfma,-march=native), arguments of magnitude 22 or more against msun'sexp.Overlap
No open PR changes
sinh_nid_postfix;sinhhas no other open or merged PR (#8023 changes the Dinkumware_FSinhscaling helper, not this function). 152 open PRs touchMath.cpp,GuestMath.cppordocs/dev/debt/libc.md(or list 100 or more files);git merge-treeof this head with each (againstmainafc780e681): 108 merge without a conflict and 44 conflict, 43 of which already conflict withmainon their own. #10250 (sin, cos and tan) edits the lines next tosinh_nid_postfixand conflicts with this change; it merges cleanly intomainon its own, so whichever lands second rebases. From 22 on,sinhcallsexp_nid_postfix; #10234 (exp) changes that function's body only, 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-26T19:25:13Z.
Review: CHANGES_REQUESTED
Reviewed head
0567c826e7fb89b1b4de97bb1e07c21a8bd0133aand compared the implementation with the referenced FreeBSD 11.4e_sinh.candk_exp.cpaths.[P2]
core/libs/tests/GuestMath.cpp,CheckSinhMsunBits: exercise the new large-input branches. Every finite added case has|x| < 22; the rest are signed zero, subnormal values, or infinities. Consequently none of these tests execute either new branch at|x| >= 22, includingSinhLdexpExpHalf's exponent reconstruction or the final overflow path. Those branches cover values close toDBL_MAXand are a substantial part of this change. Please add positive and negative cases on both sides of the 22,0x40862e42, and0x408633cecutoffs, including the largest finite result/first overflowing input as appropriate, with expectations or finite/overflow checks derived from the FreeBSD reference. The current lower-range bit vectors don't validate this code.The reported Windows and WSL runs establish the lower-range behavior, but the PR body explicitly says inputs at or above 22 were not compared against msun's
exp; no current test exercises those paths. I did not approve pending that focused coverage.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-27T00:21:42Z.
Review: APPROVED
Re-reviewed exact head
6e418d3342c1285bb92c27d3dcc391d0db17a36b. The follow-up adds positive and negative coverage across the|x| = 22,0x40862e42, and0x408633cebranch boundaries, includes the scaled-exponential case where directexpoverflows, and checks finite-near-overflow and signed overflow results. That addresses my earlier coverage request. The report says the new cases were checked against a local FreeBSD msun build; I did not run them locally in this workspace. Current hosted conventions and platform policy checks pass; product jobs are skipped by path policy. No remaining actionable finding from this review.Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-27T01:16:46Z.
Review: COMMENTED
Run on bare-metal Linux and on current
main, since the Linux run in the PR was WSL2 on an older head.Exact head
6e418d3342c1285bb92c27d3dcc391d0db17a36b, merged onmaincb45ae2dd(merges cleanly). Linux x86_64 (bare metal), i7-8700, Ubuntu 26.04, GCC 15.2, glibc 2.43, CMake 4.2.3, Ninja, Release,BUILD_TESTING=ON, full build.guest_mathpasses, 3 of 3 runs. The 22guest_math*|guest_dinkumware*tests pass,guest_math_exactandguest_dinkumware_internalsincluded.core/libs/prx/libc/src/Math.cppreverted tomain(new tests kept),guest_mathaborts withGuest sinh failed for bfd6a0128184d680: got bfd719794d320781, expected bfd719794d320780, the same first mismatch as the UCRT run in the description. So glibc 2.43'ssinhalso misses the last bit there and the test fails on Linux too.Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.