Date: Mon, 20 Oct 2014 21:10:43 +0100 From: Bruce Simpson <bms@fastmail.net> To: Andriy Gapon <avg@FreeBSD.org>, "Alexander V. Chernikov" <melifaro@FreeBSD.org>, src-committers@FreeBSD.org, svn-src-all@FreeBSD.org, svn-src-head@FreeBSD.org Subject: Re: svn commit: r273274 - head/sys/netpfil/ipfw Message-ID: <1413835843.3284180.181238849.236EFDEE@webmail.messagingengine.com> In-Reply-To: <5443A83F.5090807@FreeBSD.org> References: <201410191115.s9JBFJxA058370@svn.freebsd.org> <5443A83F.5090807@FreeBSD.org>
next in thread | previous in thread | raw e-mail | index | archive | help
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)
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?1413835843.3284180.181238849.236EFDEE>
