Skip to content

libs/libc/stream: Add %a, %b and the C23 lengths to printf(), and fix -0.0. - #21505

Merged
acassis merged 6 commits into
apache:masterfrom
casaroli:printf-output-up
Nov 8, 2026
Merged

acassis merged 6 commits into
apache:masterfrom
casaroli:printf-output-up

Conversation

@casaroli

@casaroli casaroli commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Part of #21467, step 3.

Summary

This PR fixes the output of correct calls that #21474 only made safe, and adds the missing conversions behind options:

One commit only fixes the style of lib_dtoa_engine.c, because the check reads all of a file that a commit changes.

What is not fixed

Impact

Default: the only output that changes is the sign of -0.0 and of a negative NaN. A config with LIBC_FLOATINGPOINT off has the same image text as #21493. No ABI change. Not a breaking change. No architecture code. Flat, protected and kernel builds.

The Kconfig help has the text of the options. Step 5 of #21467 is the documentation page.

Image text, each board's defaults; rv32 and arm64 have float on by default. Boards: nucleo-f072rb:nsh, nucleo-f103rb:nsh, rv-virt:nsh, qemu-armv8a:nsh:

Cortex-M0 Cortex-M3 rv32 arm64
#21493 46,724 34,436 230,438 328,272
Minimal 46,380 33,860 227,988 325,584
Default 46,724 34,436 230,462 328,268
Full 58,856 40,460 233,046 331,896
Default - #21493 +0 +0 +24 -4

The cost of -0.0, image text, float on in both builds:

Cortex-M0 Cortex-M3 rv32 arm64
#21493 56,856 38,596 230,438 328,272
With -0.0 56,856 38,604 230,462 328,268
Delta +0 +8 +24 -4
All of this PR, Default +0 +8 +24 -4

Image text with float off is the same as #21493 on all four boards in Default:

Cortex-M0 Cortex-M3 rv32 arm64
#21493 46,724 34,436 228,362 326,044
This PR 46,724 34,436 228,362 326,044

One option added, image text:

Cortex-M0 Cortex-M3 rv32 arm64
Default, float off, + LIBC_PRINTF_HEXFLOAT +816 +776 +1112 +1580
Minimal + LIBC_PRINTF_HEXFLOAT +836 +820 +1048 +1472
Default, float off, + LIBC_PRINTF_C23 +184 +180 +220 +232
Minimal + LIBC_PRINTF_C23 +160 +228 +166 +192
Default, float off, + LIBC_PRINTF_PERCENT_N +28 +48 +48 +36

vsprintf_internal frame:

Cortex-M0 Cortex-M3 rv32 arm64
#21493, Default 88 72 144 160
Default 88 72 144 160
Minimal 80 72 96 160
Full 184 176 208 256
Default, float off, + %a 112 104 144 192
Default, float off, + C23 128 112 112 176
Minimal + %a 112 104 128 192

The growth with the options on comes from new code: lib_hexfloat.c, its output in vsprintf_puthex(), and the parser of wN. With LIBC_PRINTF_C23 on, the conversion buffer holds all bits of an integer, so the frame of vsprintf_internal grows by up to 42 bytes. With LIBC_PRINTF_HEXFLOAT on, the frame holds the digits, and lib_vsprintf() with numbered arguments holds a long double for each argument where that is larger than a double.

Testing

The suite at https://github.com/casaroli/printf-report on sim arm64, against #21493:

C99 required Correctly rounded C23 POSIX XSI Bit-exact Crash + hang
#21493 67.81% 63.11% 1.08% 92.68% 36.36% 61.46% 0
This PR, options off 73.84% 69.14% 2.15% 92.68% 36.36% 68.96% 0
This PR, %a and C23 on 94.85% 90.08% 92.47% 95.12% 36.36% 90.08% 0
The same, %n on 94.89% 90.12% 96.77% 100% 36.36% 90.14% 0
Full profile 94.85% 90.08% 92.47% 95.12% 36.36% 90.08% 0
Minimal 5.50% 5.50% 1.08% 27.91% 33.33% 5.52% 0
Minimal, %a and C23 on 24.17% 24.17% 75.27% 27.91% 33.33% 24.32% 0

