Re: byteorder mess

Linus Torvalds (torvalds@transmeta.com)
Fri, 5 Dec 1997 10:12:50 -0800 (PST)


On Fri, 5 Dec 1997, Jes Sorensen wrote:
>
> The hton[sl]() stuff is horribly broken!

You aren't really batting a thousand today..

> hton[sl]() _must_ be implemented using inline functions, using defines
> breaks the networking part of libc.

No. hton[sl] _must_ be implemented as defines, because doing them as
inline functions breaks the nice optimization that you were so proud of
just two emails ago. Gcc doesn't do constant propagation into inline
functions (I do hope egcs will fix this), so the __builtin_constant_p()
thing doesn't work unless it's a define.

And the way __builtin_constant_p() works, it doesn't have any side
effects, so

__builtin_constant_p(x)? x : x;

will actually do the right thing even in the case where 'x' has side
effects.. In particular, it's also ok to do

__builtin_constant_p(x)? ((x)+(x)) : x;

or something, because _if_ 'x' is constant, then it also cannot have any
side effects, so then it's ok to use it multiple times.

HOWEVER, the architecture-specific swab things do indeed have to be inline
functions in order for side effects to happen only once for the case when
it's not constant. That may be a bug, but at that point it's no longer a
bug in the generic routines, it's a bug in the architecture-specific ones.

Linus