Subject: Re: getting rid of NTOHS() in ip_input()
To: None <tech-net@netbsd.org>
From: der Mouse <mouse@Rodents.Montreal.QC.CA>
List: tech-net
Date: 08/13/2002 12:04:02
> 	one of them (ip_off & 0x7) i kept byteswap ip_off, as the meaning of
> 	"0x7" becomes non-obvious if we byteswap it.

Surely the right thing is "ip_off & htons(0x7)", and let the compiler
constant-fold when possible?  If htons is done right, a smart compiler
could constant-fold it even on little-endian machines.  (On big-endian
machines, of course, [hn]to[hn]? all go away anyway.)  And even if the
compiler doesn't constant-fold, swapping 7 is no worse than swapping a
run-time variable, and leaves the way open for optimization when
compilers smarten up.

Perhaps htons should be written with __builtin_constant_p to handle
cases like this?

#define htons(x) (__builtin_constant_p(x) ?			\
			((((x)&0xff)<<8)|(((x)>>8)&0xff) :	\
			(htons)(x) )

(I think what I want is __builtin_constant_p.)

/~\ The ASCII				der Mouse
\ / Ribbon Campaign
 X  Against HTML	       mouse@rodents.montreal.qc.ca
/ \ Email!	     7D C8 61 52 5D E7 2D 39  4E F1 31 3E E8 B3 27 4B