RE: [PATCH 4/7] minmax: Simplify signedness check
From: David Laight
Date: Thu Jul 25 2024 - 05:01:31 EST
From: Linus Torvalds
> Sent: 24 July 2024 21:03
>
> On Wed, 24 Jul 2024 at 09:49, Arnd Bergmann <arnd@xxxxxxxxxx> wrote:
> >
> > I don't understand why this return '0' for unsigned types,
> > shouldn't this be
> >
> > ((is_unsigned_type(typeof(x)) ? 1 : __if_constexpr(x, (x) + 0, -1)) >= 0)
>
> Yes, that looks more logical.
The condition is '>= 0' so it doesn't matter if it is '1' or '0'.
> Plus why do that "__if_constexpr(x, (x) + 0, -1)) >= 0)" when it would
> appear to be more logical to move the comparison inside, ie
>
> __if_constexpr(x, (x) >= 0, 0)
That gives a 'comparison of unsigned type against 0 is always true' warning.
(The compiler generates that for code in the unused branches of both
__builtin_choose_expr() and _Generic().)
Moving the comparison to the outer level stops all such compiler warnings.
> but I also don't see why that "+ 0" existed in the original. So
> there's presumably something I'm missing.
IIRC it was there to convert a 'bool' to 'int'.
Somewhere the is a max(bool,bool) that could just be |.
If may not be needed now the expansion is '(cond ? 0 : bool) >= 0'
since the 'bool' picks up an (int) cast from the result of ?:.
David
> I do get the feeling that the problem came from us being much too
> clever with out min/max macros, and now this series is doubling down
> instead of saying "it wasn't really worth it".
>
> Linus
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)