Created
January 3, 2020 10:11
-
-
Save lvpidadiao/902fcedb3dc1ff27690737be3bdbe367 to your computer and use it in GitHub Desktop.
floor or ceil power of 2
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
inline constexpr uint32_t ceilPowerOfTwo(uint32_t num) { | |
num |= (num >> 1); | |
num |= (num >> 2); | |
num |= (num >> 4); | |
num |= (num >> 8); | |
num |= (num >> 16); | |
return num - (num >> 1); | |
} | |
inline constexpr uint32_t roundupPowerOfTwo(uint32_t num) { | |
return ceilPowerOfTwo((num - 1) << 1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment