Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef ART_RUNTIME_BASE_BIT_UTILS_H_ |
| 18 | #define ART_RUNTIME_BASE_BIT_UTILS_H_ |
| 19 | |
| 20 | #include <iterator> |
| 21 | #include <limits> |
| 22 | #include <type_traits> |
| 23 | |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 24 | #include "base/iteration_range.h" |
Andreas Gampe | 58246a1 | 2016-09-26 12:51:53 -0700 | [diff] [blame] | 25 | #include "base/logging.h" |
Vladimir Marko | 88b2b80 | 2015-12-04 14:19:04 +0000 | [diff] [blame] | 26 | #include "base/stl_util.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 27 | |
| 28 | namespace art { |
| 29 | |
| 30 | template<typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 31 | constexpr int CLZ(T x) { |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 32 | static_assert(std::is_integral<T>::value, "T must be integral"); |
Andreas Gampe | 151ab8d | 2015-08-14 23:01:49 +0000 | [diff] [blame] | 33 | static_assert(std::is_unsigned<T>::value, "T must be unsigned"); |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 34 | static_assert(sizeof(T) <= sizeof(long long), // NOLINT [runtime/int] [4] |
| 35 | "T too large, must be smaller than long long"); |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 36 | DCHECK_NE(x, 0u); |
| 37 | return (sizeof(T) == sizeof(uint32_t)) ? __builtin_clz(x) : __builtin_clzll(x); |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | template<typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 41 | constexpr int CTZ(T x) { |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 42 | static_assert(std::is_integral<T>::value, "T must be integral"); |
Andreas Gampe | 151ab8d | 2015-08-14 23:01:49 +0000 | [diff] [blame] | 43 | // It is not unreasonable to ask for trailing zeros in a negative number. As such, do not check |
| 44 | // that T is an unsigned type. |
| 45 | static_assert(sizeof(T) <= sizeof(long long), // NOLINT [runtime/int] [4] |
| 46 | "T too large, must be smaller than long long"); |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 47 | DCHECK_NE(x, static_cast<T>(0)); |
| 48 | return (sizeof(T) == sizeof(uint32_t)) ? __builtin_ctz(x) : __builtin_ctzll(x); |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 49 | } |
| 50 | |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 51 | // Return the number of 1-bits in `x`. |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 52 | template<typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 53 | constexpr int POPCOUNT(T x) { |
| 54 | return (sizeof(T) == sizeof(uint32_t)) ? __builtin_popcount(x) : __builtin_popcountll(x); |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | // Find the bit position of the most significant bit (0-based), or -1 if there were no bits set. |
| 58 | template <typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 59 | constexpr ssize_t MostSignificantBit(T value) { |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 60 | static_assert(std::is_integral<T>::value, "T must be integral"); |
| 61 | static_assert(std::is_unsigned<T>::value, "T must be unsigned"); |
| 62 | static_assert(std::numeric_limits<T>::radix == 2, "Unexpected radix!"); |
| 63 | return (value == 0) ? -1 : std::numeric_limits<T>::digits - 1 - CLZ(value); |
| 64 | } |
| 65 | |
| 66 | // Find the bit position of the least significant bit (0-based), or -1 if there were no bits set. |
| 67 | template <typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 68 | constexpr ssize_t LeastSignificantBit(T value) { |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 69 | static_assert(std::is_integral<T>::value, "T must be integral"); |
| 70 | static_assert(std::is_unsigned<T>::value, "T must be unsigned"); |
| 71 | return (value == 0) ? -1 : CTZ(value); |
| 72 | } |
| 73 | |
| 74 | // How many bits (minimally) does it take to store the constant 'value'? i.e. 1 for 1, 3 for 5, etc. |
| 75 | template <typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 76 | constexpr size_t MinimumBitsToStore(T value) { |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 77 | return static_cast<size_t>(MostSignificantBit(value) + 1); |
| 78 | } |
| 79 | |
| 80 | template <typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 81 | constexpr T RoundUpToPowerOfTwo(T x) { |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 82 | static_assert(std::is_integral<T>::value, "T must be integral"); |
| 83 | static_assert(std::is_unsigned<T>::value, "T must be unsigned"); |
| 84 | // NOTE: Undefined if x > (1 << (std::numeric_limits<T>::digits - 1)). |
| 85 | return (x < 2u) ? x : static_cast<T>(1u) << (std::numeric_limits<T>::digits - CLZ(x - 1u)); |
| 86 | } |
| 87 | |
| 88 | template<typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 89 | constexpr bool IsPowerOfTwo(T x) { |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 90 | static_assert(std::is_integral<T>::value, "T must be integral"); |
| 91 | // TODO: assert unsigned. There is currently many uses with signed values. |
| 92 | return (x & (x - 1)) == 0; |
| 93 | } |
| 94 | |
| 95 | template<typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 96 | constexpr int WhichPowerOf2(T x) { |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 97 | static_assert(std::is_integral<T>::value, "T must be integral"); |
| 98 | // TODO: assert unsigned. There is currently many uses with signed values. |
| 99 | DCHECK((x != 0) && IsPowerOfTwo(x)); |
| 100 | return CTZ(x); |
| 101 | } |
| 102 | |
| 103 | // For rounding integers. |
Vladimir Marko | 88b2b80 | 2015-12-04 14:19:04 +0000 | [diff] [blame] | 104 | // Note: Omit the `n` from T type deduction, deduce only from the `x` argument. |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 105 | template<typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 106 | constexpr T RoundDown(T x, typename Identity<T>::type n) WARN_UNUSED; |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 107 | |
| 108 | template<typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 109 | constexpr T RoundDown(T x, typename Identity<T>::type n) { |
| 110 | DCHECK(IsPowerOfTwo(n)); |
| 111 | return (x & -n); |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | template<typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 115 | constexpr T RoundUp(T x, typename std::remove_reference<T>::type n) WARN_UNUSED; |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 116 | |
| 117 | template<typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 118 | constexpr T RoundUp(T x, typename std::remove_reference<T>::type n) { |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 119 | return RoundDown(x + n - 1, n); |
| 120 | } |
| 121 | |
| 122 | // For aligning pointers. |
| 123 | template<typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 124 | inline T* AlignDown(T* x, uintptr_t n) WARN_UNUSED; |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 125 | |
| 126 | template<typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 127 | inline T* AlignDown(T* x, uintptr_t n) { |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 128 | return reinterpret_cast<T*>(RoundDown(reinterpret_cast<uintptr_t>(x), n)); |
| 129 | } |
| 130 | |
| 131 | template<typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 132 | inline T* AlignUp(T* x, uintptr_t n) WARN_UNUSED; |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 133 | |
| 134 | template<typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 135 | inline T* AlignUp(T* x, uintptr_t n) { |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 136 | return reinterpret_cast<T*>(RoundUp(reinterpret_cast<uintptr_t>(x), n)); |
| 137 | } |
| 138 | |
| 139 | template<int n, typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 140 | constexpr bool IsAligned(T x) { |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 141 | static_assert((n & (n - 1)) == 0, "n is not a power of two"); |
| 142 | return (x & (n - 1)) == 0; |
| 143 | } |
| 144 | |
| 145 | template<int n, typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 146 | inline bool IsAligned(T* x) { |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 147 | return IsAligned<n>(reinterpret_cast<const uintptr_t>(x)); |
| 148 | } |
| 149 | |
| 150 | template<typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 151 | inline bool IsAlignedParam(T x, int n) { |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 152 | return (x & (n - 1)) == 0; |
| 153 | } |
| 154 | |
Hiroshi Yamauchi | 3c3c4a1 | 2017-02-21 16:49:59 -0800 | [diff] [blame] | 155 | template<typename T> |
| 156 | inline bool IsAlignedParam(T* x, int n) { |
| 157 | return IsAlignedParam(reinterpret_cast<const uintptr_t>(x), n); |
| 158 | } |
| 159 | |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 160 | #define CHECK_ALIGNED(value, alignment) \ |
| 161 | CHECK(::art::IsAligned<alignment>(value)) << reinterpret_cast<const void*>(value) |
| 162 | |
| 163 | #define DCHECK_ALIGNED(value, alignment) \ |
| 164 | DCHECK(::art::IsAligned<alignment>(value)) << reinterpret_cast<const void*>(value) |
| 165 | |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 166 | #define CHECK_ALIGNED_PARAM(value, alignment) \ |
| 167 | CHECK(::art::IsAlignedParam(value, alignment)) << reinterpret_cast<const void*>(value) |
| 168 | |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 169 | #define DCHECK_ALIGNED_PARAM(value, alignment) \ |
| 170 | DCHECK(::art::IsAlignedParam(value, alignment)) << reinterpret_cast<const void*>(value) |
| 171 | |
| 172 | // Like sizeof, but count how many bits a type takes. Pass type explicitly. |
| 173 | template <typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 174 | constexpr size_t BitSizeOf() { |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 175 | static_assert(std::is_integral<T>::value, "T must be integral"); |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 176 | using unsigned_type = typename std::make_unsigned<T>::type; |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 177 | static_assert(sizeof(T) == sizeof(unsigned_type), "Unexpected type size mismatch!"); |
| 178 | static_assert(std::numeric_limits<unsigned_type>::radix == 2, "Unexpected radix!"); |
| 179 | return std::numeric_limits<unsigned_type>::digits; |
| 180 | } |
| 181 | |
| 182 | // Like sizeof, but count how many bits a type takes. Infers type from parameter. |
| 183 | template <typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 184 | constexpr size_t BitSizeOf(T /*x*/) { |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 185 | return BitSizeOf<T>(); |
| 186 | } |
| 187 | |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 188 | inline uint16_t Low16Bits(uint32_t value) { |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 189 | return static_cast<uint16_t>(value); |
| 190 | } |
| 191 | |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 192 | inline uint16_t High16Bits(uint32_t value) { |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 193 | return static_cast<uint16_t>(value >> 16); |
| 194 | } |
| 195 | |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 196 | inline uint32_t Low32Bits(uint64_t value) { |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 197 | return static_cast<uint32_t>(value); |
| 198 | } |
| 199 | |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 200 | inline uint32_t High32Bits(uint64_t value) { |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 201 | return static_cast<uint32_t>(value >> 32); |
| 202 | } |
| 203 | |
| 204 | // Check whether an N-bit two's-complement representation can hold value. |
| 205 | template <typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 206 | inline bool IsInt(size_t N, T value) { |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 207 | if (N == BitSizeOf<T>()) { |
| 208 | return true; |
| 209 | } else { |
| 210 | CHECK_LT(0u, N); |
| 211 | CHECK_LT(N, BitSizeOf<T>()); |
| 212 | T limit = static_cast<T>(1) << (N - 1u); |
| 213 | return (-limit <= value) && (value < limit); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | template <typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 218 | constexpr T GetIntLimit(size_t bits) { |
| 219 | DCHECK_NE(bits, 0u); |
| 220 | DCHECK_LT(bits, BitSizeOf<T>()); |
| 221 | return static_cast<T>(1) << (bits - 1); |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | template <size_t kBits, typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 225 | constexpr bool IsInt(T value) { |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 226 | static_assert(kBits > 0, "kBits cannot be zero."); |
| 227 | static_assert(kBits <= BitSizeOf<T>(), "kBits must be <= max."); |
| 228 | static_assert(std::is_signed<T>::value, "Needs a signed type."); |
| 229 | // Corner case for "use all bits." Can't use the limits, as they would overflow, but it is |
| 230 | // trivially true. |
| 231 | return (kBits == BitSizeOf<T>()) ? |
| 232 | true : |
| 233 | (-GetIntLimit<T>(kBits) <= value) && (value < GetIntLimit<T>(kBits)); |
| 234 | } |
| 235 | |
| 236 | template <size_t kBits, typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 237 | constexpr bool IsUint(T value) { |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 238 | static_assert(kBits > 0, "kBits cannot be zero."); |
| 239 | static_assert(kBits <= BitSizeOf<T>(), "kBits must be <= max."); |
| 240 | static_assert(std::is_integral<T>::value, "Needs an integral type."); |
| 241 | // Corner case for "use all bits." Can't use the limits, as they would overflow, but it is |
| 242 | // trivially true. |
| 243 | // NOTE: To avoid triggering assertion in GetIntLimit(kBits+1) if kBits+1==BitSizeOf<T>(), |
| 244 | // use GetIntLimit(kBits)*2u. The unsigned arithmetic works well for us if it overflows. |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 245 | using unsigned_type = typename std::make_unsigned<T>::type; |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 246 | return (0 <= value) && |
| 247 | (kBits == BitSizeOf<T>() || |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 248 | (static_cast<unsigned_type>(value) <= GetIntLimit<unsigned_type>(kBits) * 2u - 1u)); |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | template <size_t kBits, typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 252 | constexpr bool IsAbsoluteUint(T value) { |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 253 | static_assert(kBits <= BitSizeOf<T>(), "kBits must be <= max."); |
| 254 | static_assert(std::is_integral<T>::value, "Needs an integral type."); |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 255 | using unsigned_type = typename std::make_unsigned<T>::type; |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 256 | return (kBits == BitSizeOf<T>()) |
| 257 | ? true |
| 258 | : IsUint<kBits>(value < 0 |
| 259 | ? static_cast<unsigned_type>(-1 - value) + 1u // Avoid overflow. |
| 260 | : static_cast<unsigned_type>(value)); |
| 261 | } |
| 262 | |
Chris Larsen | dbce0d7 | 2015-09-17 13:34:00 -0700 | [diff] [blame] | 263 | // Generate maximum/minimum values for signed/unsigned n-bit integers |
| 264 | template <typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 265 | constexpr T MaxInt(size_t bits) { |
| 266 | DCHECK(std::is_unsigned<T>::value || bits > 0u) << "bits cannot be zero for signed."; |
| 267 | DCHECK_LE(bits, BitSizeOf<T>()); |
| 268 | using unsigned_type = typename std::make_unsigned<T>::type; |
| 269 | return bits == BitSizeOf<T>() |
| 270 | ? std::numeric_limits<T>::max() |
| 271 | : std::is_signed<T>::value |
| 272 | ? ((bits == 1u) ? 0 : static_cast<T>(MaxInt<unsigned_type>(bits - 1))) |
| 273 | : static_cast<T>(UINT64_C(1) << bits) - static_cast<T>(1); |
Chris Larsen | dbce0d7 | 2015-09-17 13:34:00 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | template <typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 277 | constexpr T MinInt(size_t bits) { |
| 278 | DCHECK(std::is_unsigned<T>::value || bits > 0) << "bits cannot be zero for signed."; |
| 279 | DCHECK_LE(bits, BitSizeOf<T>()); |
| 280 | return bits == BitSizeOf<T>() |
| 281 | ? std::numeric_limits<T>::min() |
| 282 | : std::is_signed<T>::value |
| 283 | ? ((bits == 1u) ? -1 : static_cast<T>(-1) - MaxInt<T>(bits)) |
| 284 | : static_cast<T>(0); |
Chris Larsen | dbce0d7 | 2015-09-17 13:34:00 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 287 | // Using the Curiously Recurring Template Pattern to implement everything shared |
| 288 | // by LowToHighBitIterator and HighToLowBitIterator, i.e. everything but operator*(). |
| 289 | template <typename T, typename Iter> |
| 290 | class BitIteratorBase |
| 291 | : public std::iterator<std::forward_iterator_tag, uint32_t, ptrdiff_t, void, void> { |
| 292 | static_assert(std::is_integral<T>::value, "T must be integral"); |
| 293 | static_assert(std::is_unsigned<T>::value, "T must be unsigned"); |
| 294 | |
| 295 | static_assert(sizeof(T) == sizeof(uint32_t) || sizeof(T) == sizeof(uint64_t), "Unsupported size"); |
| 296 | |
| 297 | public: |
| 298 | BitIteratorBase() : bits_(0u) { } |
| 299 | explicit BitIteratorBase(T bits) : bits_(bits) { } |
| 300 | |
| 301 | Iter& operator++() { |
| 302 | DCHECK_NE(bits_, 0u); |
| 303 | uint32_t bit = *static_cast<Iter&>(*this); |
| 304 | bits_ &= ~(static_cast<T>(1u) << bit); |
| 305 | return static_cast<Iter&>(*this); |
| 306 | } |
| 307 | |
| 308 | Iter& operator++(int) { |
| 309 | Iter tmp(static_cast<Iter&>(*this)); |
| 310 | ++*this; |
| 311 | return tmp; |
| 312 | } |
| 313 | |
| 314 | protected: |
| 315 | T bits_; |
| 316 | |
| 317 | template <typename U, typename I> |
| 318 | friend bool operator==(const BitIteratorBase<U, I>& lhs, const BitIteratorBase<U, I>& rhs); |
| 319 | }; |
| 320 | |
| 321 | template <typename T, typename Iter> |
| 322 | bool operator==(const BitIteratorBase<T, Iter>& lhs, const BitIteratorBase<T, Iter>& rhs) { |
| 323 | return lhs.bits_ == rhs.bits_; |
| 324 | } |
| 325 | |
| 326 | template <typename T, typename Iter> |
| 327 | bool operator!=(const BitIteratorBase<T, Iter>& lhs, const BitIteratorBase<T, Iter>& rhs) { |
| 328 | return !(lhs == rhs); |
| 329 | } |
| 330 | |
| 331 | template <typename T> |
| 332 | class LowToHighBitIterator : public BitIteratorBase<T, LowToHighBitIterator<T>> { |
| 333 | public: |
| 334 | using BitIteratorBase<T, LowToHighBitIterator<T>>::BitIteratorBase; |
| 335 | |
| 336 | uint32_t operator*() const { |
| 337 | DCHECK_NE(this->bits_, 0u); |
| 338 | return CTZ(this->bits_); |
| 339 | } |
| 340 | }; |
| 341 | |
| 342 | template <typename T> |
| 343 | class HighToLowBitIterator : public BitIteratorBase<T, HighToLowBitIterator<T>> { |
| 344 | public: |
| 345 | using BitIteratorBase<T, HighToLowBitIterator<T>>::BitIteratorBase; |
| 346 | |
| 347 | uint32_t operator*() const { |
| 348 | DCHECK_NE(this->bits_, 0u); |
| 349 | static_assert(std::numeric_limits<T>::radix == 2, "Unexpected radix!"); |
| 350 | return std::numeric_limits<T>::digits - 1u - CLZ(this->bits_); |
| 351 | } |
| 352 | }; |
| 353 | |
| 354 | template <typename T> |
| 355 | IterationRange<LowToHighBitIterator<T>> LowToHighBits(T bits) { |
| 356 | return IterationRange<LowToHighBitIterator<T>>( |
| 357 | LowToHighBitIterator<T>(bits), LowToHighBitIterator<T>()); |
| 358 | } |
| 359 | |
| 360 | template <typename T> |
| 361 | IterationRange<HighToLowBitIterator<T>> HighToLowBits(T bits) { |
| 362 | return IterationRange<HighToLowBitIterator<T>>( |
| 363 | HighToLowBitIterator<T>(bits), HighToLowBitIterator<T>()); |
| 364 | } |
| 365 | |
| 366 | } // namespace art |
| 367 | |
| 368 | #endif // ART_RUNTIME_BASE_BIT_UTILS_H_ |