feat(libc): add the floating-point trap controls #8442

Merged
hypermoist merged 2 commits from upstream-pr/8442 into main 2026-10-20 23:25:37 +00:00
hypermoist commented 2026-10-20 12:42:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by hypermoist on 2026-10-20T12:42:19Z.


What

Adds six absent libSceLibcInternal floating-point trap controls to FloatingPoint.cpp, next to the other fe* functions. Like those, they act on the current host thread's x87 control word and MXCSR.

Export NID Console (libSceLibcInternal.sprx)
feenableexcept, __feenableexcept utLW7uXm3Ss, NDOLSTFT1ns one body at 0x16d70: clears mask & 0x3f in the x87 control word and mask << 7 in MXCSR
fedisableexcept, __fedisableexcept y4WlO8qzHqI, 1IB0U3rUtBw one body at 0x16dd0: sets the same bits
fegettrapenable P38JvXuK-uE 0x16e70: ~control & 0x3f, from the x87 control word only
fesettrapenable u5a7Ofymqlg 0x16eb0: returns 1 if the low 16 bits exceed 0x3f; otherwise clears those bits in both units and returns 0

Two console behaviours are reproduced:

  • Enable/disable return value. The enable and disable calls return the exceptions that were unmasked in both units OR'd with 0xffffffc0, not masked to 6 bits. So with every exception masked they return -64, not 0.
  • fesettrapenable only unmasks. It never masks an exception that was already enabled, so fesettrapenable(0) changes nothing.

fegetexcept is not exported by the console module and is not added.

Tested

Linux (CachyOS, GCC, Release).

  • Differential test. A harness called each console export and this build's libc.prx export 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.
  • Repo test. guest_floating_point gains CheckTraps, 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_setcsr calls removed, the test fails.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none verified
  • GPUs tested: n/a
  • AI-assisted: yes (Claude Code), reviewed by the contributor

🤖 Generated with Claude Code

