diff options
Diffstat (limited to 'asl/base/numeric.hpp')
-rw-r--r-- | asl/base/numeric.hpp | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/asl/base/numeric.hpp b/asl/base/numeric.hpp index bbd229d..8d3b8ef 100644 --- a/asl/base/numeric.hpp +++ b/asl/base/numeric.hpp @@ -7,6 +7,7 @@ #include "asl/base/integers.hpp" #include "asl/base/bit.hpp" #include "asl/base/meta.hpp" +#include "asl/base/assert.hpp" namespace asl { @@ -14,10 +15,24 @@ namespace asl template<is_integer T> constexpr bool is_pow2(T x) { - using unsigned_type = select_t<is_unsigned_integer<T>, T, as_unsigned_integer<T>>; + using unsigned_type = as_unsigned_integer<T>; return x > 0 && has_single_bit(static_cast<unsigned_type>(x)); } +template<is_integer T> +constexpr T round_down_pow2(T x, T div) +{ + ASL_ASSERT(is_pow2(div)); + return x & (-div); +} + +template<is_integer T> +constexpr T round_up_pow2(T x, T div) +{ + ASL_ASSERT(is_pow2(div)); + return (x + (div - 1)) & (-div); +} + template<typename T> concept is_numeric = is_integer<T> || is_floating_point<T>; |