feat(libc): convert wide strings with _WStoull, _WStoll, _WStod, _WStof and _WStold as the console's libc does #11733

Merged
verusferro merged 1 commit from upstream-pr/11733 into main 2026-11-01 11:33:31 +00:00
verusferro commented 2026-10-27 06:21:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by verusferro on 2026-10-27T06:21:57Z.


What

libc did not export the wide Dinkumware conversions _WStoull, _WStoll, _WStod, _WStof and _WStold, which the console's libc.prx exports. They now follow the SDK 7.00 libc.prx that PPSA06416 ships in sce_module/ (0x3c8b0, 0x3c730, 0x7d3d0, 0x7d6f0 and 0x725c0), read from its disassembly (REVERSE_ENGINEERING, source 2) and run:

  • _WStoull is Dinkumware's xwstoul.c. It skips characters for which _Iswctype(c, 9) (space) is true and takes one sign. A base below 0, equal to 1 or above 36 converts nothing (*endptr is the string, result 0). Base 0 and 16 strip a 0x or 0X. Leading zeros are skipped, and each digit is looked up with _Towctrans(c, 1) and wmemchr in 0123456789abcdefghijklmnopqrstuvwxyz. Text with no digits after the prefix, a bare 0x included, converts nothing. Overflow is found from the per-base digit counts the module keeps at 0xfc790 (65 for base 2, 20 for base 10) and returns ULLONG_MAX with ERANGE whatever the sign. Otherwise a - negates the result.
  • _WStoll skips the same spaces, takes one sign and calls _WStoull on the rest, so --1 reaches _WStoull as -1 and overflows. It returns LLONG_MAX or LLONG_MIN with ERANGE when the value does not fit, and the original string as *endptr when nothing converts.
  • _WStod, _WStof and _WStold call the module's internal _WStodx-style parsers with a null error pointer, as the narrow _Stod calls _Stodx. Here they run this library's _Stodx, _Stofx and _Stoldx on the leading ASCII characters, through the helper wcstod uses. The new debt entry records where that differs.

The console's _WStoul (0x7a8f0) is the same code as _WStoull. This library's _WStoul still runs wcstoul and throws for a bad base, as its existing debt entry says; that entry is on a line that #9087, #9863 and #10453 also change, so this PR leaves it.

