Enhance printf()
Get series with b4
b4 am [email protected]Versions newest first · linked by author and title
- v5 Latest17 patches 8 repliesIn review
- v423 patches 20 repliesSuperseded
- v317 patches 16 repliesApplied
Changes in v5 from the cover letter
- Old patches 2 to 7 have been applied to nolibc-next and removed.
- Patch 3 changed to return the correct (success/fail not length) value from strerror_r() and to return ERANGE if the buffer is short.
- Patch 11 (prepend sign) has an updated comment.
Patches in v5 No applies recorded yet
| # | Subject and files | Size | Replies | Applied |
|---|---|---|---|---|
| 1/17 | tools/nolibc: Add _NOLIBC_OPTIMIZER_HIDE_VAR() to compiler.htools/ | +3 | 0 replies | |
| 2/17 | selftests/nolibc: Rename w to written in expect_vfprintf()tools/ | +4 | 0 replies | |
| 3/17 | tools/nolibc: Implement strerror() in terms of strerror_r()tools/ tools/ | +21 | 0 replies | |
| 4/17 | tools/nolibc: Rename the 'errnum' parameter to strerror()tools/ | +2 | 0 replies | |
| 5/17 | tools/nolibc/printf: Output pad characters in 16 byte chunkstools/ | +9 | 0 replies | |
| 6/17 | tools/nolibc/printf: Simplify __nolibc_printf()tools/ | +53 | 0 replies | |
| 7/17 | tools/nolibc/printf: Use goto and reduce indentationtools/ | +92 | 0 replies | |
| 8/17 | tools/nolibc/printf: Use bit-masks to hold requested flag, length and conversion charstools/ | +108 | 0 replies | |
| 9/17 | tools/nolibc/printf: Add support for length modifiers tzqL and formats iXtools/ tools/ | +27 | 0 replies | |
| 10/17 | tools/nolibc/printf: Handle "%s" with the numeric formatstools/ | +12 | 0 replies | |
| 11/17 | tools/nolibc/printf: Prepend sign to converted numbertools/ | +27 | 0 replies | |
| 12/17 | tools/nolibc/printf: Add support for conversion flags space and plustools/ | +5 | 0 replies | |
| 13/17 | tools/nolibc/printf: Special case 0 and add support for %#xtools/ tools/ | +30 | 0 replies | |
| 14/17 | tools/nolibc/printf: Add support for left aligning fieldstools/ tools/ | +8 | 0 replies | |
| 15/17 | tools/nolibc/printf: Add support for zero padding and field precisiontools/ tools/ | +79 | 0 replies | |
| 16/17 | tools/nolibc/printf: Add support for octal outputtools/ tools/ | +41 | 0 replies | |
| 17/17 | selftests/nolibc: Use printf variable field widths and precisionstools/ | +5 | 0 replies |
Reviews trailers per patch
16 of 17 patches have a Reviewed-by or Acked-by.
| Patch | Willy Tarreau |
|---|---|
| 1/17 | A-b |
| 2/17 | A-b |
| 3/17 | A-b |
| 4/17 | A-b |
| 5/17 | A-b |
| 6/17 | |
| 7/17 | A-b |
| 8/17 | A-b |
| 9/17 | A-b |
| 10/17 | A-b |
| 11/17 | A-b |
| 12/17 | A-b |
| 13/17 | A-b |
| 14/17 | A-b |
| 15/17 | A-b |
| 16/17 | A-b |
| 17/17 | A-b |
Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.
Cover letter [PATCH v5 next 00/17] 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.
- Alternate form "%#x" and "%#o".
- Field precision.
- Variable field width and precision.
- Width modifiers q, L, t and z.
- Conversion specifiers i, o and X (X generates lower case). About the only things that are missing are wide chanacters and floating point.
The tests are updated to match.
Bloat/savings (in nolibc-test, but excluding the program) to patch 11:
(Measured for v3)
Function old new delta
_nolibc_u64toa_base.isra - 143 +143
strerror - 78 +78
__nolibc_sprintf_cb 58 91 +33
itoa_r.isra 60 75 +15
utoa_r.isra 144 - -144
__nolibc_printf 1081 729 -352
(All these functions include ~40 bytes for the stack protector code.) utoa_r.isra and _nolibc_u64toa_base.isra pretty much cancel each other out. itoa_r.isra grows slightly since it calls _nolibc_u64toa_base(). strerror() used to be inlined, but over half of it is the stack check. While some of the code added to __nolibc_sprintf_cb() has come out of __nolibc_printf() 16-20 bytes is removed from the caller.
So there is a net saving of about 280 bytes (including losing a copy of the number to ascii code).
The later patches add code back in:
patch 13 - conversion flags " +#" +80 bytes
patch 14 - left aligning fields +38 bytes
patch 15 - zero padding and field precision +260 bytes
patch 16 - octal output +34 bytes
So probably about +130 bytes, but it will depend on what the application actually calls and inlining decisions made by the compiler. (All x86-64, other architectures will vary.)
The biggest size change is probably removing the .data from strerror(). This reduced the program binary file by 4k if it is the only initialised data in a small program.
Changes for v5:
- Old patches 2 to 7 have been applied to nolibc-next and removed.
- Patch 3 changed to return the correct (success/fail not length) value from strerror_r() and to return ERANGE if the buffer is short.
- Patch 11 (prepend sign) has an updated comment.
Changes for v4:
- Old patches 2, 3 and 6 have been applied to nolibc-next and removed.
- Patch 7 (fix padding) has been moved before the selftest changes so that the tests don't fail.
- NOLIBC_IGNORE_ERRNO is left only applying to %m and not strerror().
- Avoid calling memcpy(tgt, NULL, 0) in the vsnprintf callback function.
- Some of the patches have been split.
The final version of stdio.h only differs from v3 in the strerror() and vsnprintf callback code.
Significant changes for v3:
The patches have been re-ordered, split and joined but the final code is pretty much the same as v2.
- Include the patch to stdlib.h that optimises the 'number to ascii' functions. This is needed for the final patch that adds octal support. This includes a fix to the previous version that could generate negative digits.
- Add octal support in the final patch.
- Update the selftests as new features are added.
- The patch to buffer fprintf() output has been removed.
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 (17):
tools/nolibc: Add _NOLIBC_OPTIMIZER_HIDE_VAR() to compiler.h selftests/nolibc: Rename w to written in expect_vfprintf() tools/nolibc: Implement strerror() in terms of strerror_r() tools/nolibc: Rename the 'errnum' parameter to strerror() tools/nolibc/printf: Output pad characters in 16 byte chunks tools/nolibc/printf: Simplify __nolibc_printf() tools/nolibc/printf: Use goto and reduce indentation tools/nolibc/printf: Use bit-masks to hold requested flag, length and
conversion chars
tools/nolibc/printf: Add support for length modifiers tzqL and formats
iX
tools/nolibc/printf: Handle "%s" with the numeric formats tools/nolibc/printf: Prepend sign to converted number tools/nolibc/printf: Add support for conversion flags space and plus tools/nolibc/printf: Special case 0 and add support for %#x tools/nolibc/printf: Add support for left aligning fields tools/nolibc/printf: Add support for zero padding and field precision tools/nolibc/printf: Add support for octal output selftests/nolibc: Use printf variable field widths and precisions
tools/include/nolibc/compiler.h | 3 + tools/include/nolibc/stdio.h | 427 ++++++++++++++----- tools/testing/selftests/nolibc/nolibc-test.c | 99 +++-- 3 files changed, 371 insertions(+), 158 deletions(-)
Signature · 1 line
2.39.5