Re: small patch for pty.c

From: Horst von Brand (vonbrand@inf.utfsm.cl)
Date: Tue Apr 18 2000 - 08:47:30 EST


Linus Torvalds <torvalds@transmeta.com> said:
> On Mon, 17 Apr 2000, Horst von Brand wrote:
> > Up to the next cleanup, when somebody wonders why your'e not using MIN as
> > a few lines before and changes it back.
> >
> > Better fix MIN to work right, and get rid of _all_ these problems for good.

> No. Wrong moral. You should not "fix" MIN().
>
> MIN() is a _bad_ thing to use. It _always_ gets the sign wrong. Sorry, but
> that's how it is. It makes people think that "oh, it takes the minimum of
> two numbers" and at the same time it makes people completely forget
> signedness issues.

This one doesn't get it wrong:

  #define min(x, y) ({typeof (x) x_ = (x); \
                      typeof (y) y_ = (y); \
                      x_ < y_ ? x_ : y_;})

The variables x_ and y_ are the _same_ types as the x and y the macro gets,
the sizeof-ish behaviour of typeof ensures no evaluation of the expression
(i.e., no side effects); and then it does the comparison as it would with
the original values, just never evaluating anything twice. No (explicit)
mess with temporaries that have to be declared, and no huge expressions
that have to be written twice (only to get one of them wrong, as Murphy's
law assures will happen where you can't see it).

> In short, it's a really stupid macro, for something that is usually
> simpler to do by hand anyway. And doing it by hand you can make it work
> right, without confusion like this.
>
> MIN() should go. If people really want to use a macro for something as
> simple as MIN(), then you should always use UMIN() and SMIN(), and make
> people have to think about signedness issues (but even then you tend to be
> better off just doing it explicitly by hand).

Problem is, if something is int and mutates into unsigned, all macro calls
involving it would have to be changed. My version should take care of the
change automatically.

-- 
Dr. Horst H. von Brand                       mailto:vonbrand@inf.utfsm.cl
Departamento de Informatica                     Fono: +56 32 654431
Universidad Tecnica Federico Santa Maria              +56 32 654239
Casilla 110-V, Valparaiso, Chile                Fax:  +56 32 797513

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



This archive was generated by hypermail 2b29 : Sun Apr 23 2000 - 21:00:13 EST