C macro to find the next power of 2 ?

Luigi Rizzo rizzo at icir.org
Wed Feb 7 09:43:46 UTC 2007


On Wed, Feb 07, 2007 at 10:20:34AM +0100, Hans Petter Selasky wrote:
> Hi Luigi,
> 
> Why don't you hardcode the structures with the correct alignment, and maybe 
> you find the __align() macro useful.

because i want the compiler to do the dirty work -- compute the
alignment for me.

The __align() macro, which in the end expands to

	__attribute__ ((aligned (n)))

doesn't help here because requires you to supply a value for
the alignment. The other form (without 'n') computes it but only

	"to the maximum useful alignment for the target machine you
	are compiling for".

(see http://gcc.gnu.org/onlinedocs/gcc-4.1.1/gcc/Type-Attributes.html#Type-Attributes )

The example in the manpage is ambiguous but basically at
least on gcc 3.4.6 and RELENG_6 / i386 the automatic
alignment defaults to 16 bytes (hence the size is just
the next multiple of 16, not the next power of 2).

> What you are discovering is an old issue. Hardware structures must be 
> indexable without addition.

well the issue for me is just how to make the compiler do the calculation.

	cheers
	luigi


More information about the freebsd-current mailing list