Repository navigation
libs/libc/stream: Add %a, %b and the C23 lengths to printf(), and fix -0.0. - #21505
Merged
Merged
Conversation
This was referenced Nov 6, 2026
casaroli
force-pushed
the
printf-output-up
branch
from
November 6, 2026 07:44
6dc3a23 to
1d4849f
Compare
|
casaroli
force-pushed
the
printf-output-up
branch
from
November 6, 2026 12:13
1d4849f to
fb576b1
Compare
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]>
casaroli
force-pushed
the
printf-output-up
branch
from
November 8, 2026 12:32
fb576b1 to
94552bd
Compare
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]>
xiaoxiang781216
approved these changes
Nov 8, 2026
acassis
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]>
This was referenced Nov 8, 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]>
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.
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:
-nan, as C23 requires.%aand%A, behindLIBC_PRINTF_HEXFLOAT, off in Minimal and Default, on in Full. With the option off,printfreads the argument and prints?, as in libs/libc/stream: Fix the crashes and overflows of printf() for correct calls. #21474. The code works on the bits, with no floating point operation and no big numbers, so the option does not needLIBC_FLOATINGPOINTand pulls in no soft-float code. Along doublehas its own digits where it has more bits than adouble.%b,%B,wNandwfN, behindLIBC_PRINTF_C23, same defaults. With the option off, they stay unknown conversions, as in libs/libc/stream: Fix the crashes and overflows of printf() for correct calls. #21474: the rest of the format prints as text.%nwith a store, behindLIBC_PRINTF_PERCENT_N, off in all profiles, also in Full. The prompt and the help say that it is dangerous. With the option off,%nreads its pointer, stores nothing and prints?, as in libs/libc/stream: Fix the crashes and overflows of printf() for correct calls. #21474.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
%e,%f,%g. That is step 4 of [FEATURE] printf conformance: never crash, and options for flash size #21467, after a discussion there.%#gdrops trailing zeros: 917 tests of the suite.scanf:%Lmoves to its own PR.Impact
Default: the only output that changes is the sign of -0.0 and of a negative NaN. A config with
LIBC_FLOATINGPOINToff 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:The cost of
-0.0, image text, float on in both builds:-0.0Image text with float off is the same as #21493 on all four boards in Default:
One option added, image text:
LIBC_PRINTF_HEXFLOATLIBC_PRINTF_HEXFLOATLIBC_PRINTF_C23LIBC_PRINTF_C23LIBC_PRINTF_PERCENT_Nvsprintf_internalframe:%a%aThe growth with the options on comes from new code:
lib_hexfloat.c, its output invsprintf_puthex(), and the parser ofwN. WithLIBC_PRINTF_C23on, the conversion buffer holds all bits of an integer, so the frame ofvsprintf_internalgrows by up to 42 bytes. WithLIBC_PRINTF_HEXFLOATon, the frame holds the digits, andlib_vsprintf()with numbered arguments holds along doublefor each argument where that is larger than adouble.Testing
The suite at https://github.com/casaroli/printf-report on sim arm64, against #21493:
%aand C23 on%non%aand C23 onNo 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
%wf16tests that fail are notprintf:int_fast16_tisint, andINT_FAST16_MAXininclude/stdint.hisINT16_MAX. The%ntests fail by design with the option off.%aagainst 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 ofdoubleand 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.rv-virt:nsh(flat) andqemu-armv8a:knsh(kernel), each with Default, Full, Minimal, Minimal with%aand C23, Default with%a, Default with C23, Full with%n: all exit 0 with 0 failures.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
scanfcommits moved to their own PR: ostest exits 0 onqemu-armv8a:knshandqemu-armv8a:nsh, with the printf test of nuttx-apps; the patches of the other commits are unchanged.Not run: - Cortex-M0 at run time.
tools/checkpatch.sh -c -u -m -gpasses.