fix(libc): preserve errno across fmodf domain errors #9643
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/9643"
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 Noxy-HNR on 2026-10-22T05:55:06Z.
What
Preserve errno around
fmodf, as the existing double-precisionfmodwrapper does. The original Windows library changes sentinel errno 73 to EDOM 33 forfmodf(1, 0), leaking the host CRT's policy into this model.FreeBSD 11.4's C implementation returns the invalid-operation NaN without an errno store. Its selected amd64 assembly implementation likewise has no errno store. The wrapper continues to use the host remainder result and flags; console errno, flags and NaN payloads remain unmeasured and are recorded in debt.
Tested
Windows 11 26H2 (build 10.0.26300), WinLibs GCC 15.2.0 r7, full Release build, head
ee057eaon maind6e7ab4.guest_mathandguest_floating_pointpass, 2/2.FE_INVALIDand preservation of an existing exception, and restores the prior floating-point environment and errno. Against main'sfmodfit fails with errno 73 changed to EDOM 33.core/libs/prx/libc/src/Math.cppnext tofmodf. Either landing order gives one conflict hunk there, resolved by keeping both functions; the tests anddocs/dev/debt/libc.mdmerge cleanly. That combination built and passedguest_mathandguest_floating_point, 2/2, and the #9771 author confirmed either order is fine.tools/check_conventions.py --base origin/mainandtools/check_test_registration.pypass.tools/pr_overlap.pyand merge tests against the 103 open pull requests that touch these files find no conflict other than #9771.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)Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-22T07:27:03Z.
Data on the console behaviour this models, from the
acosfof thelibc.prxbundled with PPSA28319 (catalog zip sha2566426468091b4…, the same binary thesinhf,asinlandatanhfdebt entries cite), exportQI-x0SL8jhwat0x40980of the unwrapped module:|x| <= 1it is FreeBSD msun'se_acosf.cinstruction for instruction: the same branch points (0x3f800000,0x3effffff,0x32800000),pS0..qS1(0x3e2aaa75,0xbd2f13ba,0xbc0dd36b,0xbf34e5ae),pio2_hi0x3fc90fda,pio2_lo0x33a22168read from.data(msun'svolatile), and the0xfffff000mask onsqrtf(z).hx & 0x7fffffffis above0x3f800000, which covers|x| > 1,±infand every NaN, it calls a helper at0x11740withedi = 1(FE_INVALID) before returning(x - x) / (x - x). The helper stores0x21(EDOM) through libkernel's__error(the import slot at0x10f8b8is9BcDykPmo1I), calls the module'sferaiseexcept(0x11a70:fnstenv/fldenvandstmxcsr/ldmxcsrwith the flags or-ed in, thenfwait), and storesEDOMagain because bit 0 is set (ERANGE,0x22, for the overflow and underflow bits).So on the console
acosf(2.0f)setserrnotoEDOMand raisesFE_INVALID, and a quiet NaN argument takes the same branch, so it does too. That differs from "keepserrno" and "leaves a quiet NaN alone" in this PR's debt line, and from theerrno == 0check #3688 added from FreeBSD's source, wheree_acosf.citself doesn't toucherrnobut the console's build of it does. This is a reading of the binary, not a measurement on a console.I'm not touching those paths. Separately I'm porting the
|x| <= 1part, since the host libraries differ there: over the 2,130,706,434 floats in[-1, 1], msun'sacosfdiffers from glibc 2.39's on 1,046,180 and from Apple's x86-64 libm on 17,127,560 (acosf(0.5f)is0x3f860a91on macOS,0x3f860a92in msun). That changes the lines below the ones you edit, so the two should merge cleanly.Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-22T07:37:13Z.
Review: COMMENTED
Linux run, since the description lists Linux behavior as untested: the updated
guest_mathis red onmainand green with the PR on glibc.Machine: Linux, Ubuntu 26.04, GCC 15.2.0 (glibc host libm), x86-64, i7-8700. Head
bd769c3c3merged withgit merge --no-commitonto currentmaind6eafa54d. Full build,BUILD_TESTING=ON,ANYPS5_RELINKER_ONLY=OFF, Ninja Release; the whole build passes.main'sMath.cpp:guest_mathaborts on the first new case:Guest acosf failed for 3f800001: got ffc00000, expected ffc00000, invalid 0, errno 73, the same missing invalid flag and untouched sentinel errno described for Windows.guest_mathandguest_floating_pointpass (2/2).Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-22T11:31:02Z.
A data point from @JJenkx on #9668 that narrows what I wrote above: with
__errorsupplied to a harness, PPSA13934'slibc.prxdid not seterrnoforacosf(2.0f). That copy is a different build (FMA-contracted in the|x| <= 1part), so theEDOMstore I described is confirmed for PPSA28319's build only, and the error path can differ between the builds titles ship.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T14:41:21Z.
While reviewing #9771, I compared the current heads. Its
asinfinsertion and this PR'sfmodfchange produce an adjacent-hunk conflict inMath.cpp, but the two edits compose by retaining both functions.CONTRIBUTING.mdasks authors to agree merge order for overlapping PRs and record a dependency when one must land first. Could you coordinate with the #9771 author whether either order is acceptable, and note that agreement on the PR checklist? No functional conflict is apparent from the pairwise merge.