https://claude.ai/code/session_01RbLgECLQ2bpDM7oe2urGZK

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4935878767 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8442). Originally posted by [hypermoist](https://github.com/hypermoist) on 2026-10-20T12:42:19Z. --- ### What Adds six absent `libSceLibcInternal` floating-point trap controls to `FloatingPoint.cpp`, next to the other `fe*` functions. Like those, they act on the current host thread's x87 control word and MXCSR. | Export | NID | Console (`libSceLibcInternal.sprx`) | |---|---|---| | `feenableexcept`, `__feenableexcept` | `utLW7uXm3Ss`, `NDOLSTFT1ns` | one body at 0x16d70: clears `mask & 0x3f` in the x87 control word and `mask << 7` in MXCSR | | `fedisableexcept`, `__fedisableexcept` | `y4WlO8qzHqI`, `1IB0U3rUtBw` | one body at 0x16dd0: sets the same bits | | `fegettrapenable` | `P38JvXuK-uE` | 0x16e70: `~control & 0x3f`, from the x87 control word only | | `fesettrapenable` | `u5a7Ofymqlg` | 0x16eb0: returns 1 if the low 16 bits exceed 0x3f; otherwise clears those bits in both units and returns 0 | Two console behaviours are reproduced: - **Enable/disable return value.** The enable and disable calls return the exceptions that were unmasked in both units *OR'd with `0xffffffc0`*, not masked to 6 bits. So with every exception masked they return `-64`, not 0. - **`fesettrapenable` only unmasks.** It never masks an exception that was already enabled, so `fesettrapenable(0)` changes nothing. `fegetexcept` is not exported by the console module and is not added. ### Tested Linux (CachyOS, GCC, Release). - **Differential test.** A harness called each console export and this build's `libc.prx` export 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. - **Repo test.** `guest_floating_point` gains `CheckTraps`, 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_setcsr` calls removed, the test fails. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none verified - [x] GPUs tested: n/a - [x] AI-assisted: yes (Claude Code), reviewed by the contributor 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01RbLgECLQ2bpDM7oe2urGZK
github-actions[bot] commented 2026-10-20 12:43:10 +00:00 (Migrated from github.com)

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
Library Name
libc __fedisableexcept_nid_postfix
libc __feenableexcept_nid_postfix
libc fedisableexcept_nid_postfix
libc feenableexcept_nid_postfix
libc fegettrapenable_nid_postfix
libc fesettrapenable_nid_postfix
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6291107797 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8442#issuecomment-6291107797). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-20T12:43:10Z. --- <!-- progress-report --> ### Progress report (c8301bb → 13a3897) 📈 **System libraries**: 62.86% (+0.03%, +6 implemented functions) <details> <summary>✅ 6 implemented</summary> | Library | Name | | - | - | | libc | `__fedisableexcept_nid_postfix` | | libc | `__feenableexcept_nid_postfix` | | libc | `fedisableexcept_nid_postfix` | | libc | `feenableexcept_nid_postfix` | | libc | `fegettrapenable_nid_postfix` | | libc | `fesettrapenable_nid_postfix` | </details>
nikowolf123 commented 2026-10-20 20:40:46 +00:00 (Migrated from github.com)

Imported 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 what SeedFlags(0, 0) leaves behind if nothing writes MXCSR. The fesettrapenable checks (L189-L192) never read MXCSR at all. I removed both _mm_setcsr calls 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. CheckFlags clears the MXCSR overflow mask and requires the exact value back (L132). A host that keeps the masks set would therefore abort there, before CheckTraps runs. That check passes on the Linux and Windows CI jobs of main.

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 a fegettrapenable that reads MXCSR. The rule is the console behaviour described in this PR. FreeBSD's amd64 fenv.c computed the same value before 741ae1d017ef. For example, with x87 masks 0x36 and MXCSR masks 0x3a, this PR's feenableexcept(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):

  • drop || SseMasks() == 0x3f on L180 and L184;
  • on L182, L186 and L188, compare with static_cast<int>(0xffffffc9u), static_cast<int>(0xffffffc8u) and static_cast<int>(0xffffffcau) instead of PreviousTraps(), which can then be removed;
  • add && SseMasks() == 0x3b on L190, and && SseMasks() == 0x3a on L191 and L192;
  • before WriteEnvironment(seeded), add a case where the two units have different masks:
    auto mixed = ReadEnvironment();
    mixed.x87[0] = (mixed.x87[0] & ~0x3fu) | 0x36;
    mixed.sse = (mixed.sse & ~(0x3fu << 7)) | (0x3au << 7);
    WriteEnvironment(mixed);
    Require(feenableexcept_nid_postfix(0) == static_cast<int>(0xffffffc1u), "enable reports only the traps enabled on both units");
    Require(fegettrapenable_nid_postfix() == 0x09, "the trap query reads only the x87 control word");
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6301000929 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8442#issuecomment-6301000929). Originally posted by [nikowolf123](https://github.com/nikowolf123) on 2026-10-20T20:40:46Z. --- I found two gaps in the new `CheckTraps`. **The MXCSR writes are not checked.** [GuestFloatingPoint.cpp#L180](https://github.com/boykopovar/AnyPS5/blob/bf6f9cc3e2d506fab0c9c48ee008a590341f5ca1/core/libs/tests/GuestFloatingPoint.cpp#L180) and L184 also accept `SseMasks() == 0x3f`. That is exactly what `SeedFlags(0, 0)` leaves behind if nothing writes MXCSR. The `fesettrapenable` checks (L189-L192) never read MXCSR at all. I removed both `_mm_setcsr` calls from FloatingPoint.cpp ([L27](https://github.com/boykopovar/AnyPS5/blob/bf6f9cc3e2d506fab0c9c48ee008a590341f5ca1/core/libs/prx/libc/src/FloatingPoint.cpp#L27), [L111](https://github.com/boykopovar/AnyPS5/blob/bf6f9cc3e2d506fab0c9c48ee008a590341f5ca1/core/libs/prx/libc/src/FloatingPoint.cpp#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. `CheckFlags` clears the MXCSR overflow mask and requires the exact value back ([L132](https://github.com/boykopovar/AnyPS5/blob/bf6f9cc3e2d506fab0c9c48ee008a590341f5ca1/core/libs/tests/GuestFloatingPoint.cpp#L132)). A host that keeps the masks set would therefore abort there, before `CheckTraps` runs. That check passes on the Linux and Windows CI jobs of `main`. **The "unmasked in both units" return value is not pinned down.** [`PreviousTraps()`](https://github.com/boykopovar/AnyPS5/blob/bf6f9cc3e2d506fab0c9c48ee008a590341f5ca1/core/libs/tests/GuestFloatingPoint.cpp#L172) 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 a `fegettrapenable` that reads MXCSR. The rule is the console behaviour described in this PR. FreeBSD's amd64 `fenv.c` computed the same value before [741ae1d017ef](https://github.com/freebsd/freebsd-src/commit/741ae1d017ef). For example, with x87 masks 0x36 and MXCSR masks 0x3a, this PR's `feenableexcept(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): - drop `|| SseMasks() == 0x3f` on L180 and L184; - on L182, L186 and L188, compare with `static_cast<int>(0xffffffc9u)`, `static_cast<int>(0xffffffc8u)` and `static_cast<int>(0xffffffcau)` instead of `PreviousTraps()`, which can then be removed; - add `&& SseMasks() == 0x3b` on L190, and `&& SseMasks() == 0x3a` on L191 and L192; - before `WriteEnvironment(seeded)`, add a case where the two units have different masks: ```cpp auto mixed = ReadEnvironment(); mixed.x87[0] = (mixed.x87[0] & ~0x3fu) | 0x36; mixed.sse = (mixed.sse & ~(0x3fu << 7)) | (0x3au << 7); WriteEnvironment(mixed); Require(feenableexcept_nid_postfix(0) == static_cast<int>(0xffffffc1u), "enable reports only the traps enabled on both units"); Require(fegettrapenable_nid_postfix() == 0x09, "the trap query reads only the x87 control word"); ```
hypermoist commented 2026-10-20 22:49:15 +00:00 (Migrated from github.com)

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 of PreviousTraps() (removed), SSE mask checks after the fesettrapenable calls, and the mixed-mask case.

Before adding the expected values I ran the console's libSceLibcInternal.sprx exports directly on the host registers. With x87 masks 0x36 and MXCSR masks 0x3a, feenableexcept(0) returns 0xffffffc1 and fegettrapenable() returns 0x09. After fesettrapenable(4) from fully masked, both units hold 0x3b, and after fesettrapenable(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_setcsr calls (L27 and L111) turned into no-ops, guest_floating_point aborts 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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6303523386 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8442#issuecomment-6303523386). Originally posted by [hypermoist](https://github.com/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 of `PreviousTraps()` (removed), SSE mask checks after the `fesettrapenable` calls, and the mixed-mask case. Before adding the expected values I ran the console's `libSceLibcInternal.sprx` exports directly on the host registers. With x87 masks 0x36 and MXCSR masks 0x3a, `feenableexcept(0)` returns 0xffffffc1 and `fegettrapenable()` returns 0x09. After `fesettrapenable(4)` from fully masked, both units hold 0x3b, and after `fesettrapenable(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_setcsr` calls (L27 and L111) turned into no-ops, `guest_floating_point` aborts 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.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!8442
No description provided.