#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <assert.h>
static
bool is_pow2(uint64_t a)
{
return a && !(a & (a - 1));
}
static
size_t ceil_align(size_t ptr, size_t alignment)
{
assert(alignment);
if(is_pow2(alignment))
{
size_t result = ptr & -alignment;
if(result != ptr) result = result + alignment;
return result;
}
else
{
size_t remain = ptr % alignment;
if(remain == 0) return ptr;
return (ptr - remain) + alignment;
}
}
static
void *ceil_align(void *ptr, size_t alignment)
{
return (void *)ceil_align((size_t)ptr, alignment);
}
int main(void)
{
printf("%lu\n", ceil_align(65, 32));
return 0;
}
// Align stack pointer to 16. Stack must already be aligned by 8.
//mov r10, rsp
//and r10, 1000b
//add rsp, r10