public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 next 00/11] tools/nolibc: Enhance printf()
@ 2026-02-06 19:11 david.laight.linux
  2026-02-06 19:11 ` [PATCH v2 next 01/11] tools/nolibc/printf: Change variable used for format chars from 'c' to 'ch' david.laight.linux
                   ` (11 more replies)
  0 siblings, 12 replies; 60+ messages in thread
From: david.laight.linux @ 2026-02-06 19:11 UTC (permalink / raw)
  To: Willy Tarreau, Thomas Weißschuh, linux-kernel, Cheng Li; +Cc: David Laight

From: David Laight <david.laight.linux@gmail.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


^ permalink raw reply	[flat|nested] 60+ messages in thread

end of thread, other threads:[~2026-02-18 22:57 UTC | newest]

Thread overview: 60+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-02-06 19:11 [PATCH v2 next 00/11] tools/nolibc: Enhance printf() david.laight.linux
2026-02-06 19:11 ` [PATCH v2 next 01/11] tools/nolibc/printf: Change variable used for format chars from 'c' to 'ch' david.laight.linux
2026-02-07 18:51   ` Willy Tarreau
2026-02-16 18:52   ` Thomas Weißschuh
2026-02-06 19:11 ` [PATCH v2 next 02/11] tools/nolibc/printf: Move snprintf length check to callback david.laight.linux
2026-02-07 19:12   ` Willy Tarreau
2026-02-07 23:28     ` David Laight
2026-02-08 15:12       ` Willy Tarreau
2026-02-08 22:49         ` David Laight
2026-02-06 19:11 ` [PATCH v2 next 03/11] tools/nolibc/printf: Add buffering to vfprintf() callback david.laight.linux
2026-02-07 19:29   ` Willy Tarreau
2026-02-07 23:36     ` David Laight
2026-02-16 19:07       ` Thomas Weißschuh
2026-02-17 11:51         ` David Laight
2026-02-18 17:52           ` Thomas Weißschuh
2026-02-06 19:11 ` [PATCH v2 next 04/11] tools/nolibc/printf: Output pad characters in 16 byte chunks david.laight.linux
2026-02-07 19:38   ` Willy Tarreau
2026-02-07 23:43     ` David Laight
2026-02-08 15:14       ` Willy Tarreau
2026-02-16 19:30   ` Thomas Weißschuh
2026-02-16 22:29     ` David Laight
2026-02-18 17:30       ` Thomas Weißschuh
2026-02-06 19:11 ` [PATCH v2 next 05/11] tools/nolibc/printf: Simplify __nolibc_printf() david.laight.linux
2026-02-07 20:05   ` Willy Tarreau
2026-02-07 23:50     ` David Laight
2026-02-08 12:20       ` David Laight
2026-02-08 14:44         ` Willy Tarreau
2026-02-08 16:54           ` David Laight
2026-02-08 17:06             ` Willy Tarreau
2026-02-06 19:11 ` [PATCH v2 next 06/11] tools/nolibc/printf: Use bit-masks to hold requested flag, length and conversion chars david.laight.linux
2026-02-08 15:22   ` Willy Tarreau
2026-02-16 19:52   ` Thomas Weißschuh
2026-02-16 22:47     ` David Laight
2026-02-18 17:36       ` Thomas Weißschuh
2026-02-18 22:57         ` David Laight
2026-02-06 19:11 ` [PATCH v2 next 07/11] tools/nolibc/printf: Add support for conversion flags "#- +" and format "%X" david.laight.linux
2026-02-08 15:47   ` Willy Tarreau
2026-02-08 17:14     ` David Laight
2026-02-08 16:06   ` Willy Tarreau
2026-02-16 19:57   ` Thomas Weißschuh
2026-02-16 22:50     ` David Laight
2026-02-18 17:39       ` Thomas Weißschuh
2026-02-16 20:11   ` Thomas Weißschuh
2026-02-16 22:52     ` David Laight
2026-02-06 19:11 ` [PATCH v2 next 08/11] tools/nolibc/printf: Add support for zero padding and field precision david.laight.linux
2026-02-08 16:16   ` Willy Tarreau
2026-02-08 17:31     ` David Laight
2026-02-06 19:11 ` [PATCH v2 next 09/11] selftests/nolibc: Improve reporting of vfprintf() errors david.laight.linux
2026-02-16 20:05   ` Thomas Weißschuh
2026-02-17 10:48     ` David Laight
2026-02-18 17:48       ` Thomas Weißschuh
2026-02-06 19:11 ` [PATCH v2 next 10/11] selftests/nolibc: Increase coverage of printf format tests david.laight.linux
2026-02-16 20:14   ` Thomas Weißschuh
2026-02-16 20:23   ` Thomas Weißschuh
2026-02-16 22:54     ` David Laight
2026-02-18 17:41       ` Thomas Weißschuh
2026-02-06 19:11 ` [PATCH v2 next 11/11] selftests/nolibc: Use printf("%.*s", n, "") to align output david.laight.linux
2026-02-08 16:20   ` Willy Tarreau
2026-02-16 20:22   ` Thomas Weißschuh
2026-02-06 21:36 ` [PATCH v2 next 00/11] tools/nolibc: Enhance printf() David Laight

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox