feat(libSceLibcInternal): implement the Dinkumware significand scanners and wide _WSto*x conversions #18298

Merged
justdoston merged 5 commits from upstream-pr/18298 into main 2026-11-14 04:29:59 +00:00
justdoston commented 2026-11-11 04:37:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by justdoston on 2026-11-11T04:37:53Z.


What

Implement the last seven string-to-float internals in libSceLibcInternal that were still parameterless throwing placeholders in InventoryExports2.cpp:

  • _Stoflt, _Stoxflt: scan a decimal or hexadecimal significand into words plus an exponent. words[0] gets the power-of-ten (decimal) or power-of-two (hexadecimal) exponent, and words[1..n] the significand in 9 decimal or 7 hexadecimal digits per word, at most 5 words. The return value is the word count, 0 for a failed parse, and *end is rolled back to start then. Words are most significant first. The first dropped digit rounds half up with carry, trailing zeros move into the exponent, a zero significand gets exponent 0, the e/p exponent saturates at 999999999, and an incomplete exponent is left unread.
  • _WStoflt, _WStoxflt: the same over the guest char16_t text. The decimal point is the guest localeconv()'s, widened with btowc.
  • _WStodx, _WStofx, _WStoldx: the wide forms of libc's _Stodx, _Stofx and _Stoldx. They parse the text's ASCII prefix through those libc functions, exactly as libc's _WStod, _WStof and _WStold already do, but keep the caller's error pointer so a range error is reported.

The scanner in the new FloatDigits.cpp was written from the C standard's subject sequence for strtod (C11 7.22.1.3), not from any implementation. The prototypes are the declarations in Dinkumware V6.00's xmath.h, with long as std::int64_t as in the PS5 ABI. Recorded in docs/dev/debt/libSceLibcInternal.md:

  • A maxsig below 1 throws.
  • An empty locale decimal point matches nothing.
  • Null pointers throw.

No module was read, no title's trace was used, and nothing was measured on a PS5.

A manual sweep of the diffs of all open pull requests that touch libSceLibcInternal or libc found none that adds, removes or renames any of these seven names. The pr_overlap summary will follow in a comment.

Tested

