Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions core/libs/prx/libc/src/Math.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,24 @@ double APS5_VABI exp2_nid_postfix(double x) {
double APS5_VABI ldexp_nid_postfix(double x, int exponent) { return std::ldexp(x, exponent); }
double APS5_VABI scalbn_nid_postfix(double x, int exponent) { return std::scalbn(x, exponent); }
float APS5_VABI scalbnf_nid_postfix(float x, int exponent) { return std::scalbn(x, exponent); }
double APS5_VABI frexp_nid_postfix(double x, int* exponent) { return std::frexp(x, exponent); }
float APS5_VABI frexpf_nid_postfix(float x, int* exponent) { return std::frexp(x, exponent); }
double APS5_VABI frexp_nid_postfix(double x, int* exponent) {
std::uint64_t bits;
std::memcpy(&bits, &x, sizeof(bits));
if ((bits & 0x7ff0000000000000ull) == 0x7ff0000000000000ull || (bits << 1) == 0) {
*exponent = 0;
return x;
}
return std::frexp(x, exponent);
}
float APS5_VABI frexpf_nid_postfix(float x, int* exponent) {
std::uint32_t bits;
std::memcpy(&bits, &x, sizeof(bits));
if ((bits & 0x7f800000u) == 0x7f800000u || (bits << 1) == 0) {
*exponent = 0;
return x;
}
return std::frexp(x, exponent);
}
// Guest long is 64-bit, including on Windows where native long is 32-bit.
std::int64_t APS5_VABI lround_nid_postfix(double x) { return RoundToInt64(x); }
std::int64_t APS5_VABI lroundf_nid_postfix(float x) { return RoundToInt64(x); }
Expand Down
34 changes: 34 additions & 0 deletions core/libs/tests/GuestMath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,39 @@ static void CheckAcosfErrno() {
Require(errno == 0);
}

static void CheckFrexpSpecialValues() {
int exponent = 0x5a5a5a5a;
Require(frexp_nid_postfix(0.0, &exponent) == 0.0 && !std::signbit(frexp_nid_postfix(0.0, &exponent)) && exponent == 0);
exponent = 0x5a5a5a5a;
Require(frexp_nid_postfix(-0.0, &exponent) == -0.0 && std::signbit(frexp_nid_postfix(-0.0, &exponent)) && exponent == 0);
exponent = 0x5a5a5a5a;
Require(frexp_nid_postfix(std::numeric_limits<double>::infinity(), &exponent) == std::numeric_limits<double>::infinity() && exponent == 0);
exponent = 0x5a5a5a5a;
Require(frexp_nid_postfix(-std::numeric_limits<double>::infinity(), &exponent) == -std::numeric_limits<double>::infinity() && exponent == 0);
exponent = 0x5a5a5a5a;
std::feclearexcept(FE_ALL_EXCEPT);
Require(std::bit_cast<std::uint64_t>(frexp_nid_postfix(std::bit_cast<double>(UINT64_C(0x7ff0000000000001)), &exponent)) == UINT64_C(0x7ff0000000000001) && exponent == 0);
Require(std::fetestexcept(FE_INVALID) == 0);
exponent = 0x5a5a5a5a;
Require(std::bit_cast<std::uint64_t>(frexp_nid_postfix(std::bit_cast<double>(UINT64_C(0xfff8000000012345)), &exponent)) == UINT64_C(0xfff8000000012345) && exponent == 0);
exponent = 0x5a5a5a5a;
Require(frexpf_nid_postfix(0.f, &exponent) == 0.f && !std::signbit(frexpf_nid_postfix(0.f, &exponent)) && exponent == 0);
exponent = 0x5a5a5a5a;
Require(frexpf_nid_postfix(-0.f, &exponent) == -0.f && std::signbit(frexpf_nid_postfix(-0.f, &exponent)) && exponent == 0);
exponent = 0x5a5a5a5a;
Require(frexpf_nid_postfix(std::numeric_limits<float>::infinity(), &exponent) == std::numeric_limits<float>::infinity() && exponent == 0);
exponent = 0x5a5a5a5a;
Require(frexpf_nid_postfix(-std::numeric_limits<float>::infinity(), &exponent) == -std::numeric_limits<float>::infinity() && exponent == 0);
exponent = 0x5a5a5a5a;
std::feclearexcept(FE_ALL_EXCEPT);
Require(std::bit_cast<std::uint32_t>(frexpf_nid_postfix(std::bit_cast<float>(0x7f800001u), &exponent)) == 0x7f800001u && exponent == 0);
Require(std::fetestexcept(FE_INVALID) == 0);
exponent = 0x5a5a5a5a;
Require(std::bit_cast<std::uint32_t>(frexpf_nid_postfix(std::bit_cast<float>(0xffc12345u), &exponent)) == 0xffc12345u && exponent == 0);
exponent = 0x5a5a5a5a;
}
}

