Created
July 26, 2019 20:10
-
-
Save jkoppel/00ccd01ee22f5c89fe89c5b32819dd71 to your computer and use it in GitHub Desktop.
Roundup, rounddown
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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