From d92b5fc435b5444314fc1ce407a908ba0222f318 Mon Sep 17 00:00:00 2001 From: Morph <39850852+Morph1984@users.noreply.github.com> Date: Mon, 10 Jan 2022 19:44:19 -0500 Subject: common: bit_util: Add IsPow2 helper function Makes use of std::has_single_bit() to check whether the value is a power of 2. --- src/common/bit_util.h | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'src/common/bit_util.h') diff --git a/src/common/bit_util.h b/src/common/bit_util.h index eef8c1c5a..f50d3308a 100644 --- a/src/common/bit_util.h +++ b/src/common/bit_util.h @@ -45,6 +45,12 @@ template return static_cast(log2_f + static_cast((value ^ (1ULL << log2_f)) != 0ULL)); } +template +requires std::is_unsigned_v +[[nodiscard]] constexpr bool IsPow2(T value) { + return std::has_single_bit(value); +} + template requires std::is_integral_v [[nodiscard]] T NextPow2(T value) { -- cgit v1.2.3