feat(libc): implement the std::__sce_v2 ctype, numpunct and num_put virtuals for wchar_t #16560

Open
Danigamer05 wants to merge 13 commits from upstream-pr/16560 into main
Danigamer05 commented 2026-11-07 05:25:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Danigamer05 on 2026-11-07T05:25:52Z.


What

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

Implement the wchar_t virtuals of std::__sce_v2::num_put, and of the two facets it reads, ctype and numpunct, all of which _Makewloc builds. Before this PR, each of them threw NotImplemented_nid_no_patch. These slots are not exports: guest code reaches them through the facets' vtables, so this PR adds no NID.

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

Facet Vtable Virtuals
ctype<wchar_t> 0x18CD30 do_is (two), do_scan_is, do_scan_not, do_tolower (two), do_toupper (two), do_widen (two), do_narrow (two)
numpunct<wchar_t> 0x18CE78 do_decimal_point, do_thousands_sep, do_grouping, do_falsename, do_truename
num_put<wchar_t> 0x18CEE8 the same eight do_put as num_put<char>

num_put<wchar_t> is the num_put<char> of #16516 with 16-bit characters, so that code is now a template on the character type and both facets share it. The differences:

  • The fill, the buffered characters and the overflow argument are 16 bits, and the put pointer moves by two bytes. The int count still counts characters.
  • numpunct<wchar_t> returns its names in a 16-bit string with 7 inline characters, and grouping stays a narrow string.
  • Digits are formatted narrow and go through do_widen for a range, so a replaced ctype facet is honored.

In ctype<wchar_t>:

  • do_is looks up the table with do_narrow(c, 0) through the vtable, and do_scan_is and do_scan_not call do_is through the vtable.
  • do_widen calls mbrtowc with a clean state for one byte and gives 0xFFFF if that fails. do_narrow calls wcrtomb and gives the fallback unless the result is one byte.

The unsigned short facets and the other versioned virtuals still throw. The _Makeloc debt entry names what works now, and the num_put entry covers both facets and one new gap: a long double without its explicit integer bit prints nan where the module prints digits.

