From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f46.google.com (mail-wr1-f46.google.com [209.85.221.46]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DE6AC2D94AC for ; Fri, 6 Feb 2026 19:11:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.46 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770405092; cv=none; b=W4s6kGE2veB/BPDnoS/WCiECYwLY2uDoTbEXbgkS4RPAGzT3hjX0faKwKoN5HBSayxwEwCsCCG7D/N3yWxog4+pp+fNrh+Rmv+s2ipVmdgtMUM0IK+uZ00EhfXqRNKKtNiLAu1lTEFywe8sspntVHq9J0RQWV4BYCbV7ve6oevM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770405092; c=relaxed/simple; bh=eNSng+4bjQYOe00NNo3z+LycQrHSE6EwOHETdREmqtI=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=EYzAXYBvBYkMcw7suvcsDDPE4CYDfYUoRamrdjUOxq5tqm5DXnWfjtlLeUFAS+ONEt2M7gFPbCHrHuBV6DNy/rpmLxd49an+1URcCrTbyrUxa0W83L2WHnFx5/EbisLlAzFCzDC8BeKZh1lp6E5eZThavBJBatbKl1rFS0RqYxc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=CZY1qDAE; arc=none smtp.client-ip=209.85.221.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="CZY1qDAE" Received: by mail-wr1-f46.google.com with SMTP id ffacd0b85a97d-43626796202so1654088f8f.3 for ; Fri, 06 Feb 2026 11:11:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1770405090; x=1771009890; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=Lrlr7x4opKdbWT+c4xB5JQaXhHGWrUesfqVMKFyo/Ew=; b=CZY1qDAEMZdZ66reL8qRBTJj2pbM/HWwigJdDh3VqQxRfXD1YIEchahEQuAJdK+HJG ppPWsfugt/LhJidg7wg0jS437kbmA7gDJFEOzACBLyKeb7TeR1EpvE1HuHLayVxcRIeW M5e9SLvwisKDHtd3nwqcWREnQav8mTzoOzm/j3eAkrgnmdMBAB1vbo8smQKFAE4mTID2 naBNj4coyy3Uyx+QukTm97O3zSe6NwQflZ36lHGF01NYFceVaYKAAUL7BSXDQo5MiZ/8 gzv7UJrCh27PevjmQ57bUSppUg4vvtzqOO5WPDalzvNKjjgflbaZJpRSPaSWmADNC+iP Bm0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770405090; x=1771009890; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=Lrlr7x4opKdbWT+c4xB5JQaXhHGWrUesfqVMKFyo/Ew=; b=eTUzanBXRtvuAXj7theHY6FdIZ6iwfNsfZ0owP/9k8lO8W8zzBnfrQ7y6rcjmj6PWo Wc/2idBk4JFYgTpHAPMYqzwOCzHF7LIfg+0VeHvbBNlbwxsFh6ikRWyCDKq/u/3pgB+O RK2F4IRg5kfNwgLoAbaLV5wxJyx1ImnvzDsxbVo7mSfk9IYsCBxn0j9hN886BXWsrP6j 93uQruKPjiBJIxujkJz68LWS43+R/mEa7C+fyHuOOR95EVFRiMWjay+QcfJIeu7pRYs2 BhUSmqwBkzgPhWLpdBgmcXS6w0XzzBe43kQviMhBJN43PXuD49uyvjWuEp/biWeQt87c I9Jw== X-Forwarded-Encrypted: i=1; AJvYcCUA4PAEzYVARCll2s0BXMa84+r8cPigwPgxlUpSPWt/OdnNLcSL2oNfbr1lqYTGSYK+3CwwVgCRkz9/tjY=@vger.kernel.org X-Gm-Message-State: AOJu0YzHoGeU5+1tUnW18Uf/kst4WLE4qleA4pzZGFIveTef24x5bPhv pMh7vAfLUgxpzW+Y7CDF92u6yu3aR3uCBY04R3V9OxrkFb0LV7q00nbe X-Gm-Gg: AZuq6aKVty0MvDXNGZZxBum8ZyHCZyZXFN1enRrrVkRJNDOPyPh4YFWIdVuqlo6inf8 BvIG2nAxUyXFsX/YPH4KMgIc9WqpL6n+Lb/uEA9RSj7lQmy91P3zfww6gNAP+m6NAjFJvWOOf75 P7oPDMY/UbGcNYyXp17KgqFnqylhrCihvTZu/cHDotfZx6YxZaVyApf7ElmCqlI12t9jpJOBrjP 790S1U55hje+0e8Xd93xxkwnZHcnfAOVr0SIQDu6f7Yx5EBAE3UA7EH+aOYXASDmUMNuELEALVQ yTofXwwJJ3vRqsMQH3ci2AT7BSpndHSAu2yeb3rTCvkWnOHrPBk1tyaalPbXVkPGkJO4NPdV4ID 8o0v0711m9umWSlK06si9vyM2BGTdbTWt5JNKoACRH6f20eZayH0V0t53IncQaPh95/kLRPgNlX 0w0Po8yZMKJ6X0ch+UB08B3BmqQpnQempM/aYkZum0U2kc2GGZS1xkFJJzYdePQr29axSOvzVY X-Received: by 2002:a5d:64c5:0:b0:435:a363:f293 with SMTP id ffacd0b85a97d-4362904641amr6288382f8f.11.1770405090138; Fri, 06 Feb 2026 11:11:30 -0800 (PST) Received: from snowdrop.snailnet.com (82-69-66-36.dsl.in-addr.zen.co.uk. [82.69.66.36]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4362972fb81sm8703681f8f.20.2026.02.06.11.11.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 06 Feb 2026 11:11:29 -0800 (PST) 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 v2 next 00/11] tools/nolibc: Enhance printf() Date: Fri, 6 Feb 2026 19:11:10 +0000 Message-Id: <20260206191121.3602-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. - 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(-) -- 2.39.5