RE: [PATCH v4 1/2] compiler.h: add const_true()

From: David Laight
Date: Mon Nov 18 2024 - 04:27:29 EST


From: Vincent Mailhol
> Sent: 18 November 2024 03:22
>
> On Mon. 18 nov. 2024 à 07:58, Linus Torvalds
> <torvalds@xxxxxxxxxxxxxxxxxxxx> wrote:
> > The "0*!(x)" is admittedly kind of ugly, and might be prettier as
> > "0&&(x)". Same number of characters, but technically one op less and
> > not mixing booleans and integer ops.
>
> I did a tree wide replacement of __is_constexpr() with is_const() and
> did an allyesconfig build test. It yields a -Wint-in-bool-context
> warning in GCC for both the "0*!(x)" and the "0&&(x)" each time the
> expression contains non-boolean operators, for example: * or <<.
>
> I reproduced it in godbolt here:
>
> https://godbolt.org/z/5Wcbvanq3

Applies to pretty much all the variants.
Needs to be (x) == 0 (or (x) != 0) rather than !(x)

Fortunately comparison operators (and ?:) are all valid in
constant integer expressions.

Oh, one advantage of statically_const() is that you can give
it a 'local' variable that contains the value.
So this works:
#define check_lo_ho(lo, hi) do { \
__auto_type _lo = lo; \
__auto_type _hi = hi; \
BUILD_BUG_ON_MSG(_lo > _hi, "inverted bounds"); \
} while (0)

I'm trying to finalise a patch for min() and max().

David



-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)