Linux (Ubuntu, GCC 13). The full project can't be built on this machine because system dev packages are missing, so I built an ASan+UBSan harness instead: FloatDigits.cpp and the new test, with localeconv, btowc and NotImplementedAt stubbed. For the rewritten scanner, libc's _Stodx/_Stofx/_Stoldx were stubbed with host strtod/strtof/strtold. The wide _WSto*x wrappers are unchanged from 98cc3c9ab9, where the harness linked libc's real src/DinkumwareInternals.cpp.

  • guest_libc_internal_float_digits (new) passes with no sanitizer reports. It checks:
    • _Stoflt: 19 inputs. They cover exponents, a fraction with leading zeros, a 20-digit significand split over three words, rounding up and down at a one-word limit with carry through a run of nines, zeros with and without a fraction or exponent, an incomplete exponent rolled back, exponent saturation, a 50-digit significand cut to 5 words, trailing zeros moved into the exponent, and failed parses that roll *end back to start.
    • _Stoxflt: 10 inputs, with the same coverage plus the hexadecimal-to-binary exponent scale.
    • Wide forms: every case runs again through _WStoflt/_WStoxflt and must give the same count, end and words.
    • _WStodx/_WStofx/_WStoldx: power-of-ten scaling, stopping at a non-ASCII character, a non-number left unread, and *error set on overflow (and underflow for double).
  • Each of these mutations makes the test fail: no rounding, no carry, reversed word order, no exponent saturation, ignoring leading fraction zeros, *end not rolled back, accepting an exponent with no digits, a wrong hexadecimal scale, and applying an exponent to zero.
  • FloatDigits.cpp compiles cleanly with g++ -std=c++20 -Wall -Wextra -Werror.
  • python3 tools/check_conventions.py --base origin/main and python3 core/libs/tests/test_public_headers.py --audit-only pass.
  • tools/nid_names.py's compute_nid gives the inventory NID for all seven names.

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:5253903257 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18298). Originally posted by [justdoston](https://github.com/justdoston) on 2026-11-11T04:37:53Z. --- ### What Implement the last seven string-to-float internals in `libSceLibcInternal` that were still parameterless throwing placeholders in `InventoryExports2.cpp`: - `_Stoflt`, `_Stoxflt`: scan a decimal or hexadecimal significand into words plus an exponent. `words[0]` gets the power-of-ten (decimal) or power-of-two (hexadecimal) exponent, and `words[1..n]` the significand in 9 decimal or 7 hexadecimal digits per word, at most 5 words. The return value is the word count, 0 for a failed parse, and `*end` is rolled back to `start` then. Words are most significant first. The first dropped digit rounds half up with carry, trailing zeros move into the exponent, a zero significand gets exponent 0, the `e`/`p` exponent saturates at 999999999, and an incomplete exponent is left unread. - `_WStoflt`, `_WStoxflt`: the same over the guest `char16_t` text. The decimal point is the guest `localeconv()`'s, widened with `btowc`. - `_WStodx`, `_WStofx`, `_WStoldx`: the wide forms of libc's `_Stodx`, `_Stofx` and `_Stoldx`. They parse the text's ASCII prefix through those libc functions, exactly as libc's `_WStod`, `_WStof` and `_WStold` already do, but keep the caller's `error` pointer so a range error is reported. The scanner in the new `FloatDigits.cpp` was written from the C standard's subject sequence for `strtod` (C11 7.22.1.3), not from any implementation. The prototypes are the declarations in Dinkumware V6.00's [`xmath.h`](https://github.com/ganboing/crt.vc12/blob/f1d47126bf13d1c9dccaa6c53b72217fd0474968/src/xmath.h), with `long` as `std::int64_t` as in the PS5 ABI. Recorded in `docs/dev/debt/libSceLibcInternal.md`: - A `maxsig` below 1 throws. - An empty locale decimal point matches nothing. - Null pointers throw. No module was read, no title's trace was used, and nothing was measured on a PS5. A manual sweep of the diffs of all open pull requests that touch `libSceLibcInternal` or `libc` found none that adds, removes or renames any of these seven names. The `pr_overlap` summary will follow in a comment. ### Tested Linux (Ubuntu, GCC 13). The full project can't be built on this machine because system dev packages are missing, so I built an ASan+UBSan harness instead: `FloatDigits.cpp` and the new test, with `localeconv`, `btowc` and `NotImplementedAt` stubbed. For the rewritten scanner, libc's `_Stodx`/`_Stofx`/`_Stoldx` were stubbed with host `strtod`/`strtof`/`strtold`. The wide `_WSto*x` wrappers are unchanged from `98cc3c9ab9`, where the harness linked libc's real `src/DinkumwareInternals.cpp`. - `guest_libc_internal_float_digits` (new) passes with no sanitizer reports. It checks: - **`_Stoflt`:** 19 inputs. They cover exponents, a fraction with leading zeros, a 20-digit significand split over three words, rounding up and down at a one-word limit with carry through a run of nines, zeros with and without a fraction or exponent, an incomplete exponent rolled back, exponent saturation, a 50-digit significand cut to 5 words, trailing zeros moved into the exponent, and failed parses that roll `*end` back to `start`. - **`_Stoxflt`:** 10 inputs, with the same coverage plus the hexadecimal-to-binary exponent scale. - **Wide forms:** every case runs again through `_WStoflt`/`_WStoxflt` and must give the same count, end and words. - **`_WStodx`/`_WStofx`/`_WStoldx`:** power-of-ten scaling, stopping at a non-ASCII character, a non-number left unread, and `*error` set on overflow (and underflow for `double`). - Each of these mutations makes the test fail: no rounding, no carry, reversed word order, no exponent saturation, ignoring leading fraction zeros, `*end` not rolled back, accepting an exponent with no digits, a wrong hexadecimal scale, and applying an exponent to zero. - `FloatDigits.cpp` compiles cleanly with `g++ -std=c++20 -Wall -Wextra -Werror`. - `python3 tools/check_conventions.py --base origin/main` and `python3 core/libs/tests/test_public_headers.py --audit-only` pass. - `tools/nid_names.py`'s `compute_nid` gives the inventory NID for all seven names. ### 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
PrivacyOnion commented 2026-11-11 16:27:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-11T16:27:45Z.

Review: CHANGES_REQUESTED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 98cc3c9ab9:

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: guest_libc_internal_float_digits_tests built cleanly.
  • CTest (guest_libc_internal_float_digits):
    1/1 Test #3473: guest_libc_internal_float_digits ...   Passed    0.02 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libSceLibcInternal/InventoryExports2.cpp, core/libs/prx/libSceLibcInternal/include/InventoryExports2.hpp, core/libs/prx/libSceLibcInternal/libSceLibcInternal.hpp reverted to main, the new tests do not build/link without the change; restoring it makes them build and pass.
  • Code reading:
    • core/libs/prx/libSceLibcInternal/FloatDigits.cpp: Verified null pointer validation and exception throwing in ScanSignificand and ScaledWide against std::invalid_argument requirements.
    • core/libs/prx/libSceLibcInternal/FloatDigits.cpp: Verified exponent boundary limits and rollback logic on incomplete exponent markers in ScanSignificand.
    • core/libs/prx/libSceLibcInternal/FloatDigits.cpp: Analyzed word-packing order against Dinkumware V6.00 xstoflt.c / xstoxflt.c specification and discovered words are stored in reversed order (most significant first rather than least significant first).
    • core/libs/prx/libSceLibcInternal/FloatDigits.cpp: Analyzed rounding up logic (digits[limit] >= Base / 2) and confirmed lack of carry propagation, leading to digit value 10/16, unstripped trailing zeros, and word overflow beyond DigitsPerWord.
    • core/libs/prx/libSceLibcInternal/FloatDigits.cpp: Traced fractional zero handling (e.g., '0.0', '0.000') and verified words[0] is incorrectly left with negative decrements instead of 0.
    • core/libs/tests/GuestLibcInternalFloatDigits.cpp: Audited test cases and verified that multi-word tests assert reversed word order and unstripped rounding digits matching the buggy implementation.
    • docs/dev/debt/libSceLibcInternal.md and AGENTS.md: Verified documentation of deviations and confirmed absence of prohibited code comments.
  • Concerns:
    • core/libs/prx/libSceLibcInternal/FloatDigits.cpp:98-106: Significand words in ScanSignificand are packed from most significant to least significant (words[1] gets the most significant digits, words[word] gets the least significant). In Dinkumware's xstoflt.c / xstoxflt.c, lo[1] holds the least significant word and lo[n] holds the most significant word (packed right-to-left). Downstream Dinkumware multiprecision consumers (such as _MP_Get in MultiPrecision.cpp) expect little-endian word ordering and will miscalculate floats when word count > 1.
    • core/libs/prx/libSceLibcInternal/FloatDigits.cpp:88-92: Rounding up only increments digits[limit - 1] without propagating carry. When digits[limit - 1] is 9 (decimal) or 15 (hex), it becomes 10 or 16 rather than carrying to preceding digits. This prevents trailing zero stripping (as digits[count - 1] != 0) and causes words[word] to overflow DigitsPerWord (e.g. 999999999.5 with maxWords=1 yields words[1] = 1000000000, violating the 9-digit limit).
    • core/libs/prx/libSceLibcInternal/FloatDigits.cpp:76-78,95: For inputs with only fractional zeros such as '0.0' or '0.000', words[0] is decremented for each zero encountered after the decimal point and is never reset to 0 when all digits are zero, returning corrupted exponents (e.g. words[0] = -1 for '0.0' or words[0] = -3 for '0.000' with words[1] = 0).
    • core/libs/tests/GuestLibcInternalFloatDigits.cpp:68,69,82: Test cases assert incorrect behavior matching the implementation bugs: lines 68 and 82 assert reversed word order ({1, 1, 234567890, 123456789} and {0, 1, 0x2345678, 0x9ABCDEF} instead of least-significant first), and line 69 asserts {1, 123456790} retaining an unstripped trailing zero instead of normalising to {2, 12345679}.
  • Minor: please drop the Generated with footer from the description; the AI-assisted checkbox can stay.

Requesting changes for the points above.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5861980761 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18298#pullrequestreview-5861980761). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-11T16:27:45Z. **Review:** CHANGES_REQUESTED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `98cc3c9ab9`: - **Conventions**: `tools/check_conventions.py` passed; `git diff --check` clean. - **Build**: `guest_libc_internal_float_digits_tests` built cleanly. - **CTest** (`guest_libc_internal_float_digits`): ``` 1/1 Test #3473: guest_libc_internal_float_digits ... Passed 0.02 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/libs/prx/libSceLibcInternal/InventoryExports2.cpp`, `core/libs/prx/libSceLibcInternal/include/InventoryExports2.hpp`, `core/libs/prx/libSceLibcInternal/libSceLibcInternal.hpp` reverted to `main`, the new tests do not build/link without the change; restoring it makes them build and pass. - **Code reading**: - core/libs/prx/libSceLibcInternal/FloatDigits.cpp: Verified null pointer validation and exception throwing in ScanSignificand and ScaledWide against std::invalid_argument requirements. - core/libs/prx/libSceLibcInternal/FloatDigits.cpp: Verified exponent boundary limits and rollback logic on incomplete exponent markers in ScanSignificand. - core/libs/prx/libSceLibcInternal/FloatDigits.cpp: Analyzed word-packing order against Dinkumware V6.00 xstoflt.c / xstoxflt.c specification and discovered words are stored in reversed order (most significant first rather than least significant first). - core/libs/prx/libSceLibcInternal/FloatDigits.cpp: Analyzed rounding up logic (digits[limit] >= Base / 2) and confirmed lack of carry propagation, leading to digit value 10/16, unstripped trailing zeros, and word overflow beyond DigitsPerWord. - core/libs/prx/libSceLibcInternal/FloatDigits.cpp: Traced fractional zero handling (e.g., '0.0', '0.000') and verified words[0] is incorrectly left with negative decrements instead of 0. - core/libs/tests/GuestLibcInternalFloatDigits.cpp: Audited test cases and verified that multi-word tests assert reversed word order and unstripped rounding digits matching the buggy implementation. - docs/dev/debt/libSceLibcInternal.md and AGENTS.md: Verified documentation of deviations and confirmed absence of prohibited code comments. - **Concerns**: - core/libs/prx/libSceLibcInternal/FloatDigits.cpp:98-106: Significand words in ScanSignificand are packed from most significant to least significant (words[1] gets the most significant digits, words[word] gets the least significant). In Dinkumware's xstoflt.c / xstoxflt.c, lo[1] holds the least significant word and lo[n] holds the most significant word (packed right-to-left). Downstream Dinkumware multiprecision consumers (such as _MP_Get in MultiPrecision.cpp) expect little-endian word ordering and will miscalculate floats when word count > 1. - core/libs/prx/libSceLibcInternal/FloatDigits.cpp:88-92: Rounding up only increments digits[limit - 1] without propagating carry. When digits[limit - 1] is 9 (decimal) or 15 (hex), it becomes 10 or 16 rather than carrying to preceding digits. This prevents trailing zero stripping (as digits[count - 1] != 0) and causes words[word] to overflow DigitsPerWord (e.g. 999999999.5 with maxWords=1 yields words[1] = 1000000000, violating the 9-digit limit). - core/libs/prx/libSceLibcInternal/FloatDigits.cpp:76-78,95: For inputs with only fractional zeros such as '0.0' or '0.000', words[0] is decremented for each zero encountered after the decimal point and is never reset to 0 when all digits are zero, returning corrupted exponents (e.g. words[0] = -1 for '0.0' or words[0] = -3 for '0.000' with words[1] = 0). - core/libs/tests/GuestLibcInternalFloatDigits.cpp:68,69,82: Test cases assert incorrect behavior matching the implementation bugs: lines 68 and 82 assert reversed word order ({1, 1, 234567890, 123456789} and {0, 1, 0x2345678, 0x9ABCDEF} instead of least-significant first), and line 69 asserts {1, 123456790} retaining an unstripped trailing zero instead of normalising to {2, 12345679}. - Minor: please drop the `Generated with` footer from the description; the AI-assisted checkbox can stay. Requesting changes for the points above.
PrivacyOnion commented 2026-11-14 00:06:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-14T00:06:23Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against bcd0cb07a7:

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: guest_libc_internal_float_digits_tests built cleanly.
  • CTest (guest_libc_internal_float_digits):
    1/1 Test #3703: guest_libc_internal_float_digits ...   Passed    0.02 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libSceLibcInternal/InventoryExports2.cpp, core/libs/prx/libSceLibcInternal/include/InventoryExports2.hpp, core/libs/prx/libSceLibcInternal/libSceLibcInternal.hpp reverted to main, the new tests do not build/link without the change; restoring it makes them build and pass.
  • Code reading:
    • core/libs/prx/libSceLibcInternal/FloatDigits.cpp: verified null argument validation in Scan (start, text, words) and ScaledWide (text) throwing std::invalid_argument, alongside maxWords < 1 handling calling NotImplemented_nid_no_patch.
    • core/libs/prx/libSceLibcInternal/FloatDigits.cpp: verified memory and bounds safety in Significand::Accept, Normalize, and Store against digits_ (46 elements) with wordLimit capped to WordCapacity (5 words / 45 decimal / 35 hex digits), guard-digit half-up rounding with carry, and zero-significand exponent zeroing.
    • core/libs/prx/libSceLibcInternal/FloatDigits.cpp: verified ReadExponent bounds, sign handling, cursor rollback on missing exponent digits (e.g. '5e+', '1p'), and ExponentCeiling saturation at 999999999 without int64_t overflow.
    • core/libs/prx/libSceLibcInternal/FloatDigits.cpp: verified ScaledWide for _WStodx_nid_postfix, _WStofx_nid_postfix, and _WStoldx_nid_postfix correctly extracts ASCII prefixes, preserves caller error pointers across range errors, and offsets end pointers within the guest char16_t buffer.
    • core/libs/prx/libSceLibcInternal/FloatDigits.cpp and docs/dev/debt/libSceLibcInternal.md: verified strict AGENTS.md compliance, including absence of forbidden code comments, replacement of placeholders in InventoryExports2, and documentation of known gaps.
  • Note: the AI-assisted checkbox can stay.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5899831591 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18298#pullrequestreview-5899831591). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-14T00:06:23Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `bcd0cb07a7`: - **Conventions**: `tools/check_conventions.py` passed; `git diff --check` clean. - **Build**: `guest_libc_internal_float_digits_tests` built cleanly. - **CTest** (`guest_libc_internal_float_digits`): ``` 1/1 Test #3703: guest_libc_internal_float_digits ... Passed 0.02 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/libs/prx/libSceLibcInternal/InventoryExports2.cpp`, `core/libs/prx/libSceLibcInternal/include/InventoryExports2.hpp`, `core/libs/prx/libSceLibcInternal/libSceLibcInternal.hpp` reverted to `main`, the new tests do not build/link without the change; restoring it makes them build and pass. - **Code reading**: - core/libs/prx/libSceLibcInternal/FloatDigits.cpp: verified null argument validation in Scan (start, text, words) and ScaledWide (text) throwing std::invalid_argument, alongside maxWords < 1 handling calling NotImplemented_nid_no_patch. - core/libs/prx/libSceLibcInternal/FloatDigits.cpp: verified memory and bounds safety in Significand::Accept, Normalize, and Store against digits_ (46 elements) with wordLimit capped to WordCapacity (5 words / 45 decimal / 35 hex digits), guard-digit half-up rounding with carry, and zero-significand exponent zeroing. - core/libs/prx/libSceLibcInternal/FloatDigits.cpp: verified ReadExponent bounds, sign handling, cursor rollback on missing exponent digits (e.g. '5e+', '1p'), and ExponentCeiling saturation at 999999999 without int64_t overflow. - core/libs/prx/libSceLibcInternal/FloatDigits.cpp: verified ScaledWide for _WStodx_nid_postfix, _WStofx_nid_postfix, and _WStoldx_nid_postfix correctly extracts ASCII prefixes, preserves caller error pointers across range errors, and offsets end pointers within the guest char16_t buffer. - core/libs/prx/libSceLibcInternal/FloatDigits.cpp and docs/dev/debt/libSceLibcInternal.md: verified strict AGENTS.md compliance, including absence of forbidden code comments, replacement of placeholders in InventoryExports2, and documentation of known gaps. - Note: the AI-assisted checkbox can stay. Looks Good To Me
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!18298
No description provided.