fix(libc): round and cap %e, %f and %g as titles' libc.prx does #9916
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/9916"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-22T21:33:52Z.
What
The printf family formats
%e,%fand%gthrough the host. glibc and the UCRT print every exact digit and round ties to even. Thelibc.prxthat titles ship (Dinkumware_Printf) does neither. Console vsmain:main%.0fof 2.532%.1fof 0.250.30.2%#.3gof 20252.03e+032.02e+03%.20fof 0.10.100000000000000005550.10000000000000000555(same)%.40eof 0.11.0000000000000000555000000000000000000000e-011.0000000000000000555111512312578270211816e-01%.0fof 2^10012676506002282294014967000000001267650600228229401496703205376%.0fof 67108865.56710886567108866So a title's
"%.0f%%"or"%.1f"text can come out one digit different, for example a score or timer that sits on a .5 or .25 value.Measured rules. I read these from the outputs of three titles'
libc.prx, not by translating its code:kbe thefrexpbinary exponent times 30103 / 100000, truncated (an estimate of the decimal exponent). The console keeps the exact decimal digits down to position4 * floor(k / 4) - 20and prints zeros after them. That is 20 to 24 significant digits, aligned to groups of four. This rule predicts the kept-digit count of 112,609 of 112,612 sampled%.45eoutputs.kreaches 8, which is from 2^26 up. Below that, every exact tie rounds up. A tie probe over binary exponents -43…53, at every tie position and in both%eand%f, matches with no exceptions.This PR formats finite
doubleandlong doublee/E/f/F/g/Gwith those rules. A small big-integer routine generates the exact digits: division by 10^9 for the integer part, multiplication by 10^9 for the fraction. The result is the same on every host.RequiresGuestFormattingnow also routes these conversions toFormatWindows, so Linux uses the same code. The flag/width/precision parsing that%aused moves intoParseFloatSpecandPadFloat, which both conversions share.Remaining difference. The console computes the kept digits in extended precision, so its last kept digit is sometimes one unit lower than the exact digit. That affects about 7 % of outputs with more than 17 significant digits;
%fof 1e22 is9999999999999999999999.000000there. Matching it would mean reproducing the console's arithmetic step by step, so this PR keeps exact digits and records the gap as technical debt. Every output with up to 17 significant digits matches.Tested
Current Linux refresh:
182bd6aa78842891f29e00bf113524775a6b897bon main637220db112a3daf4960984290a82fcf65ba7061. The stack is rebuilt linearly in the order #9699 → #9916 → #9927 → #9958 → #10379, with no merge commits or duplicated prerequisite commits. This PR adds one decimal-formatting commit. Fresh Release libc/fixture build andguest_formatting,guest_wide_formatting,guest_stdio_variadicpass (3/3) on this exact head; conventions against the tested main and whitespace checks pass. Final-stack conventions against origin/main have zero errors (six inherited debt-length warnings). All five heads also merge cleanly with newer mainc8d34aa8f4d8058d57169cc1e423ba6db00faaf0; that merge preview is not an additional runtime test. A final-stack Linux routing negative control reverts only the narrow decimal routing:guest_formattingthen prints0instead of1for%.0fof 0.5 and fails; wide/stdio groups still pass. The exact final head was restored, rebuilt and passes 3/3. This is an integration control, not a standalone-parent comparison. All five pinned heads, patches, checks and control logs (SHA-2569cda806ab2a54a4a0ce012517eafb7db0441ab2665d2e49884c3d1fc5b9e0c8a). No fresh Windows, title-library comparison, physical-console, game or performance result is claimed; Windows testing remains paused. The requested fresh Windows validation on #9699 is still outstanding.Historical validation on previous public head
2f7dcf59c7950119cafaf13a52bc9ec796eb85f9is retained below with its original scope; references there to ‘exact head’ refer to that historical run.Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release,
BUILD_TESTING=ON. Head b1dd830f1e, on #9699's head 184733f90d (main3bbd2321de).Differential run. A scratch loader runs a title's decrypted
libc.prxnatively. It compares that copy'ssnprintfwith this build'ssnprintf_nid_postfixon 600,000 random formats: random flags, width, precision 0–29,e/E/f/F/g/Gwith and withoutL, and values from random bits, binary fractions, decimal fractions, a mid-range exponent and small integers.main-basedThe copies bundled with PPSA07923, PPSA05916 and PPSA13934 give identical counts. A second seed gives 0 of 408,691 at ≤ 17 digits, against 3,173 for
main.guest_formatting. The newCheckDecimalFloathas 29 console-measured cases:0.1,2^100, the smallest subnormal, a value nearDBL_MAXthat keeps four digits fewer);#,+and0flags;%gstyle switching and999.5carrying to1e+03;long doublecases, including0x1p-16445L.The libc formatting, scanning, stream, wide-character and string tests (34) pass in three runs out of three. With the base's
WindowsFormatting.hppandFormatting.cppand this test,guest_formattingaborts.Micro-benchmark. Of
snprintf_nid_postfixalone, not a title trace:%.2f,%g,%eof UI-range values take about 0.41 µs per call against about 1.2 µs onmain.%.2fof random-bit doubles takes 1.7 µs against 12 µs, since the console keeps at most 24 digits.python tools/check_conventions.py --base origin/mainandgit diff --checkpass.Overlap. Earlier code comparisons with #9552, #9087 and #3987 were clean; the debt entries overlap #9552, #8855 and #5303. The latest review requests refreshing the debt entry against current main. #9927 builds on this formatter and adds the wide decimal path; retain its formatter and tests when refreshing that dependent branch. #9939 independently changes narrow
%lsUTF-16 code-unit encoding. It sharesWindowsFormatting.hpp,core/libs/tests/Formatting.cppanddocs/dev/debt/libc.md; preserve both the decimal formatter/tests and #9939's code-unit encoding/tests and debt text in either landing order. This is a static overlap review, not a combined-build result; #9939 is not a functional prerequisite.Linux review (not an author-run product build). Dom3005 tested submitted head
b5e461750merged with main24c6cc33fon Ubuntu 26.04/GCC 15/glibc 2.43 in Docker/WSL2.guest_formattingfails in prerequisite #9699'sCheckHexFloat, because finite%astill routes to glibc on Linux. Removing only that call makes the remaining suite pass, supporting this PR's decimal cases but not a passing full suite. That historical failure is addressed by the retainedCheckHexFloatand the Linux production-route fix in prerequisite head4d3c9a73fa; the exact-head focused rerun below passes. The broader 19-case Linux libc selection now passes three repetitions at this exact head (see the broad Linux result below). Original failure details.Refresh (Windows complete; the broader 19-case Linux selection also passes at this head): #9699 and this branch are rebased onto
main895f47f4eeand #9927 onto this head. On Windows the 34 formatting, scanning, stream, wide-character and string tests pass in three runs out of three on 4d3c9a73fa, d9653a15e3 and 72e9e54e15 (GCC 15.2.0 UCRT r7, Release). #9699's new commit routes%a/%Athrough the guest formatter on Linux, which addresses theCheckHexFloatfailure in the Linux report above; that report is for the older head. A fresh Linux run ofd9653a15e3passes both complete formatting executables, as recorded below. No console run is claimed. NaN and infinity still use the host. The wide printf family still uses the host in this branch; dependent #9927 changes its finite decimal path.Linux (reviewer run): MarcZX10 ran a focused Linux check in an existing configured Release build with GCC 12:
guest_formattingandguest_wide_formattingpassed 2/2 with the formatting and test sources of #9958, which contains this PR and #9699. That reused an older configured tree with the PR's sources overlaid, so it is a focused Linux run, not a clean CI build.Linux (Codex, fresh exact-head build): Recorded result for
d9653a15e3cca7746a2fce60f8ca51a2a5bc4bb4: Linux 7.2.9-arch1-1, GCC 16.2.1 20260810, glibc 2.44, Ninja Release,BUILD_TESTING=ON.ctest -R '^guest_(wide_)?formatting$' -V --no-tests=error --timeout 120passes both complete executables in all three repetitions, with no skipped cases. This is two CTest cases, not the Windows 34-test selection or a full CI/title/performance result. The subsequent broader 19-case focused libc selection passes all three repetitions at this exact head; see the recorded result below.Broader Linux validation (exact head
d9653a15e3cca7746a2fce60f8ca51a2a5bc4bb4): Complete result. All 19 selected libc formatting, scanning, stream, string, character and locale CTest cases passed in each of three repetitions, with no skips. Linux 7.2.9-arch1-1, GCC 16.2.1, glibc 2.44, Ninja Release,BUILD_TESTING=ON. This is the exact submitted head, not a build against newer main, a full CI run, or a title/performance result. Dependent #9927 has a separate stream-test expectation failure under investigation; these passes do not establish that dependent branch passes.Independent Linux validation (attributed, no local rerun): Dextroid17 tested exact head
2f7dcf59c7and its clean merge onto maind284c98b2eon CachyOS / kernel7.3.0-4 / GCC16.2.1 / glibc2.44, Release, SPIRV-Tools off. Bothguest_formattingandguest_wide_formattingpass three repetitions on each tree. Reverting only this PR's implementation commit5291860811while keeping its tests and #9699 makes the first decimal regression fail:%.0fof0x1p-1returns[0]instead of[1];CheckHexFloatstill passes. Restoring the implementation passes. Wide formatting passes with or without that narrow-format change, consistent with its separate fix in #9927.On the merged tree the reporter ran the full suite: 1,971 passed,26 skipped,3 failed of2,000. All616 executed
guest_*tests passed. The three failures are theagc_driver_sample_order*tests on their RTX3080Ti and reproduce on plain maind284c98b2ein three runs each; lavapipe passes or skips them. This is an attributed exact-head/merged-tree CPU regression and baseline-failure report, not an all-green suite, a local reproduction, or a title/console/performance result.Checklist
main: the #9699 → #9916 stack is rebased ontomain895f47f4ee(#9699 at 4d3c9a73fa, this PR at d9653a15e3, dependent #9927 at 72e9e54e15), and the focused suite was rerun on Windows on each head; both complete formatting executables also pass on Linux at each exact head; the broader 19-case Linux libc selection also passes three repetitions at this exact head (see Tested)NotImplemented_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)AI-assisted (Claude Code).