Repository navigation
libs/libc/stream: Print floating point on the bits with integers in printf(). - #21507
Merged
Merged
Conversation
casaroli
force-pushed
the
printf-engine-up
branch
from
November 6, 2026 07:44
586374b to
e2355f3
Compare
|
casaroli
force-pushed
the
printf-engine-up
branch
from
November 6, 2026 12:13
e2355f3 to
7c842df
Compare
Contributor
|
@casaroli please fix conflict |
casaroli
force-pushed
the
printf-engine-up
branch
from
November 8, 2026 12:32
7c842df to
1f7fc81
Compare
Contributor
Author
|
Done. |
casaroli
added a commit
to casaroli/nuttx
that referenced
this pull request
Nov 8, 2026
The page explains how printf() works in NuttX: each conversion, flag, length, width and precision with examples and their output, each option and profile and what it prints when it is off, what happens on an unknown conversion and on a truncated format, what is correct and wrong usage, the flash and stack cost, and what is outside the guarantee. It describes the code of apache#21505 and apache#21507: the options LIBC_PRINTF_HEXFLOAT, LIBC_PRINTF_C23, LIBC_PRINTF_PERCENT_N, LIBC_PRINTF_EXACT and LIBC_PRINTF_EXACT_LDOUBLE, and %e, %f and %g with 17 significant digits and no floating point operation. The output in the examples comes from a run of the same calls on NuttX, in Default and in Full. Part of apache#21467 Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]>
casaroli
force-pushed
the
printf-engine-up
branch
from
November 8, 2026 13:11
1f7fc81 to
98affa9
Compare
xiaoxiang781216
previously approved these changes
Nov 8, 2026
xiaoxiang781216
previously approved these changes
Nov 8, 2026
casaroli
added a commit
to casaroli/nuttx
that referenced
this pull request
Nov 8, 2026
The page explains how printf() works in NuttX: each conversion, flag, length, width and precision with examples and their output, each option and profile and what it prints when it is off, what happens on an unknown conversion and on a truncated format, what is correct and wrong usage, the flash and stack cost, and what is outside the guarantee. It describes the code of apache#21505 and apache#21507: the options LIBC_PRINTF_HEXFLOAT, LIBC_PRINTF_C23, LIBC_PRINTF_PERCENT_N, LIBC_PRINTF_EXACT and LIBC_PRINTF_EXACT_LDOUBLE, and %e, %f and %g with 17 significant digits and no floating point operation. The output in the examples comes from a run of the same calls on NuttX, in Default and in Full. Part of apache#21467 Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]>
vsprintf_internal() had the output of %e, %f and %g in its body, and it held the digits of the floating point engine in its own frame, also for %d. Move this code to vsprintf_float() and vsprintf_putfloat(). Now the digits are in the frame of vsprintf_float(), and later commits can call the output with digits from another place. The output does not change. Part of apache#21467 Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]>
The engine of %e, %f and %g scaled the value with double operations. It had 15 significant digits, it did not round %.0f, and the last digits could be wrong. On a CPU with no FPU it pulled in the soft-float code of libgcc: up to 8 KB on Cortex-M0, more than the rest of printf. Replace it with an engine that takes the sign, the exponent and the mantissa from the bits. It has no floating point operation. A value below 2^63 with at most 60 bits after the point is exact in 64 bits, so its digits and its ties are correct: 2.5, 99.5, 1.125. Any other value is scaled by a table of 10^(2^i) in a 64-bit mantissa, and gives 17 correct digits in almost all cases. This table has 180 bytes in place of 272. The output changes in Default. The digits of %e, %f and %g are better at each precision, %.0f rounds, and up to 17 digits are significant. %g now uses the precision of the format to select its form. The code handles binary64 and binary32 by DBL_MANT_DIG, for both byte orders and for a 16-bit int. All 64-bit shifts have a constant count. Part of apache#21467 Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]>
Add LIBC_PRINTF_EXACT, on in the Full profile and off in the Minimal and Default profiles. With it, all digits of %e, %f and %g of a double are exact and correctly rounded, to any precision. It is the same engine with one more step. The engine tracks a bound of its error, and it gives up when the distance from a tie is not above the bound, or when more digits are needed than it has. Then vsprintf_float_big() runs: a function of its own, so only these rare calls have the big numbers on the stack. The big numbers are 16-bit limbs, so that each product fits in 32 bits on any CPU. The integer part is in base 10000 and the fraction is in base 65536. The digits are not held in a buffer. A first walk over them finds the exponent, how they round and how many count. A second walk gives them to the output. %.1074f therefore needs no 1 KB buffer in each frame, at the cost of the time of two walks. Part of apache#21467 Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]>
%Le, %Lf and %Lg convert the value to a double, so they have the precision and the range of a double. LDBL_MAX prints as inf. Add LIBC_PRINTF_EXACT_LDOUBLE, on in the Full profile and off in the Minimal and Default profiles, for binary128 and the 80-bit format of x86. With it, all digits are exact. It uses the big numbers of LIBC_PRINTF_EXACT, with 1240 limbs in place of 78. The limbs are in vsprintf_ldouble(), a function that only %L calls. So a call without %L has no cost in stack. A call with %L needs about 2.7 KB more. With numbered arguments on, the union of the arguments holds a long double. Part of apache#21467 Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]>
The # flag of %g keeps the trailing zeros, but printf() removed them in all cases: %#g of 1.0 printed "1." where C wants "1.00000", and %#g of 1e-5 printed "1.e-05" where C wants "1.00000e-05". With the flag, vsprintf_putfloat() does not remove the zeros, with or without LIBC_PRINTF_EXACT. It prints P - (X + 1) digits after the point for the f style and P - 1 for the e style, where P is the precision (0 as 1) and X the exponent. With LIBC_PRINTF_ALT_FORM off the compiler removes this code. Part of apache#21467 Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]>
C says that a negative precision argument is the same as no precision. printf() took it as 0. %.*d of 0 with -1 printed nothing, %.*s with -2 printed nothing, %.*f with -1 printed no decimals, and %0*.*d lost the 0 flag. Part of apache#21467 Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]>
C wants a negative value from fprintf() on an output error. vfprintf() returned the length of the conversion, also when the stream wrote nothing: for example on a stream that is open for reading only, or on an unbuffered stream whose write() fails. The stream counts what it wrote, so compare that count with the length. A buffered stream still reports a write error only when it flushes. Part of apache#21467 Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]>
casaroli
force-pushed
the
printf-engine-up
branch
from
November 8, 2026 18:22
374a600 to
a28e609
Compare
casaroli
marked this pull request as ready for review
November 8, 2026 18:22
acassis
approved these changes
Nov 8, 2026
xiaoxiang781216
approved these changes
Nov 9, 2026
acassis
pushed a commit
that referenced
this pull request
Nov 9, 2026
The page explains how printf() works in NuttX: each conversion, flag, length, width and precision with examples and their output, each option and profile and what it prints when it is off, what happens on an unknown conversion and on a truncated format, what is correct and wrong usage, the flash and stack cost, and what is outside the guarantee. It describes the code of #21505 and #21507: the options LIBC_PRINTF_HEXFLOAT, LIBC_PRINTF_C23, LIBC_PRINTF_PERCENT_N, LIBC_PRINTF_EXACT and LIBC_PRINTF_EXACT_LDOUBLE, and %e, %f and %g with 17 significant digits and no floating point operation. The output in the examples comes from a run of the same calls on NuttX, in Default and in Full. Part of #21467 Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]>
1 task done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The documentation of these options is in #21506, which merges after this PR. Part of #21467, step 4.
Summary
The engine of
%e,%fand%gscaled the value withdoubleoperations. It had 15 significant digits, it did not round%.0f, and the last digits could be wrong. On a CPU with no FPU it pulled in the soft-float code of libgcc.%.0frounds, and ties are correct where the value is exact in 64 bits. It handles binary64 and binary32, in both byte orders.LIBC_PRINTF_EXACT, off in Minimal and Default, on in Full. All digits of adoubleare correctly rounded, at any precision. The fast path proves its digits with an error bound. Only other values takevsprintf_float_big(), a function of its own.LIBC_PRINTF_EXACT_LDOUBLE, off in Minimal and Default, on in Full. The same forlong double, invsprintf_ldouble(), a function that only%Lcalls.Three more commits fix faults that a test against GNU libc found:
%#gof 1.0 printed1.where C wants1.00000. With the#flag the trailing zeros now stay, with and withoutLIBC_PRINTF_EXACT.*counted as 0, so%.*dof 0 with -1 printed nothing. It now means no precision, as C says.fprintf()returned the length also when the stream wrote nothing, for example an unbuffered stream whosewrite()fails. It now returnsEOF. A buffered stream reports the error when it flushes.Fixes #10012.
lib_dtoa_engine.candlib_dtoa_data.cgo away. The new files are under Apache-2.0.Impact
The digits of
%e,%fand%gchange in Default. They are nearer to the correct result at each precision. For example%.1fof 2.25 was2.3and is now2.2. A program or test that compares an exact string can see a change.%gnow uses the precision of the format to select its form.%#gand a negative precision from*now print the C result.fprintf(),printf(),vfprintf()andvprintf()now returnEOFwhen the stream does not take all of the output. No ABI change. No architecture code. Flat, protected and kernel builds.Image text, with floating point on in both builds. Boards:
nucleo-f072rb:nsh,nucleo-f103rb:nsh,rv-virt:nsh,qemu-armv8a:nsh:LIBC_PRINTF_EXACTaddsLIBC_PRINTF_EXACT_LDOUBLEaddsThe table is without the last 3 commits. They add 112 bytes on Cortex-M0 with floating point on, and 20 bytes with it off.
With floating point off, the image text is the same as #21505 on Cortex-M0 and Cortex-M3. The engine is larger than the old table code on CPUs with an FPU or with the soft-float code linked anyway. libgcc in the image, floating point on, Cortex-M0: 10,432 before, 7,376 now. Most of the rest comes from
/proc/uptime: 2,028 with the integer path in the uptime PR.Stack frames, in bytes, with both options on:
vsprintf_float()__dtoa_engine()vsprintf_putfloat()vsprintf_float_big()vsprintf_ldouble()Testing
Accuracy, against the printf of the host (Apple libc), under ASan and UBSan, in
%eand%fat each precision over random bits, decimals, integers, powers of ten and ties:LIBC_PRINTF_EXACT: 9,109,842 conversions ofdouble, 0 differences; 9,388,638 in the binary32 path, 0 differences.%e.%fof large values stops at 17 digits.LIBC_PRINTF_EXACT_LDOUBLE, against glibc: binary128 on arm64 (15,624 runs) and s390x (7,074), and x87 on x86_64 (10,296): 0 differences.The same vectors give the same hash on arm64, s390x (big endian), x86_64 and AVR with a 64-bit
double, and on arm64 and AVR with a 32-bitdouble, with and without the option. z80 with sdcc: compile only.printf-report suite, C99 and correctly rounded:
%aand C23 on%a, C23 and exact on0 tests go from pass to fail, and 0 crashes. The rest is mainly
%#g. The table is without the last 3 commits.-Wno-cpp -Werror:nucleo-f072rb:nsh,nucleo-f103rb:nsh,rv-virt:nsh,qemu-armv8a:nsh,mps2-an500:knsh,qemu-armv8a:knsh,rv-virt:knsh, 48 builds in all with the profiles and options: all pass.qemu-armv8a:knsh, Default and Full:%#gand%#Gagainst GNU libc 2.43, 714 cases: 280 (Default) and 335 (Full) differences before, 0 after. GNU libc itself drops the zeros in 4 cases where the rounding carries into the e style (%#gof 999999.5); there the test takes the C result, which macOS libc also gives. A negative precision from*and thefprintf()result: 9 of 17 cases fail before, 0 after. The printf test of nuttx-apps passes. ostest knsh and nsh: 0 failures.tools/checkpatch.sh: pass.Not run: hardware, a CMake build, a protected build at run time.