fix(libc): libm wrappers leave errno unchanged like FreeBSD msun #3690
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3690"
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 CouchRiv on 2026-10-11T15:22:47Z.
What
A guest that calls a libm function on a domain or range error and then reads
errnosees a host errno that FreeBSDnever sets:
The libm exports in
Math.cppforward to the hoststd::functions, and glibc sets errno for those errors. FreeBSDmsunnever does:math_errhandlingisMATH_ERREXCEPT(github.com/freebsd/freebsd-src@8c639945a4/lib/msun/src/math.h (L67-L69)).PreserveErrnoreads errno, runs the host call and writes errno back. Results and floating point exception flags donot change. It wraps 39 exports, each one a one-line change:
sin cos tan sinh cosh asinh acosh atanh exp expm1 log log2 log10 log1p sqrt pow atan2 hypot ldexp scalbn sincossinf cosf tanf coshf expf exp2f logf log2f log10f log1pf sqrtf acoshf atan2f fmodf scalbnf ldexpf remainderf sincosfThe two empty
asmstatements in the helper keep the read, the call and the write in order. Without them GCC treatssin,cosandtanas functions that never touch errno and removes a plain save and restore.Not in this PR:
exp2(#3599),fmod(#3600),acosf(#3688),asinf(#3693): the same errno fix, already open.powf: #3602 changes the same line.asin,acos, andlog10with a negative input: the result is also a different NaN sign than msun, which is avalue difference, not an errno one.
log10is wrapped and tested on the pole caselog10(0.0)only.nextafterf: left alone; its difference from msun is in the result for a signalling NaN, not in errno._FSinh,_FCosh, the long double functions: no msun counterpart, or covered by #3598.tanh,tanhf,atan,atanf,cbrt,cbrtf,asinhf,hypotf,logbf,modf,modff,fma,round,roundf,trunc: the host did not set errno for any input tried, so they stay as they are.Tested
Linux (CachyOS), GCC 16 and clang 23.1.1; RTX 4070 Ti (NVIDIA), Radeon 610M (RADV) and lavapipe (Mesa 26.2.4).
guest_mathhas one row per wrapped function: an error input and an in-range input, each pinning the result bitsand that the caller's errno (set to
EINTRbefore the call) is unchanged.sincosandsincosfcheck bothoutputs.
guest_mathpasses on NVIDIA, RADV and lavapipe;tools/check_conventions.py --base origin/mainandgit diff --checkare clean.main'sMath.cppthe same test aborts at the first row (errno 33).mainon 239,332 inputs over 65 ops (special values, random bit patterns, int exponents), built withGCC and with clang: result bits, exception flags and out-arguments are identical (0 differences), errno stays 0 for
every wrapped op. OpenLibm 0.8.8 (msun-derived), built offline, leaves errno at 0 on all of them and returns the
same bits as
mainon every error input the tests use.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repositoryAI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T15:28:57Z.
Review: CHANGES_REQUESTED
Thanks. Build and
guest_mathpass here, and the new check fails without the change. Points before this can merge:conventionsis red: the checklist item "Unimplemented paths throw (NotImplemented_nid_no_patch)…" is unchecked. Tick it (nothing here is unimplemented).acosh,asinh,atanhand the otherstd::wrappers inMath.cpp, which still set errno on the host for domain/range errors. Either cover the family in one place (one PR, with a test row per function) or say why onlyacoshfdiffers.GuestMath.cpp:499:CheckAcoshfErrno()is called between theabs_nid_postfixchecks inmain; move it next to the otherCheck*calls.Imported from boykopovar/AnyPS5. Originally posted by CouchRiv on 2026-10-11T15:57:11Z.
item (no unimplemented path is added).
tools/check_conventions.py --base origin/main: "All conventions checkspassed."
helper, one test row per function (
CheckLibmErrnoPreserved). Evidence: onmain, all 39 wrappers set errno (33 or 34) on atleast one of the probe inputs (44,005 of 165,897 calls); OpenLibm 0.8.8 sets errno on none of the 65 ops probed. Left out on purpose and listed in the body:
exp2#3599,fmod#3600,acosf#3688,asinf#3693 (open PRs),powf(#3602 touches the same line),asin/acos(NaN sign also differs),nextafterf.CheckAcoshfErrno()placement. Removed.CheckLibmErrnoPreserved()is called next to the otherCheck*calls atthe end of
main, afterCheckHypotSpecialValue(). Theacoshf(0.5f)FE_INVALIDcheck moved into it.One question on scope:
exp2(#3599),fmod(#3600),acosf(#3688) andasinf(#3693) are the same errno fix and conflict with this branch line by line. If you prefer them in this PR, I will fold them in and close those four; otherwise I keep them separate and rebase whichever lands second.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T22:01:31Z.
Review: APPROVED
mainmoved andMath.cppconflicted with__powisf2(adjacent lines). Rebased onto currentmain, keeping both sides; no logic change. Build andguest_mathpass after the rebase, and it fails withMath.cppreverted. Merging.Note: #3599, #3600, #3688, #3693 and #3602 edit lines next to these ones in
Math.cpp, so they will need a rebase after this lands.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-11T23:16:28Z.
Conflicts with main again in
Math.cppandGuestMath.cpp(#3693 and #3598 landed); please rebase. Keep #3599, #3600 and #3688 separate; they go on top of this one.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-12T09:04:48Z.
guest_mathaborts with this merged ontomain, on Linux and on Windows: https://github.com/boykopovar/AnyPS5/actions/runs/38220387093. It passed before, so it is probably one of the libm changes that went in since your rebase. Please rebase again and run it; I'll rebuild after that.Imported from boykopovar/AnyPS5. Originally posted by CouchRiv on 2026-10-12T09:14:12Z.
Rebased onto
mainatc36fd0a0e. Both commits are unchanged by the rebase (git range-diffshows them identical).Tested locally, Release, gcc, the same configure options as the Linux job:
ctest -R guest_math: passedctest -R guest_: 308 of 308 passedI could not reproduce the abort from run 38220387093 on the newer
main. That run merged the previous head onto8c4a295ff, which is older thanc36fd0a0e. Please rebuild on this head; if it still aborts, I will investigate from that log.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-12T20:34:41Z.
Three things before the next build:
core/libs/prx/libc/src/Math.cppsince #3599 landed: main'sexp2_nid_postfixis now a save and restore of its own, directly above theldexp,scalbnandscalbnflines this rewrites. Keep main'sexp2and your three lines; please rebase.guest_mathabort was not a merged libm change; I was wrong there. The run built 26d14e8b onmain8c4a295f, and between your old base andc36fd0a0the only change toMath.cpporGuestMath.cppis thenearbyintfline from #3648, so CI ran the same code that passes for you. The host differs: Ubuntu 24.04 with GCC 13.3 and glibc 2.39, and WinLibs GCC 15.2 on UCRT, and it aborts on both.CheckLibmErrnoPreservedcompares result bits that the host libm produces (NaN sign bits, the in-range values), andPreserveErrnodepends on how the compiler treats the emptyasm.Requireprints nothing, so the log does not show the row. Please have the table print the function before it aborts, run it with GCC 13 and with MinGW, and compare only what this PR sets: errno, andisnaninstead of NaN bits.sinhfandasinlout of alibc.prxand finds Sony's error helper settingERANGEandEDOMwhere msun sets nothing. Which of the 39 were checked against alibc.prxand not only against FreeBSD source?Imported from boykopovar/AnyPS5. Originally posted by CouchRiv on 2026-10-13T15:18:07Z.
Closing this. To answer the last question: none of the 39 functions were checked against the console's own libc, only against FreeBSD msun source, and #3725 shows that is not enough for at least
sinhfandasinl. I'd rather come back with a smaller change once there is evidence for the PS5 behaviour than keep this open. Thanks for the careful reviews.Pull request closed