[PATCH v3 next 08/17] tools/nolibc/printf: Output pad characters in 16 byte chunks

From: david . laight . linux

Date: Mon Feb 23 2026 - 05:36:44 EST


From: David Laight <david.laight.linux@xxxxxxxxx>

Simple to do and saves calls to the callback function.

Change variables written, width and len to 'signed int' to get
better code.

Acked-by: Willy Tarreau <w@xxxxxx>
Signed-off-by: David Laight <david.laight.linux@xxxxxxxxx>
---

For v3:
- Change to signed variables here rather than a later patch.

tools/include/nolibc/stdio.h | 14 +++++++++-----
1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h
index 5c8f233336b9..ea6c514c4b7a 100644
--- a/tools/include/nolibc/stdio.h
+++ b/tools/include/nolibc/stdio.h
@@ -312,8 +312,8 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list
{
char escape, lpref, ch;
unsigned long long v;
- unsigned int written, width;
- size_t len, ofs;
+ int written, width, len;
+ size_t ofs;
char outbuf[21];
const char *outstr;

@@ -411,10 +411,14 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list
outstr = fmt;
len = ofs - 1;
flush_str:
- while (width-- > len) {
- if (cb(state, " ", 1) != 0)
+ width -= len;
+ while (width > 0) {
+ /* Output pad in 16 byte blocks with the small block first. */
+ int pad_len = ((width - 1) & 15) + 1;
+ width -= pad_len;
+ written += pad_len;
+ if (cb(state, " ", pad_len) != 0)
return -1;
- written += 1;
}
if (cb(state, outstr, len) != 0)
return -1;
--
2.39.5