Skip to content

tools/nolibc: Enhance printf()

[email protected] lkml 2 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v2 Latest11 patches 48 repliesIn review
  2. v112 patches 6 repliesSuperseded

Changes in v2 from the cover letter

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:

Patches in v2 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/11tools/nolibc/printf: Change variable used for format chars from 'c' to 'ch'tools/include/nolibc/stdio.h+16 -162 replies
2/11tools/nolibc/printf: Move snprintf length check to callbacktools/include/nolibc/stdio.h+41 -274 replies
3/11tools/nolibc/printf: Add buffering to vfprintf() callback.tools/include/nolibc/stdio.h+29 -35 replies
4/11tools/nolibc/printf: Output pad characters in 16 byte chunkstools/include/nolibc/stdio.h+5 -36 replies
5/11tools/nolibc/printf: Simplify __nolibc_printf()tools/include/nolibc/stdio.h+51 -496 replies
6/11tools/nolibc/printf: Use bit-masks to hold requested flag, length and conversion charstools/include/nolibc/stdio.h+103 -485 replies
7/11tools/nolibc/printf: Add support for conversion flags "#- +" and format "%X"tools/include/nolibc/stdio.h+71 -268 replies
8/11tools/nolibc/printf: Add support for zero padding and field precisiontools/include/nolibc/stdio.h+80 -232 replies
9/11selftests/nolibc: Improve reporting of vfprintf() errorstools/testing/selftests/nolibc/nolibc-test.c+22 -53 replies
10/11selftests/nolibc: Increase coverage of printf format teststools/testing/selftests/nolibc/nolibc-test.c+37 -124 replies
11/11selftests/nolibc: Use printf("%.*s", n, "") to align outputtools/testing/selftests/nolibc/nolibc-test.c+4 -172 replies

Reviews trailers per patch

6 of 11 patches have a Reviewed-by or Acked-by.

PatchWilly TarreauThomas Weißschuh
1/11A-b A-b
2/11
3/11
4/11A-b
5/11A-b
6/11A-b
7/11
8/11A-b
9/11
10/11
11/11A-b A-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v2 thread

Cover letter [PATCH v2 next 00/11] tools/nolibc: Enhance printf()

From: David Laight <[email protected]>

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(-)
Signature · 1 line
2.39.5