svn commit: r273274 - head/sys/netpfil/ipfw

Bruce Simpson bms at fastmail.net
Mon Oct 20 20:10:46 UTC 2014



On Sun, 19 Oct 2014, at 13:02, Andriy Gapon wrote:
> I think that on platforms where an optimized version of fls() is
> available that
> would work faster than this cool piece of bit magic.

This is a common enough idiom that perhaps a macro should be added:

sys/param.h:
#define roundup(x, y)   ((((x)+((y)-1))/(y))*(y))  /* to any y */
#define roundup2(x, y)  (((x)+((y)-1))&(~((y)-1))) /* if y is powers of
two */
#define powerof2(x)     ((((x)-1)&(x))==0)

sys/amd64/amd64/mp_machdep.c:
/*
 * Round up to the next power of two, if necessary, and then
 * take log2.
 * Returns -1 if argument is zero.
 */
static __inline int
mask_width(u_int x)
{

	return (fls(x << (1 - powerof2(x))) - 1);
}


-- 
BMS (sent via webmail)


More information about the svn-src-head mailing list