Search code examples
clinuxlinux-kerneloperatorsbitwise-operators

align macro kernel


I am unable to understand what this macro does. These are defined in linux-kernel but my doubt is independent of that. I am unable to understand what does (((x)+(mask))&~(mask)) line does.

#define ALIGN(x,a)              __ALIGN_MASK(x,(typeof(x))(a)-1)
#define __ALIGN_MASK(x,mask)    (((x)+(mask))&~(mask))

Any help appreciated.


Solution

  • Say you have a number: 0x1006

    For some reasons you want to align it to a 4 bytes boundary.

    With a 4-byte boundary, you know aligned values are 0x1000, 0x1004, 0x1008, etc. You then also know the aligned value of 0x1006 is 0x1008.

    How would you get 0x1008? The alignment mask for alignment value 4 is (4 - 1) = 0x03

    Now 0x1006 + 0x03 = 0x1009 and 0x1009 & ~0x03 = 0x1008

    This operation is the __ALIGN_MASK macro.

    If you want to pass the value 4 (the alignment) instead of directly 0x03 (the alignment mask), you have the ALIGN macro