From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f45.google.com (mail-wr1-f45.google.com [209.85.221.45]) (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 C239546BF for ; Mon, 16 Feb 2026 22:47:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.45 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771282061; cv=none; b=C70us/WML0U7deuVew9HUwGZ4g8557P81NuJRXFNk3KTaBkvJkB8dZoFB2f5NQUyetyTR5zCC+tISNR7GU2Pimvx59ps2zA0Z3qTTZxAhFhJziIggYjFF7wrGWglYJRhRA6qeJaLI4DvwyEBngsZRFEH+wnzG+PdIdd0gdwVByQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771282061; c=relaxed/simple; bh=B4nIMM7ZRjqsUMbvir+DMcv2jrYTuSP4ntlR5NbHfUs=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=e0+0OV/zm8PZdhSWInwU/ztS7MVUEIIW//xYG9bA+AQVi6n7SRU7jH6Y/6fM5rfsEato/ukIM4v+XU3TJqEmuZm4NJ7mOOp3Ai0ejE5bb+GPtYjInea3CPOWF1n4MfDV+rJGik0fDeFSjqkkWA2ClYUkNq7EgzPyH1UkczU/PmI= 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=fVDfOdnK; arc=none smtp.client-ip=209.85.221.45 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="fVDfOdnK" Received: by mail-wr1-f45.google.com with SMTP id ffacd0b85a97d-4359a316d89so3205337f8f.0 for ; Mon, 16 Feb 2026 14:47:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1771282058; x=1771886858; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=ud+cmnqmP//mipu4JCyyHwFqv8NLp7DfkdLUcNGm3JU=; b=fVDfOdnKMbv5tiSJ+AU5swzwBkGWLKGG86bKM4Mlnnx/wxTYTfPBzk2sFV3Zx6aItX CPs13hRPJghbGmtYaqFOQFGfAYc+8DdTnfThRmFuHZrRArhjiAkId3uazmFnLuZjCuDx WLlP1rlDrQxfjMuJCpyo+ZVouXLshPvnz/u/Wh3yvw5JKufKUSejcf5Ko3SVmI+LUA8b 52JyxiZiOCnyJxkz43PncLcvbKCpphFVwh1i3Bq/s42uQ2pen4I0mRSWX+E/M+Usn/47 zuTAeHvMPFRT9ID2y5PwVUBepEiRyjT2aNh+jJzoztSmX3R62WCpqfX0PrLaPcnhNF6C amQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1771282058; x=1771886858; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=ud+cmnqmP//mipu4JCyyHwFqv8NLp7DfkdLUcNGm3JU=; b=eDY+Ja3RYNa0hq0FaLDKXDTx1+owvErm9lk6hh39y7Xyy/MRclXm9eyesYp0NvdRNd h14tv6YkrsZPw2VgB36q7kUfDDTRX1oUIbfk3vFBvuUE3VlrAYNW7k7kgxDzkEHg1UEZ iEug9uR5p/uoL9qEeXI1/P6y0ZtXBSJjjOEpGUCm9SvnRG2bIo4vy/v9XWdmFPZix6O4 sxK2swM/+D9lDIhudYZ05mGEPDHHkjOFvtl/+skbUFUxL1LNSqYJV2pHog5NwS21hCmW 75vBngPEaKUtcdpfQMUAwK7I2byfv83yAnYzos3arMtPhJ+XtbYIy6GikAek/PwOo83C Ql9g== X-Forwarded-Encrypted: i=1; AJvYcCWRxH2CGyzD6C9PYxBJBA91/aGI2fYN6uYUppdQq0i3Zi5/jtyjGbn4bnFsxrMR3qJ6hKdS9bmTDyX8Pw8=@vger.kernel.org X-Gm-Message-State: AOJu0Yzv79sMGMl8lwZu0vrO4NdCEFnsP9jYxrUZnt6E3/0HrCgtq+GW c+tF5x9z+3HubBLAMMmBMHZWR2n8QsPx+mE8uG24Df7lrrw0I0ixxtMl X-Gm-Gg: AZuq6aLuVvvRBUxA+RNwEYj5QoksKQ6++0QL1DR5Fzw3RAbwcwaXN4BhImExHtFPF0Y o4DGF0QTRzBRrta1BdmAcjGRsPO0I80E0j+THumaoHea6tlpgR/qYrksJ8vXZ6mEJ5AjuDL37EQ wSO3gkZdfq1Hy18MCK8w5ZNmnwiFhd62/K+L0UZ9BT9UAsMV1Wml/B3aNZmFYLXs2Y/2M25PF76 GnFKv/giUurzBmnGWXD9QddrrKZnIszwjxUxa4o1asmmKCsgJ6ey+EDuYZbMlHBJbJY76EYgimi T43XKxAtrjAAQeDWpiAmaJxuCAlzXxzYPmX0pH5jYHqaaPuKSaYv+9TaByNcNlDUr0MdmS7kAWp WAaVZfuXhojtYVnuyCMEhKJximjgYrlm3WEbWtW0dCjbyDZMVgHUGad9bXNffaQYUwMNuC5kD+X Xvsq0lP2Dm8DNFUilX5LmEZ4S/O90DV7sTcXjhVGoNS2HRfLhNQgv67FE30F4/wBY/ X-Received: by 2002:a05:6000:2011:b0:435:a2f8:151c with SMTP id ffacd0b85a97d-437978f0cd1mr22424243f8f.28.1771282057961; Mon, 16 Feb 2026 14:47:37 -0800 (PST) Received: from pumpkin (82-69-66-36.dsl.in-addr.zen.co.uk. [82.69.66.36]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-43796a6a6desm30779478f8f.10.2026.02.16.14.47.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Feb 2026 14:47:37 -0800 (PST) Date: Mon, 16 Feb 2026 22:47:36 +0000 From: David Laight To: Thomas =?UTF-8?B?V2Vpw59zY2h1aA==?= Cc: Willy Tarreau , linux-kernel@vger.kernel.org, Cheng Li Subject: Re: [PATCH v2 next 06/11] tools/nolibc/printf: Use bit-masks to hold requested flag, length and conversion chars Message-ID: <20260216224736.518df26e@pumpkin> In-Reply-To: <74f30662-2481-47eb-bfef-2195b170f4af@t-8ch.de> References: <20260206191121.3602-1-david.laight.linux@gmail.com> <20260206191121.3602-7-david.laight.linux@gmail.com> <74f30662-2481-47eb-bfef-2195b170f4af@t-8ch.de> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.38; arm-unknown-linux-gnueabihf) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On Mon, 16 Feb 2026 20:52:49 +0100 Thomas Wei=C3=9Fschuh wrote: > On 2026-02-06 19:11:16+0000, david.laight.linux@gmail.com wrote: > > From: David Laight > >=20 > > Use flags bits (1u << (ch & 31)) for the flags, length modifiers, and > > conversion specifiers. > > This makes it easy to test for multiple values at once. > >=20 > > Detect the conversion flags " #+-0" although they are currently all ign= ored. > >=20 > > Add support for length modifiers 't' and 'z' (both long) and 'q' and 'L' > > (both long long). > >=20 > > Add support for "%i" (the same as %d"). =20 >=20 > Would it be much work to split the new functionality into its own patch? > >=20 > > Unconditionally generate the signed values (for %d) to remove a second > > set of checks for the size. > >=20 > > Signed-off-by: David Laight > > --- > >=20 > > Changes for v2: > > - Use #defines to make the code a lot more readable. > > - Include the changes from the old patch 10 that used masks for the > > conversion specifiers. > > - Detect all the valid flag characters even though they are not impleme= nted. > > - Support for left justifying field is moved to patch 7. > >=20 > > tools/include/nolibc/stdio.h | 151 ++++++++++++++++++++++++----------- > > 1 file changed, 103 insertions(+), 48 deletions(-) > >=20 > > diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h > > index bb54f488c228..b14cf8224403 100644 > > --- a/tools/include/nolibc/stdio.h > > +++ b/tools/include/nolibc/stdio.h > > @@ -240,19 +240,44 @@ char *fgets(char *s, int size, FILE *stream) > > } > > =20 > > =20 > > -/* minimal printf(). It supports the following formats: > > - * - %[l*]{d,u,c,x,p} > > - * - %s > > - * - unknown modifiers are ignored. > > +/* simple printf(). It supports the following formats: > > + * - %[-][width][{l,t,z,ll,L,j,q}]{d,u,c,x,p,s,m,%} > > + * - %% > > + * - invalid formats are copied to the output buffer > > */ > > + > > +/* This code uses 'flag' variables that are indexed by the low 6 bits > > + * of characters to optimise checks for multiple characters. > > + * > > + * _NOLIBC_PF_FLAGS_CONTAIN(flags, 'a', 'b'. ...) > > + * returns non-zero if the bit for any of the specified characters is = set. > > + * > > + * _NOLIBC_PF_CHAR_IS_ONE_OF(ch, 'a', 'b'. ...) > > + * returns the flag bit for ch if it is one of the specified character= s. > > + * All the characters must be in the same 32 character block (non-alph= abetic, > > + * upper case, or lower case) of the ASCII character set.) > > + */ > > +#define _NOLIBC_PF_FLAG(ch) (1u << ((ch) & 0x1f)) > > +#define _NOLIBC_PF_FLAG_NZ(ch) ((ch) ? _NOLIBC_PF_FLAG(ch) : 0) > > +#define _NOLIBC_PF_FLAG8(cmp_1, cmp_2, cmp_3, cmp_4, cmp_5, cmp_6, cmp= _7, cmp_8, ...) \ > > + (_NOLIBC_PF_FLAG_NZ(cmp_1) | _NOLIBC_PF_FLAG_NZ(cmp_2) | \ > > + _NOLIBC_PF_FLAG_NZ(cmp_3) | _NOLIBC_PF_FLAG_NZ(cmp_4) | \ > > + _NOLIBC_PF_FLAG_NZ(cmp_5) | _NOLIBC_PF_FLAG_NZ(cmp_6) | \ > > + _NOLIBC_PF_FLAG_NZ(cmp_7) | _NOLIBC_PF_FLAG_NZ(cmp_8)) > > +#define _NOLIBC_PF_FLAGS_CONTAIN(flags, ...) \ > > + ((flags) & _NOLIBC_PF_FLAG8(__VA_ARGS__, 0, 0, 0, 0, 0, 0, 0)) > > +#define _NOLIBC_PF_CHAR_IS_ONE_OF(ch, cmp_1, ...) \ > > + (ch < (cmp_1 & ~0x1f) || ch > (cmp_1 | 0x1f) ? 0 : \ > > + _NOLIBC_PF_FLAGS_CONTAIN(_NOLIBC_PF_FLAG(ch), cmp_1, __VA_ARGS__)) = =20 >=20 > With signed chars I get: >=20 > sysroot/i386/include/stdio.h:321:37: error: comparison is always false du= e to limited range of data type [-Werror=3Dtype-limits] > 321 | (ch < (cmp_1 & ~0x1f) || ch > (cmp_1 | 0x1f) ? 0 : \ Stupid type-limits warning. It is almost impossible to get rid of without making the code unreadable. > | ^ > sysroot/i386/include/stdio.h:389:35: note: in expansion of macro '_NOLIBC= _PF_CHAR_IS_ONE_OF' > 389 | ch_flag =3D _NOLIBC_PF_CHAR_IS_ONE_OF(ch,= 'c', 'd', 'i', 'u', 'x', 'p'); > | >=20 > This can be fixed by switching 'ch' to be always unsigned. That's likely to provoke another error elsewhere. In any case optimising that test away makes the code smaller! >=20 > You can run the the builtin test suite like this: > -p triggers the download of the toolchains > -l uses LLVM/clang instead of the downloaded toolchain >=20 > $ cd tools/testing/selftests/nolibc > $ ./run-tests.sh -m user -p > $ ./run-tests.sh -m user -l I've just been running: rm nolibc-test; make -O /path/.../dir && ./nolibc-test printf >=20 > > + > > typedef int (*__nolibc_printf_cb)(void *state, const char *buf, size_t= size); > > =20 > > static __attribute__((unused, format(printf, 3, 0))) > > int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fm= t, va_list args) > > { > > - char lpref, ch; > > - unsigned long long v; > > + char ch; > > unsigned int written, width; > > + unsigned int flags, ch_flag; > > size_t len; > > char tmpbuf[21]; > > const char *outstr; > > @@ -265,6 +290,7 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *st= ate, const char *fmt, va_list > > break; > > =20 > > width =3D 0; > > + flags =3D 0; > > if (ch !=3D '%') { > > while (*fmt && *fmt !=3D '%') > > fmt++; > > @@ -274,6 +300,14 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *s= tate, const char *fmt, va_list > > =20 > > ch =3D *fmt++; > > =20 > > + /* Conversion flag characters */ > > + for (;; ch =3D *fmt++) { > > + ch_flag =3D _NOLIBC_PF_CHAR_IS_ONE_OF(ch, ' ', '#', '+', '-', '0'); > > + if (!ch_flag) > > + break; > > + flags |=3D ch_flag; > > + } =20 >=20 > What is the advantage of this over: >=20 > while (1) { > /* ... */ >=20 > ch =3D *fmt++; > } One line shorter :-) >=20 > Or combine it with the 'ch =3D *fmt++' from above and do: >=20 > while (1) { > ch =3D *fmt++; >=20 > /* ... */ > } >=20 > These look much simpler to me. The code always needs to get a new character after using one. So you come out of each bit of code with the 'next char to check' in ch. Which means you need a new character at the end of the loop for the next iteration. >=20 > > + > > /* width */ > > while (ch >=3D '0' && ch <=3D '9') { > > width *=3D 10; =20 >=20 > (...) >=20 > > +do_output: > > written +=3D len; > > =20 > > + /* An OPTIMIZER_HIDE_VAR() seems to stop gcc back-merging this > > + * code into one of the conditionals above. > > + */ > > + __asm__ volatile("" : "=3Dr"(len) : "0"(len)); =20 >=20 > Can we make a definition in nolibc/compiler.h out of this? I've added _NOLIBC_OPTIMIZER_HIDE_VAR() to compiler.h in the draft of v3. > Why the 'volatile'? Wo don't have that in the kernel. I probably wrote that before looking up the kernel definition. > Why separate input and ouput arguments instead of one combined one ('+r')? > I have been wondering the same about the kernel definition, too. I'm not sure either. Certainly "+r" is more modern - which is why it isn't used in a lot of plac= es. They may be identical (indeed "+r" might get converted to the "0" form), but maybe it gives better separation - I just copied the same version. David >=20 > > + > > while (width > len) { > > unsigned int pad_len =3D ((width - len - 1) & 15) + 1; > > width -=3D pad_len; > > --=20 > > 2.39.5 > > =20