feat(libc): add the floating-point trap controls #8442
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/8442"
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-20T12:42:19Z.
What
Adds six absent
libSceLibcInternalfloating-point trap controls toFloatingPoint.cpp, next to the otherfe*functions. Like those, they act on the current host thread's x87 control word and MXCSR.libSceLibcInternal.sprx)feenableexcept,__feenableexceptutLW7uXm3Ss,NDOLSTFT1nsmask & 0x3fin the x87 control word andmask << 7in MXCSRfedisableexcept,__fedisableexcepty4WlO8qzHqI,1IB0U3rUtBwfegettrapenableP38JvXuK-uE~control & 0x3f, from the x87 control word onlyfesettrapenableu5a7OfymqlgTwo console behaviours are reproduced:
0xffffffc0, not masked to 6 bits. So with every exception masked they return-64, not 0.fesettrapenableonly unmasks. It never masks an exception that was already enabled, sofesettrapenable(0)changes nothing.fegetexceptis not exported by the console module and is not added.Tested
Linux (CachyOS, GCC, Release).
libc.prxexport from the same random state, 100 000 times. The state was random x87 masks, rounding, MXCSR masks, DAZ and FZ, with random arguments including bits above 0x3f and above 16 bits. Return values, the x87 control word and MXCSR gave 0 mismatches for all six.guest_floating_pointgainsCheckTraps, which covers each function's register effect and return value and restores the environment. It passes. It checks the x87 and MXCSR masks after every call, including a case where the two units hold different masks (values taken from the console module run on the host registers). With the two trap_mm_setcsrcalls removed, the test fails.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_01RbLgECLQ2bpDM7oe2urGZK
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-20T12:43:10Z.
Progress report (
c8301bb→13a3897)📈 System libraries: 62.86% (+0.03%, +6 implemented functions)
✅ 6 implemented
__fedisableexcept_nid_postfix__feenableexcept_nid_postfixfedisableexcept_nid_postfixfeenableexcept_nid_postfixfegettrapenable_nid_postfixfesettrapenable_nid_postfixImported from boykopovar/AnyPS5. Originally posted by nikowolf123 on 2026-10-20T20:40:46Z.
I found two gaps in the new
CheckTraps.The MXCSR writes are not checked. GuestFloatingPoint.cpp#L180 and L184 also accept
SseMasks() == 0x3f. That is exactly whatSeedFlags(0, 0)leaves behind if nothing writes MXCSR. Thefesettrapenablechecks (L189-L192) never read MXCSR at all. I removed both_mm_setcsrcalls from FloatingPoint.cpp (L27, L111). After that,feenableexcept(0x49)leaves the SSE masks at 0x3f instead of 0x36, and the test still passes. On x86-64, float and double arithmetic runs on SSE, so the MXCSR half is what decides whether guest code traps.The description says these checks follow existing checks for hosts that keep the SSE masks set, but I could not find any such checks.
CheckFlagsclears the MXCSR overflow mask and requires the exact value back (L132). A host that keeps the masks set would therefore abort there, beforeCheckTrapsruns. That check passes on the Linux and Windows CI jobs ofmain.The "unmasked in both units" return value is not pinned down.
PreviousTraps()recomputes the implementation's own formula from the live registers, and every step keeps the x87 and SSE masks equal. As a result, these variants also pass: a return value taken from the x87 word alone, one taken from MXCSR alone, and afegettrapenablethat reads MXCSR. The rule is the console behaviour described in this PR. FreeBSD's amd64fenv.ccomputed the same value before 741ae1d017ef. For example, with x87 masks 0x36 and MXCSR masks 0x3a, this PR'sfeenableexcept(0)returns 0xffffffc1, because only invalid is unmasked in both units. An x87-only version returns 0xffffffc9 and an MXCSR-only version returns 0xffffffc5.With the changes below, the test passes on this PR's FloatingPoint.cpp and fails for each of those variants (g++ 15.2, Linux):
|| SseMasks() == 0x3fon L180 and L184;static_cast<int>(0xffffffc9u),static_cast<int>(0xffffffc8u)andstatic_cast<int>(0xffffffcau)instead ofPreviousTraps(), which can then be removed;&& SseMasks() == 0x3bon L190, and&& SseMasks() == 0x3aon L191 and L192;WriteEnvironment(seeded), add a case where the two units have different masks:Imported from boykopovar/AnyPS5. Originally posted by hypermoist on 2026-10-20T22:49:15Z.
Thanks, both gaps were real. Applied your changes in the latest push: no
|| SseMasks() == 0x3f, fixed return values (0xffffffc9,0xffffffc8,0xffffffca) instead ofPreviousTraps()(removed), SSE mask checks after thefesettrapenablecalls, and the mixed-mask case.Before adding the expected values I ran the console's
libSceLibcInternal.sprxexports directly on the host registers. With x87 masks 0x36 and MXCSR masks 0x3a,feenableexcept(0)returns 0xffffffc1 andfegettrapenable()returns 0x09. Afterfesettrapenable(4)from fully masked, both units hold 0x3b, and afterfesettrapenable(5)both hold 0x3a. So the console writes MXCSR in both calls and reads only the x87 word for the query, which is what the test now pins.Negative control: with only the two trap
_mm_setcsrcalls (L27 and L111) turned into no-ops,guest_floating_pointaborts at "enable unmasks the SSE exceptions". With them restored it passes. You were right that the description's note about hosts keeping the SSE masks set had no basis; I'll correct the description.