fix(libkernel): inherit floating-point environment on Windows threads #4981

Closed
QRMOE wants to merge 6 commits from upstream-pr/4981 into main
QRMOE commented 2026-10-13 07:14:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by QRMOE on 2026-10-13T07:14:04Z.


What

Windows guest threads inherit their creator's floating-point environment. _beginthreadex currently resets rounding to nearest, clears exception flags and selects 53-bit x87 precision; for example, a worker's asinl(0.75L) loses the low mantissa bits even when its creator uses 64-bit precision.

Capture std::fenv_t at creation and restore it immediately before guest entry, after host thread setup. Preserve the caller's settings, including an intentionally selected 53-bit precision mode. Linux continues to use native pthread inheritance. This follows POSIX pthread_create; the SCE entry point shares that path and has not been measured on console. Technical debt records that distinction and the unchanged environment of adopted host threads.

Tested

Windows, MinGW GCC 15.2, Release, BUILD_TESTING=ON, main base c2565f014:

  • The final regression fails against unchanged main: workers report rounding 0 rather than the requested modes, exception flags 0 rather than the creator's flags, and reduced asinl precision.
  • Both SCE and POSIX creation/join are tested under all four rounding modes, default and explicit MinGW 64-bit/53-bit environments, inherited exception flags, nested workers and child changes that must leave the creator unchanged. Compare asinl(0.75L) and nearbyint(0.5) with the creator's results. The regression completes in 1.50 seconds.
  • All 15 relevant tests passed after rebasing: new inheritance regression, math, thread startup, stack and attributes, mutex errors, condition waits, CPU clock, cancellation, self-join, TLS keys, destructor lifetime and patched-library loading. The explicit libs build passed on the final branch; all library targets also built during the rebased full-build attempt. Conventions and whitespace checks passed.
  • Full Windows build attempted; all libraries and unblocked components built, but Windows denied recreation of eight unrelated test outputs (guest_sce_net, guest_poll, guest_socket_read_write, guest_tcp, guest_socket_pair, guest_dup, guest_socket_events and guest_fstatfs). Avast logs automatic sandboxing of test programs. The local full-build limitation remains; local full-build success is not claimed.
  • No title playback or console measurement performed. No open PR adds floating-point inheritance. The nearby Windows thread-cleanup PRs #4888 and #4901 merge cleanly with this change. The signal-mask PR #3986 adds no new Thread.cpp conflict when combined with this patch; its pre-existing conflicts with main are in other files.

Review validation

  • After resolving the documentation conflict, the actual uploaded head 6100f51f5 passed an explicit libs build and all 14 selected thread/math runtime checks (3.02 seconds total). Patched-library loading also passed (15/15 total relevant checks) after main's unwind helper accessor fix 3a9cc6368. No implementation changes were needed to resolve the conflict.

  • xXPBSXx's Windows report: Reports the Windows inheritance test passing with the PR and failing with main's source restored.

  • Resolved the docs/dev/debt/libc.md conflict against main while preserving the new atanhf documentation and the thread precision update. The implementation remains independent of the snprintf/vsnprintf carry work discussed in the comments.

