[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-Id: <20260206191121.3602-1-david.laight.linux@gmail.com>
Date: Fri, 6 Feb 2026 19:11:10 +0000
From: david.laight.linux@...il.com
To: Willy Tarreau <w@....eu>,
Thomas Weißschuh <linux@...ssschuh.net>,
linux-kernel@...r.kernel.org,
Cheng Li <lechain@...il.com>
Cc: David Laight <david.laight.linux@...il.com>
Subject: [PATCH v2 next 00/11] tools/nolibc: Enhance printf()
From: David Laight <david.laight.linux@...il.com>
Update printf() so that it handles almost all the non-fp formats.
In particular:
- Left alignment.
- Zero padding.
- Field precision.
- Variable field width and precision.
- Width modifiers q, L, t and z.
- Conversion specifiers i and X (X generates lower case).
About the only things that are missing are octal and floating point.
The tests are updated to match.
There is a slight increase in code size, but it is minimalised
by the the heavy use of bit-pattern matches.
vfprintf() is modified to buffer data for each call and do a single
write system call at the end.
This improves performance somewhat, but is actually most useful when
using strace.
Final bloat-o-meter output for nolibc-test using gcc 12.2 on x86-64:
add/remove: 2/2 grow/shrink: 8/2 up/down: 2189/-320 (1869)
Function old new delta
run_printf 1688 3121 +1433
utoa_r - 144 +144
u64toa_r - 144 +144
__nolibc_fprintf_cb 58 202 +144
expect_vfprintf 362 435 +73
__nolibc_printf 1081 1148 +67
prepare 600 657 +57
__nolibc_sprintf_cb 58 101 +43
printf 199 241 +42
dprintf.constprop 215 257 +42
snprintf.constprop 229 204 -25
result 157 107 -50
puts.isra 101 - -101
utoa_r.isra 144 - -144
Total: Before=32966, After=34835, chg +5.67%
u64toa_r() was previously inlined into __nolibc_printf() so
__nolibc_printf() actually increases by about 200 bytes.
The largest increases come from supporting variable field widths ("%*d")
(mostly the extra va_arg() call), the subtle difference between zero pad
("%06d") and field precision ("%6.6d"), and the special rules for zero.
The 50 bytes saved from result() are from changing it to use "%.*s".
This offsets most of the cost of supporting variable widths.
(It is also removes the only call to puts().)
Changes for v2:
Mostly changes to improve the readability of the code.
- New patch #1 inserted to rename the variable 'c' to 'ch'.
- Use #define 'magic' for the bit-masks that check multiple characters.
The check for the conversion flag characters is then based on:
ch_flag = _NOLIBC_PF_CHAR_IS_ONE_OF(ch, ' ', '#', '+', '-', '0');
- Re-order the changes so that the old patch 10 (Use bit-pattern for
integral formats) is done at the same time as bit-masks are used for
the flags characters and length modifiers.
This means the restructuring changes are done before new features are
added.
- Put all the changes to the selftest together at the end.
There is one extra test for ("%#01x", 0x1234) (should be "0x1234")
which is problematic because once you've removed the length of the "0x"
from the field width there are -1 character postions for the digits.
David Laight (11):
tools/nolibc/printf: Change variable used for format chars from 'c' to
'ch'
tools/nolibc/printf: Move snprintf length check to callback
tools/nolibc/printf: Add buffering to vfprintf() callback.
tools/nolibc/printf: Output pad characters in 16 byte chunks
tools/nolibc/printf: Simplify __nolibc_printf()
tools/nolibc/printf: Use bit-masks to hold requested flag, length and
conversion chars
tools/nolibc/printf: Add support for conversion flags "#- +" and
format "%X"
tools/nolibc/printf: Add support for zero padding and field precision
selftests/nolibc: Improve reporting of vfprintf() errors
selftests/nolibc: Increase coverage of printf format tests
selftests/nolibc: Use printf("%.*s", n, "") to align test output
tools/include/nolibc/stdio.h | 445 ++++++++++++++-----
tools/testing/selftests/nolibc/nolibc-test.c | 97 ++--
2 files changed, 386 insertions(+), 156 deletions(-)
--
2.39.5
Powered by blists - more mailing lists