Skip to content

fix(libc): print a NaN's sign in swprintf as titles' libc.prx does - #10379

Merged
adriadam10 merged 1 commit into
boykopovar:mainfrom
JJenkx:windows/libc-printf-nan-sign
Oct 26, 2026
Merged

adriadam10 merged 1 commit into
boykopovar:mainfrom
JJenkx:windows/libc-printf-nan-sign

Conversation

@JJenkx

@JJenkx JJenkx commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

What

The wide printf family (swprintf, vswprintf and the rest) leaves NaN to the host. There, a negative NaN loses its sign, but a positive one gets +nan with the + flag. The libc.prx that titles ship formats a NaN in swprintf exactly as in its snprintf, the way it formats an infinity:

  • -nan for a negative NaN;
  • +nan with the + flag and nan with the space flag for a positive one;
  • NAN for %F, %E, %G and %A;
  • padded with spaces to the width, left-aligned with -, and the 0 flag ignored.

For example, swprintf(u"[%f][%+f][% F][%5.1e]", -NAN, -NAN, NAN, -NAN) gives [-nan][-nan][ NAN][ -nan] there and [nan][+nan][ NAN][ nan] here.

The wide formatter now builds that text itself for a NaN, in both the double and long double paths, through a small WideNanText helper, and pads it with the existing AppendPadded.