Tested

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

  • New test guest_sce_v2_num_put_wide: every slot of the three facets, flags, adjustment, grouping, non-ASCII fill and separators, non-finite values, names inline, on the heap and in a large allocation, the put count, overflow and failure paths, invalid bytes, fallbacks, and virtual dispatch. It passes, as do guest_sce_v2_num_put_char, 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 (11 of 11).
  • Differential harness against the module's own code, loaded and relocated, on the same inputs with facets from each side's _Makewloc: random flags, widths, fills, precisions, grouping strings, names of up to 3,000 characters, separators, buffer states, a locale without facets, every char16_t class including surrogates and invalid bytes. It compares the written characters, the count, the returned iterator or pointer, width, flags, precision, errno for ctype, string size and capacity, allocation sizes, and the order of the ctype and numpunct virtual calls.
    • With the module's sprintf_s redirected to this library's, so only these facets are compared: 400,000 calls over 2 seeds (about 300,000 num_put, 50,000 ctype, 50,000 numpunct), 0 differences.
    • With the module's own sprintf_s: 3,031 of 200,000 calls differ, 586 double and 2,445 long double; integer, bool, pointer, ctype and numpunct calls do not differ. Of these, 1,846 are long double values without the explicit integer bit, printed as nan here, which is the new debt line. By the classification I ran, the other 1,185 keep the same nan state and I read samples that differ in digits, as the %e, %f and %g debt entry describes.
  • char regression after the refactor, on the harnesses of #16516 and #16214: 200,000 num_put<char> calls and 100,256 ctype, numpunct and codecvt calls, 0 differences.
  • Negative control: 35 single-line changes to the implementation, and all fail the final test. 34 of them also ran through the harness, and all failed it. The first run showed that the test missed 6 of them, so the test was strengthened and those were rerun, with a seventh for do_scan_not, the one that ran only against the test. The showpoint clear on a non-finite value stays unobservable here, as #16516 explains, and is not in this list.
  • 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 4ba64592376. pr_overlap reports no open PR with the same functions; it could not compare 43 PRs that conflict with main on shared files. Of those, the 5 that touch LocaleSupport.cpp or GuestLocale.hpp (#7081, #9526, #11172, #12518, #12704) do not add these virtuals. No open PR found by searching for the versioned wchar_t facets.

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: #16516
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5198751284 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16560). Originally posted by [Danigamer05](https://github.com/Danigamer05) on 2026-11-07T05:25:52Z. --- ### What Builds on #16516: this branch contains its commits, and through it #16214, #16178 and #15923. Until they merge, only the last commit is this PR's change. Implement the `wchar_t` virtuals of `std::__sce_v2::num_put`, and of the two facets it reads, `ctype` and `numpunct`, all of which `_Makewloc` builds. Before this PR, each of them threw `NotImplemented_nid_no_patch`. These slots are not exports: guest code reaches them through the facets' vtables, so this PR adds no NID. The behavior comes from the `libc.prx` bundled with PPSA03671 v01.001.005 (J03848509, SHA-256 `483961e086b8a1fffa8138b3c0badf798987fb5f35ec908d828946408be2d8a6`): | Facet | Vtable | Virtuals | | --- | --- | --- | | `ctype<wchar_t>` | `0x18CD30` | `do_is` (two), `do_scan_is`, `do_scan_not`, `do_tolower` (two), `do_toupper` (two), `do_widen` (two), `do_narrow` (two) | | `numpunct<wchar_t>` | `0x18CE78` | `do_decimal_point`, `do_thousands_sep`, `do_grouping`, `do_falsename`, `do_truename` | | `num_put<wchar_t>` | `0x18CEE8` | the same eight `do_put` as `num_put<char>` | `num_put<wchar_t>` is the `num_put<char>` of #16516 with 16-bit characters, so that code is now a template on the character type and both facets share it. The differences: - The fill, the buffered characters and the `overflow` argument are 16 bits, and the put pointer moves by two bytes. The `int` count still counts characters. - `numpunct<wchar_t>` returns its names in a 16-bit string with 7 inline characters, and `grouping` stays a narrow string. - Digits are formatted narrow and go through `do_widen` for a range, so a replaced `ctype` facet is honored. In `ctype<wchar_t>`: - `do_is` looks up the table with `do_narrow(c, 0)` through the vtable, and `do_scan_is` and `do_scan_not` call `do_is` through the vtable. - `do_widen` calls `mbrtowc` with a clean state for one byte and gives `0xFFFF` if that fails. `do_narrow` calls `wcrtomb` and gives the fallback unless the result is one byte. The `unsigned short` facets and the other versioned virtuals still throw. The `_Makeloc` debt entry names what works now, and the `num_put` entry covers both facets and one new gap: a `long double` without its explicit integer bit prints `nan` where the module prints digits. ### 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 #16516. Not run on Linux or macOS, not run in a title. - New test `guest_sce_v2_num_put_wide`: every slot of the three facets, flags, adjustment, grouping, non-ASCII fill and separators, non-finite values, names inline, on the heap and in a large allocation, the put count, `overflow` and failure paths, invalid bytes, fallbacks, and virtual dispatch. It passes, as do `guest_sce_v2_num_put_char`, `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` (11 of 11). - Differential harness against the module's own code, loaded and relocated, on the same inputs with facets from each side's `_Makewloc`: random flags, widths, fills, precisions, grouping strings, names of up to 3,000 characters, separators, buffer states, a locale without facets, every `char16_t` class including surrogates and invalid bytes. It compares the written characters, the count, the returned iterator or pointer, `width`, `flags`, `precision`, `errno` for `ctype`, string size and capacity, allocation sizes, and the order of the `ctype` and `numpunct` virtual calls. - With the module's `sprintf_s` redirected to this library's, so only these facets are compared: 400,000 calls over 2 seeds (about 300,000 `num_put`, 50,000 `ctype`, 50,000 `numpunct`), 0 differences. - With the module's own `sprintf_s`: 3,031 of 200,000 calls differ, 586 `double` and 2,445 `long double`; integer, `bool`, pointer, `ctype` and `numpunct` calls do not differ. Of these, 1,846 are `long double` values without the explicit integer bit, printed as `nan` here, which is the new debt line. By the classification I ran, the other 1,185 keep the same `nan` state and I read samples that differ in digits, as the `%e, %f and %g` debt entry describes. - `char` regression after the refactor, on the harnesses of #16516 and #16214: 200,000 `num_put<char>` calls and 100,256 `ctype`, `numpunct` and `codecvt` calls, 0 differences. - Negative control: 35 single-line changes to the implementation, and all fail the final test. 34 of them also ran through the harness, and all failed it. The first run showed that the test missed 6 of them, so the test was strengthened and those were rerun, with a seventh for `do_scan_not`, the one that ran only against the test. The `showpoint` clear on a non-finite value stays unobservable here, as #16516 explains, and is not in this list. - 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` `4ba64592376`. `pr_overlap` reports no open PR with the same functions; it could not compare 43 PRs that conflict with `main` on shared files. Of those, the 5 that touch `LocaleSupport.cpp` or `GuestLocale.hpp` (#7081, #9526, #11172, #12518, #12704) do not add these virtuals. No open PR found by searching for the versioned `wchar_t` facets. ### 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: #16516 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
gabichuuuuu commented 2026-11-07 23:15:51 +00:00 (Migrated from github.com)

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

Review: COMMENTED


Read 8ac2d3a15d against main. Only f9b29a9a5a and the merge on top of #16516's head are this PR's.

Character width. A 16-bit wchar_t agrees with the rest of libc, which declares the wide stdio and mbrtowc/wcrtomb on char16_t/uint16_t in Api.hpp. SceV2BasicString<char16_t> keeps the 0x20 layout with 7 inline characters and capacity length | 7, Dinkumware's allocation mask for 2-byte elements, and an overflow result is compared with -1, the same WEOF the wide stdio here uses.

Slots. ctype<wchar_t> has its 12 virtuals in declaration order, numpunct<wchar_t> keeps grouping narrow, and the char instantiation of the templated num_put behaves as #16516's did. The put pointer moves by sizeof(Text) and the count counts characters.

Question, not blocking. do_is classifies through do_narrow(c, 0) and the 256-entry table, so a character that doesn't narrow to one byte gets the class of '\0', where the public Dinkumware source calls _Getwctype (microsoft/STL stl/inc/xlocale:2940-2942). The description says the module does the table lookup and the harness covered every char16_t class, so I'm only noting that this departs from the public source on purpose.

Not run here; this is from reading the code.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5815513799 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16560#pullrequestreview-5815513799). Originally posted by [gabichuuuuu](https://github.com/gabichuuuuu) on 2026-11-07T23:15:51Z. **Review:** COMMENTED --- Read `8ac2d3a15d` against `main`. Only `f9b29a9a5a` and the merge on top of #16516's head are this PR's. **Character width.** A 16-bit `wchar_t` agrees with the rest of libc, which declares the wide stdio and `mbrtowc`/`wcrtomb` on `char16_t`/`uint16_t` in `Api.hpp`. `SceV2BasicString<char16_t>` keeps the 0x20 layout with 7 inline characters and capacity `length | 7`, Dinkumware's allocation mask for 2-byte elements, and an overflow result is compared with -1, the same WEOF the wide stdio here uses. **Slots.** `ctype<wchar_t>` has its 12 virtuals in declaration order, `numpunct<wchar_t>` keeps `grouping` narrow, and the `char` instantiation of the templated `num_put` behaves as #16516's did. The put pointer moves by `sizeof(Text)` and the count counts characters. **Question, not blocking.** `do_is` classifies through `do_narrow(c, 0)` and the 256-entry table, so a character that doesn't narrow to one byte gets the class of `'\0'`, where the public Dinkumware source calls `_Getwctype` (microsoft/STL `stl/inc/xlocale:2940-2942`). The description says the module does the table lookup and the harness covered every `char16_t` class, so I'm only noting that this departs from the public source on purpose. Not run here; this is from reading the code. AI-assisted: yes.
MarcZX10 commented 2026-11-22 12:26:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-22T12:26:45Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this change based on the source reviewed; the checks reported by the author were not run here.

What I ran

Reviewed head e9662cf7b67b52da526ce24a8340c50d95f9ee86 from the prepared diff and pinned reader. The description reports a clean merge with main at 4ba64592376. No commands or tests were run in this review.

Results

Check Result Note
Source review passed Seven changed files read
Build and tests not run Results in the description are author-reported
Merge preview not run Clean merge reported by the author

Findings

Blocking

None verified.

Non-blocking

None.

Not verified

Build, tests, and the differential harness were not run in this review. The reported Windows 11 and MinGW GCC 15.2 results, console behavior, other host operating systems, and the excluded length_error and bad_alloc paths were not independently verified. All seven changed files marked included in the manifest were read: core/libs/prx/libc/CMakeLists.txt, core/libs/prx/libc/include/GuestLocale.hpp, core/libs/prx/libc/src/LocaleSupport.cpp, core/libs/tests/GuestSceV2CharFacetVirtuals.cpp, core/libs/tests/GuestSceV2NumPutChar.cpp, core/libs/tests/GuestSceV2NumPutWide.cpp, and docs/dev/debt/libc.md. No changed files were excluded by the manifest.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6012210425 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16560#pullrequestreview-6012210425). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-22T12:26:45Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change based on the source reviewed; the checks reported by the author were not run here. ## What I ran Reviewed head `e9662cf7b67b52da526ce24a8340c50d95f9ee86` from the prepared diff and pinned reader. The description reports a clean merge with `main` at `4ba64592376`. No commands or tests were run in this review. ## Results | Check | Result | Note | |---|---|---| | Source review | passed | Seven changed files read | | Build and tests | not run | Results in the description are author-reported | | Merge preview | not run | Clean merge reported by the author | ## Findings ### Blocking None verified. ### Non-blocking None. ## Not verified Build, tests, and the differential harness were not run in this review. The reported Windows 11 and MinGW GCC 15.2 results, console behavior, other host operating systems, and the excluded `length_error` and `bad_alloc` paths were not independently verified. All seven changed files marked included in the manifest were read: `core/libs/prx/libc/CMakeLists.txt`, `core/libs/prx/libc/include/GuestLocale.hpp`, `core/libs/prx/libc/src/LocaleSupport.cpp`, `core/libs/tests/GuestSceV2CharFacetVirtuals.cpp`, `core/libs/tests/GuestSceV2NumPutChar.cpp`, `core/libs/tests/GuestSceV2NumPutWide.cpp`, and `docs/dev/debt/libc.md`. No changed files were excluded by the manifest. ## To get this to a good state Nothing is required.
ligmaSec commented 2026-12-03 13:12:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T13:12:44Z.

Review: COMMENTED


Built the branch’s locale implementation and matching headers against the existing Linux runtime. The focused GuestSceV2NumPutWide fixture passed.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6170472883 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16560#pullrequestreview-6170472883). Originally posted by [ligmaSec](https://github.com/ligmaSec) on 2026-12-03T13:12:44Z. **Review:** COMMENTED --- Built the branch’s locale implementation and matching headers against the existing Linux runtime. The focused `GuestSceV2NumPutWide` fixture passed. 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/16560:upstream-pr/16560'
git switch 'upstream-pr/16560'

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