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

Merged
JJenkx merged 1 commit from upstream-pr/10379 into main 2026-10-26 18:56:48 +00:00
JJenkx commented 2026-10-23 20:10:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-23T20:10:34Z.


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 89f761f7ab.

  • Comparison: a scratch loader runs the decrypted libc.prx of PPSA07923 and PPSA13934 natively and calls their swprintf (NID nJz16JE1txM) on NaN formats. With this PR's libc.prx DLL, both formats print byte-identical text to both titles' swprintf. main gives [nan][+nan][ NAN][ nan] and [nan ][ nan][NAN][-inf][+NAN].
  • guest_wide_formatting: the new CheckNanSign checks those formats against the console's text:
    • [-nan][-nan][ NAN][ -nan];
    • [nan ][ -nan][-NAN][-inf][+NAN].
  • guest_wide_formatting and guest_formatting pass in three runs out of three on the exact head.
  • Negative control: with #9958's FormattingWide.cpp and these tests, guest_wide_formatting aborts in a Require, while guest_formatting still passes.
  • python tools/check_conventions.py --base #9958's head, and git diff --check, pass.
  • Order: this PR is one commit on top of my wide-printf stack #9699 → #9916 → #9927 → #9958, and lands after #9958. #9927 rewrote the wide float branch this PR edits. The NaN branch comes first, then #9927's finite path, then the host for infinities. It doesn't touch WindowsFormatting.hpp, so it doesn't conflict with #10223.
  • Not tested: a console, Linux, and the narrow family (#10223).

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 under Silent stubs in their library's technical debt file
  • 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: none
  • Generic behaviour, not specific to one title
  • Depends on: #9958 (stacked on #9927, #9916 and #9699)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4988645313 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10379). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-23T20:10:34Z. --- ### 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](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/format-stack-main637220db-five-heads-linux-20261024.tar.gz) (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 89f761f7ab. - **Comparison:** a scratch loader runs the decrypted `libc.prx` of PPSA07923 and PPSA13934 natively and calls their `swprintf` (NID `nJz16JE1txM`) on NaN formats. With this PR's `libc.prx` DLL, both formats print byte-identical text to both titles' `swprintf`. `main` gives `[nan][+nan][ NAN][ nan]` and `[nan ][ nan][NAN][-inf][+NAN]`. - **`guest_wide_formatting`:** the new `CheckNanSign` checks those formats against the console's text: - `[-nan][-nan][ NAN][ -nan]`; - `[nan ][ -nan][-NAN][-inf][+NAN]`. - `guest_wide_formatting` and `guest_formatting` pass in three runs out of three on the exact head. - **Negative control:** with #9958's `FormattingWide.cpp` and these tests, `guest_wide_formatting` aborts in a `Require`, while `guest_formatting` still passes. - `python tools/check_conventions.py --base` #9958's head, and `git diff --check`, pass. - **Order:** this PR is one commit on top of my wide-printf stack #9699 → #9916 → #9927 → #9958, and lands after #9958. #9927 rewrote the wide float branch this PR edits. The NaN branch comes first, then #9927's finite path, then the host for infinities. It doesn't touch `WindowsFormatting.hpp`, so it doesn't conflict with #10223. - **Not tested:** a console, Linux, and the narrow family (#10223). ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source: none - [x] Generic behaviour, not specific to one title - [x] Depends on: #9958 (stacked on #9927, #9916 and #9699) - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes AI-assisted (Claude Code).
MarcZX10 commented 2026-10-23 20:12:16 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-23T20:12:16Z.

Review: DISMISSED


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.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5633895851 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10379#pullrequestreview-5633895851). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-23T20:12:16Z. **Review:** DISMISSED --- 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.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!10379
No description provided.