C macro to find the next power of 2 ?

Stefan Bethke stb at lassitu.de
Wed Feb 7 10:42:33 UTC 2007


On Wed, February 7, 2007 09:41, Luigi Rizzo wrote:
> My problem is that some hardware wants data structures aligned to
> the next power of 2 with respect to their size, and the code
> in question (the ehci driver in case you care) has hardwired
> constants for this, and possibly wrong ones.
>
> It would be nice if one could write
>
> 	struct foo_desc {
> 		...
> 	};
>
> 	#define FOO_ALIGN next_power_of_2(sizeof(struct foo_desc))
>
> instead of having to count fields and make guesses on the size
> of pointers and so on.

_Hacker's Delight_ contains many cool tricks, including multiple solutions
for this, IIRC.  I'll have a look tonight when I'm back home.


Cheers,
Stefan

-- 
Stefan Bethke <stb at lassitu.de> +49 170 3460140



More information about the freebsd-current mailing list