The narrow family's NaN sign is #10223's (orezraey, measured on a retail PS5's libSceLibcInternal.sprx). This PR doesn't touch it. An earlier version of this PR changed it too, which duplicated #10223, so that part is gone. On #10223 I posted that eleven titles' bundled libc.prx print the narrow NaN text the same way.

Tested

Current Linux refresh: afe172cc68c82877b6c5f2309d70c6920a66e477 on main 637220db112a3daf4960984290a82fcf65ba7061. The stack is rebuilt linearly in the order #9699 → #9916 → #9927 → #9958 → #10379, with no merge commits or duplicated prerequisite commits. This PR adds one wide-NaN-sign commit. Fresh Release libc/fixture build and guest_formatting, guest_wide_formatting, guest_stdio_variadic pass (3/3) on this exact head; conventions against the tested main and whitespace checks pass. Final-stack conventions against origin/main have zero errors (six inherited debt-length warnings). All five heads also merge cleanly with newer main c8d34aa8f4d8058d57169cc1e423ba6db00faaf0; that merge preview is not an additional runtime test. The original WideNanText body and its two wide branches are retained after finite double/long-double support. Restored final-head tests pass three repeats. Reverting only this wide change also passes on Linux, so the Linux negative control does not distinguish the Windows bug. All five pinned heads, patches, checks and control logs (SHA-256 9cda806ab2a54a4a0ce012517eafb7db0441ab2665d2e49884c3d1fc5b9e0c8a). No fresh Windows, title-library comparison, physical-console, game or performance result is claimed; Windows testing remains paused. The requested fresh Windows validation on #9699 is still outstanding.

Historical validation on previous public head 6d8701ab2e86c426f600463dcbb9512fc6be7cba is retained below with its original scope; references there to ‘exact head’ refer to that historical run.

Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, BUILD_TESTING=ON, on #9958's head 89f761f.

Checklist

AI-assisted (Claude Code).

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reviewed the NaN formatting paths and the new narrow/wide test cases. The helper preserves the negative sign and +/space precedence, and the reported same-title comparisons cover the relevant conversions and flags. Before merge, the shared-file order still needs to be recorded: pairwise merge previews with open #9916, #9927 and #9958 conflict in WindowsFormatting.hpp; #9927 and #9958 also conflict in FormattingWide.cpp. Those PRs are explicitly described here as needing to retain both sets of branches, but this PR's checklist says Depends on: none. CONTRIBUTING requires agreement on the order and the prerequisite on the checklist. Please coordinate with the authors (these are your PRs per the descriptions), declare the agreed dependency/order—e.g. stack this after #9958 if that is the intended sequence—and confirm the final result keeps both the NaN-sign branch and the existing finite/wide/long-double formatting paths. No merge is requested before that coordination.

@github-actions

github-actions Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

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
PR 10813 core/libs/prx/libc/src/FormattingWide.cpp L85
core/libs/tests/GuestWideFormatting.cpp L4

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 5303 core/libs/prx/libc/src/FormattingWide.cpp
core/libs/tests/GuestWideFormatting.cpp
PR 10409 core/libs/tests/GuestWideFormatting.cpp

@JJenkx
JJenkx force-pushed the windows/libc-printf-nan-sign branch from a8c8ce4 to da6b45e Compare October 23, 2026 20:39
@JJenkx

JJenkx commented Oct 23, 2026

Copy link
Copy Markdown
Contributor Author

@MarcZX10 thanks. All four of those PRs are mine, so I've settled the order by stacking this PR on them. The branch is now one commit (da6b45e) on #9958's head 89f761f, so the order is #9699 → #9916 → #9927 → #9958 → this PR. The checklist now says Depends on: #9958.

Resolution on the stack:

Tested on the stacked head (Windows, GCC 15.2 UCRT, Release):

  • guest_formatting and guest_wide_formatting pass in three runs out of three.
  • Negative control: with fix(libc): format %La like titles' libc.prx does #9958's two formatting files and these tests, both abort.
  • Console comparison: this build's libc.prx DLL matches PPSA07923's and PPSA13934's snprintf on the 108 NaN and infinity outputs: 18 formats × 6 values.

AI-assisted: yes (Claude Code)

@JJenkx JJenkx changed the title fix(libc): print a NaN's sign in printf and swprintf as titles' libc.prx does fix(libc): print a NaN's sign in swprintf as titles' libc.prx does Oct 23, 2026
@JJenkx
JJenkx force-pushed the windows/libc-printf-nan-sign branch from da6b45e to 6d8701a Compare October 23, 2026 20:46
@JJenkx

JJenkx commented Oct 23, 2026

Copy link
Copy Markdown
Contributor Author

Correction: after restacking I found #10223 (orezraey), opened earlier today, which already makes the narrow printf family print a NaN's sign, measured on a retail PS5. The narrow half of this PR duplicated it; I missed it in my overlap search. I've cut this PR down to the wide family only (6d8701a, still one commit on #9958): swprintf and the rest, which #10223 doesn't touch. The narrow formatter and guest_formatting are back to #9958's. The wide fix uses its own helper and AppendPadded, so it doesn't depend on #10223's NanSpec change. Retested: guest_wide_formatting and guest_formatting 3/3; with #9958's FormattingWide.cpp, guest_wide_formatting aborts; the DLL's swprintf output matches PPSA07923's and PPSA13934's. The order stays #9699 → #9916 → #9927 → #9958 → this PR.

AI-assisted: yes (Claude Code)

@JJenkx

JJenkx commented Oct 23, 2026

Copy link
Copy Markdown
Contributor Author

Linux run at this PR's exact head 6d8701ab2e (6d8701ab2e86c426f600463dcbb9512fc6be7cba), not merged onto anything else; its base on main is 21554447d0.

Arch Linux, kernel 7.2.9, Ryzen 9 7950X, g++ (GCC) 16.2.1 20260810, glibc 2.44, x86-64. Release, BUILD_TESTING=ON.

  • guest_wide_formatting, guest_formatting: passed in each of 3 runs, exit code 0 each time, with no failed, skipped or not-run tests.

This is the focused test only; no title, console or floating-point exception flags were checked.

AI-assisted: yes (Claude Code).

@JJenkx

JJenkx commented Oct 24, 2026

Copy link
Copy Markdown
Contributor Author

The refreshed order is #9699 → #9916 → #9927 → #9958 → this PR. Head afe172cc68c82877b6c5f2309d70c6920a66e477 is one wide-NaN commit directly on refreshed #9958 252b1593b85d3c1fdef3822c795bce98666b8e6d; the checklist declares that dependency. Original hexadecimal/decimal helpers and WideNanText are byte-identical, main's newer narrow NaN paths are retained, and the exact-head Linux formatting/wide/variadic groups pass. A Linux control with only this wide change reverted also passes, so it does not reproduce the Windows-specific defect; I have not claimed otherwise or rerun Windows. Heads, patches, per-head logs and controls.

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 24, 2026
@Queuse Queuse mentioned this pull request Oct 26, 2026
12 tasks done
@JJenkx
JJenkx force-pushed the windows/libc-printf-nan-sign branch from afe172c to 8046f86 Compare October 26, 2026 12:28
@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 26, 2026
@adriadam10 adriadam10 self-assigned this Oct 26, 2026
The wide printf family left NaN to the host, which dropped the sign of a
negative NaN but printed +nan with the + flag. The libc.prx that titles
ship prints a NaN in swprintf like an infinity, as its snprintf does:
-nan for a negative NaN, +nan or " nan" with the + and space flags, NAN
for the upper-case conversions, padded with spaces. The wide formatter now
does the same through its own helper; the narrow family is boykopovar#10223's.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@adriadam10
adriadam10 force-pushed the windows/libc-printf-nan-sign branch from 8046f86 to f8c4066 Compare October 26, 2026 17:13
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 26, 2026
@adriadam10
adriadam10 dismissed MarcZX10’s stale review October 26, 2026 17:27

Dismissing a stale review: the requested changes were addressed after it (new head) and the merge queue blocks on it. Please request changes again if anything is still open.

@adriadam10
adriadam10 merged commit 6b16c9c into boykopovar:main Oct 26, 2026
17 checks passed
@adriadam10

Copy link
Copy Markdown
Collaborator

Checked WideNanText against the narrow rules (sign, +/space precedence, upper-case NAN, space padding, 0 ignored) in both the double and long double branches, and the expected strings and lengths in CheckNanSign. The branch still carried the %La commit that landed as #9958, so I rebased it onto current main (one commit left); the full Build runs on the rebased head. Merged.

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.

3 participants