From: David Laight <David.Laight@ACULAB.COM>
To: 'Linus Torvalds' <torvalds@linux-foundation.org>
Cc: Vincent Mailhol <mailhol.vincent@wanadoo.fr>,
Yury Norov <yury.norov@gmail.com>,
Rasmus Villemoes <linux@rasmusvillemoes.dk>,
"Luc Van Oostenryck" <luc.vanoostenryck@gmail.com>,
"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
"linux-sparse@vger.kernel.org" <linux-sparse@vger.kernel.org>,
Rikard Falkeborn <rikard.falkeborn@gmail.com>
Subject: RE: [PATCH v4 1/2] compiler.h: add const_true()
Date: Sun, 17 Nov 2024 22:38:19 +0000 [thread overview]
Message-ID: <2b5282038b1f46bc9a658fb2b6d78350@AcuMS.aculab.com> (raw)
In-Reply-To: <CAHk-=wgfpLdt7SFFGcByTfHdkvv7AEa3MDu_s_W1kfOxQs49pw@mail.gmail.com>
From: Linus Torvalds
> Sent: 17 November 2024 20:12
>
> On Sun, 17 Nov 2024 at 11:23, David Laight <David.Laight@aculab.com> wrote:
> >
> > Since 99% will be 1,0 maybe saving the extra expansion is best anyway.
> > So have is_const_zero(x) and add if_const_zero(x, if_z, if_nz) later.
>
> Ok. So something like this seems to give us the relevant cases:
>
> #define __is_const_zero(x) \
> _Generic(0?(void *)(long)(x):(char *)0, char *:1, void *:0)
>
> #define is_const_zero(x) __is_const_zero(!!(x))
> #define is_const_true(x) __is_const_zero(!(x))
> #define is_const(x) __is_const_zero(0*!(x))
>
> and should work with all scalar expressions that I can think of (ok,
> technically 'void' is a scalar type and it obviously won't work with
> that). And should work in all contexts.
Seems a reasonable set.
Maybe they need a set that are paired with __BUILD_BUG_ON_ZERO_MSG()
to generate an error message on failure.
Although I would add a few more ' ' characters for readability.
> It does want a comment (in addition to the comment about how NULL is
> special for the ternary op that makes it work): the '(long)' cast is
> so that there are no warnings for casting to 'void *' when it's *not*
> a constant zero expression, and the '!' pattern is to turn pointers
> and huge constants into 'int' without loss of information and without
> warnings.
The comments would need to be terse one-liners.
I wonder if it reads better (and without extra comments) if the (long)
cast is removed and the 'callers' are required to generate 'long' args.
So you have:
#define __is_const_zero(x) \
_Generic(0 ? (void *)(x) : (char *)0, char *: 1, void *: 0)
#define is_const_zero(x) __is_const_zero((x) ? 1L : 0L)
#define is_const_true(x) __is_const_zero((x) ? 0L : 1L)
#define is_const(x) __is_const_zero((x) ? 0L : 0L)
I've done a quick test of the last one in godbolt.
David
>
> Compound types obviously will generate a warning. As they should.
>
> The above looks reasonable to me, but I didn't actually test any of it
> in the actual kernel build.
>
> Linus
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
next prev parent reply other threads:[~2024-11-17 22:38 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-11-13 17:18 [PATCH v4 0/2] add const_true() to simplify GENMASK_INPUT_CHECK() Vincent Mailhol
2024-11-13 17:18 ` [PATCH v4 1/2] compiler.h: add const_true() Vincent Mailhol
2024-11-13 18:53 ` Yury Norov
2024-12-30 18:32 ` Yury Norov
2024-12-31 4:58 ` Vincent Mailhol
2024-11-17 17:42 ` David Laight
2024-11-17 18:00 ` Linus Torvalds
2024-11-17 19:02 ` Linus Torvalds
2024-11-17 19:05 ` David Laight
2024-11-17 19:09 ` Linus Torvalds
2024-11-17 19:23 ` David Laight
2024-11-17 20:12 ` Linus Torvalds
2024-11-17 22:38 ` David Laight [this message]
2024-11-17 22:58 ` Linus Torvalds
2024-11-18 3:22 ` Vincent Mailhol
2024-11-18 9:27 ` David Laight
2024-11-18 17:09 ` Linus Torvalds
2024-11-13 17:18 ` [PATCH v4 2/2] linux/bits.h: simplify GENMASK_INPUT_CHECK() Vincent Mailhol
2024-11-17 17:24 ` David Laight
2024-11-17 19:45 ` David Laight
2024-11-18 1:14 ` Vincent Mailhol
2024-11-18 1:12 ` Vincent Mailhol
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=2b5282038b1f46bc9a658fb2b6d78350@AcuMS.aculab.com \
--to=david.laight@aculab.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-sparse@vger.kernel.org \
--cc=linux@rasmusvillemoes.dk \
--cc=luc.vanoostenryck@gmail.com \
--cc=mailhol.vincent@wanadoo.fr \
--cc=rikard.falkeborn@gmail.com \
--cc=torvalds@linux-foundation.org \
--cc=yury.norov@gmail.com \
/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