Skip to content

fix(libc): round and cap %e, %f and %g as titles' libc.prx does - #9916

Merged
oneandonlydean merged 7 commits into
boykopovar:mainfrom
JJenkx:windows/printf-decimal-console
Oct 25, 2026
Merged

oneandonlydean merged 7 commits into
boykopovar:mainfrom
JJenkx:windows/printf-decimal-console

Conversation

@JJenkx

@JJenkx JJenkx commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

What

The printf family formats %e, %f and %g through the host. glibc and the UCRT print every exact digit and round ties to even. The libc.prx that titles ship (Dinkumware _Printf) does neither. Console vs main:

Call Console main
%.0f of 2.5 3 2
%.1f of 0.25 0.3 0.2
%#.3g of 2025 2.03e+03 2.02e+03
%.20f of 0.1 0.10000000000000000555 0.10000000000000000555 (same)
%.40e of 0.1 1.0000000000000000555000000000000000000000e-01 1.0000000000000000555111512312578270211816e-01
%.0f of 2^100 1267650600228229401496700000000 1267650600228229401496703205376
%.0f of 67108865.5 67108865 67108866

So 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:

  1. Digits kept. Let k be the frexp binary exponent times 30103 / 100000, truncated (an estimate of the decimal exponent). The console keeps the exact decimal digits down to position 4 * floor(k / 4) - 20 and 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 %.45e outputs.
  2. Rounding. A shorter precision rounds half up on the kept digits.
  3. Ties. An exact tie rounds down once k reaches 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 %e and %f, matches with no exceptions.

This PR formats finite double and long double e/E/f/F/g/G with 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. RequiresGuestFormatting now also routes these conversions to FormatWindows, so Linux uses the same code. The flag/width/precision parsing that %a used moves into ParseFloatSpec and PadFloat, 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; %f of 1e22 is 9999999999999999999999.000000 there. 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: 182bd6aa78842891f29e00bf113524775a6b897b on main 637220db112a3daf4960984290a82fcf65ba7061. 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 and guest_formatting, guest_wide_formatting, guest_stdio_variadic pass (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 main c8d34aa8f4d8058d57169cc1e423ba6db00faaf0; that merge preview is not an additional runtime test. A final-stack Linux routing negative control reverts only the narrow decimal routing: guest_formatting then prints 0 instead of 1 for %.0f of 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-256 9cda806ab2a54a4a0ce012517eafb7db0441ab2665d2e49884c3d1fc5b9e0c8a). 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 2f7dcf59c7950119cafaf13a52bc9ec796eb85f9 is 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 b1dd830, on #9699's head 184733f (main 3bbd232).

Independent Linux validation (attributed, no local rerun): Dextroid17 tested exact head 2f7dcf59c7 and its clean merge onto main d284c98b2e on CachyOS / kernel7.3.0-4 / GCC16.2.1 / glibc2.44, Release, SPIRV-Tools off. Both guest_formatting and guest_wide_formatting pass three repetitions on each tree. Reverting only this PR's implementation commit 5291860811 while keeping its tests and #9699 makes the first decimal regression fail: %.0f of 0x1p-1 returns [0] instead of [1]; CheckHexFloat still 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 the agc_driver_sample_order* tests on their RTX3080Ti and reproduce on plain main d284c98b2e in 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

AI-assisted (Claude Code).

