Skip to content

libs/libc/stream: Print floating point on the bits with integers in printf(). - #21507

Merged
xiaoxiang781216 merged 7 commits into
apache:masterfrom
casaroli:printf-engine-up
Nov 9, 2026
Merged

xiaoxiang781216 merged 7 commits into
apache:masterfrom
casaroli:printf-engine-up

Conversation

@casaroli

@casaroli casaroli commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

The documentation of these options is in #21506, which merges after this PR. Part of #21467, step 4.

Summary

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.

  • A new engine on the bits, in all builds with floating point. It uses integers only: no floating point operation. Up to 17 significant digits, %.0f rounds, 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 a double are correctly rounded, at any precision. The fast path proves its digits with an error bound. Only other values take vsprintf_float_big(), a function of its own.
  • LIBC_PRINTF_EXACT_LDOUBLE, off in Minimal and Default, on in Full. The same for long double, in vsprintf_ldouble(), a function that only %L calls.
  • The first commit only moves code, with no output change.

Three more commits fix faults that a test against GNU libc found:

  • %#g of 1.0 printed 1. where C wants 1.00000. With the # flag the trailing zeros now stay, with and without LIBC_PRINTF_EXACT.
  • A negative precision from * counted as 0, so %.*d of 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 whose write() fails. It now returns EOF. A buffered stream reports the error when it flushes.

Fixes #10012.

lib_dtoa_engine.c and lib_dtoa_data.c go away. The new files are under Apache-2.0.

Impact

The digits of %e, %f and %g change in Default. They are nearer to the correct result at each precision. For example %.1f of 2.25 was 2.3 and is now 2.2. A program or test that compares an exact string can see a change. %g now uses the precision of the format to select its form. %#g and a negative precision from * now print the C result. fprintf(), printf(), vfprintf() and vprintf() now return EOF when 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:

Cortex-M0 Cortex-M3 rv32 arm64
#21505 56,856 38,604 230,462 328,268
This PR 54,424 36,684 231,272 329,284
Delta -2,432 -1,920 +810 +1,016
LIBC_PRINTF_EXACT adds +1,528 +1,360 +1,624 +1,912
LIBC_PRINTF_EXACT_LDOUBLE adds +0 +0 +162 +324

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

Cortex-M0 Cortex-M3 rv32 arm64
vsprintf_float() 88 96 96 112
__dtoa_engine() 104 88 112 64
vsprintf_putfloat() 72 80 96 128
vsprintf_float_big() 256 256 256 288
vsprintf_ldouble() - - 2,624 2,624

Testing

Accuracy, against the printf of the host (Apple libc), under ASan and UBSan, in %e and %f at each precision over random bits, decimals, integers, powers of ten and ties:

  • LIBC_PRINTF_EXACT: 9,109,842 conversions of double, 0 differences; 9,388,638 in the binary32 path, 0 differences.
  • Default: 100% of ties, and 99.6% or more at each precision up to 17 digits in %e. %f of 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-bit double, with and without the option. z80 with sdcc: compile only.

printf-report suite, C99 and correctly rounded:

#21505 This PR
Default 73.84 / 69.14 75.73 / 72.73
%a and C23 on 94.85 / 90.08 96.74 / 93.74
%a, C23 and exact on 97.39 / 97.38

0 tests go from pass to fail, and 0 crashes. The rest is mainly %#g. The table is without the last 3 commits.

  • Host harness: 58 builds with clang and 59 with gcc, under ASan and UBSan, all profiles and options: 0 reports. The correct-usage cases that differ are only in builds with an option off.
  • Runtime: ostest and the printf test on rv32 flat and on arm64 kernel, in Default, Full, Default + exact, Default + both options and Minimal + float: all pass, 0 failures. Cortex-M0 under Renode: the printf test and a test of the digits, with the option on and off: all pass.
  • Builds with -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.
  • The last 3 commits, on qemu-armv8a:knsh, Default and Full: %#g and %#G against 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 (%#g of 999999.5); there the test takes the C result, which macOS libc also gives. A negative precision from * and the fprintf() 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.

@github-actions github-actions Bot added Area: Documentation Improvements or additions to documentation Size: XL The size of the change in this PR is very large. Consider breaking down the PR into smaller pieces. labels Nov 6, 2026
@github-actions

github-actions Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

arduino-mega2560

  • flash: .text -28 B (-0.0%, 66,559 B / 262,144 B, total: 25% used)

esp32-devkitc

  • ROM: .flash.rodata -88 B, .flash.text +624 B (+0.4%, 129,868 B / 4,194,272 B, total: 3% used)
  • drom0_0_seg: .flash.rodata -88 B (-0.6%, 13,836 B / 4,194,272 B, total: 0% used)
  • irom0_0_seg: .flash.text +624 B (+0.7%, 95,804 B / 3,342,304 B, total: 3% used)

hifive1-revb

  • flash: .text +12 B (+0.0%, 86,104 B / 4,194,304 B, total: 2% used)

mirtoo

  • kseg0_progmem: .text +12 B (+0.0%, 68,436 B / 131,072 B, total: 52% used)

qemu-armv8a

  • Code: .rodata -104 B, .text.__dtoa_engine +572 B, .text.vfprintf +16 B, .text.vsprintf_internal.constprop.0 -1,192 B (+0.4%, 362,236 B)

qemu-intel64

  • Code: .text +909 B (+0.0%, 8,695,012 B)
  • Data: .rodata -128 B (-0.1%, 126,521 B)

rx65n-rsk2mb

  • ROM: .text +32 B (+0.0%, 91,736 B / 2,097,152 B, total: 4% used)

s698pm-dkit

  • Code: .text +48 B (+0.0%, 389,952 B)

stm32-nucleo-f103rb

  • flash: .text +16 B (+0.0%, 34,908 B / 131,072 B, total: 27% used)

Catch memory regressions in CI with MemBrowse. Free for open source.

@acassis

acassis commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

@casaroli please fix conflict

@casaroli

casaroli commented Nov 8, 2026

Copy link
Copy Markdown
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]>
@github-actions github-actions Bot added Area: OS Components OS Components issues and removed Area: Documentation Improvements or additions to documentation labels Nov 8, 2026
xiaoxiang781216
xiaoxiang781216 previously approved these changes Nov 8, 2026
xiaoxiang781216
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
casaroli marked this pull request as ready for review November 8, 2026 18:22
@xiaoxiang781216
xiaoxiang781216 merged commit 8304648 into apache:master Nov 9, 2026
63 checks passed
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: OS Components OS Components issues Size: XL The size of the change in this PR is very large. Consider breaking down the PR into smaller pieces.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fprintf() does not return any errors.

3 participants