Tested

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

  • The title's libc.prx, converted by the relinker and loaded in a host process, against this library's unpatched libc.prx, comparing the result, the characters consumed and errno. The inputs are 30,000 random wide strings built as in JJenkx's differential: a leading space, tab or U+3000, then +, -, -- or no sign, 0x, 0X or no prefix, then up to 27 characters from 0-9a-fA-F zZ . e + - p P x n and space.
    • 514,530 integer calls to _WStoull and _WStoll, with bases 0, 2, 8, 10, 16, 36, 1, 37 and -1. All match. On the earlier base fe2b52c7e4, 175,560 hand-picked calls also matched: every base from -1 to 37 and 100; no-break, ideographic and NEL spaces; 0b prefixes; Latin-1, fullwidth and Arabic-Indic digits; and values at, below and above the overflow limit in every base.
    • 105,642 floating calls to _WStod, _WStof and _WStold, with a scale of 0 and, for text without an x, one more scale from -400 to 400. 8,818 differ, none of them from _WStold. Every one of the 8,818 is a text whose narrow form gives the same two results from the module's _Stod or _Stof and from this library's: the difference is in _Stodx and _Stofx, which the wide functions call. There are three kinds: 8,732 zeros whose sign differs after a - with nothing convertible, 42 hexadecimal overflows where this library sets ERANGE, and 44 long hexadecimal mantissas that end one unit apart. -nan and the 1e-30 subnormal with a scale of -10, checked separately, differ in the narrow functions the same way. The debt entry lists all of them.
  • guest_wide_conversion gains cases for each function, taken from the module's results: overflow on either side of the limit, a sign before a second sign or a space, base detection, an empty string, a bare 0x, leading zeros past the digit count, and scaling. Making _WStoll accept 2^63 with a + fails it.
  • ctest -R '^(guest_wide_conversion|guest_dinkumware_internals|patched_libraries|conventions_unknown_nids|public_headers)$': 5/5 pass.
  • tools/progress.py: +5 implemented. libc goes from 1419/1436 to 1424/1441.
  • python3 tools/check_conventions.py --base origin/main: passed. python3 tools/pr_overlap.py --branch feat/libc-wide-string-to-number: no conflicting lines and no same exports. The debt entry is placed after the jrand48 entry, between lines no open PR changes; next to the _Stod entry, and where it first was, open PRs (#11270, #12291, #12397) change the neighbouring lines.

Not tested: Windows, macOS, a title that calls these functions, a console run.

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: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5029570650 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11733). Originally posted by [verusferro](https://github.com/verusferro) on 2026-10-27T06:21:57Z. --- ### What libc did not export the wide Dinkumware conversions `_WStoull`, `_WStoll`, `_WStod`, `_WStof` and `_WStold`, which the console's `libc.prx` exports. They now follow the SDK 7.00 `libc.prx` that PPSA06416 ships in `sce_module/` (0x3c8b0, 0x3c730, 0x7d3d0, 0x7d6f0 and 0x725c0), read from its disassembly ([REVERSE_ENGINEERING](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/REVERSE_ENGINEERING.md), source 2) and run: - `_WStoull` is Dinkumware's `xwstoul.c`. It skips characters for which `_Iswctype(c, 9)` (space) is true and takes one sign. A base below 0, equal to 1 or above 36 converts nothing (`*endptr` is the string, result 0). Base 0 and 16 strip a `0x` or `0X`. Leading zeros are skipped, and each digit is looked up with `_Towctrans(c, 1)` and `wmemchr` in `0123456789abcdefghijklmnopqrstuvwxyz`. Text with no digits after the prefix, a bare `0x` included, converts nothing. Overflow is found from the per-base digit counts the module keeps at 0xfc790 (65 for base 2, 20 for base 10) and returns `ULLONG_MAX` with `ERANGE` whatever the sign. Otherwise a `-` negates the result. - `_WStoll` skips the same spaces, takes one sign and calls `_WStoull` on the rest, so `--1` reaches `_WStoull` as `-1` and overflows. It returns `LLONG_MAX` or `LLONG_MIN` with `ERANGE` when the value does not fit, and the original string as `*endptr` when nothing converts. - `_WStod`, `_WStof` and `_WStold` call the module's internal `_WStodx`-style parsers with a null error pointer, as the narrow `_Stod` calls `_Stodx`. Here they run this library's `_Stodx`, `_Stofx` and `_Stoldx` on the leading ASCII characters, through the helper `wcstod` uses. The new debt entry records where that differs. The console's `_WStoul` (0x7a8f0) is the same code as `_WStoull`. This library's `_WStoul` still runs `wcstoul` and throws for a bad base, as its existing debt entry says; that entry is on a line that #9087, #9863 and #10453 also change, so this PR leaves it. ### 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`, on `main` `2227f13661`: - The title's `libc.prx`, converted by the relinker and loaded in a host process, against this library's unpatched `libc.prx`, comparing the result, the characters consumed and `errno`. The inputs are 30,000 random wide strings built as in JJenkx's [differential](https://github.com/boykopovar/AnyPS5/pull/11733#issuecomment-6443862410): a leading space, tab or U+3000, then `+`, `-`, `--` or no sign, `0x`, `0X` or no prefix, then up to 27 characters from `0-9a-fA-F zZ . e + - p P x n` and space. - 514,530 integer calls to `_WStoull` and `_WStoll`, with bases 0, 2, 8, 10, 16, 36, 1, 37 and -1. All match. On the earlier base `fe2b52c7e4`, 175,560 hand-picked calls also matched: every base from -1 to 37 and 100; no-break, ideographic and NEL spaces; `0b` prefixes; Latin-1, fullwidth and Arabic-Indic digits; and values at, below and above the overflow limit in every base. - 105,642 floating calls to `_WStod`, `_WStof` and `_WStold`, with a scale of 0 and, for text without an `x`, one more scale from -400 to 400. 8,818 differ, none of them from `_WStold`. Every one of the 8,818 is a text whose narrow form gives the same two results from the module's `_Stod` or `_Stof` and from this library's: the difference is in `_Stodx` and `_Stofx`, which the wide functions call. There are three kinds: 8,732 zeros whose sign differs after a `-` with nothing convertible, 42 hexadecimal overflows where this library sets `ERANGE`, and 44 long hexadecimal mantissas that end one unit apart. `-nan` and the 1e-30 subnormal with a scale of -10, checked separately, differ in the narrow functions the same way. The debt entry lists all of them. - `guest_wide_conversion` gains cases for each function, taken from the module's results: overflow on either side of the limit, a sign before a second sign or a space, base detection, an empty string, a bare `0x`, leading zeros past the digit count, and scaling. Making `_WStoll` accept 2^63 with a `+` fails it. - `ctest -R '^(guest_wide_conversion|guest_dinkumware_internals|patched_libraries|conventions_unknown_nids|public_headers)$'`: 5/5 pass. - `tools/progress.py`: +5 implemented. libc goes from 1419/1436 to 1424/1441. - `python3 tools/check_conventions.py --base origin/main`: passed. `python3 tools/pr_overlap.py --branch feat/libc-wide-string-to-number`: no conflicting lines and no same exports. The debt entry is placed after the `jrand48` entry, between lines no open PR changes; next to the `_Stod` entry, and where it first was, open PRs (#11270, #12291, #12397) change the neighbouring lines. Not tested: Windows, macOS, a title that calls these functions, a console run. ### 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: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
MarcZX10 commented 2026-10-27 06:33:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-27T06:33:29Z.

Review: APPROVED


Reviewed exact head 14cd697dd201cfb6a2c0dd85cd1a08d1b77ba8c2. The integer conversions consistently handle whitespace/sign/prefix before digit counting, leave endptr at the original string when no conversion occurs, and distinguish unsigned overflow from the signed range checks. The floating wrappers reuse the existing ASCII-prefix path and narrow _Stodx family, with the observed differences explicitly recorded in technical debt. The focused cases cover the main boundary behaviors and are not export-only tests.

The author reports direct result/end-pointer/errno comparisons against the shipped module over 175,560 integer calls and 1,038 floating calls; the documented two _WStof discrepancies are retained as debt. Hosted conventions, intake, Linux/Windows relinker, ASan and Ubuntu register-context checks pass; product Linux/Windows jobs are skipped, and several macOS jobs remain queued. I did not independently run the full build or module comparison in this workspace.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5663112288 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11733#pullrequestreview-5663112288). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-27T06:33:29Z. **Review:** APPROVED --- Reviewed exact head `14cd697dd201cfb6a2c0dd85cd1a08d1b77ba8c2`. The integer conversions consistently handle whitespace/sign/prefix before digit counting, leave `endptr` at the original string when no conversion occurs, and distinguish unsigned overflow from the signed range checks. The floating wrappers reuse the existing ASCII-prefix path and narrow `_Stodx` family, with the observed differences explicitly recorded in technical debt. The focused cases cover the main boundary behaviors and are not export-only tests. The author reports direct result/end-pointer/`errno` comparisons against the shipped module over 175,560 integer calls and 1,038 floating calls; the documented two `_WStof` discrepancies are retained as debt. Hosted conventions, intake, Linux/Windows relinker, ASan and Ubuntu register-context checks pass; product Linux/Windows jobs are skipped, and several macOS jobs remain queued. I did not independently run the full build or module comparison in this workspace.
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!11733
No description provided.