output.Value(spec + "L" + conversion, value);
}
} else {
if (!length.empty() && length != "l") throw std::invalid_argument("Invalid floating length");

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.

ParseFloatSpec accepts precision == INT_MAX, but this sum adds the sign, decimal point and (for %e) exponent after that precision. For example %.2147483647e has an output length greater than INT_MAX; the check happens only after the signed sum. Please compute this with checked additions or reject when each component exceeds maximumLength - accumulatedLength before adding, so the intended output bound cannot be bypassed by an oversized format. I have not run this on the full Windows target locally; this is a source-level boundary finding. AI-assisted review.

@JJenkx JJenkx Oct 22, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks. The sum can't wrap: each term is a long long, precision is at most INT_MAX (ParseFloatSpec rejects anything larger), and the other terms add at most a few thousand. That's the sign, whole (at most 4,933 integer digits for a long double), the point and an exponent of at most 7 characters. So length is exact and the length > maximumLength check runs on the full value before anything is allocated. %.2147483647e is 2,147,483,653 characters, which the check rejects.

This wasn't covered by a test, so b5e4617 adds one in CheckDecimalFloat. These formats must throw std::overflow_error: %.2147483647e, %+.2147483647E, %.2147483647f, % .2147483646F, %#.2147483647g and %.2147483647Le. So must the smallest cases one character past the bound: %.2147483643e (2,147,483,649 characters), x%.2147483645f (output already holds 1 character) and x%-2147483647e.

Writing the test caught two cases I had first picked that are not oversized. %.2147483640e and x%.2147483642f produce 2,147,483,646 and 2,147,483,645 characters, within the bound, so they're legitimately formatted, as %a does. Windows build of b5e4617: the 34 formatting, scanning, stream, wide-character and string tests pass in three runs out of three, and conventions pass.

@JJenkx
JJenkx force-pushed the windows/printf-decimal-console branch from b1dd830 to b5e4617 Compare October 22, 2026 22:03
@github-actions

github-actions Bot commented Oct 22, 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 3965 docs/dev/debt/libc.md
PR 5021 core/libs/prx/libc/include/WindowsFormatting.hpp
core/libs/tests/Formatting.cpp
docs/dev/debt/libc.md
PR 5303 docs/dev/debt/libc.md
PR 6713 docs/dev/debt/libc.md
PR 7081 docs/dev/debt/libc.md
PR 7201 core/libs/tests/Formatting.cpp
PR 7386 core/libs/prx/libc/src/Formatting.cpp
core/libs/tests/Formatting.cpp
docs/dev/debt/libc.md
PR 7447 docs/dev/debt/libc.md
PR 7544 docs/dev/debt/libc.md
PR 7648 docs/dev/debt/libc.md
PR 7715 docs/dev/debt/libc.md
PR 8150 docs/dev/debt/libc.md
PR 8417 docs/dev/debt/libc.md
PR 9269 docs/dev/debt/libc.md
PR 9340 core/libs/prx/libc/src/Formatting.cpp
core/libs/tests/Formatting.cpp
docs/dev/debt/libc.md
PR 9371 docs/dev/debt/libc.md
PR 9447 docs/dev/debt/libc.md
PR 9526 docs/dev/debt/libc.md
PR 9527 core/libs/prx/libc/src/Formatting.cpp
docs/dev/debt/libc.md
PR 9552 core/libs/prx/libc/include/WindowsFormatting.hpp
core/libs/tests/Formatting.cpp
docs/dev/debt/libc.md
PR 9669 docs/dev/debt/libc.md
PR 9863 docs/dev/debt/libc.md
PR 9929 docs/dev/debt/libc.md
PR 10007 docs/dev/debt/libc.md
PR 10405 docs/dev/debt/libc.md
PR 10460 docs/dev/debt/libc.md
PR 10786 docs/dev/debt/libc.md

@Dom3005

Dom3005 commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

Linux run. Docker on Windows: Ubuntu 26.04, GCC 15, glibc 2.43, WSL2 kernel 6.6, Release, BUILD_TESTING=ON. Head b5e4617 merged onto main 24c6cc3.

guest_formatting fails on Linux, but in #9699's CheckHexFloat (%a still goes to glibc on Linux; details in #9699). With only the CheckHexFloat() call removed from main(), the test passes on Linux, so this PR's %e/%f/%g checks hold on Linux through the shared FormatWindows path. No findings in this PR's own change.

AI-assisted: yes

@MarcZX10

Copy link
Copy Markdown
Contributor

I reviewed the finite decimal formatter changes and their integration path. The exact-digit generator, precision/rounding path and shared width/flag handling are clearly scoped; the tests and differential survey describe the remaining extended-precision tail explicitly. I ran git diff --check upstream/main...refs/review/9916 and python3 -I tools/check_conventions.py --base upstream/main --head refs/review/9916; both pass. Hosted relinker, ASan, register-context and write-watch matrices pass; Linux/Windows product jobs are skipped by repository policy. The extensive title libc.prx differential and focused formatting tests in the description are author-reported. I did not run local C++ tests because the root filesystem is full.

This head is 5 commits ahead/120 behind current main. Main has not changed the formatter implementation or test files since the merge base, but it has changed docs/dev/debt/libc.md; refresh that shared entry before merge. Please retain the formatter/test updates from dependent #9927, and coordinate #9939's distinct %ls code-unit encoding and tests when rebasing/landing so the same WindowsFormatting.hpp, Formatting.cpp and debt changes survive. I found no independent code defect in this pass.

@JJenkx
JJenkx force-pushed the windows/printf-decimal-console branch from b5e4617 to d9653a1 Compare October 22, 2026 23:51
@JJenkx

JJenkx commented Oct 22, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto main 895f47f, on top of #9699's new head 4d3c9a7; head is now d9653a1.

  • Linux routing. fix(libc): format %a like the console on Windows #9699 now sends %a/%A through the guest formatter on Linux too, which is what made CheckHexFloat fail on Linux for Dom3005. So this PR's line in RequiresGuestFormatting became "aAeEfFgG". The debt entry now says that Linux keeps host formatting only for formats without wide arguments, %m, %p or a floating-point conversion. I dropped its sentence about NaN following glibc there, since no floating-point conversion reaches glibc any more.
  • Tests. Windows (GCC 15.2.0 UCRT r7, Release) build of that head: the formatting, scanning, stream, wide-character and string tests (34) pass in three runs out of three, and conventions pass.
  • Merge order. Main's new debt entries and test registration are kept. The only conflict was this PR's RequiresGuestFormatting line against fix(libc): format %a like the console on Windows #9699's.

@MarcZX10

Copy link
Copy Markdown
Contributor

The new exact-head conventions check is still failing: it reports the unchecked template item “Refresh the #9699 → #9916 stack onto current main and rerun the full focused checks; #9927 is a dependent wide-formatting extension.” The rebase onto 895f47f4 and the reported fresh Windows 34-test run address part of it. The Linux report linked in the description is for older head b5e461750 on main 24c6cc33f and documents a failure in #9699’s hex-float check, so it does not verify this refreshed stack. Please run the complete focused formatting suite on this exact head on Linux, report the actual result, and update the checklist only when the full item is satisfied. The hosted Linux product job is skipped by policy.

@JJenkx

JJenkx commented Oct 23, 2026

Copy link
Copy Markdown
Contributor Author

You're right that the Linux report doesn't cover this head. I can't run a Linux build on this machine, so I haven't ticked anything for Linux. Instead, the checklist's first item is back to the template's "Based on current main" line. It states exactly what was refreshed and tested on Windows, and that a Linux run of d9653a1 is still to be reported. The Tested section says the same. Dom3005, if you have time, a run of guest_formatting and guest_wide_formatting on #9927's head 72e9e54, which contains this one and #9699's, would cover all three.

@JJenkx

JJenkx commented Oct 23, 2026

Copy link
Copy Markdown
Contributor Author

Linux rerun completed on exact submitted head d9653a15e3cca7746a2fce60f8ca51a2a5bc4bb4 in a fresh detached worktree (not a source overlay or merge with newer main): Arch Linux, kernel 7.2.9, GCC 16.2.1, glibc 2.44, Ninja Release, BUILD_TESTING=ON, ANYPS5_ENABLE_SPIRV_TOOLS=OFF.

Built guest_formatting_tests and guest_wide_formatting_tests, then ran:

ctest --test-dir BUILD -R '^guest_(wide_)?formatting$' -V --no-tests=error --timeout 120

Both complete executables pass 2/2 in each of three runs, with CheckHexFloat retained. This supersedes the pending local Linux rerun for this head; it does not replace the earlier reviewer results for older heads.

Scope: these two formatting executables only, not the Windows 34-test selection, a full suite, a console run, a game test or a performance comparison. Raw configure/build/test logs and their SHA-256 hashes are retained locally under workspace/formatting-stack-linux-20261023/. AI-assisted validation (Codex).

@JJenkx

JJenkx commented Oct 23, 2026

Copy link
Copy Markdown
Contributor Author

Linux follow-up at exact head d9653a15e3cca7746a2fce60f8ca51a2a5bc4bb4: built 17 libc test targets and ran 19 exact-name CTest cases three times, 57/57 passed. Linux 7.2.9, glibc 2.44, GCC 16.2.1, Release. Coverage: allocating strings, scan count, wide formatting/strings/conversion, character, wide stream scan, stdio variadic, three standard-stream cases, file streams, narrow formatting/strings, wcsrtombs and its secure form, collate, locale and rune locale.

This checks the libc suite listed above, not the full Windows 34-test regex or all CI. No game, GPU or performance claim. Local logs and the exact-head manifest are retained for follow-up.

AI-assisted: yes (Codex).

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 23, 2026
@JJenkx
JJenkx force-pushed the windows/printf-decimal-console branch from d9653a1 to 5291860 Compare October 23, 2026 07:26
@JJenkx

JJenkx commented Oct 23, 2026

Copy link
Copy Markdown
Contributor Author

Refreshed with the rest of the formatting stack: 5291860811 on current main 0db47b263d. No conflicts, and git range-diff shows every commit unchanged. Conventions and git diff --check pass. Windows Release, ctest -V three times: guest_formatting and guest_wide_formatting (2/2) pass in each run. Stack details are in #9958.

@JJenkx

JJenkx commented Oct 23, 2026

Copy link
Copy Markdown
Contributor Author

Merged the updated #9699 head (ac59a74011, which carries main 21554447d0) as 2f7dcf59c7 to resolve a conflict in docs/dev/debt/libc.md:

  • The narrow printf entry keeps this PR's "or floating-point conversion" in place of the %a/NaN clause, on top of main's current wording.
  • The %e, %f and %g entry is unchanged.

Windows run on 2f7dcf59c7 (MinGW-w64 GCC 15.2 UCRT, Release): guest_formatting and guest_wide_formatting passed in 3 of 3 runs, and tools/check_conventions.py passes.

@Dextroid17

Copy link
Copy Markdown
Contributor

Linux run of the current head 2f7dcf59c7 on its own and merged into main. JJenkx's Linux rerun in #9958 is on 89f761f7ab, which contains this head plus #9927 and #9958. What this adds is a check that the new tests catch the bug, and the full suite on the merge.

Setup: CachyOS, kernel 7.3.0-4, GCC 16.2.1, glibc 2.44, CMake 4.4.3, Ninja, -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTING=ON -DANYPS5_ENABLE_SPIRV_TOOLS=OFF. One worktree is at the exact head, and the other has it merged into main at d284c98b2e (735 commits ahead of the branch, clean merge; the 42 later main commits don't touch libc).

  • Tests catch the bug. On the head, I reverse-applied only this PR's commit 5291860811 to libc/include/WindowsFormatting.hpp and libc/src/Formatting.cpp, keeping its test changes and fix(libc): format %a like the console on Windows #9699's commits. CheckHexFloat still passes, then guest_formatting aborts on the first decimal case: [%.0f] of 0x1p-1 gave "[0]", expected "[1]". Restoring the commit makes both tests pass again. guest_wide_formatting passes either way, as expected while the wide path is in fix(libc): format wide %a, %e, %f and %g with the narrow console rules #9927.
  • Focused suite. ctest -R '^guest_(wide_)?formatting$' -V --no-tests=error --repeat until-fail:3 --timeout 120 passes both tests in each of 3 runs, on both trees.
  • Full suite on the merge. I built everything and ran ctest -j8: 1,971 passed, 26 skipped and 3 failed of 2,000. All 616 guest_* tests that ran passed, including every libc formatting, scanning, stream, wide and string test; the 6 that skipped are video-out tests. The 3 failures are the agc_driver_sample_order* tests on my RTX 3080 Ti. They fail the same way on plain main d284c98b2e (3 runs each), and on lavapipe, the CI device, they pass or skip.

No title, console or performance run.

AI-assisted: yes (Claude Code).

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 24, 2026
JJenkx and others added 4 commits October 24, 2026 16:14
The Windows printf family handed %a and %A to the host, whose UCRT prints
a subnormal unnormalised (0x0.0000000000001p-1022) and pads with zeros
even when the - flag is given (%-010a of 1.5 gave 0x001.8p+0). The
libc.prx that titles ship normalises subnormals (0x1p-1074), rounds a
shorter precision half up without renormalising a carry, and lets -
override 0, as C requires.

Finite doubles are now formatted here, checked against three titles'
copies of libc.prx run side by side.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
JJenkx and others added 3 commits October 24, 2026 16:14
Linux handed a format without wide arguments, %m or %p to glibc, so %a
there still printed subnormals unnormalised and rounded hexadecimal
digits half to even, and CheckHexFloat, which runs on every host,
failed on Linux. Route %a and %A to FormatWindows on Linux as well.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The libc debt entry for %a and %A still described the Windows-only
formatter and said Linux hands %a to glibc; since Linux routes %a and %A
to the same formatter, describe it for both platforms.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The printf family formatted floating-point conversions through the host,
which prints every exact digit and rounds ties to even. The libc.prx
that titles ship keeps the exact digits only down to a position derived
from the binary exponent and prints zeros after them, rounds half up on
the kept digits, and rounds an exact tie down once that exponent
estimate reaches 8. So %.0f of 2.5 is 3 and %.1f of 0.25 is 0.3 there,
and %.20f of 0.1 is 0.10000000000000000555.

Format finite double and long double e, f and g conversions with those
rules from exactly generated digits, on every host. On 600,000 random
formats every output with up to 17 significant digits now matches the
copies bundled with PPSA07923, PPSA05916 and PPSA13934; longer outputs
still differ where the console's extended-precision digits end one unit
lower.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@JJenkx
JJenkx force-pushed the windows/printf-decimal-console branch from 2f7dcf5 to 182bd6a Compare October 24, 2026 20:26
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 24, 2026
@oneandonlydean
oneandonlydean merged commit 5e67516 into boykopovar:main Oct 25, 2026
18 of 19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants