From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailtransmit04.runbox.com (mailtransmit04.runbox.com [185.226.149.37]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8869235CB6A for ; Mon, 23 Feb 2026 10:35:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.226.149.37 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771842958; cv=none; b=MKUzhz0XOzggUDXnao7Eu1OEji+Un0wmXTO5iwnWQry0D3BxphwW3wjm2eHSIhLZiGRjPbZoxwfgWysEmsc3Tht9R9B+lg29e/jCwe7GGYA9KXkn6r4NSN3Qh/FBGbzz+VwBscq7bpIur2bIyaDv6W5XbblukS11WuXC+wnPzRM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771842958; c=relaxed/simple; bh=z4MqH+uI60f5bguQYZTiU9n4Rdz+s/V3jxSc87k6zVU=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=a0HtWy3M2Lf3s+HVE4e59ElXM7CkZisdpO7yEwKVX+Nj3AmPrhXWWubPQB/nz0T3ii/WiDuuuH0KS/wnI/VczTwxyJpjocRFgUc7Pchor2Fdm9bKg7LfRjnB2hxUwIuh+wu8Op5SVtHLXeYSPoNtrw0h46/z4DyVuzMrNWjfLNE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=runbox.com; dkim=pass (2048-bit key) header.d=runbox.com header.i=@runbox.com header.b=vNxNyeE1; arc=none smtp.client-ip=185.226.149.37 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=runbox.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=runbox.com header.i=@runbox.com header.b="vNxNyeE1" Received: from mailtransmit03.runbox ([10.9.9.163] helo=aibo.runbox.com) by mailtransmit04.runbox.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.93) (envelope-from ) id 1vuT19-00GgoH-7N; Mon, 23 Feb 2026 11:18:19 +0100 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=runbox.com; s=selector2; h=Content-Transfer-Encoding:MIME-Version:Message-Id:Date: Subject:Cc:To:From; bh=yqRXla3lS/yTVqmpM/X7yovjBFouJpYhfkQozW7PnNY=; b=vNxNye E1tGvzuQUH+2jYUF9xfSg8pRbilgcn9iphZrv72vqGeN5TOBEr/J/9YdN91shBtahh0pw2Ta56cnu 11/ne3I8kP19Kbe/29RYp872f5Wgf2ZfGU7fdBv+VzrP0xMIeTgD5bBQup5HRSoDl4pZR7+QxG+7j KrJcsoPru9BoYPm2grujI7dq6jZiehbVtS5OIpiyLDsYVslXQa6POl2dDHmuZhC7cmcGm050OBOHc 3BSNV8EZC5EfZVWsmBqqNlgXmic+b2popD1aRltgrCn6QqJ3AtEp8mKqHx9lCWOL42PFMEHmpKIKG C7s6XQNchYvq2vHp3+07s5wJ4VXg==; Received: from [10.9.9.73] (helo=submission02.runbox) by mailtransmit03.runbox with esmtp (Exim 4.86_2) (envelope-from ) id 1vuT18-0001R2-Oy; Mon, 23 Feb 2026 11:18:19 +0100 Received: by submission02.runbox with esmtpsa [Authenticated ID (1493616)] (TLS1.2:ECDHE_SECP256R1__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim 4.93) id 1vuT0v-006AjD-0S; Mon, 23 Feb 2026 11:18:05 +0100 From: david.laight.linux@gmail.com To: Willy Tarreau , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , linux-kernel@vger.kernel.org, Cheng Li Cc: David Laight Subject: [PATCH v3 next 00/17] Enhance printf() Date: Mon, 23 Feb 2026 10:17:18 +0000 Message-Id: <20260223101735.2922-1-david.laight.linux@gmail.com> X-Mailer: git-send-email 2.39.5 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: David Laight 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 12: 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. 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 tools/nolibc: Optimise and common up the number to ascii functions selftests/nolibc: Fix build with host headers and libc selftests/nolibc: Improve reporting of vfprintf() errors tools/nolibc: Implement strerror() in terms of strerror_r() tools/nolibc/printf: Change variables 'c' to 'ch' and 'tmpbuf[]' to 'outbuf[]' tools/nolibc/printf: Move snprintf length check to callback 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: Handle "%s" with the numeric formats tools/nolibc/printf: Add support for conversion flags " +#" 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 | 5 + tools/include/nolibc/stdio.h | 502 ++++++++++++++----- tools/include/nolibc/stdlib.h | 169 +++---- tools/testing/selftests/nolibc/nolibc-test.c | 179 ++++--- 4 files changed, 575 insertions(+), 280 deletions(-) -- 2.39.5