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 | |
| 24 | #include "base/logging.h" |
| 25 | #include "base/iteration_range.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 | |
| 155 | #define CHECK_ALIGNED(value, alignment) \ |
| 156 | CHECK(::art::IsAligned<alignment>(value)) << reinterpret_cast<const void*>(value) |
| 157 | |
| 158 | #define DCHECK_ALIGNED(value, alignment) \ |
| 159 | DCHECK(::art::IsAligned<alignment>(value)) << reinterpret_cast<const void*>(value) |
| 160 | |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 161 | #define CHECK_ALIGNED_PARAM(value, alignment) \ |
| 162 | CHECK(::art::IsAlignedParam(value, alignment)) << reinterpret_cast<const void*>(value) |
| 163 | |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 164 | #define DCHECK_ALIGNED_PARAM(value, alignment) \ |
| 165 | DCHECK(::art::IsAlignedParam(value, alignment)) << reinterpret_cast<const void*>(value) |
| 166 | |
| 167 | // Like sizeof, but count how many bits a type takes. Pass type explicitly. |
| 168 | template <typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame^] | 169 | constexpr size_t BitSizeOf() { |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 170 | static_assert(std::is_integral<T>::value, "T must be integral"); |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame^] | 171 | using unsigned_type = typename std::make_unsigned<T>::type; |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 172 | static_assert(sizeof(T) == sizeof(unsigned_type), "Unexpected type size mismatch!"); |
| 173 | static_assert(std::numeric_limits<unsigned_type>::radix == 2, "Unexpected radix!"); |
| 174 | return std::numeric_limits<unsigned_type>::digits; |
| 175 | } |
| 176 | |
| 177 | // Like sizeof, but count how many bits a type takes. Infers type from parameter. |
| 178 | template <typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame^] | 179 | constexpr size_t BitSizeOf(T /*x*/) { |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 180 | return BitSizeOf<T>(); |
| 181 | } |
| 182 | |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame^] | 183 | inline uint16_t Low16Bits(uint32_t value) { |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 184 | return static_cast<uint16_t>(value); |
| 185 | } |
| 186 | |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame^] | 187 | inline uint16_t High16Bits(uint32_t value) { |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 188 | return static_cast<uint16_t>(value >> 16); |
| 189 | } |
| 190 | |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame^] | 191 | inline uint32_t Low32Bits(uint64_t value) { |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 192 | return static_cast<uint32_t>(value); |
| 193 | } |
| 194 | |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame^] | 195 | inline uint32_t High32Bits(uint64_t value) { |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 196 | return static_cast<uint32_t>(value >> 32); |
| 197 | } |
| 198 | |
| 199 | // Check whether an N-bit two's-complement representation can hold value. |
| 200 | template <typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame^] | 201 | inline bool IsInt(size_t N, T value) { |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 202 | if (N == BitSizeOf<T>()) { |
| 203 | return true; |
| 204 | } else { |
| 205 | CHECK_LT(0u, N); |
| 206 | CHECK_LT(N, BitSizeOf<T>()); |
| 207 | T limit = static_cast<T>(1) << (N - 1u); |
| 208 | return (-limit <= value) && (value < limit); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | template <typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame^] | 213 | constexpr T GetIntLimit(size_t bits) { |
| 214 | DCHECK_NE(bits, 0u); |
| 215 | DCHECK_LT(bits, BitSizeOf<T>()); |
| 216 | return static_cast<T>(1) << (bits - 1); |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | template <size_t kBits, typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame^] | 220 | constexpr bool IsInt(T value) { |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 221 | static_assert(kBits > 0, "kBits cannot be zero."); |
| 222 | static_assert(kBits <= BitSizeOf<T>(), "kBits must be <= max."); |
| 223 | static_assert(std::is_signed<T>::value, "Needs a signed type."); |
| 224 | // Corner case for "use all bits." Can't use the limits, as they would overflow, but it is |
| 225 | // trivially true. |
| 226 | return (kBits == BitSizeOf<T>()) ? |
| 227 | true : |
| 228 | (-GetIntLimit<T>(kBits) <= value) && (value < GetIntLimit<T>(kBits)); |
| 229 | } |
| 230 | |
| 231 | template <size_t kBits, typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame^] | 232 | constexpr bool IsUint(T value) { |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 233 | static_assert(kBits > 0, "kBits cannot be zero."); |
| 234 | static_assert(kBits <= BitSizeOf<T>(), "kBits must be <= max."); |
| 235 | static_assert(std::is_integral<T>::value, "Needs an integral type."); |
| 236 | // Corner case for "use all bits." Can't use the limits, as they would overflow, but it is |
| 237 | // trivially true. |
| 238 | // NOTE: To avoid triggering assertion in GetIntLimit(kBits+1) if kBits+1==BitSizeOf<T>(), |
| 239 | // 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^] | 240 | using unsigned_type = typename std::make_unsigned<T>::type; |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 241 | return (0 <= value) && |
| 242 | (kBits == BitSizeOf<T>() || |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame^] | 243 | (static_cast<unsigned_type>(value) <= GetIntLimit<unsigned_type>(kBits) * 2u - 1u)); |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | template <size_t kBits, typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame^] | 247 | constexpr bool IsAbsoluteUint(T value) { |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 248 | static_assert(kBits <= BitSizeOf<T>(), "kBits must be <= max."); |
| 249 | static_assert(std::is_integral<T>::value, "Needs an integral type."); |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame^] | 250 | using unsigned_type = typename std::make_unsigned<T>::type; |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 251 | return (kBits == BitSizeOf<T>()) |
| 252 | ? true |
| 253 | : IsUint<kBits>(value < 0 |
| 254 | ? static_cast<unsigned_type>(-1 - value) + 1u // Avoid overflow. |
| 255 | : static_cast<unsigned_type>(value)); |
| 256 | } |
| 257 | |
Chris Larsen | dbce0d7 | 2015-09-17 13:34:00 -0700 | [diff] [blame] | 258 | // Generate maximum/minimum values for signed/unsigned n-bit integers |
| 259 | template <typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame^] | 260 | constexpr T MaxInt(size_t bits) { |
| 261 | DCHECK(std::is_unsigned<T>::value || bits > 0u) << "bits cannot be zero for signed."; |
| 262 | DCHECK_LE(bits, BitSizeOf<T>()); |
| 263 | using unsigned_type = typename std::make_unsigned<T>::type; |
| 264 | return bits == BitSizeOf<T>() |
| 265 | ? std::numeric_limits<T>::max() |
| 266 | : std::is_signed<T>::value |
| 267 | ? ((bits == 1u) ? 0 : static_cast<T>(MaxInt<unsigned_type>(bits - 1))) |
| 268 | : static_cast<T>(UINT64_C(1) << bits) - static_cast<T>(1); |
Chris Larsen | dbce0d7 | 2015-09-17 13:34:00 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | template <typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame^] | 272 | constexpr T MinInt(size_t bits) { |
| 273 | DCHECK(std::is_unsigned<T>::value || bits > 0) << "bits cannot be zero for signed."; |
| 274 | DCHECK_LE(bits, BitSizeOf<T>()); |
| 275 | return bits == BitSizeOf<T>() |
| 276 | ? std::numeric_limits<T>::min() |
| 277 | : std::is_signed<T>::value |
| 278 | ? ((bits == 1u) ? -1 : static_cast<T>(-1) - MaxInt<T>(bits)) |
| 279 | : static_cast<T>(0); |
Chris Larsen | dbce0d7 | 2015-09-17 13:34:00 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 282 | // Using the Curiously Recurring Template Pattern to implement everything shared |
| 283 | // by LowToHighBitIterator and HighToLowBitIterator, i.e. everything but operator*(). |
| 284 | template <typename T, typename Iter> |
| 285 | class BitIteratorBase |
| 286 | : public std::iterator<std::forward_iterator_tag, uint32_t, ptrdiff_t, void, void> { |
| 287 | static_assert(std::is_integral<T>::value, "T must be integral"); |
| 288 | static_assert(std::is_unsigned<T>::value, "T must be unsigned"); |
| 289 | |
| 290 | static_assert(sizeof(T) == sizeof(uint32_t) || sizeof(T) == sizeof(uint64_t), "Unsupported size"); |
| 291 | |
| 292 | public: |
| 293 | BitIteratorBase() : bits_(0u) { } |
| 294 | explicit BitIteratorBase(T bits) : bits_(bits) { } |
| 295 | |
| 296 | Iter& operator++() { |
| 297 | DCHECK_NE(bits_, 0u); |
| 298 | uint32_t bit = *static_cast<Iter&>(*this); |
| 299 | bits_ &= ~(static_cast<T>(1u) << bit); |
| 300 | return static_cast<Iter&>(*this); |
| 301 | } |
| 302 | |
| 303 | Iter& operator++(int) { |
| 304 | Iter tmp(static_cast<Iter&>(*this)); |
| 305 | ++*this; |
| 306 | return tmp; |
| 307 | } |
| 308 | |
| 309 | protected: |
| 310 | T bits_; |
| 311 | |
| 312 | template <typename U, typename I> |
| 313 | friend bool operator==(const BitIteratorBase<U, I>& lhs, const BitIteratorBase<U, I>& rhs); |
| 314 | }; |
| 315 | |
| 316 | template <typename T, typename Iter> |
| 317 | bool operator==(const BitIteratorBase<T, Iter>& lhs, const BitIteratorBase<T, Iter>& rhs) { |
| 318 | return lhs.bits_ == rhs.bits_; |
| 319 | } |
| 320 | |
| 321 | template <typename T, typename Iter> |
| 322 | bool operator!=(const BitIteratorBase<T, Iter>& lhs, const BitIteratorBase<T, Iter>& rhs) { |
| 323 | return !(lhs == rhs); |
| 324 | } |
| 325 | |
| 326 | template <typename T> |
| 327 | class LowToHighBitIterator : public BitIteratorBase<T, LowToHighBitIterator<T>> { |
| 328 | public: |
| 329 | using BitIteratorBase<T, LowToHighBitIterator<T>>::BitIteratorBase; |
| 330 | |
| 331 | uint32_t operator*() const { |
| 332 | DCHECK_NE(this->bits_, 0u); |
| 333 | return CTZ(this->bits_); |
| 334 | } |
| 335 | }; |
| 336 | |
| 337 | template <typename T> |
| 338 | class HighToLowBitIterator : public BitIteratorBase<T, HighToLowBitIterator<T>> { |
| 339 | public: |
| 340 | using BitIteratorBase<T, HighToLowBitIterator<T>>::BitIteratorBase; |
| 341 | |
| 342 | uint32_t operator*() const { |
| 343 | DCHECK_NE(this->bits_, 0u); |
| 344 | static_assert(std::numeric_limits<T>::radix == 2, "Unexpected radix!"); |
| 345 | return std::numeric_limits<T>::digits - 1u - CLZ(this->bits_); |
| 346 | } |
| 347 | }; |
| 348 | |
| 349 | template <typename T> |
| 350 | IterationRange<LowToHighBitIterator<T>> LowToHighBits(T bits) { |
| 351 | return IterationRange<LowToHighBitIterator<T>>( |
| 352 | LowToHighBitIterator<T>(bits), LowToHighBitIterator<T>()); |
| 353 | } |
| 354 | |
| 355 | template <typename T> |
| 356 | IterationRange<HighToLowBitIterator<T>> HighToLowBits(T bits) { |
| 357 | return IterationRange<HighToLowBitIterator<T>>( |
| 358 | HighToLowBitIterator<T>(bits), HighToLowBitIterator<T>()); |
| 359 | } |
| 360 | |
| 361 | } // namespace art |
| 362 | |
| 363 | #endif // ART_RUNTIME_BASE_BIT_UTILS_H_ |