utility.hpp (853B)
1 #ifndef UTILITY_HPP 2 #define UTILITY_HPP 3 4 #include <cassert> 5 #include <cstddef> 6 #include <cstdint> 7 8 namespace Test::Utility { 9 constexpr bool IsPowerOfTwo(uintptr_t value) { 10 return (value & (value - 1)) == 0; 11 } 12 13 constexpr bool IsAlignedTo(uintptr_t value, size_t alignment) { 14 assert(IsPowerOfTwo(alignment)); 15 16 uintptr_t mask = (alignment - 1); 17 uintptr_t res = value & mask; 18 19 return res == 0; 20 } 21 22 constexpr uintptr_t AlignPrev(uintptr_t value, size_t alignment) { 23 assert(IsPowerOfTwo(alignment)); 24 25 uintptr_t mask = ~(alignment - 1); 26 uintptr_t res = value & mask; 27 28 return res; 29 } 30 31 constexpr uintptr_t AlignNext(uintptr_t value, size_t alignment) { 32 assert(IsPowerOfTwo(alignment)); 33 34 uintptr_t upper_bound = value + (alignment - 1); 35 uintptr_t res = AlignPrev(upper_bound, alignment); 36 37 return res; 38 } 39 } 40 41 #endif /* UTILITY_HPP */