From 95ad0d0a66ee935fd7b0c5b0392abaffe14f9ff2 Mon Sep 17 00:00:00 2001 From: MerryMage Date: Wed, 27 Jun 2018 14:37:52 +0100 Subject: [PATCH] bit_util: Use Ones to implement Bits --- src/common/bit_util.h | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/common/bit_util.h b/src/common/bit_util.h index 0ed24da5..b83fc6ec 100644 --- a/src/common/bit_util.h +++ b/src/common/bit_util.h @@ -21,15 +21,23 @@ constexpr size_t BitSize() { return sizeof(T) * CHAR_BIT; } +template +inline T Ones(size_t count) { + ASSERT_MSG(count <= BitSize(), "count larger than bitsize of T"); + if (count == BitSize()) + return static_cast(~static_cast(0)); + return ~(static_cast(~static_cast(0)) << count); +} + /// Extract bits [begin_bit, end_bit] inclusive from value of type T. template constexpr T Bits(const T value) { static_assert(begin_bit <= end_bit, "invalid bit range (position of beginning bit cannot be greater than that of end bit)"); static_assert(begin_bit < BitSize(), "begin_bit must be smaller than size of T"); - static_assert(end_bit < BitSize(), "begin_bit must be smaller than size of T"); + static_assert(end_bit < BitSize(), "end_bit must be smaller than size of T"); - return (value >> begin_bit) & ((1 << (end_bit - begin_bit + 1)) - 1); + return (value >> begin_bit) & Ones(end_bit - begin_bit + 1); } #ifdef _MSC_VER @@ -143,14 +151,6 @@ inline size_t LowestSetBit(T value) { return result; } -template -inline T Ones(size_t count) { - ASSERT_MSG(count <= BitSize(), "count larger than bitsize of T"); - if (count == BitSize()) - return ~static_cast(0); - return ~(~static_cast(0) << count); -} - template inline T Replicate(T value, size_t element_size) { ASSERT_MSG(BitSize() % element_size == 0, "bitsize of T not divisible by element_size");