| 126 | |
| 127 | template <typename T> |
| 128 | constexpr T RoundUpToPowerOfTwo(T x) { |
| 129 | static_assert(std::is_integral<T>::value, "T must be integral"); |
| 130 | static_assert(std::is_unsigned<T>::value, "T must be unsigned"); |
| 131 | // NOTE: Undefined if x > (1 << (std::numeric_limits<T>::digits - 1)). |
| 132 | return (x < 2u) ? x : static_cast<T>(1u) << (std::numeric_limits<T>::digits - CLZ(x - 1u)); |
| 133 | } |
| 134 | |
| 135 | // Return highest possible N - a power of two - such that val >= N. |
| 136 | template <typename T> |