feat(libc): add exact long double and BSD math exports #6345
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/6345"
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 hypermoist on 2026-10-15T12:39:08Z.
What
Adds 40 libc math exports that AnyPS5 did not have, in a new
src/MathExact.cpp. Every one is matched exactly against the console:copysignl,fdiml,fmodl,remainderl,remquol,logbl,ilogbl,rintl,roundl,lroundl,lrintl,llrintl,sqrtl,modfl,nanl,scalblnl,fmalrint(f),lrint(f),llrint(f),drem(f),scalbln(f),scalb(f),finite(f)__fpclassifyl,__isinfl,__isnanl,__isnormall,_LDtest,_Dclass,_FDclass,_LDclass,_LDsignConsole behaviour that is reproduced (found by running the console binary side by side, see Tested):
fdiml,roundl,modfl, andfmalwith a NaNz, return a signalling NaN operand unquieted;rint/rintfquiet it.nanlignores its argument and always returns the default quiet NaN.fmodlsetsEDOMwheneverxis infinite or NaN,yis zero oryis NaN (NaN inputs included);remquol,remainderlanddrem(f)set it only for real domain errors.scalb(f)never touches errno.remquolreports the quotient modulo 8 with its sign (glibc can report ±8 where the console reports 0).ilogbl(NaN)isINT_MAX;scalblnltruncates its exponent toint(scalblnl(1, 2^32 + 1) == 2).fmalis FreeBSD's:x*y + zwhenxoryis zero or non-finite,x*ywhenzis zero,zwhen onlyzis non-finite, andx*ywhenzis more than 128 binades below the product (so a midpoint product rounds to even instead of usingz's sign).__fpclassifylreturns the Dinkumware codes (-2 denormal, -1 finite, 0 zero, 1 infinite, 2 NaN), like_LDtest; they differ only on pseudo-denormals, which_LDtesttreats as finite._LDsignreturns the raw sign bit (-32768).Two known gaps are recorded in
docs/dev/debt/libc.md:remquolleaves*quounchanged when the result is NaN (the console stores a value that depends on earlier x87 state), and non-canonical 80-bit encodings passed to the arithmetic functions follow the host x87.Tested
Linux (CachyOS, GCC Release build, plus a clang
-Wall -Wextrasyntax check): newguest_math_exacttest and the fullctestsuite pass.tools/check_conventions.pypasses;tools/pr_overlap.py --branchreports no line overlap.Differential test against the console: the PPSA08427
libc.prxwas mapped into a Linux process (its__errorimport bound to the test's errno) and all 40 functions were run side by side with this implementation on 200,000 random inputs each (random exponents and mantissas, signalling and quiet NaNs with payloads, infinities, zeros, subnormals, hugelongexponents). Results agree bit for bit, and errno agrees, everywhere except theremquolNaN quotient noted above. All 40 NIDs are also exported by a newerlibc.prxbuild (J03848509).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
https://claude.ai/code/session_01Q8tinbP1MUpUaDsbNWnGnu
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-15T12:39:53Z.
Progress report (
9f5d4bd→1181068)📈 System libraries: 91.8% (+0.08%, +40 implemented functions)
✅ 40 implemented
_Dclass_nid_postfix_FDclass_nid_postfix_LDclass_nid_postfix_LDsign_nid_postfix_LDtest_nid_postfix__fpclassifyl_nid_postfix__isinfl_nid_postfix__isnanl_nid_postfix__isnormall_nid_postfixcopysignl_nid_postfixdrem_nid_postfixdremf_nid_postfixfdiml_nid_postfixfinite_nid_postfixfinitef_nid_postfixfmal_nid_postfixfmodl_nid_postfixilogbl_nid_postfixllrint_nid_postfixllrintf_nid_postfixllrintl_nid_postfixlogbl_nid_postfixlrint_nid_postfixlrintf_nid_postfixlrintl_nid_postfixlroundl_nid_postfixmodfl_nid_postfixnanl_nid_postfixremainderl_nid_postfixremquol_nid_postfixrint_nid_postfixrintf_nid_postfixrintl_nid_postfixroundl_nid_postfixscalb_nid_postfixscalbf_nid_postfixscalbln_nid_postfixscalblnf_nid_postfixscalblnl_nid_postfixsqrtl_nid_postfixImported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-15T12:41:57Z.
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.
main)docs/dev/debt/libc.mdL64Some 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.
docs/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.mddocs/dev/debt/libc.mddocs/dev/debt/libc.mdcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mdcore/libs/prx/libc/CMakeLists.txtcore/libs/prx/libc/CMakeLists.txtcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mdcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mdImported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-15T14:41:50Z.
Title evidence for this PR: Spider-Man: Edge of Time (PPSA99300), added to the homebrew catalog today, stops on
llrintonmain.Windows 11 10.0.26300, WinLibs GCC 15.2.0 r7, Release.
main0afa6a607ealone, then with this PR's commit4f608a8c4cherry-picked onto it (4 files, no conflicts); the catalog release converted withrelinker --windowsand run for 60 s.main: the title exits after 1 s with 0xC0000139 (entry point not found):-431A-YBAksisllrint(tools/nid_names.py --nid=-431A-YBAks).With this PR it starts, runs its ReXGlue startup and exits by itself with code 0 after reporting the files it needs from the user:
That message is the title's own check for the game data it does not ship, so this PR is all it needs to reach it.
AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-15T18:36:07Z.
Review: COMMENTED
On macOS,
drem,dremfandremainderldon't setEDOM. The other paths that the description says seterrno(fmodl,remquol) set it explicitly. These three returnstd::remainderdirectly, so they rely on the host libm settingerrno. glibc does that. Apple's libm never setserrno:math_errhandlingisMATH_ERREXCEPTonly.Snippet, macOS 27.2, Apple clang (Xcode 27), M2 Max:
So on a macOS host a guest gets
errno == 0where the console setsEDOM.CheckErrnoinGuestMathExact.cppwould also abort atRequire(std::isnan(drem_nid_postfix(1.0, 0.0)) && errno == EDOM). The fix would be the same explicit check thatremquolalready uses (!isnan(x) && !isnan(y) && (isinf(x) || y == 0)→errno = EDOM) inremainderl,dremanddremf. That makes them independent of the host libm.Not tested: I did not build this PR.
libcdoesn't build on macOS frommain, so this is the host-libm behaviour checked with a standalone snippet. Head4f608a8c4.Written with AI assistance (Claude Code). I ran the check above on my machine.
Imported from boykopovar/AnyPS5. Originally posted by nicolasfalesy on 2026-10-15T19:29:42Z.
A title that reaches this: Spider-Man: Edge of Time (PPSA99300) from the homebrew catalog, on Linux (Intel Arc, Mesa ANV).
main92e50418f: stops at load with./app.elf: symbol lookup error: ./app.elf: undefined symbol: -431A-YBAks(llrint, libc).main+ this PR: it loads and runs to its own check,startup: fatal: reeot - game files missing: No Default.xex in /app0/gamedata, then exits with status 0 (no game data was installed for the test).Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-18T23:13:20Z.
Review: CHANGES_REQUESTED
Thanks for the thorough differential testing. One thing to fix before merge, already reported by @jotitadev:
drem,dremfandremainderl(MathExact.cpp:83-85,:179-185) returnstd::remainderdirectly, soerrno == EDOMforxinfinite ory == 0depends on the host libm. glibc sets it; Apple's libm (and likely MinGW's) does not, so a guest getserrno == 0where the console setsEDOM, andCheckErrno(GuestMathExact.cpp:71) aborts on those hosts. Use the same explicit check asremquol(!isnan(x) && !isnan(y) && (isinf(x) || y == 0)→errno = EDOM) in all three, and addremainderlanddremfcases to the test.Imported from boykopovar/AnyPS5. Originally posted by cadenasynyster on 2026-10-19T00:33:22Z.
Windows results for this head (
4f608a8c4), merged onto current main without conflicts. Windows 11, WinLibs GCC 15.2.0 r7 (x86_64-ucrt-posix-seh), Release.guest_math_exact aborts on Windows, 3 of 3 runs (exit code 0xc0000409, SIGABRT inside the test). It is not CheckErrno. I ran a local copy of the test where Require prints its line number and carries on, so every failing check shows: exactly one fails, GuestMathExact.cpp:77 in CheckConsoleQuirks, the pair
scalblnl(1.0L, 0x100000001L) == 2.0L && scalblnl(1.0L, LONG_MAX) == 0.5L. I did not isolate which half. The LONG_MAX half cannot hold on Windows, since long is 32 bits there: a standalone program prints sizeof(long) = 4, LONG_MAX = 2147483647, and std::scalbln(1.0L, LONG_MAX) = inf. On the console (and Linux), truncating LONG_MAX to int gives -1, hence 0.5. Everything else in the test passes on Windows, including the drem errno row (CheckErrno, line 71). The check probably needs an exponent that does not depend on the width of the host long.errno of the host remainder family, for the review point on drem, dremf and remainderl. A standalone program calling std::remainder, std::fmod and std::remquo (not this PR's code), same toolchain:
So on Windows/MinGW only remainderl has the gap; "likely MinGW's" holds for long double only. Going by this, a remainderl row in CheckErrno would abort on Windows without the explicit check (not run).
Not run: Linux and macOS, and the fix itself.
AI-assisted: yes