diff options
author | bunnei <bunneidev@gmail.com> | 2022-01-20 18:06:11 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-20 18:06:11 -0800 |
commit | ef7c50b276611daf21b66cbd9e82e01e8663024f (patch) | |
tree | f8a1d0bde3bb1a5551fcd25a4443b2b48682c095 /src | |
parent | e781f6e7676351302eba229bb940ed4923b32f3b (diff) | |
parent | d92b5fc435b5444314fc1ce407a908ba0222f318 (diff) |
Merge pull request #7695 from Morph1984/is-pow2
common: bit_util: Add IsPow2 helper function
Diffstat (limited to 'src')
-rw-r--r-- | src/common/bit_util.h | 6 |
1 files changed, 6 insertions, 0 deletions
diff --git a/src/common/bit_util.h b/src/common/bit_util.h index eef8c1c5a7..f50d3308a9 100644 --- a/src/common/bit_util.h +++ b/src/common/bit_util.h @@ -46,6 +46,12 @@ template <typename T> } template <typename T> +requires std::is_unsigned_v<T> +[[nodiscard]] constexpr bool IsPow2(T value) { + return std::has_single_bit(value); +} + +template <typename T> requires std::is_integral_v<T> [[nodiscard]] T NextPow2(T value) { return static_cast<T>(1ULL << ((8U * sizeof(T)) - std::countl_zero(value - 1U))); |