Ready for maintainer review. Full Linux/Windows CI requires a maintainer's manual dispatch; marking this PR ready does not claim those jobs passed.

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
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4839733447 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4981). Originally posted by [QRMOE](https://github.com/QRMOE) on 2026-10-13T07:14:04Z. --- ### What Windows guest threads inherit their creator's floating-point environment. `_beginthreadex` currently resets rounding to nearest, clears exception flags and selects 53-bit x87 precision; for example, a worker's `asinl(0.75L)` loses the low mantissa bits even when its creator uses 64-bit precision. Capture `std::fenv_t` at creation and restore it immediately before guest entry, after host thread setup. Preserve the caller's settings, including an intentionally selected 53-bit precision mode. Linux continues to use native pthread inheritance. This follows [POSIX pthread_create](https://pubs.opengroup.org/onlinepubs/000095399/functions/pthread_create.html); the SCE entry point shares that path and has not been measured on console. Technical debt records that distinction and the unchanged environment of adopted host threads. ### Tested Windows, MinGW GCC 15.2, Release, `BUILD_TESTING=ON`, main base `c2565f014`: - The final regression fails against unchanged main: workers report rounding 0 rather than the requested modes, exception flags 0 rather than the creator's flags, and reduced `asinl` precision. - Both SCE and POSIX creation/join are tested under all four rounding modes, default and explicit MinGW 64-bit/53-bit environments, inherited exception flags, nested workers and child changes that must leave the creator unchanged. Compare `asinl(0.75L)` and `nearbyint(0.5)` with the creator's results. The regression completes in 1.50 seconds. - All 15 relevant tests passed after rebasing: new inheritance regression, math, thread startup, stack and attributes, mutex errors, condition waits, CPU clock, cancellation, self-join, TLS keys, destructor lifetime and patched-library loading. The explicit libs build passed on the final branch; all library targets also built during the rebased full-build attempt. Conventions and whitespace checks passed. - Full Windows build attempted; all libraries and unblocked components built, but Windows denied recreation of eight unrelated test outputs (`guest_sce_net`, `guest_poll`, `guest_socket_read_write`, `guest_tcp`, `guest_socket_pair`, `guest_dup`, `guest_socket_events` and `guest_fstatfs`). Avast logs automatic sandboxing of test programs. The local full-build limitation remains; local full-build success is not claimed. - No title playback or console measurement performed. No open PR adds floating-point inheritance. The nearby Windows thread-cleanup PRs #4888 and #4901 merge cleanly with this change. The signal-mask PR #3986 adds no new Thread.cpp conflict when combined with this patch; its pre-existing conflicts with main are in other files. ### Review validation - After resolving the documentation conflict, the actual uploaded head `6100f51f5` passed an explicit libs build and all 14 selected thread/math runtime checks (3.02 seconds total). Patched-library loading also passed (15/15 total relevant checks) after main's unwind helper accessor fix `3a9cc6368`. No implementation changes were needed to resolve the conflict. - [xXPBSXx's Windows report](https://github.com/boykopovar/AnyPS5/pull/4981#issuecomment-6142325228): Reports the Windows inheritance test passing with the PR and failing with main's source restored. - Resolved the `docs/dev/debt/libc.md` conflict against main while preserving the new `atanhf` documentation and the thread precision update. The implementation remains independent of the `snprintf`/`vsnprintf` carry work discussed in the comments. Ready for maintainer review. Full Linux/Windows CI requires a maintainer's manual dispatch; marking this PR ready does not claim those jobs passed. ### 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 - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's technical debt file - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: none - [x] AI-assisted: yes
xXPBSXx commented 2026-10-13 09:16:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-13T09:16:31Z.


Windows counter-test: Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, this PR merged onto main 2a2a3dc65. guest_pthread_fenv passes with the PR; with the PR's source files replaced by main's, same test, it fails (SCE thread did not inherit fenv).

Not tested: Linux, a console, a title run.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6142325228 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4981#issuecomment-6142325228). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-13T09:16:31Z. --- Windows counter-test: Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, this PR merged onto `main` `2a2a3dc65`. `guest_pthread_fenv` passes with the PR; with the PR's source files replaced by `main`'s, same test, it fails (`SCE thread did not inherit fenv`). Not tested: Linux, a console, a title run. AI-assisted (Claude Code).
MarcZX10 commented 2026-10-13 10:29:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-13T10:29:29Z.


I am preparing a carry PR for issue #3974 (Windows snprintf/vsnprintf size overflow). The code changes are separate, but the current branches conflict in docs/dev/debt/libc.md. I can list #4981 as a merge-order dependency and rebase after it lands. Does that ordering work for you?

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6143557781 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4981#issuecomment-6143557781). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-13T10:29:29Z. --- I am preparing a carry PR for issue #3974 (Windows `snprintf`/`vsnprintf` size overflow). The code changes are separate, but the current branches conflict in `docs/dev/debt/libc.md`. I can list #4981 as a merge-order dependency and rebase after it lands. Does that ordering work for you?
QRMOE commented 2026-10-13 11:12:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by QRMOE on 2026-10-13T11:12:08Z.


@MarcZX100 Yes, that ordering works for me: merge #4981 first, then rebase #5070 on the updated main. I’ve resolved #4981’s conflict with main and marked it ready for review. The updated head passed the explicit library build and all 15 relevant runtime/loading checks.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6144261274 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4981#issuecomment-6144261274). Originally posted by [QRMOE](https://github.com/QRMOE) on 2026-10-13T11:12:08Z. --- @MarcZX100 Yes, that ordering works for me: merge #4981 first, then rebase #5070 on the updated main. I’ve resolved #4981’s conflict with main and marked it ready for review. The updated head passed the explicit library build and all 15 relevant runtime/loading checks.
github-actions[bot] commented 2026-10-13 13:13:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-13T13:13:10Z.


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.

Pull request Conflicting files (lines on main) Same exports Same new files
#5143 docs/dev/debt/libkernel.md L98
#5572 docs/dev/debt/libkernel.md L98
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6146358338 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4981#issuecomment-6146358338). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-13T13:13:10Z. --- <!-- pr-overlap --> 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**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #5143 | `docs/dev/debt/libkernel.md` [L98](https://github.com/boykopovar/AnyPS5/blob/0df6e295c52e7cd6afa394ed35513bd252045c74/docs/dev/debt/libkernel.md#L98) | | | | #5572 | `docs/dev/debt/libkernel.md` [L98](https://github.com/boykopovar/AnyPS5/blob/0df6e295c52e7cd6afa394ed35513bd252045c74/docs/dev/debt/libkernel.md#L98) | | |
oneandonlydean commented 2026-10-14 02:34:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T02:34:58Z.


Closing: no title is known to reach this. With the queue at 700+ open PRs, please hold off on new PRs that don't name a title needing the change until it's down. If a title stops on this on current main, reopen with the title ID and the log line.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6159611279 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4981#issuecomment-6159611279). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T02:34:58Z. --- Closing: no title is known to reach this. With the queue at 700+ open PRs, please hold off on new PRs that don't name a title needing the change until it's down. If a title stops on this on current main, reopen with the title ID and the log line.
QRMOE commented 2026-10-15 07:41:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by QRMOE on 2026-10-15T07:41:36Z.


Understood. I’ll leave this PR closed and hold off on new PRs without a title needing the change. If this is revisited, I’ll bring a title ID and a current-main failure log.

@MarcZX100 Since #4981 is now closed, please don’t wait for it as a merge-order dependency; my earlier proposed ordering no longer applies.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6187084263 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4981#issuecomment-6187084263). Originally posted by [QRMOE](https://github.com/QRMOE) on 2026-10-15T07:41:36Z. --- Understood. I’ll leave this PR closed and hold off on new PRs without a title needing the change. If this is revisited, I’ll bring a title ID and a current-main failure log. @MarcZX100 Since #4981 is now closed, please don’t wait for it as a merge-order dependency; my earlier proposed ordering no longer applies.

Pull request closed

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!4981
No description provided.