RE: [PATCH next v4 0/5] minmax: Relax type checks in min() and max().

From: David Laight
Date: Fri Jan 12 2024 - 07:16:33 EST


From: Dan Carpenter
> Sent: 12 January 2024 09:13
>
> I've often wondered why so many people use min_t(int, size, limit) when
> they really do not want negative sizes... Is there a performance reason?
> git grep 'min_t(int,' says there are 872 instances of this. Probably
> some do want negatives but it's a quite small percent.

But you really don't a negative 'size' converted to a large
unsigned value above the limit - that would be worse.
All the type checking is there to stop that happening.

Even with my changes min(int_var, sizeof()) is a compile error.
To do otherwise would really requite the sizeof() to be converted
to int - leaving the other type alone.
Easiest done by using 'int' instead of 'typeof(y)' for the
local variable inside cmp_once().

However Linus didn't like that change so I removed it from the
version that got committed.
It would also bloat the expansion even more.
That could be reduced by expecting min(var, const) not min(const, var)
and only doing all the constant checks on the second argument.

FWIW min_t() should really skip all the type checks.
Once both value have been cast the 'same type' test will pass.

David

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