Repository navigation
fix(libc): start the Windows initial thread with FreeBSD's x87 control word #1563
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Thimo188
wants to merge
1
commit into
main
Choose a base branch
from
upstream-pr/27619
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+32
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
docs/dev/debt/changes/fix-libc-windows-initial-thread-x87-control.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # libc | ||
|
|
||
| ## change [acosl] | ||
|
|
||
| - [acosl](../../../core/libs/prx/libc/src/Math.cpp) (libc) runs at the caller's x87 precision, as the binary does, so on a Windows initial thread at 53 bits (see `asinl`) it returns the binary's 53-bit result, not the console's 64-bit one. It follows the `acosl` of the `libc.prx` bundled with PPSA21564 (0x3e5a0): FreeBSD msun's [`e_acosl.c`](https://github.com/freebsd/freebsd-src/blob/9f0432041b8fb69832f10c32da50a67f34feeb61/lib/msun/src/e_acosl.c) with the ld80 `invtrig.c` coefficients that `asinl` uses, where `_Feraise(FE_INVALID)` raises `FE_INVALID` and sets `errno` to `EDOM` for |x| > 1, the infinities and NaN before `(x - x) / (x - x)`. 20 million random arguments in (-1, 1) give the same bits as that code run natively on Linux at 64-, 53- and 24-bit precision; the host `acosl` differed on 1,603,469 of them at 64 bits. Other SDK versions and the system `libSceLibcInternal` are not compared | ||
|
|
||
| ## change [_LLog] | ||
|
|
||
| - [_LLog](../../../core/libs/prx/libc/src/DinkumwareInternals.cpp) (libc) runs at the caller's x87 precision, as the binary does, so a Windows initial thread at 53 bits (see `asinl`) gets the binary's 53-bit result, not the console's 64-bit one. It follows the `_LLog` of the `libc.prx` bundled with PPSA21564 (0x3fd50): Dinkumware's `_LDunscale` split, the 8-term `_LPoly` table and the split `ln 2`, `log10 e` and `log2 e` constants taken from the binary, with `_Feraise` raising `FE_DIVBYZERO` and `ERANGE` for a zero mantissa, pseudo-zeros included, and `FE_INVALID` and `EDOM` below zero. 4 million random arguments under bases 0, 1, -1, 7 and -9 give the same bits, `errno` and flags as that code run natively on Linux at 64-, 53- and 24-bit precision; the host `logl`, `log10l` and `log2l` differed on 869,197, 2,000,696 and 306,649 at 64 bits. Its `logl`, `log10l` and `log2l` call `_LLog` with 0, 1 and -1, and this libc forwards them there too. Other SDK versions are not compared | ||
|
|
||
| ## change [asinl] | ||
|
|
||
| - [asinl](../../../core/libs/prx/libc/src/Math.cpp) (libc) follows the `asinl` of the `libc.prx` bundled with PPSA28319 (disassembly), FreeBSD msun's [`e_asinl.c`](https://github.com/freebsd/freebsd-src/blob/9f0432041b8fb69832f10c32da50a67f34feeb61/lib/msun/src/e_asinl.c) with the ld80 [`invtrig.c`](https://github.com/freebsd/freebsd-src/blob/9f0432041b8fb69832f10c32da50a67f34feeb61/lib/msun/ld80/invtrig.c) coefficients, which the binary holds as the same 80-bit constants. For |x| > 1, the infinities and NaN, Sony's error helper raises `FE_INVALID` and sets `errno` to `EDOM` before returning `(x - x) / (x - x)`, so a NaN argument also sets `EDOM`; msun sets no `errno`. 20 million random arguments in (-1, 1) give the same bits as that code run on a Windows host. A Windows initial thread outside `EnterGuestMainStack` runs at 53 bits, where `asinl(0.75)` has mantissa `…e000`, not `…e041`. The system `libSceLibcInternal` that RetroArch (PPSA99169) imports is assumed to have the same code | ||
|
|
||
| # libkernel | ||
|
|
||
| ## change [Guest thread x87 control word] | ||
|
|
||
| - [Guest thread x87 control word](../../../core/libs/prx/libkernel/Pthread/src/Thread.cpp) (libkernel) - not measured on a console, and only on Windows does the emulator load `0x037f` into the initial thread, when the entry stub enters through libc's `EnterGuestMainStack` (`libc.prx` imported, or unscoped imports); otherwise the initial thread keeps the host's start word, `0x27f` on Windows and `0x037f` on Linux and macOS unless the host changed it. A new guest thread starts with `0x037f` (64-bit precision, every exception masked) whatever its creator uses. That is FreeBSD 11's start state, whose [`cpu_copy_thread`](https://github.com/freebsd/freebsd-src/blob/release/11.0.0/sys/amd64/amd64/vm_machdep.c#L428-L446) leaves a new thread's FPU state uninitialized so that its first FPU instruction loads `fpu_initialstate` (`__INITIAL_FPUCW__`), and the control word of the libSceFiber option that starts fibers with the default FPU state | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replace “the emulator” with “AnyPS5.”
This entry incorrectly describes the project as an emulator.
As per coding guidelines, AnyPS5 “relinks PS5 executables into native Linux or Windows programs” and “is not an emulator.”
Source: Coding guidelines