feat(libc): implement j0, j1, jn, y0, y1 and yn #7998
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/7998"
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 priyanshrv-netizen on 2026-10-19T17:14:52Z.
What
Implements six libc exports that no source in
core/libs/prxdeclared yet: the double Bessel functionsj0,j1,jn,y0,y1andyn(NIDstcN0ngcXegg,BNbWdC9Jg+4,QdE7Arjzxos,RvsFE8j3C38,vh9aGR3ALP0,eWSt5lscApo). I found them by diffing the export table of a systemlibc.prx(buildJ03093718) against the NIDs declared in the tree and in every open pull request. The float versions (j0f…ynf) already exist and are unchanged.core/libs/prx/libc/src/Bessel.cppfollows that module's disassembly (0x51D60, 0x52B10, 0x53850, 0x520E0, 0x52E50, 0x53CA0). It is FreeBSD msun'se_j0.c,e_j1.cande_jn.cbuilt with FMA contraction, and every constant and all 32pzero/qzero/pone/qonetables match the binary:vfmadd/vfmsub) is one FMA here, and the unfused ones stay unfused. The FMA is the instruction when__builtin_cpu_supports("fma"), otherwisestd::fma. Square roots usesqrtsd. This guards against runtimes whosefmaorsqrtis not correctly rounded. I hit that with the MSYS2mingw64(msvcrt) runtime:std::fmadiffered fromvfmaddin 9% of 10 million cancellation cases, and at-O0std::sqrtdiffered fromsqrtsdin 1,313 of 5 million arguments. The WinLibs UCRT toolchain from BUILD.md does not have the problem (measured in review), so on supported toolchains this changes nothing.y0,y1andynreturn-inffor ±0 and the quiet NaN0x7ff8000000000000for negative arguments as constants, as the console does. They raise no exception, and none of the six setserrno.jnandynhandle the order with the console's 32-bit wraparound, son = INT_MIN,INT_MAXand1 << 30behave as they do there (the last one never returns, on the console too).sin,cos,sincosandlogare the host's, as for the other libc math functions, so the last bits can differ from the console. This is recorded indocs/dev/debt/libc.md, which also notes that the y-functions deliberately return the console's constants without exceptions, as msun did before FreeBSD commit186f620727, and that the console'sy0f/y1f/ynfraise none either (measured).core/libs/tests/GuestBessel.cppchecks exact console bits on every path that does not go throughsin/cos/log, including inputs where fused and unfused evaluation differ. It also checks special values,errno, the absence of FP exceptions for the y-functions, the order identities includingINT_MIN, and agreement within 1e-12 with 60-digit mpmath values (it passes at 1e-14), withoutstd::cyl_bessel_j/std::cyl_neumann, which libc++ lacks.Tested
Windows 10, AMD Ryzen 5 4600H (Zen 2), MSYS2 MinGW-w64 GCC 15.2.0 (
mingw64, not the WinLibs UCRT toolchain from BUILD.md). I did not configure the full CMake build.GuestBessel.cppwas compiled withBessel.cppand a stub__error_nid_postfix, at-O0and-O2, and passes;Bessel.cppbuilds with-Wall -Wextra -Werror. Each of six deliberate breaks (unfusing one multiply-add, a non-FMA Horner step, fusing an unfused step, reassociating-tpi/x, dropping the rescaled recurrence, losing a sign) makes the test fail.Against the console code itself, I mapped the module's segments into executable memory, applied its relocations, and called its
j0…ynthrough System V function pointers. With this implementation using the console's ownsin,cos,sincosandlog, results matched in every bit, NaN payloads included, and in the invalid, divide-by-zero, overflow and underflow flags. That covered 90 million calls at-O2and 18 million at-O0: special values, ±40 ulps around every branch threshold, random bit patterns, log-uniform and linear ranges, orders −40…40 and 64…4096, plusINT_MIN,INT_MAX, 2^30 and 2^30+1. With MinGW's own primitives, about 3% of arguments between 2 and 30 differ from the console, against 93% for MinGW's ownj0/j1/y0/y1.tools/check_conventions.py --base origin/mainpasses.tools/pr_overlap.py --branch feat/libc-besselreports no overlap. I checked by hand the 11 pull requests it could not compare (each conflicts withmain): none adds these exports or edits nearby lines. Not tested on Linux; no title was run.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 github-actions[bot] on 2026-10-19T17:33:38Z.
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.
core/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mddocs/dev/debt/libc.mdcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mddocs/dev/debt/libc.mdcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mdcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mdcore/libs/prx/libc/CMakeLists.txtcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mddocs/dev/debt/libc.mddocs/dev/debt/libc.mdcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mddocs/dev/debt/libc.mdImported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-20T00:16:30Z.
Windows 11, Ryzen 5 9600X, WinLibs GCC 15.2.0 (UCRT, the BUILD.md toolchain), Release, full CMake configure with
BUILD_TESTING=ON. Head727bb4f1dmerged ontomaineb7500680.guest_besselpasses. Linux: WSL Ubuntu 24.04, GCC 13.3, Release, same merge: builds, andguest_besselpasses.Accuracy and bit-exactness. I used a host harness that links
Bessel.cppagainst FreeBSD release/11.4.0lib/msun/src/e_j0.c,e_j1.cande_jn.c:-O2 -mfma -ffp-contract=fast-mno-fma -ffp-contract=offAll three use the same host sin, cos and log. The reference is mpmath 1.3.0 at 60 digits; above 1e5 it uses the Hankel expansion with exact reduction, which agrees with
besselj/besselyto 1e-56. There are 77,972 inputs:Results:
std::fmasoftware path forced (77,918 inputs). Against FreeBSD-NoFMA, thousands differ (y0 on 0..60: 944/4000; jn on 0..2n+20: 2557/4980). The FMA placement is real and right.Domain-error paths differ from the FreeBSD source the debt links. In stable/11,
e_j0.c,e_j1.cande_jn.ccompute these withvone/vzero, so they raise exceptions. That came from FreeBSD commit 186f620727 (2015-03-10, "According to POSIX.1-2008 ... should raise a divide-by-zero ... invalid"); release/10.0.0 still had the constant-one/zero.0xfff8000000000000, raises FE_INVALID0x7ff8000000000000, raises nothingThe description says the console does the latter, measured by running the console module. We can't reproduce that here, but the debt should say this deliberately departs from the linked msun.
On
main, the float versions do the opposite:y0f/y1f/ynf(±0)callferaiseexcept(FE_DIVBYZERO)(Math.cpp:208, #7906, citinge_jn.c). So after this PR,y0(0.0)andy0f(0.0f)disagree on the flag. One of the two needs changing, or a note in the debt.Minor notes
fmais not correctly rounded", "std::sqrtat -O0 differs fromsqrtsd") does not reproduce on the WinLibs UCRT toolchain from BUILD.md. I measured 0/10,000,000std::fmaversusvfmaddin cancellation cases, 0/10,000,000 random, and 0/5,000,000std::sqrtat -O0 versussqrtsd. It is specific to the MSYS2mingw64(msvcrt) runtime the author used. The workaround is harmless, but the wording overstates it.GuestBessel.cpplines 137-144 add new uses ofstd::cyl_bessel_j/std::cyl_neumann, which open #7524 removes fromGuestMath.cppbecause libc++ (macOS, #7332) lacks them.check_conventions.pywarns that the newlibc.mdentry is 1,362 characters, over the 1,000-character limit.AI-assisted: yes (Claude Code); results above were run on this machine.
Imported from boykopovar/AnyPS5. Originally posted by priyanshrv-netizen on 2026-10-20T01:12:34Z.
Thanks for the thorough review and for running it on WinLibs UCRT and Linux. Pushed
16a7cece3, rebased onmain:release/10.0.0, where-one/zeroandzero/zeroare folded constants, and says this departs from msun since186f620727. I measured the console behaviour by running the module's code:y0/y1/yn(±0)return-inf, negative arguments return0x7ff8000000000000, and no flags are raised.-infat ±0 and0x7fc00000for negative arguments, raising nothing. So the doubles here match the console, and theferaiseexcept(FE_DIVBYZERO)from #7906 is what differs. The debt now says so, and I leftMath.cppalone to keep this PR to one topic.fma/sqrtcame from the MSYS2 msvcrt runtime. I removed it from the debt and corrected the description.std::cyl_*is replaced with 60 mpmath 1.3.0 references (1e-12 tolerance; it passes at 1e-14).check_conventions.pypasses without warnings.AI-assisted: yes (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-20T02:24:55Z.
I re-checked
16a7cece3.Bessel.cppis byte-identical to727bb4f1d, so the accuracy and bit-exactness results from my earlier comment still apply.main7e12b3da8, WinLibs GCC 15.2.0 UCRT, Release, full build.guest_besselpasses.ctest -R "math|bessel|nid"passes 15 of 15.main049a826e9.guest_besselpasses.tools/check_conventions.py --base origin/mainpasses with no warnings on Python 3.13 and 3.9. The debt entry is now 977 characters.besselj/besselyat 60 digits.release/10.0.0. Itse_j0.c(lines 155-156, 225-226) has the constant-one/zeroandzero/zero, and the entry names186f620727as the point where msun changed.y0f/y1f/ynfdifference from #7906 is now noted.fma/sqrtclaim is gone.Nothing else from me.
AI-assisted: yes (Claude Code); results above were run on this machine.