[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <6576cf10fc424eab874a31714e60474b@AcuMS.aculab.com>
Date: Fri, 6 Dec 2024 09:34:15 +0000
From: David Laight <David.Laight@...LAB.COM>
To: 'Vincent Mailhol' <mailhol.vincent@...adoo.fr>, 'Martin Uecker'
<muecker@...g.de>
CC: 'Linus Torvalds' <torvalds@...ux-foundation.org>, 'Luc Van Oostenryck'
<luc.vanoostenryck@...il.com>, 'Nathan Chancellor' <nathan@...nel.org>,
"'Nick Desaulniers'" <ndesaulniers@...gle.com>, 'Bill Wendling'
<morbo@...gle.com>, 'Justin Stitt' <justinstitt@...gle.com>, 'Yury Norov'
<yury.norov@...il.com>, 'Rasmus Villemoes' <linux@...musvillemoes.dk>, 'Kees
Cook' <kees@...nel.org>, "'Gustavo A. R. Silva'" <gustavoars@...nel.org>,
'Jani Nikula' <jani.nikula@...ux.intel.com>, 'Joonas Lahtinen'
<joonas.lahtinen@...ux.intel.com>, 'Rodrigo Vivi' <rodrigo.vivi@...el.com>,
'Tvrtko Ursulin' <tursulin@...ulin.net>, 'David Airlie' <airlied@...il.com>,
'Simona Vetter' <simona@...ll.ch>, 'Suzuki K Poulose'
<suzuki.poulose@....com>, 'Mike Leach' <mike.leach@...aro.org>, 'James Clark'
<james.clark@...aro.org>, 'Alexander Shishkin'
<alexander.shishkin@...ux.intel.com>, 'Rikard Falkeborn'
<rikard.falkeborn@...il.com>, "'linux-sparse@...r.kernel.org'"
<linux-sparse@...r.kernel.org>, "'linux-kernel@...r.kernel.org'"
<linux-kernel@...r.kernel.org>, "'llvm@...ts.linux.dev'"
<llvm@...ts.linux.dev>, "'linux-hardening@...r.kernel.org'"
<linux-hardening@...r.kernel.org>, "'intel-gfx@...ts.freedesktop.org'"
<intel-gfx@...ts.freedesktop.org>, "'dri-devel@...ts.freedesktop.org'"
<dri-devel@...ts.freedesktop.org>, "'coresight@...ts.linaro.org'"
<coresight@...ts.linaro.org>, "'linux-arm-kernel@...ts.infradead.org'"
<linux-arm-kernel@...ts.infradead.org>
Subject: RE: [PATCH 02/10] compiler.h: add is_const() as a replacement of
__is_constexpr()
From: David Laight
> Sent: 06 December 2024 02:26
(now it is no longer 2am...)
Linus suggested this a while back:
> in https://lore.kernel.org/all/CAHk-=wiq=GUNWJwWh1CRAYchW73UmOaSkaCovLatfDKeveZctA@mail.gmail.com/
>
> /*
> * iff 'x' is a non-zero constant integer expression,
> * then '!(x)' will be a zero constant integer expression,
> * and casting that to 'void *' will result in a NULL pointer.
> * Otherwise casting it to 'void *' will be just a regular 'void *'.
> *
> * The type of '0 ? NULL : (char *)' is 'char *'
> * The type of '0 ? (void *) : (char *) is 'void *'
> */
> #define const_true(x) \
> _Generic(0 ? (void *)((long)!(x)) : (char *)0, char *: 1, void *: 0)
But some of the things built on it has issues with compiler warnings.
I think there have several variations before and since with subtle differences.
Probably const_zero() const_true() const_false() and const_expr().
While the 'base' define is really const_zero() with just (long)(x) that
will mask high bits off 'long long'.
A const_false() could 'fix' that using (long)!!(x) but even !(x) starts
giving compile errors.
If called for pointers (long)((x) == 0) is also problematic.
(Perhaps this is less likely now that min/max don't use it.)
So we may end up with (long)((x) ? 0 : 1) which really might as
well be written ((x) ? 0L : 1L).
The use is likely to be const_true(x > y) so perhaps there isn't a
reason to have const_false() since the boolean operator can be inverted.
const_expr() has relied on '* 0' to make all constant expressions zero.
But it has to handle pointers - so needs a conditional.
Since it is only one line, maybe just replicate the whole thing as:
#define const_true(x) _Generic(0 ? (void *)((x) ? 0L : 1L) : (char *)0, char *: 1, void *: 0)
#define const_expr(x) _Generic(0 ? (void *)((x) ? 0L : 0L) : (char *)0, char *: 1, void *: 0)
Or maybe (mostly because the lines are shorter):
#define const_NULL(x) _Generic(0 ? (x) : (char *)0, char *: 1, void *: 0)
#define const_true(x) const_NULL((void *)(x) ? 0L : 1L))
#define const_expr(x) const_NULL((void *)(x) ? 0L : 0L))
Or even:
#define const_true(x) const_NULL((x) ? NULL : (void *)1L))
#define const_expr(x) const_NULL((x) ? NULL : NULL))
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
Powered by blists - more mailing lists