From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-lj1-f176.google.com (mail-lj1-f176.google.com [209.85.208.176]) (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 72119178368 for ; Wed, 18 Dec 2024 09:19:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.176 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734513549; cv=none; b=ALDMW69vbQbbciqytf0tw0B5puvHiyD4T69Z8j35poEo64LHNvlU4kfxZONeeW19KpZ+Q7OGQtvueCX3yrFZfeMgj7+5MGCknyit5dN0j2HOIqXH62tumR/+oXgTqgmz56b0iPXaGAQ1TJWBoCMTirQ33dnDlzvWHKkYC+RJ/wE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734513549; c=relaxed/simple; bh=QJz8k1Kjw+oae3Kp3XGV2LOzijTPJvtEsjZywKvJY4E=; h=From:To:Cc:Subject:In-Reply-To:References:Date:Message-ID: MIME-Version:Content-Type; b=h5UEAHb+zzLNx6A/dZKA+4d8tBTcFBWUUWt3UbqkfluUEd0F/I07VhWhmADiTNyTnEE0PwmNtZSRLFGFL1LBi30mWfEryajfWmbXBE1GCYyyjPvaN96rdx+iWSdmHrMXCoJLeZY9FOBZJ2oRSNfbPxPsn7WAquu1v6GtAkBzSKY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rasmusvillemoes.dk; spf=pass smtp.mailfrom=rasmusvillemoes.dk; dkim=pass (1024-bit key) header.d=rasmusvillemoes.dk header.i=@rasmusvillemoes.dk header.b=IxgdND3l; arc=none smtp.client-ip=209.85.208.176 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rasmusvillemoes.dk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rasmusvillemoes.dk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=rasmusvillemoes.dk header.i=@rasmusvillemoes.dk header.b="IxgdND3l" Received: by mail-lj1-f176.google.com with SMTP id 38308e7fff4ca-303548a933aso24841541fa.3 for ; Wed, 18 Dec 2024 01:19:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rasmusvillemoes.dk; s=google; t=1734513544; x=1735118344; darn=vger.kernel.org; h=mime-version:user-agent:message-id:date:references:in-reply-to :subject:cc:to:from:from:to:cc:subject:date:message-id:reply-to; bh=mankhUwSowYBB2Cznf0cWVw0fwDxI8T4dZEgqrd2fBo=; b=IxgdND3lkPenVfLPkXLpAvLxeWMfsjzFV0ZVdMw2S0mi9HjxnUB1iErjZhKYmpkhi9 ixOo/9Cp/pI4MUYgkCrHldkqmKQ2Sq/pZqJrAMIxMOh7CiPgCdxENFskYhSHtsYKIlXR NttTnLeV0teJ4OyFwJVPwZoHTNBA4AWyCWLmk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734513544; x=1735118344; h=mime-version:user-agent:message-id:date:references:in-reply-to :subject:cc:to:from:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=mankhUwSowYBB2Cznf0cWVw0fwDxI8T4dZEgqrd2fBo=; b=duLXM5onspvn9egY0k6G50V6IzvYOp6cN/Cjs90yJ4jamapnKS13uQMX0x4HD1xtM1 1OW6q/G4lKPmPzNXllmsVReTJXTio9i9tZSB587V+XcT2Y6lfIMxsVTBAD6eLiHRh9sq HC5lkkrBv6bwWGXBpT/JF5pnP41/EQxTYbRpPzZUk9NRNUjTASVqp2kohQqvmLFdIpbR cXcZKW1We/IAtaFTA6bQSl+KA1sTJGn74b870boKGYUhCjRkKKRvcYc33LNF/a6gHUfG akHApJIOFFzhb547srTj7xa1ip7yarpUQAeaCetzIPQMRbPi3yXjQne3kPt2pbsOdwBj pFCg== X-Forwarded-Encrypted: i=1; AJvYcCXHhLaH8b5yFO6mEu5bS6gWLCp/i+xBI+5O/be7dW1EWP4mS5qvCEJl+p3FbibAZN+5R8MtftbSxzDmWuU=@vger.kernel.org X-Gm-Message-State: AOJu0YxXpmdDlGqXoDGowZcSmqfLXfOvdaXbVfQBl2EkCnmASQI/0aQ3 HDPxiuKXlwjqw5I+tw7ZnYNCFnfyFHXErxY2/38besC2iEHO252irV79VIKpONs= X-Gm-Gg: ASbGncs8P1/avLmhKPx1ZBo2kxtGP1cCBKVpw8W8FYIZdCUvQsOQgDiCkzji7bzbj8+ KBf78rOS7SwY4T995whWkChP39ZfvZMSCKDoebW2miCq+IUn7/mbu4+X+atmvRFL54VX0I5w3b3 f4m2v35JbhVwWHfrFhrQEwqXq3vjukuKtj9t7/MxFIIteUo1cgsN6pPGv48dGOU4+Im2rPZUHdk jQczmTv2ij6TTv9Tj3WjWgIKCdP7rjtr7UsHCGrCC4rOpYyPubTCdJ69Q== X-Google-Smtp-Source: AGHT+IF/W8rsS5/PLFAoP7mr1xrLrsYtpxrq9gVAvqoHiKZLa1EVgIGlz7KwfJBKOw5B3CwpQTp94Q== X-Received: by 2002:a2e:a9a7:0:b0:300:1d45:871f with SMTP id 38308e7fff4ca-3044da69776mr7071541fa.5.1734513544366; Wed, 18 Dec 2024 01:19:04 -0800 (PST) Received: from localhost ([81.216.59.226]) by smtp.gmail.com with ESMTPSA id 38308e7fff4ca-303440450fbsm15369121fa.39.2024.12.18.01.19.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Dec 2024 01:19:04 -0800 (PST) From: Rasmus Villemoes To: Linus Torvalds Cc: Alexei Starovoitov , Daniel Borkmann , Florent Revest , Steven Rostedt , LKML , bpf Subject: Re: [PATCH] vsprintf: simplify number handling In-Reply-To: <20241218013620.1679088-1-torvalds@linux-foundation.org> (Linus Torvalds's message of "Tue, 17 Dec 2024 17:32:09 -0800") References: <20241218013620.1679088-1-torvalds@linux-foundation.org> Date: Wed, 18 Dec 2024 10:19:03 +0100 Message-ID: <871py5gxh4.fsf@prevas.dk> User-Agent: Gnus/5.13 (Gnus v5.13) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain On Tue, Dec 17 2024, Linus Torvalds wrote: > Instead of dealing with all the different special types (size_t, > unsigned char, ptrdiff_t..) just deal with the size of the integer type > and the sign. > > This avoids a lot of unnecessary case statements, and the games we play > with the value of the 'SIGN' flags value > > Signed-off-by: Linus Torvalds > --- > > NOTE! Only very lightly tested. Also meant to be purely preparatory. > It might be broken. > > I started doing this in the hope that the vsnprintf() core code could > maybe be further cleaned up enough that it would actually be something > we could use more generally and export to other users that want to > basically do the printk format handling. > > The code is *not* there yet, though. Small steps. > > +/* Turn a 1/2/4-byte value into a 64-bit one with sign handling */ > +static unsigned long long get_num(unsigned int val, struct printf_spec spec) > +{ > + unsigned int shift = 32 - spec.type*8; > + > + val <<= shift; > + if (!(spec.flags & SIGN)) > + return val >> shift; > + return (int)val >> shift; > +} > + I think this needs to be a little more explicit that it's not just about handling sign extension, but also truncation to the desired width. Otherwise somebody will wonder why this isn't if (!(spec.flags & SIGN)) return val; return (int)(val << shift) >> shift; [with the latter line duplicating our sign_extend32 helper]. The rest looks good. Rasmus