12345678910111213141516171819202122232425262728293031323334353637383940 |
-
- #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;
- }
|