[I'm behind on my mail :-)]
On Wed, 12 Sep 2001 13:19:40 -0400, Dan Sugalski wrote:
>We're trying to align to a power-of-two boundary, and mask is set to
>chop off the low bits, not the high ones. It should be something like:
>
> 111111110000
>
>The calc:
>
> mem & mask + (~mask + 1)
>
>will chop the low bits off of mem, making it too small, but power-of-two
>aligned. Then we add in the inverse of mask + 1 (in the above example,
>that'd be 10000) to jump it to the next power-of-two boundary.
>
>Horribly wasteful of memory, definitely, and the final allocation system
>will do things better, but this is OK to start.
So to stop it waste memory, subtract 1 first and add it again later.
((mem-1) | ~mask) + 1
or (equivalent):
(mem+~mask) & mask
mask is a constant, like -8 (...111111000), so ~mask is a constant too,
like 7 (...00000111).
There must be even more tricks like this.
--
Bart.