Skip to content

Instantly share code, notes, and snippets.

@jkoppel
Created Jul 26, 2019
Embed
What would you like to do?
Roundup, rounddown
// Rounding operations (efficient when n is a power of 2)
// Round down to the nearest multiple of n
#define ROUNDDOWN(a, n) \
({ \
uint32_t __a = (uint32_t) (a); \
(typeof(a)) (__a - __a % (n)); \
})
// Round up to the nearest multiple of n
#define ROUNDUP(a, n) \
({ \
uint32_t __n = (uint32_t) (n); \
(typeof(a)) (ROUNDDOWN((uint32_t) (a) + __n - 1, __n)); \
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment