#define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1)
the alignment, a
, is cast to x
's type, and then one is subtracted. The alignment should be a power of 2, so that results in a number of the bit-pattern 00..011..11
of x
's type, the mask (k
1s if a = 2^k
).
Then
#define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask))
adds the value of the mask to x
, so that (x)+ (mask)
is at least as large as the smallest multiple of the alignment that is not smaller than x
and smaller than the next larger multiple. Then the bitwise and with the complement of the mask reduces that number to that multiple of the alignment.
For masks of the form 2^k - 1
, the computation
(x + mask) & ~mask
is the same as
(x + 2^k - 1) - ((x + 2^k - 1) % (2^k))
or
((x + 2^k - 1)/(2^k)) * (2^k)