static void CheckLongDouble() {
Require(ExtendedBits(acosl_nid_postfix(-1.0L), 0xc90fdaa22168c235ull, 0x4000));
Require(ExtendedBits(acosl_nid_postfix(0.0L), 0xc90fdaa22168c235ull, 0x3fff));
Expand Down Expand Up @@ -915,6 +948,7 @@ int main() {
const auto minimum = div_nid_postfix(std::numeric_limits<int>::min(), 10);
Require(minimum.quot == -214748364 && minimum.rem == -8);
CheckPowfSpecialValue();
CheckFrexpSpecialValues();
Require(abs_nid_postfix(0) == 0);
CheckAcosfErrno();
Require(abs_nid_postfix(42) == 42);
Expand Down
1 change: 1 addition & 0 deletions docs/dev/debt/libc.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
- [realpath](../../../core/libs/prx/libc/src/RealPath.cpp) (libc) follows FreeBSD 11's [`realpath.c`](https://github.com/freebsd/freebsd-src/blob/stable/11/lib/libc/stdlib/realpath.c) except for symlinks. The console's guest filesystem has none, so a host symlink under the guest root is followed for existence and type but not expanded, and `..` drops the previous component lexically, as `Resolve` does for every guest path. Through a host link `linkdir -> sub/deep`, `linkdir/../x` gives `x` beside the link, not beside its target.
- [strtod, strtof, strtold](../../../core/libs/prx/libc/src/Strings.cpp) (libc) - `NAN(n-char-sequence)` converts to the quiet NaN that `nan` and `nanf` return, negated after a minus sign, and ignores the sequence as they ignore their tag. This is inferred from C17 7.12.11.2p2, which makes `nan("n")` equivalent to `strtod("NAN(n)", NULL)`, and not observed on the console. No console `nanl` was observed, so `strtold` returns the x87 default quiet NaN (exponent `0x7fff`, significand `0xc000000000000000`)
- [nan and nanf](../../../core/libs/prx/libc/src/Math.cpp) (libc) return fixed quiet NaN values (`0x7fc00000` and `0x7ff8000000000000`), ignoring the tag argument as observed in the console's `libc.prx` (both exports load a global constant via `mov rax,[rip+..]; vmovsd xmm0,[rax]; ret`); VLC (PPSA85300) stops on nanf at start-up after strsep.
- [frexp and frexpf](../../../core/libs/prx/libc/src/Math.cpp) (libc) store 0 in `*exp` for zero, infinity and NaN and return the argument unchanged, as FreeBSD's [s_frexp.c](https://github.com/freebsd/freebsd-src/blob/releng/11.4/lib/msun/src/s_frexp.c) and [s_frexpf.c](https://github.com/freebsd/freebsd-src/blob/releng/11.4/lib/msun/src/s_frexpf.c) (libm) do, without raising `FE_INVALID` for a signaling NaN (libc has no `frexpf`, so only libm defines it). C17 7.12.6.4 leaves `*exp` unspecified for infinity and NaN, FreeBSD's libc [frexp.c](https://github.com/freebsd/freebsd-src/blob/releng/11.4/lib/libc/gen/frexp.c) leaves it untouched, and the value the console's libc stores is unmeasured
- [lgamma](../../../core/libs/prx/libc/src/Math.cpp) (libc) returns the host's `lgamma` but does not store the sign of Γ(x) in the guest's `signgam`, as FreeBSD 11's [`e_lgamma.c`](https://github.com/freebsd/freebsd-src/blob/stable/11/lib/msun/src/e_lgamma.c) does through `__ieee754_lgamma_r`, because no guest `signgam` is exported. PPSA99004 imports it. Console behaviour is unverified
- [sinhf](../../../core/libs/prx/libc/src/Math.cpp) (libc) follows the `sinhf` of the `libc.prx` bundled with PPSA28319 (disassembly): FreeBSD msun's [`e_sinhf.c`](https://github.com/freebsd/freebsd-src/blob/5aa554c7e5b6fd9042986df07f12ac5687e41392/lib/msun/src/e_sinhf.c) from before `__ldexp_expf`, which squares `expf(|x| / 2)` between `logf(FLT_MAX)` and the overflow threshold, calling msun's [`expm1f`](https://github.com/freebsd/freebsd-src/blob/59bc2f095289a10b8cfa55cbb2b45c3bd6c2d4ae/lib/msun/src/s_expm1f.c) and [`expf`](https://github.com/freebsd/freebsd-src/blob/59bc2f095289a10b8cfa55cbb2b45c3bd6c2d4ae/lib/msun/src/e_expf.c), whose paths for the arguments it passes are inlined here. Past the threshold Sony's error helper sets `errno` to `ERANGE` before returning `x * 1e37`; msun itself sets no `errno`. That code, run on a Windows host, gives the same bits as this implementation for every finite argument up to the threshold, of both signs; it was not run on Linux. That the system `libSceLibcInternal`, which Ruffle (PPSA68091) and RetroArch (PPSA99169) import while bundling a zero-return `libc.prx`, has the same code is assumed
- [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 itself sets no `errno`. 20 million random arguments in (-1, 1) give the same bits as that code run on a Windows host. The result needs the x87 precision control at 64 bits, as FreeBSD amd64 starts threads (control word `0x37f`), but guest threads do not set it: on Windows a thread started by `_beginthreadex` runs at 53 bits (`0x27f`), where `asinl(0.75)` returns mantissa `0xd91a98ae3406e000` instead of `0xd91a98ae3406e041`. The system `libSceLibcInternal` that RetroArch (PPSA99169) imports is assumed to have the same code
Expand Down
Loading