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_LIBARTBASE_BASE_BIT_UTILS_H_ 18 #define ART_LIBARTBASE_BASE_BIT_UTILS_H_ 19 20 #include <limits> 21 #include <type_traits> 22 23 #include <android-base/logging.h> 24 25 #include "globals.h" 26 #include "stl_util_identity.h" 27 28 namespace art { 29 30 // Like sizeof, but count how many bits a type takes. Pass type explicitly. 31 template <typename T> 32 constexpr size_t BitSizeOf() { 33 static_assert(std::is_integral<T>::value, "T must be integral"); 34 using unsigned_type = typename std::make_unsigned<T>::type; 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 46 template<typename T> 47 constexpr int CLZ(T x) { 48 static_assert(std::is_integral<T>::value, "T must be integral"); 49 static_assert(std::is_unsigned<T>::value, "T must be unsigned"); 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)"); 53 DCHECK_NE(x, 0u); 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; 58 } 59 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) { 63 static_assert(std::is_integral<T>::value, "T must be integral"); 64 using unsigned_type = typename std::make_unsigned<T>::type; 65 return (x == 0) ? BitSizeOf<T>() : CLZ(static_cast<unsigned_type>(x)); 66 } 67 68 template<typename T> 69 constexpr int CTZ(T x) { 70 static_assert(std::is_integral<T>::value, "T must be integral"); 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. 73 static_assert(sizeof(T) == sizeof(uint64_t) || sizeof(T) <= sizeof(uint32_t), 74 "Unsupported sizeof(T)"); 75 DCHECK_NE(x, static_cast<T>(0)); 76 return (sizeof(T) == sizeof(uint64_t)) ? __builtin_ctzll(x) : __builtin_ctz(x); 77 } 78 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) { 82 static_assert(std::is_integral<T>::value, "T must be integral"); 83 using unsigned_type = typename std::make_unsigned<T>::type; 84 return (x == 0) ? BitSizeOf<T>() : CTZ(static_cast<unsigned_type>(x)); 85 } 86 87 // Return the number of 1-bits in `x`. 88 template<typename T> 89 constexpr int POPCOUNT(T x) { 90 return (sizeof(T) == sizeof(uint32_t)) ? __builtin_popcount(x) : __builtin_popcountll(x); 91 } 92 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 105 // Find the bit position of the most significant bit (0-based), or -1 if there were no bits set. 106 template <typename T> 107 constexpr ssize_t MostSignificantBit(T value) { 108 static_assert(std::is_integral<T>::value, "T must be integral"); 109 static_assert(std::is_unsigned<T>::value, "T must be unsigned"); 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> 116 constexpr ssize_t LeastSignificantBit(T value) { 117 static_assert(std::is_integral<T>::value, "T must be integral"); 118 static_assert(std::is_unsigned<T>::value, "T must be unsigned"); 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> 124 constexpr size_t MinimumBitsToStore(T value) { 125 return static_cast<size_t>(MostSignificantBit(value) + 1); 126 } 127 128 template <typename T> 129 constexpr T RoundUpToPowerOfTwo(T x) { 130 static_assert(std::is_integral<T>::value, "T must be integral"); 131 static_assert(std::is_unsigned<T>::value, "T must be unsigned"); 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 136 // Return highest possible N - a power of two - such that val >= N. 137 template <typename T> 138 constexpr T TruncToPowerOfTwo(T val) { 139 static_assert(std::is_integral<T>::value, "T must be integral"); 140 static_assert(std::is_unsigned<T>::value, "T must be unsigned"); 141 return (val != 0) ? static_cast<T>(1u) << (BitSizeOf<T>() - CLZ(val) - 1u) : 0; 142 } 143 144 template<typename T> 145 constexpr bool IsPowerOfTwo(T x) { 146 static_assert(std::is_integral<T>::value, "T must be integral"); 147 // TODO: assert unsigned. There is currently many uses with signed values. 148 return (x & (x - 1)) == 0; 149 } 150 151 template<typename T> 152 constexpr int WhichPowerOf2(T x) { 153 static_assert(std::is_integral<T>::value, "T must be integral"); 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. 160 // Note: Omit the `n` from T type deduction, deduce only from the `x` argument. 161 template<typename T> 162 constexpr T RoundDown(T x, typename Identity<T>::type n) WARN_UNUSED; 163 164 template<typename T> 165 constexpr T RoundDown(T x, typename Identity<T>::type n) { 166 DCHECK(IsPowerOfTwo(n)); 167 return (x & -n); 168 } 169 170 template<typename T> 171 constexpr T RoundUp(T x, typename std::remove_reference<T>::type n) WARN_UNUSED; 172 173 template<typename T> 174 constexpr T RoundUp(T x, typename std::remove_reference<T>::type n) { 175 return RoundDown(x + n - 1, n); 176 } 177 178 // For aligning pointers. 179 template<typename T> 180 inline T* AlignDown(T* x, uintptr_t n) WARN_UNUSED; 181 182 template<typename T> 183 inline T* AlignDown(T* x, uintptr_t n) { 184 return reinterpret_cast<T*>(RoundDown(reinterpret_cast<uintptr_t>(x), n)); 185 } 186 187 template<typename T> 188 inline T* AlignUp(T* x, uintptr_t n) WARN_UNUSED; 189 190 template<typename T> 191 inline T* AlignUp(T* x, uintptr_t n) { 192 return reinterpret_cast<T*>(RoundUp(reinterpret_cast<uintptr_t>(x), n)); 193 } 194 195 template<int n, typename T> 196 constexpr bool IsAligned(T x) { 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> 202 inline bool IsAligned(T* x) { 203 return IsAligned<n>(reinterpret_cast<const uintptr_t>(x)); 204 } 205 206 template<typename T> 207 inline bool IsAlignedParam(T x, int n) { 208 return (x & (n - 1)) == 0; 209 } 210 211 template<typename T> 212 inline bool IsAlignedParam(T* x, int n) { 213 return IsAlignedParam(reinterpret_cast<const uintptr_t>(x), n); 214 } 215 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 222 #define CHECK_ALIGNED_PARAM(value, alignment) \ 223 CHECK(::art::IsAlignedParam(value, alignment)) << reinterpret_cast<const void*>(value) 224 225 #define DCHECK_ALIGNED_PARAM(value, alignment) \ 226 DCHECK(::art::IsAlignedParam(value, alignment)) << reinterpret_cast<const void*>(value) 227 228 inline uint16_t Low16Bits(uint32_t value) { 229 return static_cast<uint16_t>(value); 230 } 231 232 inline uint16_t High16Bits(uint32_t value) { 233 return static_cast<uint16_t>(value >> 16); 234 } 235 236 inline uint32_t Low32Bits(uint64_t value) { 237 return static_cast<uint32_t>(value); 238 } 239 240 inline uint32_t High32Bits(uint64_t value) { 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> 246 inline bool IsInt(size_t N, T value) { 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> 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); 262 } 263 264 template <size_t kBits, typename T> 265 constexpr bool IsInt(T value) { 266 static_assert(kBits > 0, "kBits cannot be zero."); 267 static_assert(kBits <= BitSizeOf<T>(), "kBits must be <= max."); 268 static_assert(std::is_signed<T>::value, "Needs a signed type."); 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> 277 constexpr bool IsUint(T value) { 278 static_assert(kBits > 0, "kBits cannot be zero."); 279 static_assert(kBits <= BitSizeOf<T>(), "kBits must be <= max."); 280 static_assert(std::is_integral<T>::value, "Needs an integral type."); 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. 285 using unsigned_type = typename std::make_unsigned<T>::type; 286 return (0 <= value) && 287 (kBits == BitSizeOf<T>() || 288 (static_cast<unsigned_type>(value) <= GetIntLimit<unsigned_type>(kBits) * 2u - 1u)); 289 } 290 291 template <size_t kBits, typename T> 292 constexpr bool IsAbsoluteUint(T value) { 293 static_assert(kBits <= BitSizeOf<T>(), "kBits must be <= max."); 294 static_assert(std::is_integral<T>::value, "Needs an integral type."); 295 using unsigned_type = typename std::make_unsigned<T>::type; 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 303 // Generate maximum/minimum values for signed/unsigned n-bit integers 304 template <typename T> 305 constexpr T MaxInt(size_t bits) { 306 DCHECK(std::is_unsigned<T>::value || bits > 0u) << "bits cannot be zero for signed."; 307 DCHECK_LE(bits, BitSizeOf<T>()); 308 using unsigned_type = typename std::make_unsigned<T>::type; 309 return bits == BitSizeOf<T>() 310 ? std::numeric_limits<T>::max() 311 : std::is_signed<T>::value 312 ? ((bits == 1u) ? 0 : static_cast<T>(MaxInt<unsigned_type>(bits - 1))) 313 : static_cast<T>(UINT64_C(1) << bits) - static_cast<T>(1); 314 } 315 316 template <typename T> 317 constexpr T MinInt(size_t bits) { 318 DCHECK(std::is_unsigned<T>::value || bits > 0) << "bits cannot be zero for signed."; 319 DCHECK_LE(bits, BitSizeOf<T>()); 320 return bits == BitSizeOf<T>() 321 ? std::numeric_limits<T>::min() 322 : std::is_signed<T>::value 323 ? ((bits == 1u) ? -1 : static_cast<T>(-1) - MaxInt<T>(bits)) 324 : static_cast<T>(0); 325 } 326 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) { 337 using unsigned_type = typename std::make_unsigned<T>::type; 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); 354 using unsigned_type = typename std::make_unsigned<T>::type; 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 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 { 397 auto kOne = static_cast<unsigned_T>(1); // Do not truncate for T>size_t. 398 return static_cast<unsigned_T>((kOne << bits) - kOne); 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)); 488 if (std::is_signed<T>::value) { 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 503 inline static constexpr size_t BitsToBytesRoundUp(size_t num_bits) { 504 return RoundUp(num_bits, kBitsPerByte) / kBitsPerByte; 505 } 506 507 } // namespace art 508 509 #endif // ART_LIBARTBASE_BASE_BIT_UTILS_H_ 510