fix(libc): compute sincos with FreeBSD msun's algorithm #16740
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/16740"
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 AlexWhitehouse on 2026-11-07T14:16:45Z.
What
libc
sincos(NIDjMB7EFyu30Y) returned the host library'ssin(x)andcos(x)(glibc on Linux, the UCRT on Windows). The console's libc uses FreeBSD msun, and this file's ownsin,cos,tan,sinf,cosf,tanfandsincosfare already msun ports that match titles'libc.prx. On ordinary inputs the host result can be 1 ulp off msun. For example, for0xbff38db77b728354glibc's cosine is0x3fd5ddfc9f502f3aand msun's is0x3fd5ddfc9f502f39; for0x3ff3cdd6b716682fthe sine is…f80bagainst msun's…f80c.This PR ports FreeBSD release/11.4.0 msun's
s_sincos.cintocore/libs/prx/libc/src/Math.cpp, with its notice copied byte for byte. It reuses theHighWordandRemPio2argument reduction and theKernelSinandKernelCoskernels thatsinandcosalready use, which are the 11.4 revisions their debt entry cites. Upstream'sk_sincos.hevaluates the same expressions ask_sin.candk_cos.c(itsy / 2andz / 2equal0.5 * yand0.5 * zexactly), so it is not copied. Upstream's tiny-argument cutoff andx - xNaN/Inf results are kept.errno: upstream msun sets none, but titles'libc.prxsetsEDOMfor these functions. This file'ssinandcosset it when the argument's high word is0x7ff00000or0xfff00000(the infinities and the NaNs sharing that word), andsincosfsets it for an infinite argument, both measured against titles' copies.sincosnow followssinandcos, so it keeps theEDOMthatmain(glibc) gave for infinities. Other NaNs leaveerrnoalone, as withsinandcos. Thelibc.prxbundled with PPSA07923 and PPSA05916 givessincosthe same bits anderrnoas its ownsinandcoson 300,000 inputs each, as a reviewer measured (https://github.com/boykopovar/AnyPS5/pull/16740#issuecomment-6718722071); the debt entry cites it.Tested
guest_math_elementary(core/libs/tests/GuestMathElementary.cpp) gains 88 exact-bitsincoscases, each with its expectederrno, plus 10,000 sampled finite inputs. Every existing assertion is kept.|x| < π/4, the medium reduction, and the largek_rem_pio2reduction up to ±DBL_MAX), infinities and NaNs.-ffp-contract=off.sinandcosbit for bit.main'sMath.cpp,guest_math_elementaryfails withsincos(bff38db77b728354): got bfee12fe51d52ed4 3fd5ddfc9f502f3a, expected bfee12fe51d52ed4 3fd5ddfc9f502f39. With this PR (5c27ee6caa),guest_math,guest_math_elementaryandguest_math_exactpass 3/3.3a43c7f59b, reusingKernelSin/KernelCos): on Linux,guest_math,guest_math_elementaryandguest_math_exactpass with the same 88 exact cases and 10,000 samples. On Windows 11 (WinLibs GCC 15.2.0 UCRT, Release), a reviewer's run merged ontomain6b733c4ef2passes the same three, 3 of 3 (https://github.com/boykopovar/AnyPS5/pull/16740#pullrequestreview-5815124572).-ffp-contract=off): the same three pass at5c27ee6caa, with no warnings. Withmain'sMath.cpp,guest_math_elementaryfails on the same input with the same message.sincos(3ff3cdd6b716682f): got 3fd4ec422e520ddc 3fee3dddabdaf80c, expected 3fee3dddabdaf80c 3fd4ec422e520ddc, at3a43c7f59b);EDOMassignment fails itserrnocheck.python3 tools/check_conventions.py --base origin/main: all conventions checks passed.git diff --checkflags only whitespace inside the copied upstream notices.python3 tools/pr_overlap.py --branch fix/libc-sincos-msun: no open PR changessincos. #13778 (msun kernels in libSceLibcInternal) does not touch this wrapper.Math.cpp,GuestMathElementary.cpporlibc.mdadd no conflict. That includes the open msun ports #10002, #10007, #10113, #10274, #10321, #10226, #11262, #11266, #11634, #16438, #8417, #7544, #12053, #10512, #12947 and #15802.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)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by gabichuuuuu on 2026-11-07T16:06:19Z.
Review: COMMENTED
Read against FreeBSD release/11.4.0
s_sincos.candk_sincos.h.sincos_nid_postfixmatches statement by statement: the0x3e400000threshold,(int)x == 0,x - xfor Inf/NaN, and the four quadrants with the swapped pointers.As an independent check, I ported msun's
sin/cospath to Python, without FMA, as-ffp-contract=offbuilds libc. All 66 small- and medium-range cases in the test match bit for bit. I did not portk_rem_pio2, so the 22 large-argument cases are not checked independently. The description's example also reproduces: the host givescos(0xbff38db77b728354) = 0x3fd5ddfc9f502f3a, and msun gives…39.Two small points, both optional:
KernelSincos(Math.cpp:2805) repeatsKernelSinandKernelCoswith the same coefficients. Its only differences arey / 2andz / 2in place of0.5 * yand0.5 * z, which are exact.*sinp = KernelSin(x, y, iy); *cosp = KernelCos(x, y);would keep the single argument reduction without the copy.Adapted from FreeBSD release/11.4.0 …: <URL>line, which the file's other msun notices carry.AI-assisted: yes.
Imported from boykopovar/AnyPS5. Originally posted by gabichuuuuu on 2026-11-07T21:39:08Z.
Review: COMMENTED
The Windows rerun for
3a43c7f59b. Merged ontomain6b733c4ef2(clean). Windows 11 x64, WinLibs GCC 15.2.0 r7 UCRT, Release,ctest -R '^guest_math':guest_math,guest_math_elementaryandguest_math_exactpass 3 of 3, plus a verboseguest_math_elementaryrun, also passing.mainpasses the same set.I also checked the refactor against
s_sincos.cquadrant by quadrant: case 1 givessin = KernelCos,cos = -KernelSin, case 2 negates both, and case 3 givessin = -KernelCos,cos = KernelSin, the same as the swapped-pointer calls with the negations applied after. The small-argument path keepsiy = 0. Nothing to add.AI-assisted: yes.