Skip to content

fix(libc): return the default quiet NaN from the strtod family - #4616

Merged
adriadam10 merged 3 commits into
boykopovar:mainfrom
EagleBearerYS:fix/libc-strtod-quiet-nan-windows
Oct 13, 2026
Merged

adriadam10 merged 3 commits into
boykopovar:mainfrom
EagleBearerYS:fix/libc-strtod-quiet-nan-windows

Conversation

@EagleBearerYS

@EagleBearerYS EagleBearerYS commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

What

C17 7.22.1.3p4, and 7.29.4.1.1p4 for the wide functions, read NAN(n-char-sequence) as a quiet NaN and leave the meaning of the sequence to the implementation. 7.12.11.2p2 makes nan("n-char-sequence") equivalent to strtod("NAN(n-char-sequence)", NULL), and nan and nanf on main return the console's fixed quiet NaNs and ignore the tag (31906bf, from the console's libc.prx). The strtod family did not match them. On Windows it returned signaling NaNs, x87 pseudo-NaNs and infinities, measured by the new test with main's implementation:

Input strtod and atof strtof strtold
nan(0x123) 0x7ff0000000000123 (signaling) 0x7f800123 (signaling) 0x7fff'0000000000000123 (pseudo-NaN)
-NaN(1) 0xfff0000000000001 0xff800001 0xffff'0000000000000001
nan(0x800000) 0x7ff0000000800000 0x7f800000 (+inf) 0x7fff'0000000000800000
nan(0x10000000000000) 0x7ff0000000000000 (+inf) 0x7f800001 0x7fff'0010000000000000
nan(0x8000000000000000) 0x7ff0000000000001 0x7f800001 0x7fff'8000000000000000 (+inf)

With this change every row gives what nan and nanf return: 0x7ff8000000000000 and 0x7fc00000, with the sign bit set for -NaN(1), and for strtold the x87 default quiet NaN 0x7fff'c000000000000000. The end pointers are unchanged.

In C++ the MinGW-w64 toolchain defines __USE_MINGW_ANSI_STDIO, so std::strtod and std::strtof are __mingw_strtod and __mingw_strtof, and strtold is mingw-w64's x87 strtold. Their NaN-with-payload case copies the hex digits into the significand without the quiet bit (strtodnrp.c builds 0x7ff00000 | bits[1]; strtof.c and strtopx.c fall through to the normal-number case), so a payload whose only set bit is the implicit or the x87 integer bit gives an infinity. On Linux, glibc returns quiet NaNs that keep the payload.

ParseFloat, which strtod, strtof, strtold, atof and the wide variants share since #3905, now replaces an infinite or NaN result with the default quiet NaN of the same sign when the subject, after white space and a sign, starts with n or N; inf and infinity subjects are untouched. The result is tested on its bits rather than with isnan, so the signaling host value raises no FE_INVALID. TechnicalDebt records that the result is inferred from nan, nanf and 7.12.11.2p2, not observed on the console, and that no console nanl was observed for strtold.

Tested

Windows 11 (10.0.26300), WinLibs MinGW-w64 GCC 15.2.0 r7 (UCRT), Release, -DBUILD_TESTING=ON, on main at 2a5eca1:

  • New CheckNanConversions in guest_math checks the bits and end pointer of the five inputs through strtod, atof, strtof and strtold, and guest_wide_strings checks wcstod, wcstof and wcstold on u" -nan(0x123)é". With main's Strings.cpp, guest_math prints the 20 values in the table and fails, and guest_wide_strings aborts at the wcstod check. With the change both pass, 20 runs in a row.
  • All 128 tests registered in core/libs/prx/libc/CMakeLists.txt and core/libs/prx/libkernel/CMakeLists.txt that build on Windows pass.
  • tools/check_conventions.py --base origin/main passes.
  • Linux: WSL2 (Ubuntu 24.04, GCC 13.3.0, Release with CI's configure flags), merged into main at c952c2b. With main's Strings.cpp, guest_math and guest_wide_strings fail, since glibc keeps the payload in the quiet NaN (strtod("nan(0x123)") gives 0x7ff8000000000123). With the change both pass, 20 runs in a row.
  • Not tested: macOS, any title.

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 in TechnicalDebt
  • 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
  • AI-assisted: yes

C17 7.22.1.3 and 7.29.4.1.1 interpret NAN(n-char-sequence) as a quiet
NaN. With MinGW-w64, std::strtod, strtof and strtold are mingw-w64's
gdtoa functions, which copy the hex digits into the significand
without the quiet bit, and for long double without the x87 integer
bit: strtod("nan(0x123)") returned the signaling NaN
0x7ff0000000000123 and strtold a pseudo-NaN. A payload whose only set
bit is the implicit or x87 integer bit gave infinity:
strtof("nan(0x800000)"), strtod("nan(0x10000000000000)") and
strtold("nan(0x8000000000000000)") returned +inf. glibc returns quiet
NaNs that keep the payload.

C17 7.12.11.2p2 makes nan("n-char-sequence") equivalent to
strtod("NAN(n-char-sequence)", NULL), and nan and nanf already return
the console's fixed quiet NaNs, ignoring the tag. ParseFloat, which
strtod, strtof, strtold, atof and the wide variants share, now returns
the same values, 0x7ff8000000000000 and 0x7fc00000, negated after a
minus sign, on Windows and Linux. strtold returns the x87 default
quiet NaN, since no console nanl was observed.
@github-actions github-actions Bot added needs rebase Conflicts with main, rebase needed labels Oct 12, 2026
…-nan-windows

# Conflicts:
#	docs/dev/TechnicalDebt.md
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 12, 2026
@adriadam10

Copy link
Copy Markdown
Collaborator

Checked the diff, guest_math and guest_wide_strings (pass with the change, both fail with Strings.cpp reverted), and the TechnicalDebt entry (inferred behaviour recorded). Merging once the full Build is green.

@adriadam10
adriadam10 merged commit 7e4edf5 into boykopovar:main Oct 13, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants