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 | |
David Sehr | 67bf42e | 2018-02-26 16:43:04 -0800 | [diff] [blame] | 17 | #ifndef ART_LIBARTBASE_BASE_BIT_UTILS_H_ |
| 18 | #define ART_LIBARTBASE_BASE_BIT_UTILS_H_ |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 19 | |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 20 | #include <limits> |
| 21 | #include <type_traits> |
| 22 | |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 23 | #include <android-base/logging.h> |
| 24 | |
David Srbecky | 68fefac | 2018-05-10 17:49:33 +0100 | [diff] [blame] | 25 | #include "globals.h" |
David Sehr | 1979c64 | 2018-04-26 14:41:18 -0700 | [diff] [blame] | 26 | #include "stl_util_identity.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 27 | |
| 28 | namespace art { |
| 29 | |
buzbee | 31afbec | 2017-03-14 15:30:19 -0700 | [diff] [blame] | 30 | // Like sizeof, but count how many bits a type takes. Pass type explicitly. |
| 31 | template <typename T> |
| 32 | constexpr size_t BitSizeOf() { |
Vladimir Marko | 495311c | 2022-04-14 10:59:53 +0000 | [diff] [blame] | 33 | static_assert(std::is_integral_v<T>, "T must be integral"); |
| 34 | using unsigned_type = std::make_unsigned_t<T>; |
buzbee | 31afbec | 2017-03-14 15:30:19 -0700 | [diff] [blame] | 35 | static_assert(sizeof(T) == sizeof(unsigned_type), "Unexpected type size mismatch!"); |
| 36 | static_assert(std::numeric_limits<unsigned_type>::radix == 2, "Unexpected radix!"); |
| 37 | return std::numeric_limits<unsigned_type>::digits; |
| 38 | } |
| 39 | |
| 40 | // Like sizeof, but count how many bits a type takes. Infers type from parameter. |
| 41 | template <typename T> |
| 42 | constexpr size_t BitSizeOf(T /*x*/) { |
| 43 | return BitSizeOf<T>(); |
| 44 | } |
| 45 | |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 46 | template<typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 47 | constexpr int CLZ(T x) { |
Vladimir Marko | 495311c | 2022-04-14 10:59:53 +0000 | [diff] [blame] | 48 | static_assert(std::is_integral_v<T>, "T must be integral"); |
| 49 | static_assert(std::is_unsigned_v<T>, "T must be unsigned"); |
Vladimir Marko | dc682aa | 2018-01-04 18:42:57 +0000 | [diff] [blame] | 50 | static_assert(std::numeric_limits<T>::radix == 2, "Unexpected radix!"); |
| 51 | static_assert(sizeof(T) == sizeof(uint64_t) || sizeof(T) <= sizeof(uint32_t), |
| 52 | "Unsupported sizeof(T)"); |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 53 | DCHECK_NE(x, 0u); |
Vladimir Marko | dc682aa | 2018-01-04 18:42:57 +0000 | [diff] [blame] | 54 | constexpr bool is_64_bit = (sizeof(T) == sizeof(uint64_t)); |
| 55 | constexpr size_t adjustment = |
| 56 | is_64_bit ? 0u : std::numeric_limits<uint32_t>::digits - std::numeric_limits<T>::digits; |
| 57 | return is_64_bit ? __builtin_clzll(x) : __builtin_clz(x) - adjustment; |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 58 | } |
| 59 | |
buzbee | 31afbec | 2017-03-14 15:30:19 -0700 | [diff] [blame] | 60 | // Similar to CLZ except that on zero input it returns bitwidth and supports signed integers. |
| 61 | template<typename T> |
| 62 | constexpr int JAVASTYLE_CLZ(T x) { |
Vladimir Marko | 495311c | 2022-04-14 10:59:53 +0000 | [diff] [blame] | 63 | static_assert(std::is_integral_v<T>, "T must be integral"); |
| 64 | using unsigned_type = std::make_unsigned_t<T>; |
buzbee | 31afbec | 2017-03-14 15:30:19 -0700 | [diff] [blame] | 65 | return (x == 0) ? BitSizeOf<T>() : CLZ(static_cast<unsigned_type>(x)); |
| 66 | } |
| 67 | |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 68 | template<typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 69 | constexpr int CTZ(T x) { |
Vladimir Marko | 495311c | 2022-04-14 10:59:53 +0000 | [diff] [blame] | 70 | static_assert(std::is_integral_v<T>, "T must be integral"); |
Andreas Gampe | 151ab8d | 2015-08-14 23:01:49 +0000 | [diff] [blame] | 71 | // It is not unreasonable to ask for trailing zeros in a negative number. As such, do not check |
| 72 | // that T is an unsigned type. |
Vladimir Marko | dc682aa | 2018-01-04 18:42:57 +0000 | [diff] [blame] | 73 | static_assert(sizeof(T) == sizeof(uint64_t) || sizeof(T) <= sizeof(uint32_t), |
| 74 | "Unsupported sizeof(T)"); |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 75 | DCHECK_NE(x, static_cast<T>(0)); |
Vladimir Marko | dc682aa | 2018-01-04 18:42:57 +0000 | [diff] [blame] | 76 | return (sizeof(T) == sizeof(uint64_t)) ? __builtin_ctzll(x) : __builtin_ctz(x); |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 77 | } |
| 78 | |
buzbee | 31afbec | 2017-03-14 15:30:19 -0700 | [diff] [blame] | 79 | // Similar to CTZ except that on zero input it returns bitwidth and supports signed integers. |
| 80 | template<typename T> |
| 81 | constexpr int JAVASTYLE_CTZ(T x) { |
Vladimir Marko | 495311c | 2022-04-14 10:59:53 +0000 | [diff] [blame] | 82 | static_assert(std::is_integral_v<T>, "T must be integral"); |
| 83 | using unsigned_type = std::make_unsigned_t<T>; |
buzbee | 31afbec | 2017-03-14 15:30:19 -0700 | [diff] [blame] | 84 | return (x == 0) ? BitSizeOf<T>() : CTZ(static_cast<unsigned_type>(x)); |
| 85 | } |
| 86 | |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 87 | // Return the number of 1-bits in `x`. |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 88 | template<typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 89 | constexpr int POPCOUNT(T x) { |
| 90 | return (sizeof(T) == sizeof(uint32_t)) ? __builtin_popcount(x) : __builtin_popcountll(x); |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 91 | } |
| 92 | |
buzbee | 31afbec | 2017-03-14 15:30:19 -0700 | [diff] [blame] | 93 | // Swap bytes. |
| 94 | template<typename T> |
| 95 | constexpr T BSWAP(T x) { |
| 96 | if (sizeof(T) == sizeof(uint16_t)) { |
| 97 | return __builtin_bswap16(x); |
| 98 | } else if (sizeof(T) == sizeof(uint32_t)) { |
| 99 | return __builtin_bswap32(x); |
| 100 | } else { |
| 101 | return __builtin_bswap64(x); |
| 102 | } |
| 103 | } |
| 104 | |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 105 | // Find the bit position of the most significant bit (0-based), or -1 if there were no bits set. |
| 106 | template <typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 107 | constexpr ssize_t MostSignificantBit(T value) { |
Vladimir Marko | 495311c | 2022-04-14 10:59:53 +0000 | [diff] [blame] | 108 | static_assert(std::is_integral_v<T>, "T must be integral"); |
| 109 | static_assert(std::is_unsigned_v<T>, "T must be unsigned"); |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 110 | static_assert(std::numeric_limits<T>::radix == 2, "Unexpected radix!"); |
| 111 | return (value == 0) ? -1 : std::numeric_limits<T>::digits - 1 - CLZ(value); |
| 112 | } |
| 113 | |
| 114 | // Find the bit position of the least significant bit (0-based), or -1 if there were no bits set. |
| 115 | template <typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 116 | constexpr ssize_t LeastSignificantBit(T value) { |
Vladimir Marko | 495311c | 2022-04-14 10:59:53 +0000 | [diff] [blame] | 117 | static_assert(std::is_integral_v<T>, "T must be integral"); |
| 118 | static_assert(std::is_unsigned_v<T>, "T must be unsigned"); |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 119 | return (value == 0) ? -1 : CTZ(value); |
| 120 | } |
| 121 | |
| 122 | // How many bits (minimally) does it take to store the constant 'value'? i.e. 1 for 1, 3 for 5, etc. |
| 123 | template <typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 124 | constexpr size_t MinimumBitsToStore(T value) { |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 125 | return static_cast<size_t>(MostSignificantBit(value) + 1); |
| 126 | } |
| 127 | |
| 128 | template <typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 129 | constexpr T RoundUpToPowerOfTwo(T x) { |
Vladimir Marko | 495311c | 2022-04-14 10:59:53 +0000 | [diff] [blame] | 130 | static_assert(std::is_integral_v<T>, "T must be integral"); |
| 131 | static_assert(std::is_unsigned_v<T>, "T must be unsigned"); |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 132 | // NOTE: Undefined if x > (1 << (std::numeric_limits<T>::digits - 1)). |
| 133 | return (x < 2u) ? x : static_cast<T>(1u) << (std::numeric_limits<T>::digits - CLZ(x - 1u)); |
| 134 | } |
| 135 | |
Artem Serov | f26bb6c | 2017-09-01 10:59:03 +0100 | [diff] [blame] | 136 | // Return highest possible N - a power of two - such that val >= N. |
| 137 | template <typename T> |
| 138 | constexpr T TruncToPowerOfTwo(T val) { |
Vladimir Marko | 495311c | 2022-04-14 10:59:53 +0000 | [diff] [blame] | 139 | static_assert(std::is_integral_v<T>, "T must be integral"); |
| 140 | static_assert(std::is_unsigned_v<T>, "T must be unsigned"); |
Artem Serov | f26bb6c | 2017-09-01 10:59:03 +0100 | [diff] [blame] | 141 | return (val != 0) ? static_cast<T>(1u) << (BitSizeOf<T>() - CLZ(val) - 1u) : 0; |
| 142 | } |
| 143 | |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 144 | template<typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 145 | constexpr bool IsPowerOfTwo(T x) { |
Vladimir Marko | 495311c | 2022-04-14 10:59:53 +0000 | [diff] [blame] | 146 | static_assert(std::is_integral_v<T>, "T must be integral"); |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 147 | // TODO: assert unsigned. There is currently many uses with signed values. |
| 148 | return (x & (x - 1)) == 0; |
| 149 | } |
| 150 | |
| 151 | template<typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 152 | constexpr int WhichPowerOf2(T x) { |
Vladimir Marko | 495311c | 2022-04-14 10:59:53 +0000 | [diff] [blame] | 153 | static_assert(std::is_integral_v<T>, "T must be integral"); |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 154 | // TODO: assert unsigned. There is currently many uses with signed values. |
| 155 | DCHECK((x != 0) && IsPowerOfTwo(x)); |
| 156 | return CTZ(x); |
| 157 | } |
| 158 | |
| 159 | // For rounding integers. |
Vladimir Marko | 88b2b80 | 2015-12-04 14:19:04 +0000 | [diff] [blame] | 160 | // 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] | 161 | template<typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 162 | constexpr T RoundDown(T x, typename Identity<T>::type n) WARN_UNUSED; |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 163 | |
| 164 | template<typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 165 | constexpr T RoundDown(T x, typename Identity<T>::type n) { |
| 166 | DCHECK(IsPowerOfTwo(n)); |
| 167 | return (x & -n); |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | template<typename T> |
Vladimir Marko | 495311c | 2022-04-14 10:59:53 +0000 | [diff] [blame] | 171 | constexpr T RoundUp(T x, std::remove_reference_t<T> n) WARN_UNUSED; |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 172 | |
| 173 | template<typename T> |
Vladimir Marko | 495311c | 2022-04-14 10:59:53 +0000 | [diff] [blame] | 174 | constexpr T RoundUp(T x, std::remove_reference_t<T> n) { |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 175 | return RoundDown(x + n - 1, n); |
| 176 | } |
| 177 | |
| 178 | // For aligning pointers. |
| 179 | template<typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 180 | inline T* AlignDown(T* x, uintptr_t n) WARN_UNUSED; |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 181 | |
| 182 | template<typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 183 | inline T* AlignDown(T* x, uintptr_t n) { |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 184 | return reinterpret_cast<T*>(RoundDown(reinterpret_cast<uintptr_t>(x), n)); |
| 185 | } |
| 186 | |
| 187 | template<typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 188 | inline T* AlignUp(T* x, uintptr_t n) WARN_UNUSED; |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 189 | |
| 190 | template<typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 191 | inline T* AlignUp(T* x, uintptr_t n) { |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 192 | return reinterpret_cast<T*>(RoundUp(reinterpret_cast<uintptr_t>(x), n)); |
| 193 | } |
| 194 | |
| 195 | template<int n, typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 196 | constexpr bool IsAligned(T x) { |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 197 | static_assert((n & (n - 1)) == 0, "n is not a power of two"); |
| 198 | return (x & (n - 1)) == 0; |
| 199 | } |
| 200 | |
| 201 | template<int n, typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 202 | inline bool IsAligned(T* x) { |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 203 | return IsAligned<n>(reinterpret_cast<const uintptr_t>(x)); |
| 204 | } |
| 205 | |
| 206 | template<typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 207 | inline bool IsAlignedParam(T x, int n) { |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 208 | return (x & (n - 1)) == 0; |
| 209 | } |
| 210 | |
Hiroshi Yamauchi | 3c3c4a1 | 2017-02-21 16:49:59 -0800 | [diff] [blame] | 211 | template<typename T> |
| 212 | inline bool IsAlignedParam(T* x, int n) { |
| 213 | return IsAlignedParam(reinterpret_cast<const uintptr_t>(x), n); |
| 214 | } |
| 215 | |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 216 | #define CHECK_ALIGNED(value, alignment) \ |
| 217 | CHECK(::art::IsAligned<alignment>(value)) << reinterpret_cast<const void*>(value) |
| 218 | |
| 219 | #define DCHECK_ALIGNED(value, alignment) \ |
| 220 | DCHECK(::art::IsAligned<alignment>(value)) << reinterpret_cast<const void*>(value) |
| 221 | |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 222 | #define CHECK_ALIGNED_PARAM(value, alignment) \ |
| 223 | CHECK(::art::IsAlignedParam(value, alignment)) << reinterpret_cast<const void*>(value) |
| 224 | |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 225 | #define DCHECK_ALIGNED_PARAM(value, alignment) \ |
| 226 | DCHECK(::art::IsAlignedParam(value, alignment)) << reinterpret_cast<const void*>(value) |
| 227 | |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 228 | inline uint16_t Low16Bits(uint32_t value) { |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 229 | return static_cast<uint16_t>(value); |
| 230 | } |
| 231 | |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 232 | inline uint16_t High16Bits(uint32_t value) { |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 233 | return static_cast<uint16_t>(value >> 16); |
| 234 | } |
| 235 | |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 236 | inline uint32_t Low32Bits(uint64_t value) { |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 237 | return static_cast<uint32_t>(value); |
| 238 | } |
| 239 | |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 240 | inline uint32_t High32Bits(uint64_t value) { |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 241 | return static_cast<uint32_t>(value >> 32); |
| 242 | } |
| 243 | |
| 244 | // Check whether an N-bit two's-complement representation can hold value. |
| 245 | template <typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 246 | inline bool IsInt(size_t N, T value) { |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 247 | if (N == BitSizeOf<T>()) { |
| 248 | return true; |
| 249 | } else { |
| 250 | CHECK_LT(0u, N); |
| 251 | CHECK_LT(N, BitSizeOf<T>()); |
| 252 | T limit = static_cast<T>(1) << (N - 1u); |
| 253 | return (-limit <= value) && (value < limit); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | template <typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 258 | constexpr T GetIntLimit(size_t bits) { |
| 259 | DCHECK_NE(bits, 0u); |
| 260 | DCHECK_LT(bits, BitSizeOf<T>()); |
| 261 | return static_cast<T>(1) << (bits - 1); |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | template <size_t kBits, typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 265 | constexpr bool IsInt(T value) { |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 266 | static_assert(kBits > 0, "kBits cannot be zero."); |
| 267 | static_assert(kBits <= BitSizeOf<T>(), "kBits must be <= max."); |
Vladimir Marko | 495311c | 2022-04-14 10:59:53 +0000 | [diff] [blame] | 268 | static_assert(std::is_signed_v<T>, "Needs a signed type."); |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 269 | // Corner case for "use all bits." Can't use the limits, as they would overflow, but it is |
| 270 | // trivially true. |
| 271 | return (kBits == BitSizeOf<T>()) ? |
| 272 | true : |
| 273 | (-GetIntLimit<T>(kBits) <= value) && (value < GetIntLimit<T>(kBits)); |
| 274 | } |
| 275 | |
| 276 | template <size_t kBits, typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 277 | constexpr bool IsUint(T value) { |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 278 | static_assert(kBits > 0, "kBits cannot be zero."); |
| 279 | static_assert(kBits <= BitSizeOf<T>(), "kBits must be <= max."); |
Vladimir Marko | 495311c | 2022-04-14 10:59:53 +0000 | [diff] [blame] | 280 | static_assert(std::is_integral_v<T>, "Needs an integral type."); |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 281 | // Corner case for "use all bits." Can't use the limits, as they would overflow, but it is |
| 282 | // trivially true. |
| 283 | // NOTE: To avoid triggering assertion in GetIntLimit(kBits+1) if kBits+1==BitSizeOf<T>(), |
| 284 | // use GetIntLimit(kBits)*2u. The unsigned arithmetic works well for us if it overflows. |
Vladimir Marko | 495311c | 2022-04-14 10:59:53 +0000 | [diff] [blame] | 285 | using unsigned_type = std::make_unsigned_t<T>; |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 286 | return (0 <= value) && |
| 287 | (kBits == BitSizeOf<T>() || |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 288 | (static_cast<unsigned_type>(value) <= GetIntLimit<unsigned_type>(kBits) * 2u - 1u)); |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | template <size_t kBits, typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 292 | constexpr bool IsAbsoluteUint(T value) { |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 293 | static_assert(kBits <= BitSizeOf<T>(), "kBits must be <= max."); |
Vladimir Marko | 495311c | 2022-04-14 10:59:53 +0000 | [diff] [blame] | 294 | static_assert(std::is_integral_v<T>, "Needs an integral type."); |
| 295 | using unsigned_type = std::make_unsigned_t<T>; |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 296 | return (kBits == BitSizeOf<T>()) |
| 297 | ? true |
| 298 | : IsUint<kBits>(value < 0 |
| 299 | ? static_cast<unsigned_type>(-1 - value) + 1u // Avoid overflow. |
| 300 | : static_cast<unsigned_type>(value)); |
| 301 | } |
| 302 | |
Chris Larsen | dbce0d7 | 2015-09-17 13:34:00 -0700 | [diff] [blame] | 303 | // Generate maximum/minimum values for signed/unsigned n-bit integers |
| 304 | template <typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 305 | constexpr T MaxInt(size_t bits) { |
Vladimir Marko | 495311c | 2022-04-14 10:59:53 +0000 | [diff] [blame] | 306 | DCHECK(std::is_unsigned_v<T> || bits > 0u) << "bits cannot be zero for signed."; |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 307 | DCHECK_LE(bits, BitSizeOf<T>()); |
Vladimir Marko | 495311c | 2022-04-14 10:59:53 +0000 | [diff] [blame] | 308 | using unsigned_type = std::make_unsigned_t<T>; |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 309 | return bits == BitSizeOf<T>() |
| 310 | ? std::numeric_limits<T>::max() |
Vladimir Marko | 495311c | 2022-04-14 10:59:53 +0000 | [diff] [blame] | 311 | : std::is_signed_v<T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 312 | ? ((bits == 1u) ? 0 : static_cast<T>(MaxInt<unsigned_type>(bits - 1))) |
| 313 | : static_cast<T>(UINT64_C(1) << bits) - static_cast<T>(1); |
Chris Larsen | dbce0d7 | 2015-09-17 13:34:00 -0700 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | template <typename T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 317 | constexpr T MinInt(size_t bits) { |
Vladimir Marko | 495311c | 2022-04-14 10:59:53 +0000 | [diff] [blame] | 318 | DCHECK(std::is_unsigned_v<T> || bits > 0) << "bits cannot be zero for signed."; |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 319 | DCHECK_LE(bits, BitSizeOf<T>()); |
| 320 | return bits == BitSizeOf<T>() |
| 321 | ? std::numeric_limits<T>::min() |
Vladimir Marko | 495311c | 2022-04-14 10:59:53 +0000 | [diff] [blame] | 322 | : std::is_signed_v<T> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 323 | ? ((bits == 1u) ? -1 : static_cast<T>(-1) - MaxInt<T>(bits)) |
| 324 | : static_cast<T>(0); |
Chris Larsen | dbce0d7 | 2015-09-17 13:34:00 -0700 | [diff] [blame] | 325 | } |
| 326 | |
buzbee | 31afbec | 2017-03-14 15:30:19 -0700 | [diff] [blame] | 327 | // Returns value with bit set in lowest one-bit position or 0 if 0. (java.lang.X.lowestOneBit). |
| 328 | template <typename kind> |
| 329 | inline static kind LowestOneBitValue(kind opnd) { |
| 330 | // Hacker's Delight, Section 2-1 |
| 331 | return opnd & -opnd; |
| 332 | } |
| 333 | |
| 334 | // Returns value with bit set in hightest one-bit position or 0 if 0. (java.lang.X.highestOneBit). |
| 335 | template <typename T> |
| 336 | inline static T HighestOneBitValue(T opnd) { |
Vladimir Marko | 495311c | 2022-04-14 10:59:53 +0000 | [diff] [blame] | 337 | using unsigned_type = std::make_unsigned_t<T>; |
buzbee | 31afbec | 2017-03-14 15:30:19 -0700 | [diff] [blame] | 338 | T res; |
| 339 | if (opnd == 0) { |
| 340 | res = 0; |
| 341 | } else { |
| 342 | int bit_position = BitSizeOf<T>() - (CLZ(static_cast<unsigned_type>(opnd)) + 1); |
| 343 | res = static_cast<T>(UINT64_C(1) << bit_position); |
| 344 | } |
| 345 | return res; |
| 346 | } |
| 347 | |
| 348 | // Rotate bits. |
| 349 | template <typename T, bool left> |
| 350 | inline static T Rot(T opnd, int distance) { |
| 351 | int mask = BitSizeOf<T>() - 1; |
| 352 | int unsigned_right_shift = left ? (-distance & mask) : (distance & mask); |
| 353 | int signed_left_shift = left ? (distance & mask) : (-distance & mask); |
Vladimir Marko | 495311c | 2022-04-14 10:59:53 +0000 | [diff] [blame] | 354 | using unsigned_type = std::make_unsigned_t<T>; |
buzbee | 31afbec | 2017-03-14 15:30:19 -0700 | [diff] [blame] | 355 | return (static_cast<unsigned_type>(opnd) >> unsigned_right_shift) | (opnd << signed_left_shift); |
| 356 | } |
| 357 | |
| 358 | // TUNING: use rbit for arm/arm64 |
| 359 | inline static uint32_t ReverseBits32(uint32_t opnd) { |
| 360 | // Hacker's Delight 7-1 |
| 361 | opnd = ((opnd >> 1) & 0x55555555) | ((opnd & 0x55555555) << 1); |
| 362 | opnd = ((opnd >> 2) & 0x33333333) | ((opnd & 0x33333333) << 2); |
| 363 | opnd = ((opnd >> 4) & 0x0F0F0F0F) | ((opnd & 0x0F0F0F0F) << 4); |
| 364 | opnd = ((opnd >> 8) & 0x00FF00FF) | ((opnd & 0x00FF00FF) << 8); |
| 365 | opnd = ((opnd >> 16)) | ((opnd) << 16); |
| 366 | return opnd; |
| 367 | } |
| 368 | |
| 369 | // TUNING: use rbit for arm/arm64 |
| 370 | inline static uint64_t ReverseBits64(uint64_t opnd) { |
| 371 | // Hacker's Delight 7-1 |
| 372 | opnd = (opnd & 0x5555555555555555L) << 1 | ((opnd >> 1) & 0x5555555555555555L); |
| 373 | opnd = (opnd & 0x3333333333333333L) << 2 | ((opnd >> 2) & 0x3333333333333333L); |
| 374 | opnd = (opnd & 0x0f0f0f0f0f0f0f0fL) << 4 | ((opnd >> 4) & 0x0f0f0f0f0f0f0f0fL); |
| 375 | opnd = (opnd & 0x00ff00ff00ff00ffL) << 8 | ((opnd >> 8) & 0x00ff00ff00ff00ffL); |
| 376 | opnd = (opnd << 48) | ((opnd & 0xffff0000L) << 16) | ((opnd >> 16) & 0xffff0000L) | (opnd >> 48); |
| 377 | return opnd; |
| 378 | } |
| 379 | |
Igor Murashkin | 1e77d27 | 2017-10-07 14:09:43 +0000 | [diff] [blame] | 380 | // Create a mask for the least significant "bits" |
| 381 | // The returned value is always unsigned to prevent undefined behavior for bitwise ops. |
| 382 | // |
| 383 | // Given 'bits', |
| 384 | // Returns: |
| 385 | // <--- bits ---> |
| 386 | // +-----------------+------------+ |
| 387 | // | 0 ............0 | 1.....1 | |
| 388 | // +-----------------+------------+ |
| 389 | // msb lsb |
| 390 | template <typename T = size_t> |
| 391 | inline static constexpr std::make_unsigned_t<T> MaskLeastSignificant(size_t bits) { |
| 392 | DCHECK_GE(BitSizeOf<T>(), bits) << "Bits out of range for type T"; |
| 393 | using unsigned_T = std::make_unsigned_t<T>; |
| 394 | if (bits >= BitSizeOf<T>()) { |
| 395 | return std::numeric_limits<unsigned_T>::max(); |
| 396 | } else { |
Igor Murashkin | 2f36629 | 2017-10-09 13:37:50 -0700 | [diff] [blame] | 397 | auto kOne = static_cast<unsigned_T>(1); // Do not truncate for T>size_t. |
| 398 | return static_cast<unsigned_T>((kOne << bits) - kOne); |
Igor Murashkin | 1e77d27 | 2017-10-07 14:09:43 +0000 | [diff] [blame] | 399 | } |
| 400 | } |
| 401 | |
| 402 | // Clears the bitfield starting at the least significant bit "lsb" with a bitwidth of 'width'. |
| 403 | // (Equivalent of ARM BFC instruction). |
| 404 | // |
| 405 | // Given: |
| 406 | // <-- width --> |
| 407 | // +--------+------------+--------+ |
| 408 | // | ABC... | bitfield | XYZ... + |
| 409 | // +--------+------------+--------+ |
| 410 | // lsb 0 |
| 411 | // Returns: |
| 412 | // <-- width --> |
| 413 | // +--------+------------+--------+ |
| 414 | // | ABC... | 0........0 | XYZ... + |
| 415 | // +--------+------------+--------+ |
| 416 | // lsb 0 |
| 417 | template <typename T> |
| 418 | inline static constexpr T BitFieldClear(T value, size_t lsb, size_t width) { |
| 419 | DCHECK_GE(BitSizeOf(value), lsb + width) << "Bit field out of range for value"; |
| 420 | const auto val = static_cast<std::make_unsigned_t<T>>(value); |
| 421 | const auto mask = MaskLeastSignificant<T>(width); |
| 422 | |
| 423 | return static_cast<T>(val & ~(mask << lsb)); |
| 424 | } |
| 425 | |
| 426 | // Inserts the contents of 'data' into bitfield of 'value' starting |
| 427 | // at the least significant bit "lsb" with a bitwidth of 'width'. |
| 428 | // Note: data must be within range of [MinInt(width), MaxInt(width)]. |
| 429 | // (Equivalent of ARM BFI instruction). |
| 430 | // |
| 431 | // Given (data): |
| 432 | // <-- width --> |
| 433 | // +--------+------------+--------+ |
| 434 | // | ABC... | bitfield | XYZ... + |
| 435 | // +--------+------------+--------+ |
| 436 | // lsb 0 |
| 437 | // Returns: |
| 438 | // <-- width --> |
| 439 | // +--------+------------+--------+ |
| 440 | // | ABC... | 0...data | XYZ... + |
| 441 | // +--------+------------+--------+ |
| 442 | // lsb 0 |
| 443 | |
| 444 | template <typename T, typename T2> |
| 445 | inline static constexpr T BitFieldInsert(T value, T2 data, size_t lsb, size_t width) { |
| 446 | DCHECK_GE(BitSizeOf(value), lsb + width) << "Bit field out of range for value"; |
| 447 | if (width != 0u) { |
| 448 | DCHECK_GE(MaxInt<T2>(width), data) << "Data out of range [too large] for bitwidth"; |
| 449 | DCHECK_LE(MinInt<T2>(width), data) << "Data out of range [too small] for bitwidth"; |
| 450 | } else { |
| 451 | DCHECK_EQ(static_cast<T2>(0), data) << "Data out of range [nonzero] for bitwidth 0"; |
| 452 | } |
| 453 | const auto data_mask = MaskLeastSignificant<T2>(width); |
| 454 | const auto value_cleared = BitFieldClear(value, lsb, width); |
| 455 | |
| 456 | return static_cast<T>(value_cleared | ((data & data_mask) << lsb)); |
| 457 | } |
| 458 | |
| 459 | // Extracts the bitfield starting at the least significant bit "lsb" with a bitwidth of 'width'. |
| 460 | // Signed types are sign-extended during extraction. (Equivalent of ARM UBFX/SBFX instruction). |
| 461 | // |
| 462 | // Given: |
| 463 | // <-- width --> |
| 464 | // +--------+-------------+-------+ |
| 465 | // | | bitfield | + |
| 466 | // +--------+-------------+-------+ |
| 467 | // lsb 0 |
| 468 | // (Unsigned) Returns: |
| 469 | // <-- width --> |
| 470 | // +----------------+-------------+ |
| 471 | // | 0... 0 | bitfield | |
| 472 | // +----------------+-------------+ |
| 473 | // 0 |
| 474 | // (Signed) Returns: |
| 475 | // <-- width --> |
| 476 | // +----------------+-------------+ |
| 477 | // | S... S | bitfield | |
| 478 | // +----------------+-------------+ |
| 479 | // 0 |
| 480 | // where S is the highest bit in 'bitfield'. |
| 481 | template <typename T> |
| 482 | inline static constexpr T BitFieldExtract(T value, size_t lsb, size_t width) { |
| 483 | DCHECK_GE(BitSizeOf(value), lsb + width) << "Bit field out of range for value"; |
| 484 | const auto val = static_cast<std::make_unsigned_t<T>>(value); |
| 485 | |
| 486 | const T bitfield_unsigned = |
| 487 | static_cast<T>((val >> lsb) & MaskLeastSignificant<T>(width)); |
Vladimir Marko | 495311c | 2022-04-14 10:59:53 +0000 | [diff] [blame] | 488 | if (std::is_signed_v<T>) { |
Igor Murashkin | 1e77d27 | 2017-10-07 14:09:43 +0000 | [diff] [blame] | 489 | // Perform sign extension |
| 490 | if (width == 0) { // Avoid underflow. |
| 491 | return static_cast<T>(0); |
| 492 | } else if (bitfield_unsigned & (1 << (width - 1))) { // Detect if sign bit was set. |
| 493 | // MSB <width> LSB |
| 494 | // 0b11111...100...000000 |
| 495 | const auto ones_negmask = ~MaskLeastSignificant<T>(width); |
| 496 | return static_cast<T>(bitfield_unsigned | ones_negmask); |
| 497 | } |
| 498 | } |
| 499 | // Skip sign extension. |
| 500 | return bitfield_unsigned; |
| 501 | } |
| 502 | |
David Srbecky | 68fefac | 2018-05-10 17:49:33 +0100 | [diff] [blame] | 503 | inline static constexpr size_t BitsToBytesRoundUp(size_t num_bits) { |
| 504 | return RoundUp(num_bits, kBitsPerByte) / kBitsPerByte; |
| 505 | } |
| 506 | |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 507 | } // namespace art |
| 508 | |
David Sehr | 67bf42e | 2018-02-26 16:43:04 -0800 | [diff] [blame] | 509 | #endif // ART_LIBARTBASE_BASE_BIT_UTILS_H_ |