Repository navigation
fix(libc): generate printf's decimal digits as the bundled libc.prx does - #23444
Conversation
PrivacyOnion
left a comment
There was a problem hiding this comment.
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against e473d492cd:
- Conventions:
tools/check_conventions.pypassed. - Build:
guest_formatting_tests,guest_math_exact_testsbuilt 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.cppreverted tomainand the new tests kept, the suite fails:Restoring the change makes it pass again.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 - 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
e473d49 to
3066e4a
Compare
|
Rebased onto AI-assisted (Claude Code). |
PrivacyOnion
left a comment
There was a problem hiding this comment.
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 3066e4a9a7:
- Conventions:
tools/check_conventions.pypassed. - Build:
guest_formatting_testsbuilt 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.cppreverted tomainand the new tests kept, the suite fails:Restoring the change makes it pass again.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 - 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
left a comment
There was a problem hiding this comment.
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.
|
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.
|
What
%e,%fand%gprinted 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):%fof 1e22, 1e23 and 2^70, and%.10fof 123456789012345678.The digits are now generated the way the
libc.prxbundled with PPSA10122 01.101.000 (SDK 5.00) generates them, in its conversion routine at 0x1700:_LXp_*arithmetic that fix(libc): compute powl as the bundled libc.prx does #23367 brings in forpowl);qthe 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;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
%.0fof 67108865.5 is67108865.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.hppis included by other libraries, so thelong doubleinstantiation of the multi-part templates and the three helpers it needs (TruncateExtended,ScaleExtendedBits,UnscaleExtended) move fromMathExact.cppintoExtendedPrecisionParts.hppas inline functions;MathExact.cppuses them from there. On a host whoselong doubleis not the x87 format, and in a build that is not x86-64 (the Emscripten library build compilesFormatting.cpp, andExtendedPrecisionParts.hpphas SSE inline assembly), the earlier method is kept.Tested
Windows 11 Home 10.0.26200, WinLibs GCC 15.2.0 r7 UCRT, Release, on
mainb461185 (#23367 is merged; the branch is one commit,3066e4a9a7).doubletexts that the firmware 11.60 payload process printed (raw output in the first comment of docs(debt): add the printf digit comparison on firmware 11.60 to the printf float fragment #22772;%f,%e,%gand%acases a.01 to f.4 without a.10, which is logged by length, head and tail) are all reproduced, the four above included; docs(debt): add the printf digit comparison on firmware 11.60 to the printf float fragment #22772 had found 236 of the 240 equal onmain. The module run natively gives the same 239 texts.%.0fofDBL_MAX(a.10) has the logged length of 309 and the logged first 50 and last 40 digits.snprintfnatively:%e,%E,%f,%F,%g,%Gwith random flags, widths and precisions, 100,000doubleand 100,000long doublevalues at each of the x87 control words 0x37f, 0x27f, 0x7f, 0x77f, 0xb7f and 0xf7f: 0 differences in the text and the return value. Before the change 601 of 50,000doubleand 253 of 50,000long doubleoutputs differed at 0x37f (that sample included%a).Formatting.cpp,guest_formattingaborts on the base sources ([%f] of 0x1.52d02c7e14af6p+76 gave "[99999999999999991611392.000000]", expected "[99999999999999991611391.000000]") and passes with the change.ctestover the formatting, scanning, stdio, wide, locale, stream, string and math tests (228 tests, 173 targets rebuilt): all pass.libSceAvPlayerandlibSceNKWebKit, which include the header, build.tools/check_conventions.py --base origin/mainandtools/debt_fold.py --checkpass.tools/pr_overlap.py --branch fix/printf-digits: no overlap among the pull requests it could merge. It could not compare six that conflict withmain: feat(libc): implement the nextafter and nexttoward families #6295, fix(libc): match lgamma's signgam for -0 and its errno at poles as measured on a PS5 #7544 and fix(libc): lgamma stores 1 in signgam for -0 #17026 shareMath.cpponly through thepowlcommit of fix(libc): compute powl as the bundled libc.prx does #23367; fix(libc): follow snprintf_s and sscanf_s constraints as measured on a PS5 #9087 (snprintf_sandsscanf_sconstraints) and feat(libc): decode non-ASCII scanset members and non-BMP characters in the guest wide scanf #9340 (wide scanset) do not touch the digit generation; fix(libc): bound narrow formatting fields #11332 (draft, bounds of narrow formatting fields) changes the signature ofDecimalFloatand will meet this change in the first lines of that function, with nothing in common in what the two do.%e, %f and %gentry is edited insidedocs/dev/debt/changes/docs-libc-printf-float-console-1160.md, the unfolded fragment that already changes it; thenum_putentry, which described the same gap forsnprintf, is updated in a fragment of its own.%aand%Aare untouched; at 53- and 24-bit x87 precision they differ from the module (the module's hexadecimal digits depend on the precision too).long doubleand a title process are not measured on a console.num_put<char>::do_putwas not compared with its module again. Linux and macOS builds not tested.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
https://claude.ai/code/session_011TXsSW8zeAgKgHLCFsYwM3