Skip to content

Instantly share code, notes, and snippets.

@jkoppel
Created July 26, 2019 20:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkoppel/00ccd01ee22f5c89fe89c5b32819dd71 to your computer and use it in GitHub Desktop.
Save jkoppel/00ccd01ee22f5c89fe89c5b32819dd71 to your computer and use it in GitHub Desktop.
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