Re: [IDEA+RFC] Possible solution for min()/max() war

Peter T. Breuer (ptb@it.uc3m.es)
Fri, 31 Aug 2001 04:10:16 +0200 (MET DST)


"Peter T. Breuer wrote:"
> "Gordon Oliver wrote:"
> > - if the sizeof the arguments is different, it is a bug to have
> > the _larger_ argument unsigned.
>
> This can't be caught at compile time.

Oops - I misinterpreted what you said. You mean "longer", not "larger".
Yes, this can be caught by a variant of the solution I proposed ...

typeof(x) _x = ~(typeof(x))0;
typeof(y) _y = ~(typeof(y))0;
...
if (sizeof(_x) > sizeof(_y) && _x > 0)
BUG();
if (sizeof(_x) < sizeof(_y) && _y > 0)
BUG();

Though I'm too sleepy to think about why there's a bug in this case ...
Isn't it only a bug if the longer arg is unsigned and the shorter is
signed? (I suppose the shorter is promoted with sign and then converted
to unsigned but I'm not going to get up and open a book to find out ..).

That would be:

if (sizeof(_x) > sizeof(_y) && _x > 0 && _y < 0)
BUG();
if (sizeof(_x) < sizeof(_y) && _y > 0 && _x < 0)
BUG();

Moral, always use ints.

Peter
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/