feat(libc): implement the std::__sce_v2 num_put virtuals for char #16516

Open
Danigamer05 wants to merge 11 commits from upstream-pr/16516 into main
Danigamer05 commented 2026-11-06 23:53:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Danigamer05 on 2026-11-06T23:53:04Z.


What

Builds on #16214: this branch contains its commits, and through it #16178 and #15923. Until they merge, only the last commit is this PR's change.

Implement the eight do_put virtuals of the versioned std::__sce_v2::num_put<char> facet that _Makeloc builds. Before this PR, each of them threw NotImplemented_nid_no_patch. These slots are not exports: guest code reaches them through the facet's vtable, so this PR adds no NID.

The behavior comes from the libc.prx bundled with PPSA03671 v01.001.005 (J03848509, SHA-256 483961e086b8a1fffa8138b3c0badf798987fb5f35ec908d828946408be2d8a6), vtable 0x18C700:

Offset Virtual Format
0x20 do_put(bool) without boolalpha, calls the long slot with the byte zero-extended; with it, do_truename or do_falsename
0x28, 0x30 do_put(long), do_put(unsigned long) %[+][#]l + d/u, o, x/X
0x38, 0x40 do_put(long long), do_put(unsigned long long) the same with ll
0x48, 0x50 do_put(double), do_put(long double) %[+][#].*[L] + g/G, e/E, f, a/A
0x58 do_put(const void*) %p

Where the versioned facet differs from the existing unversioned num_put<char>, so it has its own code:

  • The ios_base layout and flag bits differ: flags at 0x18, 64-bit precision at 0x20 and width at 0x28; uppercase 0x4, showbase 0x8, showpoint 0x10, showpos 0x20, left 0x40, internal 0x100, oct 0x400, hex 0x800, scientific 0x1000, fixed 0x2000, boolalpha 0x4000. SceV2IosBase now names these fields.
  • A direct write checks the int count that the buffer holds a pointer to at 0x58, and decrements it. Otherwise the character goes through overflow. A null buffer or a rejected overflow sets failed, keeps the buffer and keeps writing.
  • Hex float keeps .* with precision -1. For the other fields the precision passes through unchanged, and only its low 32 bits size the buffer.
  • A non-finite value clears showpoint and skips grouping. The hex prefix counts only under hex float, and comes after the sign.
  • The versioned string is 0x20 bytes, not the 0x28 of GuestLocale::String, so the numpunct strings have their own reader.
  • Digits go through sprintf_s, as in the module, not snprintf.
  • Facets come from UseSceV2Facet, which falls back to the global locale and then a cached default as the module does.

The other versioned virtuals still throw, including the wide and unsigned short num_put, and the classic _Locimp stays without facets. The _Makeloc debt entry now names num_put<char>, and a new entry lists the gaps:

  • floating-point digits come from this library's sprintf_s
  • a buffer too small for the result throws length_error
  • a string too large to allocate throws the host's bad_alloc
  • the lookups take no reference on the stream's locale

Tested

Windows 11, MinGW GCC 15.2, Release, head on top of #16214. Not run on Linux or macOS, not run in a title.

  • New test guest_sce_v2_num_put_char: every slot, flags, adjustment, grouping, non-finite values, the put count, overflow and failure paths. It passes, as do guest_sce_v2_char_facet_virtuals, guest_sce_v2_makeloc, guest_sce_v2_locale, guest_sce_v2_locale_basics, guest_sce_v2_locimp_references, guest_sce_v2_facet_ids, guest_locale, guest_sce_v2_exceptions and public_headers (10 of 10).
  • Differential harness against the module's own code, loaded and relocated, on the same inputs with facets from each side's _Makeloc: random flags, widths, fills, precisions (including values with large or negative low 32 bits), grouping strings, truename and falsename lengths, separators, buffer states, and a locale without facets. It compares the written bytes, the count, the returned iterator, width, flags, precision, and the order of the ctype and numpunct virtual calls.
    • With the module's sprintf_s redirected to this library's, so only num_put is compared: 400,000 calls over 2 seeds, 0 differences.
    • With the module's own sprintf_s: 4,076 of 200,000 calls differ, 794 double and 3,282 long double, and the integer, bool and pointer slots do not differ. The samples I read differ in digits, as the %e, %f and %g debt entry describes.
  • Negative control: 15 single-line changes to the implementation. 14 fail both the test and the harness. The other, keeping showpoint for a non-finite value, fails neither: # does not change inf or nan under this library's formatter, so the module's flag clear is kept but not observable here.
  • Not reached by the test or the harness: the length_error paths and the bad_alloc path.
  • check_conventions, git diff --check: clean. Merges cleanly with main 0e297f14250 and with each of my 20 open PRs. pr_overlap finds no open PR with the same functions. It could not compare 38 PRs that conflict with main, on the shared CMakeLists.txt, docs/dev/debt/libc.md, Api.hpp or LocaleSupport.cpp. No open PR found by searching for __sce_v2 num_put.

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: #16214
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5196448362 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16516). Originally posted by [Danigamer05](https://github.com/Danigamer05) on 2026-11-06T23:53:04Z. --- ### What Builds on #16214: this branch contains its commits, and through it #16178 and #15923. Until they merge, only the last commit is this PR's change. Implement the eight `do_put` virtuals of the versioned `std::__sce_v2::num_put<char>` facet that `_Makeloc` builds. Before this PR, each of them threw `NotImplemented_nid_no_patch`. These slots are not exports: guest code reaches them through the facet's vtable, so this PR adds no NID. The behavior comes from the `libc.prx` bundled with PPSA03671 v01.001.005 (J03848509, SHA-256 `483961e086b8a1fffa8138b3c0badf798987fb5f35ec908d828946408be2d8a6`), vtable `0x18C700`: | Offset | Virtual | Format | | --- | --- | --- | | `0x20` | `do_put(bool)` | without `boolalpha`, calls the `long` slot with the byte zero-extended; with it, `do_truename` or `do_falsename` | | `0x28`, `0x30` | `do_put(long)`, `do_put(unsigned long)` | `%[+][#]l` + `d`/`u`, `o`, `x`/`X` | | `0x38`, `0x40` | `do_put(long long)`, `do_put(unsigned long long)` | the same with `ll` | | `0x48`, `0x50` | `do_put(double)`, `do_put(long double)` | `%[+][#].*[L]` + `g`/`G`, `e`/`E`, `f`, `a`/`A` | | `0x58` | `do_put(const void*)` | `%p` | Where the versioned facet differs from the existing unversioned `num_put<char>`, so it has its own code: - The `ios_base` layout and flag bits differ: `flags` at `0x18`, 64-bit `precision` at `0x20` and `width` at `0x28`; uppercase `0x4`, showbase `0x8`, showpoint `0x10`, showpos `0x20`, left `0x40`, internal `0x100`, oct `0x400`, hex `0x800`, scientific `0x1000`, fixed `0x2000`, boolalpha `0x4000`. `SceV2IosBase` now names these fields. - A direct write checks the `int` count that the buffer holds a pointer to at `0x58`, and decrements it. Otherwise the character goes through `overflow`. A null buffer or a rejected `overflow` sets `failed`, keeps the buffer and keeps writing. - Hex float keeps `.*` with precision -1. For the other fields the precision passes through unchanged, and only its low 32 bits size the buffer. - A non-finite value clears showpoint and skips grouping. The hex prefix counts only under hex float, and comes after the sign. - The versioned string is `0x20` bytes, not the `0x28` of `GuestLocale::String`, so the numpunct strings have their own reader. - Digits go through `sprintf_s`, as in the module, not `snprintf`. - Facets come from `UseSceV2Facet`, which falls back to the global locale and then a cached default as the module does. The other versioned virtuals still throw, including the wide and `unsigned short` `num_put`, and the classic `_Locimp` stays without facets. The `_Makeloc` debt entry now names `num_put<char>`, and a new entry lists the gaps: - floating-point digits come from this library's `sprintf_s` - a buffer too small for the result throws `length_error` - a string too large to allocate throws the host's `bad_alloc` - the lookups take no reference on the stream's locale ### 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. --> Windows 11, MinGW GCC 15.2, Release, head on top of #16214. Not run on Linux or macOS, not run in a title. - New test `guest_sce_v2_num_put_char`: every slot, flags, adjustment, grouping, non-finite values, the put count, `overflow` and failure paths. It passes, as do `guest_sce_v2_char_facet_virtuals`, `guest_sce_v2_makeloc`, `guest_sce_v2_locale`, `guest_sce_v2_locale_basics`, `guest_sce_v2_locimp_references`, `guest_sce_v2_facet_ids`, `guest_locale`, `guest_sce_v2_exceptions` and `public_headers` (10 of 10). - Differential harness against the module's own code, loaded and relocated, on the same inputs with facets from each side's `_Makeloc`: random flags, widths, fills, precisions (including values with large or negative low 32 bits), grouping strings, truename and falsename lengths, separators, buffer states, and a locale without facets. It compares the written bytes, the count, the returned iterator, `width`, `flags`, `precision`, and the order of the `ctype` and `numpunct` virtual calls. - With the module's `sprintf_s` redirected to this library's, so only `num_put` is compared: 400,000 calls over 2 seeds, 0 differences. - With the module's own `sprintf_s`: 4,076 of 200,000 calls differ, 794 `double` and 3,282 `long double`, and the integer, `bool` and pointer slots do not differ. The samples I read differ in digits, as the `%e, %f and %g` debt entry describes. - Negative control: 15 single-line changes to the implementation. 14 fail both the test and the harness. The other, keeping showpoint for a non-finite value, fails neither: `#` does not change `inf` or `nan` under this library's formatter, so the module's flag clear is kept but not observable here. - Not reached by the test or the harness: the `length_error` paths and the `bad_alloc` path. - `check_conventions`, `git diff --check`: clean. Merges cleanly with `main` `0e297f14250` and with each of my 20 open PRs. `pr_overlap` finds no open PR with the same functions. It could not compare 38 PRs that conflict with `main`, on the shared `CMakeLists.txt`, `docs/dev/debt/libc.md`, `Api.hpp` or `LocaleSupport.cpp`. No open PR found by searching for `__sce_v2 num_put`. ### 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: #16214 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
gabichuuuuu commented 2026-11-07 23:15:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by gabichuuuuu on 2026-11-07T23:15:49Z.

Review: COMMENTED


Read 13fc6e65e0 against main. Only the commits on top of #16214's head 2ffd07f90b are this PR's.

Reference. The guest library is Dinkumware (_Makeloc, _Locimp, _Sto*x), not libc++, so I compared against its public descendant in microsoft/STL (stl/inc/xlocnum at e1ed302). libc++ would give the wrong slot order: it declares long long before unsigned long (libcxx/include/locale:1227-1234 at llvmorg-19.1.0).

Slots and layout. The eight do_put are in Dinkumware's order (bool, long, unsigned long, long long, unsigned long long, double, long double, const void*; xlocnum:1278-1448). The SceV2IosBase offsets (flags 0x18, precision 0x20, width 0x28), the flag bits, ostreambuf_iterator as {bool, streambuf*}, and _IPnext/_IPcount at 0x40/0x58 with overflow in vtable slot 4 all match it.

Formatting. PutSceV2Field puts fill where _Iput does for left, internal and default adjustment (xlocnum:1561-1581). For integers the 0x prefix counts only under basefield hex (xlocnum:1619-1623), for floating point only under hexfloat, after the sign (xlocnum:1519-1528). Non-finite values clear showpoint (xlocnum:1396-1397) and skip grouping, and bool without boolalpha goes through the vtable's long slot, so an override is honoured.

Nothing to change that I could find.

Not run here; this is from reading the code.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5815513684 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16516#pullrequestreview-5815513684). Originally posted by [gabichuuuuu](https://github.com/gabichuuuuu) on 2026-11-07T23:15:49Z. **Review:** COMMENTED --- Read `13fc6e65e0` against `main`. Only the commits on top of #16214's head `2ffd07f90b` are this PR's. **Reference.** The guest library is Dinkumware (`_Makeloc`, `_Locimp`, `_Sto*x`), not libc++, so I compared against its public descendant in microsoft/STL (`stl/inc/xlocnum` at `e1ed302`). libc++ would give the wrong slot order: it declares `long long` before `unsigned long` (`libcxx/include/locale:1227-1234` at `llvmorg-19.1.0`). **Slots and layout.** The eight `do_put` are in Dinkumware's order (bool, long, unsigned long, long long, unsigned long long, double, long double, const void*; xlocnum:1278-1448). The `SceV2IosBase` offsets (flags 0x18, precision 0x20, width 0x28), the flag bits, `ostreambuf_iterator` as {bool, streambuf*}, and `_IPnext`/`_IPcount` at 0x40/0x58 with `overflow` in vtable slot 4 all match it. **Formatting.** `PutSceV2Field` puts fill where `_Iput` does for left, internal and default adjustment (xlocnum:1561-1581). For integers the `0x` prefix counts only under basefield hex (xlocnum:1619-1623), for floating point only under hexfloat, after the sign (xlocnum:1519-1528). Non-finite values clear showpoint (xlocnum:1396-1397) and skip grouping, and `bool` without boolalpha goes through the vtable's `long` slot, so an override is honoured. Nothing to change that I could find. Not run here; this is from reading the code. AI-assisted: yes.
MarcZX10 commented 2026-11-20 10:56:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-20T10:56:20Z.

Review: APPROVED


Reviewed — looks good

The implementation and all changed files listed for review were inspected at head a08bbb5e78e732d7596ec361990a6a38a16e5ed4; no verified blocking defect was found, and the author reports successful Windows 11 tests and differential checks.

What I ran

No commands or tests were run in this review. The prepared diff was reviewed at the exact head above; the base was not independently checked.

Results

Check Result Note
Changed code review passed All six changed files read
Tests in this review not run Results in the description are author-reported
Console and other OS behavior not run No title, Linux, or macOS run confirmed

Findings

Blocking

None.

Non-blocking

None.

Not verified

No build, tests, negative control, merged-preview check, or physical-console behavior was run or independently confirmed. The author reports 10 passing tests, differential results, and clean merge checks in the PR description; these remain author-reported. No files were excluded by the manifest.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5991536089 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16516#pullrequestreview-5991536089). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-20T10:56:20Z. **Review:** APPROVED --- # Reviewed — looks good The implementation and all changed files listed for review were inspected at head `a08bbb5e78e732d7596ec361990a6a38a16e5ed4`; no verified blocking defect was found, and the author reports successful Windows 11 tests and differential checks. ## What I ran No commands or tests were run in this review. The prepared diff was reviewed at the exact head above; the base was not independently checked. ## Results | Check | Result | Note | | --- | --- | --- | | Changed code review | passed | All six changed files read | | Tests in this review | not run | Results in the description are author-reported | | Console and other OS behavior | not run | No title, Linux, or macOS run confirmed | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified No build, tests, negative control, merged-preview check, or physical-console behavior was run or independently confirmed. The author reports 10 passing tests, differential results, and clean merge checks in the PR description; these remain author-reported. No files were excluded by the manifest. ## To get this to a good state Nothing is required.
ligmaSec commented 2026-12-03 17:43:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T17:43:55Z.

Review: COMMENTED


guest_sce_v2_num_put_char passed with a08bbb5e78 and its prerequisite stack integrated into a Linux release build. The test calls the guest facet through its vtable and covers the new char do_put implementations through the repository's native target.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6175232807 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16516#pullrequestreview-6175232807). Originally posted by [ligmaSec](https://github.com/ligmaSec) on 2026-12-03T17:43:55Z. **Review:** COMMENTED --- `guest_sce_v2_num_put_char` passed with `a08bbb5e78` and its prerequisite stack integrated into a Linux release build. The test calls the guest facet through its vtable and covers the new char `do_put` implementations through the repository's native target. AI-assisted: yes.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/16516:upstream-pr/16516'
git switch 'upstream-pr/16516'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/16516'
git switch 'upstream-pr/16516'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/16516'
git switch 'upstream-pr/16516'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/16516'
git switch 'main'
git merge --squash 'upstream-pr/16516'
git switch 'main'
git merge --ff-only 'upstream-pr/16516'
git switch 'main'
git merge 'upstream-pr/16516'
git push origin 'main'
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!16516
No description provided.