Skip to content

feat(libc): implement the std::__sce_v2 num_get virtuals for char floating point - #16944

Open
Danigamer05 wants to merge 19 commits into
boykopovar:mainfrom
Danigamer05:feat/libc-sce-v2-num-get-floating-20261108
Open

Danigamer05 wants to merge 19 commits into
boykopovar:mainfrom
Danigamer05:feat/libc-sce-v2-num-get-floating-20261108

Conversation

@Danigamer05

@Danigamer05 Danigamer05 commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

What

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

Implement the three floating-point do_get virtuals of std::__sce_v2::num_get<char>, the last slots of the facet that still threw NotImplemented_nid_no_patch: float, double and long double. These slots are not exports, so this PR adds no NID. The wchar_t facet of #16924 keeps throwing in these slots.

The behavior comes from the libc.prx bundled with PPSA03671 v01.001.005 (J03848509, SHA-256 483961e086b8a1fffa8138b3c0badf798987fb5f35ec908d828946408be2d8a6), read from its disassembly and run. The module reads these fields in two generations of Dinkumware code, and this PR keeps both where it does:

  • float and double (0xD24C0, 0xD2710) read the field with the current reader (0xD4D80, microsoft/STL's _Parse_fp_with_locale). It takes up to 768 significant digits and turns later ones into a power of ten. It reads 0x hex fields with p exponents whatever the flags say, takes the decimal point from numpunct and writes the C locale's, and checks groups as the integer reader does. It folds the power into a clamped exponent that it writes into the field. The field is converted with _Stof or _Stod with errno saved and restored. A failure assigns failbit and stores the converted value, or 0 when nothing converted. A bad grouping fails but keeps the value.
  • long double (0xD2960) uses the older reader (0xD69A0). Without hexfloat it keeps 48 significant digits, rounds up on a nonzero lost digit, carries into the backstop digit with an extra power of ten, keeps 8 exponent digits and returns the power for _Stoldx. A double separator or a separator at the start ends the field, and a bad grouping empties it. With hexfloat it calls the current reader with 36 significant digits and empties the field on a failure or a bad grouping. The value is always stored, eofbit and then failbit are added to the state rather than assigned, and the grouping does not set failbit by itself.

The _Makeloc debt entry now lists all of num_get<char>, and the num_get entry names the conversion functions and the module's 768-byte buffer, which a field of about 760 significant digits overruns there.

Tested

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

  • guest_sce_v2_num_get_char now also covers the three slots: decimal and hex fields, exponents, a lone zero, point or e without digits, leading fraction zeros, overflow to infinity with errno restored, digits past 768, the numpunct decimal point, good and bad groups for both readers, hexfloat for long double, its added failbit, and the carry into the backstop. Each of these inputs was also run against the module. The test passes, as do guest_sce_v2_num_put_char, guest_sce_v2_num_put_wide, 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, guest_sce_v2_string_to_integer and public_headers (13 of 13).
  • The differential harness of feat(libc): implement the std::__sce_v2 num_get virtuals for char integers, bool and pointers #16919 on the three slots, with texts that mix decimal and hex numbers, points, exponents and separators, fields of up to 700 digits, random decimal points, separators and groupings, and hexfloat on a quarter of the calls. It compares the value bytes, the state, errno, the stream's fields, the iterator and buffer state, and the order of the ctype, numpunct, underflow and uflow calls.
    • With the module's _Sto*x, _Stof, _Stod and _Stoldx redirected to this library's: 400,000 calls over 2 seeds, 0 differences.
    • With the module's own conversion functions: 114 of 200,000 calls differ, all in the last bit of a float (97) or double (17), which the strtof debt entry already describes.
  • Integer, bool and pointer regression on the harness of feat(libc): implement the std::__sce_v2 num_get virtuals for char integers, bool and pointers #16919: 50,000 calls, 0 differences.
  • Negative control: 34 single-change mutations of the new code. The harness catches 24 and the final test 23, 26 together. The first test run missed 9 that the harness caught, so the test was strengthened and those were rerun; the harness alone still catches 3 (kept leading zeros and the two significant-digit limits of the current reader). The other 8 make no visible difference: the trailing separator skip cannot run because the digit loop already consumes separators. Putting back a zero before a fraction or an exponent, a missing 0 after 0x, storing leading fraction zeros, and two more exponent digits give the same converted value. The 48-digit limit and the lost-digit nudge would need a field that decides rounding past those digits.
  • check_conventions, git diff --check: clean. Merges cleanly with main. pr_overlap finds no open PR with the same functions. It lists feat(libc): implement the std::__sce_v2 num_get virtuals for wchar_t integers, bool and pointers #16924, which builds on feat(libc): implement the std::__sce_v2 num_get virtuals for char integers, bool and pointers #16919 like this PR and adds the wchar_t virtuals. Merging the two branches conflicts only in the two debt entries both rewrite; the code merges cleanly, and the wchar_t floating-point slots keep throwing after it. Whichever merges second needs main merged in. It could not compare 34 PRs that conflict with main on shared files; of those, the 4 that touch LocaleSupport.cpp (feat(libc): add std::__sce_v2::_Random_device, _Fiopen and setw #9526, feat(libc): wire the codecvt<wchar_t,char> facet vtable #11172, feat(libc): export the money_put facet vtable as data #12518, feat(libc): export the wide time_put facet vtable as data #12704) do not add these virtuals.

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: feat(libc): implement the std::__sce_v2 num_get virtuals for char integers, bool and pointers #16919
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

Danigamer05 and others added 14 commits November 5, 2026 21:33
…irtuals for char

The facets built by _Makeloc threw NotImplemented from every virtual
except the lifecycle slots. The char ctype, numpunct and codecvt facets
now answer as the console's libc.prx does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…irtuals for wchar_t

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Main removed GuestStringInlineCapacity, GuestStringCapacityMask and
GuestStringLargeAllocation from LocaleSupport.cpp, so the versioned
facets' strings use GuestLocale::StringInlineCapacity,
GuestLocale::StringCapacityMask and GuestStringStorage::Deallocate.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…egers, bool and pointers

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Main removed GuestStringInlineCapacity and GuestStringCapacityMask from
LocaleSupport.cpp, so AssignSceV2String uses
GuestLocale::StringInlineCapacity and GuestLocale::StringCapacityMask.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Main removed GuestStringInlineCapacity and GuestStringLargeAllocation
from LocaleSupport.cpp, so TakeSceV2String tests
GuestLocale::StringInlineCapacity and frees through
GuestStringStorage::Deallocate.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…sce-v2-num-put-wide-20261107

Brings in current main through boykopovar#16516. The templated TakeSceV2String
frees through GuestStringStorage::Deallocate, since main removed
GuestStringLargeAllocation from LocaleSupport.cpp.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ating point

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Nov 8, 2026 •

Copy link
Copy Markdown

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 7715 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 8965 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 9087 docs/dev/debt/libc.md
PR 11661 docs/dev/debt/libc.md
PR 11689 docs/dev/debt/libc.md
PR 11698 docs/dev/debt/libc.md
PR 12682 core/libs/prx/libc/CMakeLists.txt
PR 12688 core/libs/prx/libc/CMakeLists.txt
PR 12700 core/libs/prx/libc/CMakeLists.txt
PR 12717 core/libs/prx/libc/CMakeLists.txt
PR 12849 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 12965 core/libs/prx/libc/CMakeLists.txt
PR 15973 docs/dev/debt/libc.md
PR 17746 core/libs/prx/libc/include/GuestLocale.hpp
core/libs/prx/libc/src/LocaleSupport.cpp
PR 23395 core/libs/prx/libc/CMakeLists.txt
PR 24907 core/libs/prx/libc/include/GuestLocale.hpp
core/libs/prx/libc/src/LocaleSupport.cpp
PR 25573 core/libs/prx/libc/include/GuestLocale.hpp
core/libs/prx/libc/src/LocaleSupport.cpp
PR 28051 core/libs/prx/libc/CMakeLists.txt
PR 28565 core/libs/prx/libc/CMakeLists.txt
PR 28568 core/libs/prx/libc/include/GuestLocale.hpp
core/libs/prx/libc/src/LocaleSupport.cpp
PR 28709 core/libs/prx/libc/CMakeLists.txt
core/libs/prx/libc/include/GuestLocale.hpp
PR 28808 core/libs/prx/libc/CMakeLists.txt

@JJenkx

JJenkx commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

Windows check of head 90d09a066f, merged onto main 8eb293447e (bfc3300969, clean merge).

Setup: Windows 11 Enterprise 22621, WinLibs GCC 15.2 UCRT, Release. The hosted Windows check does not run these tests on pull requests.

  • guest_sce_v2_char_facet_virtuals, guest_sce_v2_facet_ids, guest_sce_v2_makeloc, guest_sce_v2_num_get_char, guest_sce_v2_num_put_char, guest_sce_v2_num_put_wide pass in 3 of 3 runs, none skipped.

AI-assisted: yes.

@Doorriiaann

Copy link
Copy Markdown
Contributor

The head does not compile on Linux x86-64 (GCC 14.2.0, libstdc++, Debian 13), and neither do the PRs stacked on it; PrivacyOnion's reviews on #17277 and #17296 show the same error at their lines. The cause is this PR's own commit:

core/libs/prx/libc/src/LocaleSupport.cpp:2745:49: error: no matching function for call to 'min(const int64_t&, long long int)'

90d09a0 adds const std::int64_t adjustment = std::min(roundedUp, std::llabs(power));. std::int64_t is long on Linux x86-64 and std::llabs returns long long, so std::min gets two types; on MinGW (and macOS) std::int64_t is long long, which is why the Windows job builds it. Something like std::min<std::int64_t>(roundedUp, std::llabs(power)) fixes it. The linux CI job is skipped on pull requests, so CI could not show this.

Every PR stacked on this one contains 90d09a0 and fails the same way, at the line this moves to in each: #16949, #16975, #17277, #17296, #17331, #17539, #17568, #17578, #17607 and #17610 (checked on all eleven, this one included, merged onto current main 0dd2fffe35 with git merge-tree, no conflicts, and on the heads of #16944, #17568, #17578, #17607 and #17610; main itself compiles LocaleSupport.cpp clean). Fixing it here and rebasing the stack covers all of them.

Checked with g++ -fsyntax-only on LocaleSupport.cpp using the project's compile command from a main build.

AI-assisted: yes (run and drafted with an AI agent, checked by me)

Danigamer05 and others added 2 commits November 12, 2026 22:56
…61108

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
std::int64_t is long on Linux x86-64 and std::llabs returns long long,
so std::min could not deduce a single type and LocaleSupport.cpp did not
build there. Name std::int64_t explicitly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Danigamer05

Copy link
Copy Markdown
Contributor Author

@Doorriiaann Thanks, that was the cause. Fixed in 942b87f with std::min<std::int64_t>(roundedUp, std::llabs(power)).

The fix is merged into every PR stacked on this one, one changed line each: #16949, #16975, #17277, #17296, #17331, #17539, #17568, #17578, #17607 and #17610 (top of the stack: a78c139).

Checked at the top of the stack:

  • Linux: Ubuntu 24.04 (WSL), GCC 13.3, g++ -fsyntax-only with the project's compile flags on LocaleSupport.cpp and on every core/libs/tests/GuestSceV2*.cpp the stack touches. The min(const int64_t&, long long int) error reproduces before the fix; after it there are no errors. This is a syntax check, not a full Linux build.
  • Windows: Windows 11 Pro 26200, MinGW GCC, Release. The 27 guest_sce_v2_* tests pass (the two filesystem tests are not part of this stack).

The description footer is gone too.

AI-assisted: yes

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Nov 17, 2026
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Nov 20, 2026

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed — changes requested

Changes are required before merge: prevent out-of-bounds access when rounding a field whose significant digits were all discarded.

What I ran

  • Reviewed head 9f337f9b9bde0fc05ff6261781bbe1e2c417f807 using repo_read.py.
  • No tests or build commands were run in this review.

Results

Check Result Note
Source review failed Found a verified out-of-bounds write
Tests not run Author-reported tests were not run here

Findings

Blocking

  • core/libs/prx/libc/src/LocaleSupport.cpp:3041: When the integer part has at least maxSignificant digits but all are zero, significant remains zero while out still points after the initial 0. If the fraction then contains a nonzero digit, the fraction loop reaches the limit and sets lost = true without writing a fraction digit. The rounding code then selects out[-1] (or out[-2] if that is the decimal point) and increments it. For input such as 0. followed by 769 zeros and then 1, this modifies the decimal point or the leading zero instead of a retained significant digit. Handle this all-zero retained prefix before applying the lost-digit rounding adjustment; add a regression test for a tiny nonzero value beyond the significant-digit limit.

Non-blocking

None.

Not verified

  • Read the prepared description, commits, manifest and diff, and targeted ranges in LocaleSupport.cpp. The full text for changed files was excluded from the review bundle; source inspection used the pinned reader. Other changed files and behavior outside the cited code were not fully reviewed.
  • Did not run the author-reported Windows test suite or differential harness; Linux, macOS and console behavior were not verified.

To get this to a good state

  • Fix the rounding path for an all-zero retained prefix and add a regression test for a nonzero digit discarded after the significant-digit limit. Rerun the relevant floating-point tests and differential harness on the new head.

@Danigamer05

Copy link
Copy Markdown
Contributor Author

@MarcZX10 Thanks for the review. That path can't be reached, because a zero is never counted while nothing significant has been written:

  • In the integer part, takeDigit(0) with significant == 0 takes neither branch: it writes nothing and leaves significant at 0. A run of integer zeros, however long, never moves it toward the limit.
  • After the decimal point, if (significant == 0) consumes every leading zero, decrementing power and writing nothing.
  • maxSignificant is always a positive constant (SceV2FloatSignificant = 768, or SceV2HexSignificant), so lost is set only once significant >= maxSignificant, after at least that many digits have been written through out. out[-1] is then the last kept digit, or the decimal point right after it, in which case out[-2] is that digit.

With 0. followed by 769 zeros and a 1, the zeros go to power, the 1 becomes the first significant digit, and lost stays false. The existing case at GuestSceV2NumGetChar.cpp:340 ("1." + 800 zeros + "1") covers the rounding mark with kept digits before it. This is the same structure as the module's _Getffld, which the differential runs in the description compare against.

A review asked whether a field whose first 768 or more digits are zeros
could reach the lost-digit rounding with no kept digit, and bump the
leading zero or the decimal point. It cannot: leading zeros in the
integer part are not counted, and leading fraction zeros only lower the
power, so the first nonzero digit is always kept. Pin that down with 800
integer zeros, 769 fraction zeros before a 1 scaled back by e770, and
both together. Counting leading zeros as significant fails two of them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Danigamer05

Copy link
Copy Markdown
Contributor Author

@MarcZX10 Added the regression test you asked for in a856bf5 (GuestSceV2NumGetChar.cpp), next to the existing lost-digit case:

  • 800 zeros + ".25": 0.25, eofbit, 803 characters read. The integer part has more zeros than the 768-digit limit, all of them leading.
  • "0." + 769 zeros + "1e770": 1.0, eofbit, 776 characters read. This is your example, scaled back so the value is representable.
  • 800 zeros + "." + 769 zeros + "15e770": 1.5, eofbit, 1576 characters read. Both runs of zeros together, with two kept digits after them.

On Windows 11 with MinGW GCC 15.2, guest_sce_v2_num_get_char passes. As a negative control, counting leading zeros as significant digits (else if (true) in place of else if (index != 0 || significant != 0)) makes the first and third cases fail. The test also compiles on Ubuntu 24.04 with GCC 13.3, and tools/check_conventions.py passes. The commit is merged into the PRs stacked on this one, where it changes only this test file.

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed — looks good

Nothing blocks this change based on the code reviewed; the points below are optional.

What I ran

Reviewed head a856bf56c1b41b6eca62a0c835cca7498bfe6531. No commands or tests were run. The reported Windows and harness results are author supplied; no merged preview was run.

Results

Check Result Note
Code review passed Floating point paths inspected
Tests not run Results supplied by author
Build not run No local build performed

Findings

Blocking

None.

Non-blocking

None.

Not verified

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/GuestSceV2NumGetChar.cpp, core/libs/tests/GuestSceV2NumPutChar.cpp, core/libs/tests/GuestSceV2NumPutWide.cpp, and docs/dev/debt/libc.md. These are all files listed as reviewed in the manifest; no changed files were excluded. Only relevant portions of the larger files were inspected. Build and runtime behavior were not independently confirmed, including Linux, macOS, and title execution.

To get this to a good state

Nothing is required. The author supplied detailed differential and regression test results; these were not independently rerun.

@ligmaSec ligmaSec left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

AI-assisted: yes.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants