feat(libc): format num_put<char>::do_put as the console's libc does #11054

Merged
verusferro merged 2 commits from upstream-pr/11054 into main 2026-10-26 22:00:13 +00:00
verusferro commented 2026-10-25 15:33:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by verusferro on 2026-10-25T15:33:41Z.


What

All eight num_put<char>::do_put overloads threw (NumPutMethodUnsupported), so a title that writes a number to a stream (std::ostream << 42) stopped there. They now do what the SDK libc.prx that PPSA06416 ships in sce_module/ (title SDK 7.00) does, read from its disassembly (REVERSE_ENGINEERING, source 2). The module exports them as ke36E2bqNmI (bool), I3+xmBWGPGo, nlAk46weq1w, H2KGT3vA7yQ, Vbeoft607aI (long, unsigned long, long long, unsigned long long), F+cp2B3cWNU, rLiFc4+HyHw (double, long double) and 0xgFRKf0Lc4 (const void*), with the helpers _Iput (0x95710) and _Fput (0x968d0). The structure is Dinkumware's, between the VC12 and current MSVC versions, with Sony's flag values:

flag value flag value
left / right / internal 0x2 / 0x4 / 0x8 showpoint 0x100
dec / oct / hex 0x10 / 0x20 / 0x40 uppercase 0x200
showbase 0x80 showpos 0x400
scientific / fixed / hexfloat 0x800 / 0x1000 / 0x1800 boolalpha 0x8000
  • Integers and pointers: %[+][#] (showpos, showbase), l or ll, then o, x/X or d/u, or %p, into a 64-byte buffer. _Iput keeps a sign, and with hex a 0x, as the prefix.
  • Floating point: the precision is used as is, or 6 under hexfloat. fixed values above 1e10 grow the buffer by |exp2| * 30103 / 100000, plus 50. The format is %[+][#][.*][L] with f (lowercase even with uppercase), a/A, e/E or g/G. Unlike VC12, _Fput takes a sign or else a leading 0x as the prefix, so -0x1.8p+0 gets internal fill after the -, and it skips grouping when the text after the prefix starts with I, N, i or n.
  • Both helpers widen the digits through the stream locale's ctype<char>, put the thousands separator of its numpunct<char> by the grouping, put its decimal point (where localeconv puts the C one), then fill to the width: before the field, after the prefix (internal) or after the field (left), and reset the width to 0.
  • bool without boolalpha calls the facet's own do_put(long) slot; with it, it writes truename() or falsename() with the same fill.

The facets come from the stream's locale as the inlined use_facet does: the facet id (assigned under the libc lock if still 0), the locale's facet vector, then the global locale when the locale is transparent. Missing facets fall back to the C locale: a cached numpunct from _Getcat, and digits left as they are for ctype, since this library has no ctype<char>::_Getcat and the console's C-locale ctype does not change them. numpunct strings are freed with the console's large-allocation rule. GuestLocale.hpp describes the num_put and ctype vtables; the iterator and stream buffer types come from #11044, which this builds on.

Two known differences, recorded in a new debt entry:

  • The text comes from this library's snprintf, which converts floating point exactly and rounds ties to even. The module's converter keeps about 18 exact significant digits and prints later ones from an inexact scaling, so 1e10 at precision 22 is 9999999999.999999999999 there, and -2.5 at precision 0 is -3.
  • A precision so negative that the module's buffer (precision + 50 bytes) cannot hold the result throws here. The module reads stale bytes past what sprintf wrote.

Tested

Ubuntu 24.04 (WSL2) x86-64, GCC 13.3, Release, full build with BUILD_TESTING=ON:

  • The title's libc.prx, converted by the relinker and loaded in a host process, compared against this library's facet with the same fake stream buffer and stream: 502,800 calls per side. They cover every overload, base and float field, showbase/showpos/uppercase/showpoint combination, all four adjustments, widths -1 to 30, precisions -1 to 40, values from 0 and denormals to INT64_MIN, UINT64_MAX, 1e300, 1e4000L, NaN and infinities, a stream buffer with room, two bytes of room with overflow accepting or refusing, a null buffer and a null put pointer, and the module's C locale or a locale with a guest numpunct (, point, . separator, grouping \3\2, a 22-character and a 5000-character name).
    • Integers, bool and pointers match in every call.
    • Floating point matches in every call where the module's sprintf and this library's snprintf print the same text for the same format, precision and value. The 58,880 calls that differ are exactly those where the two sprintf outputs already differ.
    • With grouping \1, both leave inf, -inf and nan ungrouped and group 1.2.3.4,5.
  • guest_locale gains CheckNumPut: 19 calls with strings taken from the module's output, including internal fill with a sign and with 0X, the -0x1.8p+0 prefix, a custom locale with grouping \1\2 (which would split inf without the infinity rule), and the throw for precision -45. Taking 0x as part of the prefix only under hexfloat after the sign (VC12) fails it, and so does grouping infinities.
  • ctest -R '^(guest_locale|guest_strftime|guest_wide_formatting|patched_libraries|conventions_unknown_nids)$': 5/5 pass.
  • tools/progress.py: libc +8 implemented (NumPutBool … NumPutPointer), NumPutMethodUnsupported removed.
  • python3 tools/check_conventions.py --base origin/main: passed. python3 tools/pr_overlap.py: no conflicting lines or same exports. Of the 39 pull requests it could not compare, only #9526 names one of the types this touches, by creating an IosBase in its own code; none changes num_put, the stream buffer types or this debt entry.

Windows: not run by me. guest_locale passed in @ycbuzi's run of e6c4608736 and in @wachi35676's run of this head 1d31901c4b.

Not tested: macOS, a title that writes numbers to a stream.

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
  • Generic behaviour, not specific to one title
  • Depends on: #11044
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5006091691 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11054). Originally posted by [verusferro](https://github.com/verusferro) on 2026-10-25T15:33:41Z. --- ### What All eight `num_put<char>::do_put` overloads threw (`NumPutMethodUnsupported`), so a title that writes a number to a stream (`std::ostream << 42`) stopped there. They now do what the SDK `libc.prx` that PPSA06416 ships in `sce_module/` (title SDK 7.00) does, read from its disassembly ([REVERSE_ENGINEERING](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/REVERSE_ENGINEERING.md), source 2). The module exports them as `ke36E2bqNmI` (bool), `I3+xmBWGPGo`, `nlAk46weq1w`, `H2KGT3vA7yQ`, `Vbeoft607aI` (long, unsigned long, long long, unsigned long long), `F+cp2B3cWNU`, `rLiFc4+HyHw` (double, long double) and `0xgFRKf0Lc4` (`const void*`), with the helpers `_Iput` (0x95710) and `_Fput` (0x968d0). The structure is Dinkumware's, between the [VC12](https://github.com/ganboing/crt.vc12/blob/f1d47126bf13d1c9dccaa6c53b72217fd0474968/src/xlocnum) and current MSVC versions, with Sony's flag values: | flag | value | flag | value | |---|---|---|---| | `left` / `right` / `internal` | 0x2 / 0x4 / 0x8 | `showpoint` | 0x100 | | `dec` / `oct` / `hex` | 0x10 / 0x20 / 0x40 | `uppercase` | 0x200 | | `showbase` | 0x80 | `showpos` | 0x400 | | `scientific` / `fixed` / `hexfloat` | 0x800 / 0x1000 / 0x1800 | `boolalpha` | 0x8000 | - Integers and pointers: `%[+][#]` (showpos, showbase), `l` or `ll`, then `o`, `x`/`X` or `d`/`u`, or `%p`, into a 64-byte buffer. `_Iput` keeps a sign, and with `hex` a `0x`, as the prefix. - Floating point: the precision is used as is, or 6 under `hexfloat`. `fixed` values above 1e10 grow the buffer by `|exp2| * 30103 / 100000`, plus 50. The format is `%[+][#][.*][L]` with `f` (lowercase even with `uppercase`), `a`/`A`, `e`/`E` or `g`/`G`. Unlike VC12, `_Fput` takes a sign or else a leading `0x` as the prefix, so `-0x1.8p+0` gets internal fill after the `-`, and it skips grouping when the text after the prefix starts with `I`, `N`, `i` or `n`. - Both helpers widen the digits through the stream locale's `ctype<char>`, put the thousands separator of its `numpunct<char>` by the grouping, put its decimal point (where `localeconv` puts the C one), then fill to the width: before the field, after the prefix (`internal`) or after the field (`left`), and reset the width to 0. - `bool` without `boolalpha` calls the facet's own `do_put(long)` slot; with it, it writes `truename()` or `falsename()` with the same fill. The facets come from the stream's locale as the inlined `use_facet` does: the facet id (assigned under the libc lock if still 0), the locale's facet vector, then the global locale when the locale is transparent. Missing facets fall back to the C locale: a cached `numpunct` from `_Getcat`, and digits left as they are for `ctype`, since this library has no `ctype<char>::_Getcat` and the console's C-locale `ctype` does not change them. `numpunct` strings are freed with the console's large-allocation rule. `GuestLocale.hpp` describes the `num_put` and `ctype` vtables; the iterator and stream buffer types come from #11044, which this builds on. Two known differences, recorded in a new debt entry: - The text comes from this library's `snprintf`, which converts floating point exactly and rounds ties to even. The module's converter keeps about 18 exact significant digits and prints later ones from an inexact scaling, so 1e10 at precision 22 is `9999999999.999999999999` there, and -2.5 at precision 0 is `-3`. - A precision so negative that the module's buffer (precision + 50 bytes) cannot hold the result throws here. The module reads stale bytes past what `sprintf` wrote. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Ubuntu 24.04 (WSL2) x86-64, GCC 13.3, Release, full build with `BUILD_TESTING=ON`: - The title's `libc.prx`, converted by the relinker and loaded in a host process, compared against this library's facet with the same fake stream buffer and stream: 502,800 calls per side. They cover every overload, base and float field, showbase/showpos/uppercase/showpoint combination, all four adjustments, widths -1 to 30, precisions -1 to 40, values from 0 and denormals to `INT64_MIN`, `UINT64_MAX`, 1e300, 1e4000L, NaN and infinities, a stream buffer with room, two bytes of room with `overflow` accepting or refusing, a null buffer and a null put pointer, and the module's C locale or a locale with a guest `numpunct` (`,` point, `.` separator, grouping `\3\2`, a 22-character and a 5000-character name). - Integers, `bool` and pointers match in every call. - Floating point matches in every call where the module's `sprintf` and this library's `snprintf` print the same text for the same format, precision and value. The 58,880 calls that differ are exactly those where the two `sprintf` outputs already differ. - With grouping `\1`, both leave `inf`, `-inf` and `nan` ungrouped and group `1.2.3.4,5`. - `guest_locale` gains `CheckNumPut`: 19 calls with strings taken from the module's output, including internal fill with a sign and with `0X`, the `-0x1.8p+0` prefix, a custom locale with grouping `\1\2` (which would split `inf` without the infinity rule), and the throw for precision -45. Taking `0x` as part of the prefix only under `hexfloat` after the sign (VC12) fails it, and so does grouping infinities. - `ctest -R '^(guest_locale|guest_strftime|guest_wide_formatting|patched_libraries|conventions_unknown_nids)$'`: 5/5 pass. - `tools/progress.py`: libc +8 implemented (`NumPutBool` … `NumPutPointer`), `NumPutMethodUnsupported` removed. - `python3 tools/check_conventions.py --base origin/main`: passed. `python3 tools/pr_overlap.py`: no conflicting lines or same exports. Of the 39 pull requests it could not compare, only #9526 names one of the types this touches, by creating an `IosBase` in its own code; none changes `num_put`, the stream buffer types or this debt entry. Windows: not run by me. `guest_locale` passed in [@ycbuzi's run](https://github.com/boykopovar/AnyPS5/pull/11054#issuecomment-6399720487) of `e6c4608736` and in [@wachi35676's run](https://github.com/boykopovar/AnyPS5/pull/11044#issuecomment-6403822734) of this head `1d31901c4b`. Not tested: macOS, a title that writes numbers to a stream. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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 - [x] Generic behaviour, not specific to one title - [x] Depends on: #11044 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
MarcZX10 commented 2026-10-25 15:46:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T15:46:18Z.

Review: APPROVED


Reviewed exact head e6c46087366fec43ada4f9f18bcbc0d67e133c88. The eight vtable slots and their argument types match the inspected SDK layout; integer, pointer, floating, boolalpha, locale grouping/decimal-point, fill/adjustment, and width-reset paths are exercised by the new guest_locale cases. The PR documents the observed snprintf rounding differences and oversized negative-precision behavior instead of claiming parity where it differs. The author's 502,800-call comparison against the title's SDK libc.prx is detailed and reports integer/bool/pointer agreement and float differences only where snprintf text differs. I independently ran diff-check, conventions, and GCC C++20 syntax checks for LocaleSupport.cpp and GuestLocale.cpp; all passed. All applicable hosted jobs now pass, including the previously pending macOS Clang relinker; product Linux/Windows jobs are skipped by path policy. I could not run the runtime suite locally because this checkout lacks the SDL2 submodule, so the focused CTest and SDK comparison remain author-reported. I found no blocking issue in the reviewed change.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5643953875 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11054#pullrequestreview-5643953875). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T15:46:18Z. **Review:** APPROVED --- Reviewed exact head e6c46087366fec43ada4f9f18bcbc0d67e133c88. The eight vtable slots and their argument types match the inspected SDK layout; integer, pointer, floating, boolalpha, locale grouping/decimal-point, fill/adjustment, and width-reset paths are exercised by the new guest_locale cases. The PR documents the observed snprintf rounding differences and oversized negative-precision behavior instead of claiming parity where it differs. The author's 502,800-call comparison against the title's SDK libc.prx is detailed and reports integer/bool/pointer agreement and float differences only where snprintf text differs. I independently ran diff-check, conventions, and GCC C++20 syntax checks for LocaleSupport.cpp and GuestLocale.cpp; all passed. All applicable hosted jobs now pass, including the previously pending macOS Clang relinker; product Linux/Windows jobs are skipped by path policy. I could not run the runtime suite locally because this checkout lacks the SDL2 submodule, so the focused CTest and SDK comparison remain author-reported. I found no blocking issue in the reviewed change.
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!11054
No description provided.