You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
726 B
40 lines
726 B
|
|
#include <stdint.h>
|
|
|
|
#ifdef DEBUG
|
|
#include <stdio.h>
|
|
#endif
|
|
|
|
void* align_after(void* address, int align) {
|
|
uintptr_t addr = (uintptr_t) address;
|
|
uintptr_t offset = addr % align;
|
|
void *rv;
|
|
if (offset == 0) {
|
|
rv = address;
|
|
} else {
|
|
rv = (void*) (addr + align - align % offset);
|
|
}
|
|
#ifdef DEBUG
|
|
printf("aligning %p at alignment %x to %p\n", address, align, rv);
|
|
#endif
|
|
return rv;
|
|
}
|
|
|
|
uintptr_t gcd(uintptr_t a, uintptr_t b) {
|
|
if (b > a) {
|
|
return gcd(b, a);
|
|
}
|
|
if (b == 0) {
|
|
return a;
|
|
}
|
|
return gcd(b, a % b);
|
|
}
|
|
|
|
uintptr_t lcm(uintptr_t a, uintptr_t b) {
|
|
uintptr_t lcm = (a / gcd(a, b)) * b;
|
|
#ifdef DEBUG
|
|
printf("so apparently lcm(%lu, %lu) = %lu\n", a, b, lcm);
|
|
#endif
|
|
return lcm;
|
|
}
|
|
|