Skip to content
Merged
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
2 changes: 1 addition & 1 deletion core/libs/prx/libc/src/Strings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@ char16_t* APS5_VABI wmemset_nid_postfix(char16_t* s, char16_t c, size_t n) {
}

double APS5_VABI wcstod_nid_postfix(const char16_t* str, char16_t** endptr) {
return ParseAsciiPrefix(str, endptr, [](const char* text, char** end) { return ParseFloat<double>(text, end, std::strtod); });
return ParseAsciiPrefix(str, endptr, [](const char* text, char** end) { return strtod_nid_postfix(text, end); });
}

float APS5_VABI wcstof_nid_postfix(const char16_t* str, char16_t** endptr) {
Expand Down
7 changes: 7 additions & 0 deletions core/libs/tests/GuestMath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,13 @@ static void CheckNanConversions() {
}

static void CheckFloatUnderflowErrno() {
struct WideUnderflow { const char16_t* text; std::uint64_t bits; int error; };
for (const WideUnderflow& test : {WideUnderflow{u"1e-310", 0x000012688b70e62bull, ERANGE}, WideUnderflow{u"-4.9406564584124654e-324", 0x8000000000000001ull, ERANGE},
WideUnderflow{u"1e-330", 0, ERANGE}, WideUnderflow{u"2.2250738585072014e-308", 0x0010000000000000ull, 0}, WideUnderflow{u"0x1p-1074", 1, 0},
WideUnderflow{u"0x1.8p-1075", 1, ERANGE}, WideUnderflow{u"-0x1.000001p-1075", 0x8000000000000001ull, ERANGE}}) {
errno = 0;
Require(std::bit_cast<std::uint64_t>(wcstod_nid_postfix(test.text, nullptr)) == test.bits && errno == test.error);
}
char* end = nullptr;
errno = 7;
Require(strtod_nid_postfix("1e-310", &end) == 1e-310 && errno == ERANGE);
Expand Down
2 changes: 1 addition & 1 deletion docs/dev/debt/libc.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,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) with two gaps. A host symbolic link with a relative target under the guest root is expanded as `realpath1` does (a dangling link fails with `ENOENT`, more than 32 links with `ELOOP`), but a link with an absolute target is not expanded, nor is anything below it, because the target is a host path with no guest path to map to. Only Windows symbolic links are expanded there, not junctions. Expansion works on the guest path, so a host link whose text climbs above a path alias root is resolved against the guest tree, not the host one. Otherwise `..` drops the previous component lexically, as `Resolve` does for every guest path.
- [strtod, strtof, strtold](../../../core/libs/prx/libc/src/Strings.cpp) (libc) - `strtod` and `wcstod` (and `atof`, which calls `strtod` here) read a `NAN(...)` sequence as FreeBSD 11's gdtoa [`hexnan`](https://github.com/freebsd/freebsd-src/blob/release/11.0.0/contrib/gdtoa/hexnan.c) does: hexadecimal digits with an optional `0x`, blanks starting a new 32-bit word, the first 16 digits kept and the payload cut to 52 bits, a zero payload made 1, any other character giving the default NaN but consuming up to the closing parenthesis, and an unterminated sequence keeping its payload while consuming only `nan`. The payload goes under the quiet bit with the sign, as in gdtoa's [`strtod`](https://github.com/freebsd/freebsd-src/blob/release/11.0.0/contrib/gdtoa/strtod.c#L316-L328). `strtold` and `wcstold` return the default quiet NaN (exponent `0x7fff`, significand `0xc000000000000000`) whatever the sequence, and consume a sequence of letters, digits and underscores up to its closing parenthesis. The console's `libSceLibcInternal.sprx`, called from a payload process on a PS5 (firmware 12.70), gives these results for the 64 NaN inputs tried through `strtod`, 50 of them also through `strtold` and `wcstod`, and 18 through `wcstold`, and, through `strtod`, for `nan(1 2)` and for `nan( ` followed by NUL and `)`; titles' own `libc.prx` copies give the same `strtod` results. In `hexnan`, a blank after a payload digit, while a 32-bit word of the payload is still free, starts a blank skip that has no NUL check, so when only blanks follow up to the terminating NUL the console's `strtod` reads past it: `nan(1 ` followed by NUL and `)` consumes the `)` after the NUL, so the result depends on the bytes after the string, and here that case throws. A blank right after `nan(` is skipped with a NUL check, and the console stopped at the NUL there
- [strtof and wcstof NaN payloads](../../../core/libs/prx/libc/src/Strings.cpp) (libc) - the console's `libSceLibcInternal.sprx` (firmware 12.70) returns the default NaN for every `NAN(...)` sequence, and modules that import it reach this function through the `libc.prx` fallback, so they get the payload the titles' copy keeps. That copy, in eleven titles (64 inputs), reads the sequence as `strtod` does but into one 32-bit word: a blank after the first group of digits ends the payload, the first 8 digits are kept and cut to 24 bits under the quiet bit, and a zero payload becomes 1.
- [strtod](../../../core/libs/prx/libc/src/Strings.cpp) (libc) sets `ERANGE` for tested decimal inputs with a nonzero subnormal result, including the exact decimal expansion of 2^-1074, matching nine distinct title-bundled `libc.prx` builds measured on Windows. Two other `libc.prx` builds and the console's `libSceLibcInternal.sprx` leave `errno` alone for decimal subnormals. `wcstod` still parses through the host `std::strtod` path without this explicit rule; title and console `wcstod` behavior for decimal subnormals is unverified. Hexadecimal subnormal `strtod` and `wcstod` match one title's `libc.prx` only (PPSA08329, SHA-256 a485b34ef160, run on Linux, not on a console). Results of at least 2^-1074 come from the host `strtod`, and glibc 2.35 rounds some a unit low: `0x3ffffffffffff5p-1077` gives `0x0007fffffffffffe`, that `libc.prx` `0x0007ffffffffffff`.
- [strtod](../../../core/libs/prx/libc/src/Strings.cpp) (libc) sets `ERANGE` for tested decimal inputs with a nonzero subnormal result, including the exact decimal expansion of 2^-1074, matching nine distinct title-bundled `libc.prx` builds measured on Windows. Two other `libc.prx` builds and the console's `libSceLibcInternal.sprx` leave `errno` alone for decimal subnormals. `wcstod` parses through `strtod` and follows the same rule, as the `wcstod` of the `libc.prx` bundled with eleven titles does (PPSA04158 to PPSA35169, measured on Windows); the console's `libSceLibcInternal` `wcstod` is unmeasured. Hexadecimal subnormal `strtod` and `wcstod` match one title's `libc.prx` only (PPSA08329, SHA-256 a485b34ef160, run on Linux, not on a console). Results of at least 2^-1074 come from the host `strtod`, and glibc 2.35 rounds some a unit low: `0x3ffffffffffff5p-1077` gives `0x0007fffffffffffe`, that `libc.prx` `0x0007ffffffffffff`.
- [remquol](../../../core/libs/prx/libc/src/MathExact.cpp) (libc) leaves `*quo` unchanged when the result is NaN; the `libc.prx` bundled with PPSA08427 writes a value that depends on earlier x87 state (`remquol(1, 0)` stored 0 on a first call and 4 on later ones)
- [_Stod, _Stodx, _Stofx, _Stoldx, _Stof and _Stold](../../../core/libs/prx/libc/src/DinkumwareInternals.cpp) (libSceLibcInternal, implemented in libc) throw for hexadecimal text with a non-zero power-of-ten scale. The console's result there does not follow a simple rule: `0x3` with a scale of -1 gives 0.29999999999999999. A nonzero subnormal result sets neither `errno` nor the error argument, as measured in firmware 13.60's `libSceLibcInternal.sprx` run in a Linux host process, not on a console; there about a fifth of decimal inputs with a subnormal result come out one unit apart in the last place (`_Stodx("2.2250738585072014e-308", 0)` is the largest subnormal), which is not reproduced. Hexadecimal underflow and subnormal results with a zero scale are unverified for `libSceLibcInternal.sprx`; they follow the `strtod` rule of PPSA08329's `libc.prx`, so `_Stodx("0x1.00000000000001p-1075", 0)` gives 0 with an error of 1, not 2^-1074
- [_LLog](../../../core/libs/prx/libc/src/DinkumwareInternals.cpp) (libc) follows the public Dinkumware CRT contract for its base flag (`0` natural, positive base 10, negative base 2). The exact console results, signaling-NaN behavior and floating-point exception behavior are not verified.
Expand Down
Loading