From: Rasmus Villemoes <linux@rasmusvillemoes.dk>
To: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Alexei Starovoitov <alexei.starovoitov@gmail.com>,
Daniel Borkmann <daniel@iogearbox.net>,
Florent Revest <revest@google.com>,
Steven Rostedt <rostedt@goodmis.org>,
LKML <linux-kernel@vger.kernel.org>, bpf <bpf@vger.kernel.org>
Subject: Re: [PATCH] vsprintf: simplify number handling
Date: Wed, 18 Dec 2024 10:19:03 +0100 [thread overview]
Message-ID: <871py5gxh4.fsf@prevas.dk> (raw)
In-Reply-To: <20241218013620.1679088-1-torvalds@linux-foundation.org> (Linus Torvalds's message of "Tue, 17 Dec 2024 17:32:09 -0800")
On Tue, Dec 17 2024, Linus Torvalds <torvalds@linux-foundation.org> 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 <torvalds@linux-foundation.org>
> ---
>
> 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
next prev parent reply other threads:[~2024-12-18 9:19 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20241217173237.836878448@goodmis.org>
[not found] ` <20241217173520.314190793@goodmis.org>
[not found] ` <CAHk-=wg5Kcr=sBuZcWs90CSGbJuKy0QsLaCC5oD15gS+Hk8j1A@mail.gmail.com>
[not found] ` <20241217130454.5bb593e8@gandalf.local.home>
[not found] ` <CAHk-=whLJW1SWvJTHYmdVAL2yL=dh4RzMuxgT7rnksSpkfUVaA@mail.gmail.com>
[not found] ` <20241217133318.06f849c9@gandalf.local.home>
[not found] ` <CAHk-=wgi1z85Cs4VmxTqFiG75qzoS_h_nszg6qP1ennEpdokkw@mail.gmail.com>
[not found] ` <20241217140153.22ac28b0@gandalf.local.home>
[not found] ` <CAHk-=wgpjLhSv9_rnAGS1adekEHMHbjVFvmZEuEmVftuo2sJBw@mail.gmail.com>
[not found] ` <20241217144411.2165f73b@gandalf.local.home>
[not found] ` <CAHk-=whWfmZbwRmySSpOyYEZJgcKG3d-qheYidnwu+b+rk6THg@mail.gmail.com>
[not found] ` <20241217175301.03d25799@gandalf.local.home>
[not found] ` <CAHk-=wg9x1Xt2cmiBbCz5XTppDQ=RNkjkmegwaF6=QghG6kBtA@mail.gmail.com>
2024-12-18 0:47 ` [PATCH 1/3] ring-buffer: Add uname to match criteria for persistent ring buffer Alexei Starovoitov
2024-12-18 1:26 ` Linus Torvalds
2024-12-18 1:32 ` [PATCH] vsprintf: simplify number handling Linus Torvalds
2024-12-18 9:19 ` Rasmus Villemoes [this message]
2024-12-18 15:32 ` Steven Rostedt
2024-12-18 17:32 ` Linus Torvalds
2024-12-18 18:04 ` Steven Rostedt
2024-12-18 18:31 ` Steven Rostedt
2024-12-23 20:11 ` Linus Torvalds
2024-12-18 1:39 ` [PATCH 1/3] ring-buffer: Add uname to match criteria for persistent ring buffer Linus Torvalds
2024-12-18 1:53 ` Alexei Starovoitov
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=871py5gxh4.fsf@prevas.dk \
--to=linux@rasmusvillemoes.dk \
--cc=alexei.starovoitov@gmail.com \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=linux-kernel@vger.kernel.org \
--cc=revest@google.com \
--cc=rostedt@goodmis.org \
--cc=torvalds@linux-foundation.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox