Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 07 Feb 2007 02:01:01 -0800
From:      Bakul Shah <bakul@bitblocks.com>
To:        Luigi Rizzo <rizzo@icir.org>
Cc:        current@freebsd.org
Subject:   Re: C macro to find the next power of 2 ? 
Message-ID:  <20070207100101.211C95B2E@mail.bitblocks.com>
In-Reply-To: Your message of "Wed, 07 Feb 2007 00:41:31 PST." <20070207004131.A62183@xorpc.icir.org> 

next in thread | previous in thread | raw e-mail | index | archive | help
> 	#define b2(x)   (   (x) | (   (x) >> 1) )
> 	#define b4(x)   ( b2(x) | ( b2(x) >> 2) )
> 	#define b8(x)   ( b4(x) | ( b4(x) >> 4) )
> 	#define b16(x)  ( b8(x) | ( b8(x) >> 8) )
> 	#define b32(x)  (b16(x) | (b16(x) >>16) )
> 	#define next_power_of_2(x)      (b32(x-1) + 1)
...
> But i was wondering if there is some simpler code which
> can still be computed as a compile-time constant ?

If this works, why bother?  let the compiler work harder!



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20070207100101.211C95B2E>