Mjolnir Core
Core functionality of the Mjolnir API

◆ misalignment() [2/2]

auto misalignment ( const volatile T_Type *  pointer,
UST  alignment 
) -> UST
inlinenoexcept

Calculate the misalignment of a pointer.

Template Parameters
T_TypeThe pointer type
Parameters
[in]pointerPointer that should be checked
[in]alignmentRequired alignment
Returns
Misalignment of the pointer in bytes
Remarks
Use the template version if the required alignment is known at compile-time. It is usually faster. Clang 10 with -O3 generates the following assembly for this function:
misalignment(void const volatile*, unsigned long):
mov rax, rdi
xor edx, edx
div rsi
mov rax, rdx
ret
auto misalignment(const volatile T_Type *pointer) noexcept -> UST
Calculate the misalignment of a pointer.
Definition: pointer_operations.h:211
The output for the template version with 4 byte alignment looks like this:
unsigned long misalignment<4ul>(void const volatile*):
mov rax, rdi
and eax, 3
ret
Note that it consists of less instructions and that the expensive div instruction is removed.