Skip to content

fix(libc): generate printf's decimal digits as the bundled libc.prx does - #23444

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
DerKarussellbremser:fix/printf-digits
Nov 19, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
DerKarussellbremser:fix/printf-digits

Conversation

@DerKarussellbremser

@DerKarussellbremser DerKarussellbremser commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

What

%e, %f and %g printed the exact decimal digits of a value, cut off at a position that had been fitted to the titles' output. The debt entry named the gap: the console computes the digits in extended precision, so some outputs longer than 17 significant digits end one unit lower, and a PS5 payload process on firmware 11.60 showed four such cases out of 240 (#22772): %f of 1e22, 1e23 and 2^70, and %.10f of 123456789012345678.

The digits are now generated the way the libc.prx bundled with PPSA10122 01.101.000 (SDK 5.00) generates them, in its conversion routine at 0x1700:

  • the magnitude is put into three 32-bit parts (the _LXp_* arithmetic that fix(libc): compute powl as the bundled libc.prx does #23367 brings in for powl);
  • with q the binary exponent times 30103 / 100000, it is multiplied in that arithmetic by a power of ten, 10^n for small values and 0.1^n for large ones (n a multiple of four), so that its integer part has at most eight digits;
  • digits are taken eight at a time: the integer part is subtracted, the rest multiplied by 1e8. At most 22 digits are produced, fewer when the precision asks for fewer;
  • the digit string is rounded half up on what was produced.

Nothing here comes from the module's data: the constants are 10, 1e8 and 0.1 cut after 32, 64 and 96 fraction bits. The scaling is not exact, which is where the "one unit lower" comes from, and why %.0f of 67108865.5 is 67108865.

Because the routine works in long double, the digits depend on the caller's x87 precision and rounding mode, as the module's do.

What moves. WindowsFormatting.hpp is included by other libraries, so the long double instantiation of the multi-part templates and the three helpers it needs (TruncateExtended, ScaleExtendedBits, UnscaleExtended) move from MathExact.cpp into ExtendedPrecisionParts.hpp as inline functions; MathExact.cpp uses them from there. On a host whose long double is not the x87 format, and in a build that is not x86-64 (the Emscripten library build compiles Formatting.cpp, and ExtendedPrecisionParts.hpp has SSE inline assembly), the earlier method is kept.

Tested

Windows 11 Home 10.0.26200, WinLibs GCC 15.2.0 r7 UCRT, Release, on main b461185 (#23367 is merged; the branch is one commit, 3066e4a9a7).

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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

🤖 Generated with Claude Code

https://claude.ai/code/session_011TXsSW8zeAgKgHLCFsYwM3

@PrivacyOnion PrivacyOnion 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.

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

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_formatting_tests, guest_math_exact_tests built cleanly.
  • CTest (guest_math_exact, guest_formatting, guest_sprintf_s_constraints):
    1/3 Test #1230: guest_math_exact .................   Passed    0.01 sec
    2/3 Test #1328: guest_sprintf_s_constraints ......   Passed    0.01 sec
    3/3 Test #1327: guest_formatting .................   Passed    0.20 sec
    100% tests passed, 0 tests failed out of 3
    
  • Negative control: with core/libs/prx/libc/include/ExtendedPrecisionParts.hpp, core/libs/prx/libc/include/WindowsFormatting.hpp, core/libs/prx/libc/src/DinkumwareInternals.cpp, core/libs/prx/libc/src/Math.cpp, core/libs/prx/libc/src/MathExact.cpp, core/libs/prx/libc/src/VersionedExtendedPrecision.cpp reverted to main and the new tests kept, the suite fails:
    1/3 Test #1230: guest_math_exact .................   Passed    0.01 sec
    2/3 Test #1328: guest_sprintf_s_constraints ......   Passed    0.01 sec
    3/3 Test #1327: guest_formatting .................Subprocess aborted***Exception:   0.57 sec
    67% tests passed, 1 tests failed out of 3
    
    Restoring the change makes it pass again.
  • Code reading:
    • ExtendedPrecisionParts.hpp arithmetic: verified AddHalf, MultiplyHalf, SetWide, MultiplyWide, and ScaleExtendedBits for edge cases, sign handling, and bitwise round trip.
    • WindowsFormatting.hpp GeneratedDigits: verified base-10 digit extraction loop, leading zero stripping, and rounding half-up carries on boundaries.
    • WindowsFormatting.hpp DecimalFloat %g handling: verified precision adjustment and exponential decision logic against format specifiers.
    • MathExact.cpp powl_nid_postfix integration: verified delegation to PowerExtended and correct exception raising via _Feraise_nid_postfix.
    • AGENTS.md rules and Technical Debt: checked technical debt fragment formatting in docs/dev/debt/changes/ and ensured no illegal comments or forbidden headers.

Looks Good To Me

%e, %f and %g printed exact digits cut at a fitted position. The
libc.prx bundled with PPSA10122 01.101.000 scales the value by a power
of ten in three-part arithmetic and takes eight digits at a time, which
is where the console's outputs that end one unit lower come from. The
four such cases measured on firmware 11.60 are now reproduced.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011TXsSW8zeAgKgHLCFsYwM3
@DerKarussellbremser

Copy link
Copy Markdown
Contributor Author

library-portability failed on the first head: the Emscripten build compiles Formatting.cpp, which now reached ExtendedPrecisionParts.hpp and its SSE inline assembly (invalid output constraint '+x' in asm). The include and the new digit generation are now inside #if defined(__x86_64__); other builds keep the earlier method, as a host without the x87 long double already did.

Rebased onto main b461185 at the same time, since #23367 is merged: the branch is one commit (3066e4a9a7) and no longer depends on anything. Rebuilt on Windows after the rebase: guest_formatting, guest_wide_formatting and guest_math_exact pass, the 239 console texts of #22772 are still reproduced, and 100,000 double and 100,000 long double values with random %e/%f/%g formats give the module's text. The description is updated.

AI-assisted (Claude Code).

@PrivacyOnion PrivacyOnion 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.

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

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_formatting_tests built cleanly.
  • CTest (guest_formatting, guest_sprintf_s_constraints):
    1/2 Test #1328: guest_sprintf_s_constraints ......   Passed    0.00 sec
    2/2 Test #1327: guest_formatting .................   Passed    0.10 sec
    100% tests passed, 0 tests failed out of 2
    
  • Negative control: with core/libs/prx/libc/include/ExtendedPrecisionParts.hpp, core/libs/prx/libc/include/WindowsFormatting.hpp, core/libs/prx/libc/src/MathExact.cpp reverted to main and the new tests kept, the suite fails:
    1/2 Test #1328: guest_sprintf_s_constraints ......   Passed    0.00 sec
    2/2 Test #1327: guest_formatting .................Subprocess aborted***Exception:   0.19 sec
    50% tests passed, 1 tests failed out of 2
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified GeneratedDigits in WindowsFormatting.hpp for buffer safety, loop bounds, exponent calculation, and std::string operations on all branches
    • Verified precision clamping and rounding half-up edge cases including carry propagation into leading digits in GeneratedDigits
    • Verified x86_64 preprocessor guards and fallback path to ConsoleDecimalDigits on non-x86_64 host environments
    • Checked ExtendedPrecisionParts.hpp functions (WordsOf, StoreWords, SpecialCode, TestWords, TruncateExtended, ScaleExtendedBits, UnscaleExtended) for bit-level correctness, denormals, and NaNs
    • Verified MathExact.cpp namespace alias updates matching LibcDetail exports
    • Verified AGENTS.md rules, technical debt fragment formatting in docs/dev/debt/changes/, and absence of invalid comments

Looks Good To Me

@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

The changed lines support the stated formatting change, and I found no concrete blocking defect in the code inspected.

What I ran

Read the prepared diff and changed lines at head 3066e4a9a783093c76d8f5229beab4e9a50fbb4e using repo_read.py. No build or tests were run here; the test results in the PR description are author-reported.

Results

Check Result Note
Changed-line review passed All six changed files' added lines were read.
Build and tests not run No execution environment was available.

Findings

Blocking

None.

Non-blocking

None.

Not verified

No build, test, console, or performance checks were run independently. Read the changed lines in ExtendedPrecisionParts.hpp, WindowsFormatting.hpp, MathExact.cpp, Formatting.cpp, and both changed debt fragments. No changed files were excluded; unchanged callers beyond the inspected formatting paths were not read.

To get this to a good state

Nothing is required.

@oneandonlydean
oneandonlydean merged commit 3f31596 into boykopovar:main Nov 19, 2026
22 of 24 checks passed
@github-actions

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 9087 core/libs/prx/libc/include/WindowsFormatting.hpp
core/libs/tests/Formatting.cpp
PR 9340 core/libs/tests/Formatting.cpp
PR 11332 core/libs/prx/libc/include/WindowsFormatting.hpp
core/libs/tests/Formatting.cpp

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.

4 participants