blob: ffaffc3a303a88291ee4364281a1a78084ed18a5 [file] [log] [blame]
Vladimir Marko80afd022015-05-19 18:08:00 +01001/*
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 Sehr67bf42e2018-02-26 16:43:04 -080017#ifndef ART_LIBARTBASE_BASE_BIT_UTILS_H_
18#define ART_LIBARTBASE_BASE_BIT_UTILS_H_
Vladimir Marko80afd022015-05-19 18:08:00 +010019
Vladimir Marko80afd022015-05-19 18:08:00 +010020#include <limits>
21#include <type_traits>
22
Andreas Gampe57943812017-12-06 21:39:13 -080023#include <android-base/logging.h>
24
David Srbecky68fefac2018-05-10 17:49:33 +010025#include "globals.h"
David Sehr1979c642018-04-26 14:41:18 -070026#include "stl_util_identity.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010027
28namespace art {
29
buzbee31afbec2017-03-14 15:30:19 -070030// Like sizeof, but count how many bits a type takes. Pass type explicitly.
31template <typename T>
32constexpr size_t BitSizeOf() {
Vladimir Marko495311c2022-04-14 10:59:53 +000033 static_assert(std::is_integral_v<T>, "T must be integral");
34 using unsigned_type = std::make_unsigned_t<T>;
buzbee31afbec2017-03-14 15:30:19 -070035 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.
41template <typename T>
42constexpr size_t BitSizeOf(T /*x*/) {
43 return BitSizeOf<T>();
44}
45
Vladimir Marko80afd022015-05-19 18:08:00 +010046template<typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +010047constexpr int CLZ(T x) {
Vladimir Marko495311c2022-04-14 10:59:53 +000048 static_assert(std::is_integral_v<T>, "T must be integral");
49 static_assert(std::is_unsigned_v<T>, "T must be unsigned");
Vladimir Markodc682aa2018-01-04 18:42:57 +000050 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 Markof04cf542016-08-31 15:25:25 +010053 DCHECK_NE(x, 0u);
Vladimir Markodc682aa2018-01-04 18:42:57 +000054 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 Marko80afd022015-05-19 18:08:00 +010058}
59
buzbee31afbec2017-03-14 15:30:19 -070060// Similar to CLZ except that on zero input it returns bitwidth and supports signed integers.
61template<typename T>
62constexpr int JAVASTYLE_CLZ(T x) {
Vladimir Marko495311c2022-04-14 10:59:53 +000063 static_assert(std::is_integral_v<T>, "T must be integral");
64 using unsigned_type = std::make_unsigned_t<T>;
buzbee31afbec2017-03-14 15:30:19 -070065 return (x == 0) ? BitSizeOf<T>() : CLZ(static_cast<unsigned_type>(x));
66}
67
Vladimir Marko80afd022015-05-19 18:08:00 +010068template<typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +010069constexpr int CTZ(T x) {
Vladimir Marko495311c2022-04-14 10:59:53 +000070 static_assert(std::is_integral_v<T>, "T must be integral");
Andreas Gampe151ab8d2015-08-14 23:01:49 +000071 // 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 Markodc682aa2018-01-04 18:42:57 +000073 static_assert(sizeof(T) == sizeof(uint64_t) || sizeof(T) <= sizeof(uint32_t),
74 "Unsupported sizeof(T)");
Vladimir Markof04cf542016-08-31 15:25:25 +010075 DCHECK_NE(x, static_cast<T>(0));
Vladimir Markodc682aa2018-01-04 18:42:57 +000076 return (sizeof(T) == sizeof(uint64_t)) ? __builtin_ctzll(x) : __builtin_ctz(x);
Vladimir Marko80afd022015-05-19 18:08:00 +010077}
78
buzbee31afbec2017-03-14 15:30:19 -070079// Similar to CTZ except that on zero input it returns bitwidth and supports signed integers.
80template<typename T>
81constexpr int JAVASTYLE_CTZ(T x) {
Vladimir Marko495311c2022-04-14 10:59:53 +000082 static_assert(std::is_integral_v<T>, "T must be integral");
83 using unsigned_type = std::make_unsigned_t<T>;
buzbee31afbec2017-03-14 15:30:19 -070084 return (x == 0) ? BitSizeOf<T>() : CTZ(static_cast<unsigned_type>(x));
85}
86
Roland Levillain0d5a2812015-11-13 10:07:31 +000087// Return the number of 1-bits in `x`.
Vladimir Marko80afd022015-05-19 18:08:00 +010088template<typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +010089constexpr int POPCOUNT(T x) {
90 return (sizeof(T) == sizeof(uint32_t)) ? __builtin_popcount(x) : __builtin_popcountll(x);
Vladimir Marko80afd022015-05-19 18:08:00 +010091}
92
buzbee31afbec2017-03-14 15:30:19 -070093// Swap bytes.
94template<typename T>
95constexpr 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 Marko80afd022015-05-19 18:08:00 +0100105// Find the bit position of the most significant bit (0-based), or -1 if there were no bits set.
106template <typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +0100107constexpr ssize_t MostSignificantBit(T value) {
Vladimir Marko495311c2022-04-14 10:59:53 +0000108 static_assert(std::is_integral_v<T>, "T must be integral");
109 static_assert(std::is_unsigned_v<T>, "T must be unsigned");
Vladimir Marko80afd022015-05-19 18:08:00 +0100110 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.
115template <typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +0100116constexpr ssize_t LeastSignificantBit(T value) {
Vladimir Marko495311c2022-04-14 10:59:53 +0000117 static_assert(std::is_integral_v<T>, "T must be integral");
118 static_assert(std::is_unsigned_v<T>, "T must be unsigned");
Vladimir Marko80afd022015-05-19 18:08:00 +0100119 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.
123template <typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +0100124constexpr size_t MinimumBitsToStore(T value) {
Vladimir Marko80afd022015-05-19 18:08:00 +0100125 return static_cast<size_t>(MostSignificantBit(value) + 1);
126}
127
128template <typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +0100129constexpr T RoundUpToPowerOfTwo(T x) {
Vladimir Marko495311c2022-04-14 10:59:53 +0000130 static_assert(std::is_integral_v<T>, "T must be integral");
131 static_assert(std::is_unsigned_v<T>, "T must be unsigned");
Vladimir Marko80afd022015-05-19 18:08:00 +0100132 // 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 Serovf26bb6c2017-09-01 10:59:03 +0100136// Return highest possible N - a power of two - such that val >= N.
137template <typename T>
138constexpr T TruncToPowerOfTwo(T val) {
Vladimir Marko495311c2022-04-14 10:59:53 +0000139 static_assert(std::is_integral_v<T>, "T must be integral");
140 static_assert(std::is_unsigned_v<T>, "T must be unsigned");
Artem Serovf26bb6c2017-09-01 10:59:03 +0100141 return (val != 0) ? static_cast<T>(1u) << (BitSizeOf<T>() - CLZ(val) - 1u) : 0;
142}
143
Vladimir Marko80afd022015-05-19 18:08:00 +0100144template<typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +0100145constexpr bool IsPowerOfTwo(T x) {
Vladimir Marko495311c2022-04-14 10:59:53 +0000146 static_assert(std::is_integral_v<T>, "T must be integral");
Vladimir Marko80afd022015-05-19 18:08:00 +0100147 // TODO: assert unsigned. There is currently many uses with signed values.
148 return (x & (x - 1)) == 0;
149}
150
151template<typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +0100152constexpr int WhichPowerOf2(T x) {
Vladimir Marko495311c2022-04-14 10:59:53 +0000153 static_assert(std::is_integral_v<T>, "T must be integral");
Vladimir Marko80afd022015-05-19 18:08:00 +0100154 // 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 Marko88b2b802015-12-04 14:19:04 +0000160// Note: Omit the `n` from T type deduction, deduce only from the `x` argument.
Vladimir Marko80afd022015-05-19 18:08:00 +0100161template<typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +0100162constexpr T RoundDown(T x, typename Identity<T>::type n) WARN_UNUSED;
Vladimir Marko80afd022015-05-19 18:08:00 +0100163
164template<typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +0100165constexpr T RoundDown(T x, typename Identity<T>::type n) {
166 DCHECK(IsPowerOfTwo(n));
167 return (x & -n);
Vladimir Marko80afd022015-05-19 18:08:00 +0100168}
169
170template<typename T>
Vladimir Marko495311c2022-04-14 10:59:53 +0000171constexpr T RoundUp(T x, std::remove_reference_t<T> n) WARN_UNUSED;
Vladimir Marko80afd022015-05-19 18:08:00 +0100172
173template<typename T>
Vladimir Marko495311c2022-04-14 10:59:53 +0000174constexpr T RoundUp(T x, std::remove_reference_t<T> n) {
Vladimir Marko80afd022015-05-19 18:08:00 +0100175 return RoundDown(x + n - 1, n);
176}
177
178// For aligning pointers.
179template<typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +0100180inline T* AlignDown(T* x, uintptr_t n) WARN_UNUSED;
Vladimir Marko80afd022015-05-19 18:08:00 +0100181
182template<typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +0100183inline T* AlignDown(T* x, uintptr_t n) {
Vladimir Marko80afd022015-05-19 18:08:00 +0100184 return reinterpret_cast<T*>(RoundDown(reinterpret_cast<uintptr_t>(x), n));
185}
186
187template<typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +0100188inline T* AlignUp(T* x, uintptr_t n) WARN_UNUSED;
Vladimir Marko80afd022015-05-19 18:08:00 +0100189
190template<typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +0100191inline T* AlignUp(T* x, uintptr_t n) {
Vladimir Marko80afd022015-05-19 18:08:00 +0100192 return reinterpret_cast<T*>(RoundUp(reinterpret_cast<uintptr_t>(x), n));
193}
194
195template<int n, typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +0100196constexpr bool IsAligned(T x) {
Vladimir Marko80afd022015-05-19 18:08:00 +0100197 static_assert((n & (n - 1)) == 0, "n is not a power of two");
198 return (x & (n - 1)) == 0;
199}
200
201template<int n, typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +0100202inline bool IsAligned(T* x) {
Vladimir Marko80afd022015-05-19 18:08:00 +0100203 return IsAligned<n>(reinterpret_cast<const uintptr_t>(x));
204}
205
206template<typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +0100207inline bool IsAlignedParam(T x, int n) {
Vladimir Marko80afd022015-05-19 18:08:00 +0100208 return (x & (n - 1)) == 0;
209}
210
Hiroshi Yamauchi3c3c4a12017-02-21 16:49:59 -0800211template<typename T>
212inline bool IsAlignedParam(T* x, int n) {
213 return IsAlignedParam(reinterpret_cast<const uintptr_t>(x), n);
214}
215
Vladimir Marko80afd022015-05-19 18:08:00 +0100216#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 Markocf36d492015-08-12 19:27:26 +0100222#define CHECK_ALIGNED_PARAM(value, alignment) \
223 CHECK(::art::IsAlignedParam(value, alignment)) << reinterpret_cast<const void*>(value)
224
Vladimir Marko80afd022015-05-19 18:08:00 +0100225#define DCHECK_ALIGNED_PARAM(value, alignment) \
226 DCHECK(::art::IsAlignedParam(value, alignment)) << reinterpret_cast<const void*>(value)
227
Vladimir Markof04cf542016-08-31 15:25:25 +0100228inline uint16_t Low16Bits(uint32_t value) {
Vladimir Marko80afd022015-05-19 18:08:00 +0100229 return static_cast<uint16_t>(value);
230}
231
Vladimir Markof04cf542016-08-31 15:25:25 +0100232inline uint16_t High16Bits(uint32_t value) {
Vladimir Marko80afd022015-05-19 18:08:00 +0100233 return static_cast<uint16_t>(value >> 16);
234}
235
Vladimir Markof04cf542016-08-31 15:25:25 +0100236inline uint32_t Low32Bits(uint64_t value) {
Vladimir Marko80afd022015-05-19 18:08:00 +0100237 return static_cast<uint32_t>(value);
238}
239
Vladimir Markof04cf542016-08-31 15:25:25 +0100240inline uint32_t High32Bits(uint64_t value) {
Vladimir Marko80afd022015-05-19 18:08:00 +0100241 return static_cast<uint32_t>(value >> 32);
242}
243
244// Check whether an N-bit two's-complement representation can hold value.
245template <typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +0100246inline bool IsInt(size_t N, T value) {
Vladimir Marko80afd022015-05-19 18:08:00 +0100247 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
257template <typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +0100258constexpr 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 Marko80afd022015-05-19 18:08:00 +0100262}
263
264template <size_t kBits, typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +0100265constexpr bool IsInt(T value) {
Vladimir Marko80afd022015-05-19 18:08:00 +0100266 static_assert(kBits > 0, "kBits cannot be zero.");
267 static_assert(kBits <= BitSizeOf<T>(), "kBits must be <= max.");
Vladimir Marko495311c2022-04-14 10:59:53 +0000268 static_assert(std::is_signed_v<T>, "Needs a signed type.");
Vladimir Marko80afd022015-05-19 18:08:00 +0100269 // 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
276template <size_t kBits, typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +0100277constexpr bool IsUint(T value) {
Vladimir Marko80afd022015-05-19 18:08:00 +0100278 static_assert(kBits > 0, "kBits cannot be zero.");
279 static_assert(kBits <= BitSizeOf<T>(), "kBits must be <= max.");
Vladimir Marko495311c2022-04-14 10:59:53 +0000280 static_assert(std::is_integral_v<T>, "Needs an integral type.");
Vladimir Marko80afd022015-05-19 18:08:00 +0100281 // 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 Marko495311c2022-04-14 10:59:53 +0000285 using unsigned_type = std::make_unsigned_t<T>;
Vladimir Marko80afd022015-05-19 18:08:00 +0100286 return (0 <= value) &&
287 (kBits == BitSizeOf<T>() ||
Vladimir Markof04cf542016-08-31 15:25:25 +0100288 (static_cast<unsigned_type>(value) <= GetIntLimit<unsigned_type>(kBits) * 2u - 1u));
Vladimir Marko80afd022015-05-19 18:08:00 +0100289}
290
291template <size_t kBits, typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +0100292constexpr bool IsAbsoluteUint(T value) {
Vladimir Marko80afd022015-05-19 18:08:00 +0100293 static_assert(kBits <= BitSizeOf<T>(), "kBits must be <= max.");
Vladimir Marko495311c2022-04-14 10:59:53 +0000294 static_assert(std::is_integral_v<T>, "Needs an integral type.");
295 using unsigned_type = std::make_unsigned_t<T>;
Vladimir Marko80afd022015-05-19 18:08:00 +0100296 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 Larsendbce0d72015-09-17 13:34:00 -0700303// Generate maximum/minimum values for signed/unsigned n-bit integers
304template <typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +0100305constexpr T MaxInt(size_t bits) {
Vladimir Marko495311c2022-04-14 10:59:53 +0000306 DCHECK(std::is_unsigned_v<T> || bits > 0u) << "bits cannot be zero for signed.";
Vladimir Markof04cf542016-08-31 15:25:25 +0100307 DCHECK_LE(bits, BitSizeOf<T>());
Vladimir Marko495311c2022-04-14 10:59:53 +0000308 using unsigned_type = std::make_unsigned_t<T>;
Vladimir Markof04cf542016-08-31 15:25:25 +0100309 return bits == BitSizeOf<T>()
310 ? std::numeric_limits<T>::max()
Vladimir Marko495311c2022-04-14 10:59:53 +0000311 : std::is_signed_v<T>
Vladimir Markof04cf542016-08-31 15:25:25 +0100312 ? ((bits == 1u) ? 0 : static_cast<T>(MaxInt<unsigned_type>(bits - 1)))
313 : static_cast<T>(UINT64_C(1) << bits) - static_cast<T>(1);
Chris Larsendbce0d72015-09-17 13:34:00 -0700314}
315
316template <typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +0100317constexpr T MinInt(size_t bits) {
Vladimir Marko495311c2022-04-14 10:59:53 +0000318 DCHECK(std::is_unsigned_v<T> || bits > 0) << "bits cannot be zero for signed.";
Vladimir Markof04cf542016-08-31 15:25:25 +0100319 DCHECK_LE(bits, BitSizeOf<T>());
320 return bits == BitSizeOf<T>()
321 ? std::numeric_limits<T>::min()
Vladimir Marko495311c2022-04-14 10:59:53 +0000322 : std::is_signed_v<T>
Vladimir Markof04cf542016-08-31 15:25:25 +0100323 ? ((bits == 1u) ? -1 : static_cast<T>(-1) - MaxInt<T>(bits))
324 : static_cast<T>(0);
Chris Larsendbce0d72015-09-17 13:34:00 -0700325}
326
buzbee31afbec2017-03-14 15:30:19 -0700327// Returns value with bit set in lowest one-bit position or 0 if 0. (java.lang.X.lowestOneBit).
328template <typename kind>
329inline 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).
335template <typename T>
336inline static T HighestOneBitValue(T opnd) {
Vladimir Marko495311c2022-04-14 10:59:53 +0000337 using unsigned_type = std::make_unsigned_t<T>;
buzbee31afbec2017-03-14 15:30:19 -0700338 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.
349template <typename T, bool left>
350inline 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 Marko495311c2022-04-14 10:59:53 +0000354 using unsigned_type = std::make_unsigned_t<T>;
buzbee31afbec2017-03-14 15:30:19 -0700355 return (static_cast<unsigned_type>(opnd) >> unsigned_right_shift) | (opnd << signed_left_shift);
356}
357
358// TUNING: use rbit for arm/arm64
359inline 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
370inline 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 Murashkin1e77d272017-10-07 14:09:43 +0000380// 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
390template <typename T = size_t>
391inline 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 Murashkin2f366292017-10-09 13:37:50 -0700397 auto kOne = static_cast<unsigned_T>(1); // Do not truncate for T>size_t.
398 return static_cast<unsigned_T>((kOne << bits) - kOne);
Igor Murashkin1e77d272017-10-07 14:09:43 +0000399 }
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
417template <typename T>
418inline 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
444template <typename T, typename T2>
445inline 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'.
481template <typename T>
482inline 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 Marko495311c2022-04-14 10:59:53 +0000488 if (std::is_signed_v<T>) {
Igor Murashkin1e77d272017-10-07 14:09:43 +0000489 // 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 Srbecky68fefac2018-05-10 17:49:33 +0100503inline static constexpr size_t BitsToBytesRoundUp(size_t num_bits) {
504 return RoundUp(num_bits, kBitsPerByte) / kBitsPerByte;
505}
506
Vladimir Marko80afd022015-05-19 18:08:00 +0100507} // namespace art
508
David Sehr67bf42e2018-02-26 16:43:04 -0800509#endif // ART_LIBARTBASE_BASE_BIT_UTILS_H_