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 366EF395DA0 for ; Mon, 2 Mar 2026 10:18:38 +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=1772446722; cv=none; b=XiDlKJ03guut1ICmwM1jJrSWmuHUWcQCCbsjSkiqVxVccKat52smrUuQNB8BLo6+LvQe3hq2h8zobUTDfi+aOytyEau5BqgKowxLDua0yW2A+fJQ6juZL49WJTAD5e4MEXmE77TPOSwnJifkMUWZ8L4JWH9L4Dr4AwoSp3TwkFE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772446722; c=relaxed/simple; bh=bzR0mk6G/ad0HnUtj+FLqHeC/BVcXBFVXEMXP0KnHmc=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=SaKRQ97+7GqJOnBAM0AdClpOn2A5WFncwOIavYAsx1KeZmrjz7iRyY6detQIXXE9SVw/T7GbI8sCgQDs6VRXc3HUHux/oBFRWgkkLU2WJ8ho3gI/gNLMzJuURWYsBYqH1oVNn24As4SMvQ8Qs2oLkyKfsYMaKMIc+x4ImjHVKX0= 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=JQLIc7rm; 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="JQLIc7rm" Received: from mailtransmit02.runbox ([10.9.9.162] 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 1vx0M8-0037z9-QS; Mon, 02 Mar 2026 11:18:28 +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=TNAjI92VrBGDY4BMJjQkolaJqSKkwZO4TdWTMVlGcDc=; b=JQLIc7 rmg7ox94fmIwJRzqbyQkeVhPpfxp+5dFEar2Bz8ghlIgaL6MMX0n0Tub5ifz4m0FFsIk9hq9ERW55 Hjt0GTeqPsRD6+gBiyyDGrqp3g5Am38mexPhUyEs+AaM/WJzj8qVnS9Oh63RybylfbEpFy5Zydco6 XXkIGV+tYFXhGmt5dIJkpNgz+uwdemIznq4ZbnKs/uUsGUi3FanFKy4Vu+O4x2J3u2foWyNMdtiBy mpHuZ7CG96/2uqj4s4uuQCA3IQ+PYcTIWZ2+VQOfHUYVBrACl+ABVPpsNSBFKry4W/BnjRdBbLyrv PZhqnYu530z+897356QWoI4kbRqw==; Received: from [10.9.9.72] (helo=submission01.runbox) by mailtransmit02.runbox with esmtp (Exim 4.86_2) (envelope-from ) id 1vx0M8-00051x-Ey; Mon, 02 Mar 2026 11:18:28 +0100 Received: by submission01.runbox with esmtpsa [Authenticated ID (1493616)] (TLS1.2:ECDHE_SECP256R1__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim 4.93) id 1vx0M6-006y7o-58; Mon, 02 Mar 2026 11:18:26 +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 v4 next 00/23] Enhance printf() Date: Mon, 2 Mar 2026 10:17:52 +0000 Message-Id: <20260302101815.3043-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 14: (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 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 (23): tools/nolibc: Add _NOLIBC_OPTIMIZER_HIDE_VAR() to compiler.h tools/nolibc/printf: Move snprintf length check to callback selftests/nolibc: Return correct value when printf test fails selftests/nolibc: check vsnprintf() output buffer before the length selftests/nolibc: Use length of 'expected' string to check snprintf() output selftests/nolibc: Check that snprintf() doesn't write beyond the buffer end selftests/nolibc: Let EXPECT_VFPRINTF() tests be skipped 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 | 501 ++++++++++++++----- tools/testing/selftests/nolibc/nolibc-test.c | 157 +++--- 3 files changed, 469 insertions(+), 192 deletions(-) -- 2.39.5