No test goes from pass to fail. 540 tests go from "allowed alternative" to "pass": a NaN with a sign bit prints its sign. The 3 %wf16 tests that fail are not printf: int_fast16_t is int, and INT_FAST16_MAX in include/stdint.h is INT16_MAX. The %n tests fail by design with the option off.

  • %a against GNU libc 2.41: 3,123,276 conversions with precision none and 0 to 32, flags and widths, on boundary, tie, subnormal and random values of double and binary128: 0 differences. 471,276 of them also on big-endian s390x: 0 differences. macOS libc does not round ties to even and normalizes subnormal values, so it is not a reference. The 80-bit format of x86: 0 value differences without a precision, and with a precision not compared, because GNU libc has another first digit.
  • Host harness under ASan and UBSan, 49 builds, clang on macOS and gcc on Linux arm64: each profile, each option added or removed: clang on macOS, 49 builds: 49 builds correct usage: 0 reports, 0 timeouts, 0 bad returns, 0 sentinel errors, 49 builds wrong usage: 0 reports, 0 timeouts, 0 bad returns, 0 sentinel errors. gcc on Linux arm64, 49 builds: 49 builds correct usage: 0 reports, 0 timeouts, 0 bad returns, 0 sentinel errors, 49 builds wrong usage: 0 reports, 0 timeouts, 0 bad returns, 0 sentinel errors..
  • Runtime: ostest with the new cases of the test in nuttx-apps, and a test program: rv-virt:nsh (flat) and qemu-armv8a:knsh (kernel), each with Default, Full, Minimal, Minimal with %a and C23, Default with %a, Default with C23, Full with %n: all exit 0 with 0 failures.
  • Builds with EXTRAFLAGS="-Wno-cpp -Werror": Minimal, Default, Full on the four flat boards (12 of 12), each option added on them. mps2-an500:knsh (protected), qemu-armv8a:knsh, rv-virt:knsh (kernel), each profile and Minimal with all options: 12 of 12 on this head.

Rebased on master 1df6401 after #21493 merged, with the two scanf commits moved to their own PR: ostest exits 0 on qemu-armv8a:knsh and qemu-armv8a:nsh, with the printf test of nuttx-apps; the patches of the other commits are unchanged.

Not run: - Cortex-M0 at run time.

  • A CMake build.
  • The suite on a 32-bit target: only the sim runs it here.

tools/checkpatch.sh -c -u -m -g passes.

@github-actions github-actions Bot added 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. labels Nov 6, 2026
@github-actions

github-actions Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

esp32-devkitc

  • ROM: .flash.text -20 B (-0.0%, 129,332 B / 4,194,272 B, total: 3% used)
  • irom0_0_seg: .flash.text -20 B (-0.0%, 95,180 B / 3,342,304 B, total: 3% used)

qemu-armv8a

  • Code: .text.__dtoa_engine -4 B (-0.0%, 360,948 B)

s698pm-dkit

  • Code: .text +32 B (+0.0%, 389,904 B)

No memory changes detected for:


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

Add the braces that nxstyle asks for.  The next commit changes this file,
and the check reads all of a file that a commit changes.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
The floating point engine tested the sign with x < 0, and this is false
for -0.0.  %f, %e and %g of -0.0 printed "0.000000", "0.000000e+00" and
"0", and %+f printed "+0.000000".  C requires the sign.

Test the sign bit of the value.  A compare needs a call of the soft-float
code on a CPU with no FPU, and a test of one byte needs no call, so the
object is smaller there.  A byte works on every compiler and for each
size of double.

A NaN with its sign bit set now prints "-nan", as the standard allows and
as C23 requires.

Part of apache#21467

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
printf() read the argument of %a and printed "?".  Add the option
LIBC_PRINTF_HEXFLOAT.  It is on in the Full profile, and off in the
Minimal and Default profiles, which keep "?".

The conversion takes the sign, the exponent and the mantissa from the
bits of the value, and rounds on the hexadecimal digits to nearest, ties
to even.  It needs no floating point operation and no big numbers, so it
does not depend on LIBC_FLOATINGPOINT and it pulls in no soft-float code.
All 64-bit shifts have a constant count.

A long double has its own digits where it has more bits than a double:
binary128, and the 80-bit format of x86.  With numbered arguments on, the
union of the arguments then also holds a long double.  %a of a subnormal
value has the leading digit 0 and the smallest exponent, as in GNU libc.

Part of apache#21467

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
printf() took %b and %B as unknown conversions.  Add the option
LIBC_PRINTF_C23, on in the Full profile, and off in the Minimal and
Default profiles.  With the option off they stay unknown conversions: the
rest of the format prints as text and no argument is read.

The conversion prints the integer in binary, with the prefix 0b or 0B for
the # flag.  The conversion buffer has room for all bits of the integer
when the option is on.

Part of apache#21467

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
With the option LIBC_PRINTF_C23 on, printf() takes the length modifiers
wN and wfN for the sizes 8, 16, 32 and 64, as in %w64d and %wf16x.  The
parser sets the flags of the standard length with this size, so the
argument, the conversion and the numbered arguments work as for that
length.  LIBC_PRINTF_SHORT and LIBC_PRINTF_LONG_LONG apply in the same way.

Any other size stays an unknown conversion.  With the option off, all of
them are unknown conversions, so %w64d prints as text.

Part of apache#21467

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
printf() reads the pointer of %n, stores nothing and prints "?", because a
format then cannot write to memory.  A program that needs the count can turn
on LIBC_PRINTF_PERCENT_N.  With it, %n stores the count at the size of its
length modifier, and prints nothing.  A NULL pointer stores nothing.

The option is off in all profiles, also in Full, and its prompt and its help
say that it is dangerous.

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 Size: L The size of the change in this PR is large and removed Size: XL The size of the change in this PR is very large. Consider breaking down the PR into smaller pieces. labels Nov 8, 2026
@casaroli
casaroli marked this pull request as ready for review November 8, 2026 12:33
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 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]>
@acassis
acassis merged commit 6d1fa23 into apache:master Nov 8, 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: L The size of the change in this PR is large

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants