blob: ffaffc3a303a88291ee4364281a1a78084ed18a5 [file] [log] [blame]
Martin Stjernholmc15e7e42020-12-02 22:50:53 +00001/*
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
28namespace art {
29
30// Like sizeof, but count how many bits a type takes. Pass type explicitly.
31template <typename T>
32constexpr size_t BitSizeOf() {
android-t13d2c5b22022-10-12 13:43:18 +080033 static_assert(std::is_integral_v<T>, "T must be integral");
34 using unsigned_type = std::make_unsigned_t<T>;
Martin Stjernholmc15e7e42020-12-02 22:50:53 +000035 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
46template<typename T>
47constexpr int CLZ(T x) {
android-t13d2c5b22022-10-12 13:43:18 +080048 static_assert(std::is_integral_v<T>, "T must be integral");
49 static_assert(std::is_unsigned_v<T>, "T must be unsigned");
Martin Stjernholmc15e7e42020-12-02 22:50:53 +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)");
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.
61template<typename T>
62constexpr int JAVASTYLE_CLZ(T x) {
android-t13d2c5b22022-10-12 13:43:18 +080063 static_assert(std::is_integral_v<T>, "T must be integral");
64 using unsigned_type = std::make_unsigned_t<T>;
Martin Stjernholmc15e7e42020-12-02 22:50:53 +000065 return (x == 0) ? BitSizeOf<T>() : CLZ(static_cast<unsigned_type>(x));
66}
67
68template<typename T>
69constexpr int CTZ(T x) {
android-t13d2c5b22022-10-12 13:43:18 +080070 static_assert(std::is_integral_v<T>, "T must be integral");
Martin Stjernholmc15e7e42020-12-02 22:50:53 +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.
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.
80template<typename T>
81constexpr int JAVASTYLE_CTZ(T x) {
android-t13d2c5b22022-10-12 13:43:18 +080082 static_assert(std::is_integral_v<T>, "T must be integral");
83 using unsigned_type = std::make_unsigned_t<T>;
Martin Stjernholmc15e7e42020-12-02 22:50:53 +000084 return (x == 0) ? BitSizeOf<T>() : CTZ(static_cast<unsigned_type>(x));
85}
86
87// Return the number of 1-bits in `x`.
88template<typename T>
89constexpr int POPCOUNT(T x) {
90 return (sizeof(T) == sizeof(uint32_t)) ? __builtin_popcount(x) : __builtin_popcountll(x);
91}
92
93// 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
105// Find the bit position of the most significant bit (0-based), or -1 if there were no bits set.
106template <typename T>
107constexpr ssize_t MostSignificantBit(T value) {
android-t13d2c5b22022-10-12 13:43:18 +0800108 static_assert(std::is_integral_v<T>, "T must be integral");
109 static_assert(std::is_unsigned_v<T>, "T must be unsigned");
Martin Stjernholmc15e7e42020-12-02 22:50:53 +0000110 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>
116constexpr ssize_t LeastSignificantBit(T value) {
android-t13d2c5b22022-10-12 13:43:18 +0800117 static_assert(std::is_integral_v<T>, "T must be integral");
118 static_assert(std::is_unsigned_v<T>, "T must be unsigned");
Martin Stjernholmc15e7e42020-12-02 22:50:53 +0000119 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>
124constexpr size_t MinimumBitsToStore(T value) {
125 return static_cast<size_t>(MostSignificantBit(value) + 1);
126}
127
128template <typename T>
129constexpr T RoundUpToPowerOfTwo(T x) {
android-t13d2c5b22022-10-12 13:43:18 +0800130 static_assert(std::is_integral_v<T>, "T must be integral");
131 static_assert(std::is_unsigned_v<T>, "T must be unsigned");
Martin Stjernholmc15e7e42020-12-02 22:50:53 +0000132 // 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.
137template <typename T>
138constexpr T TruncToPowerOfTwo(T val) {
android-t13d2c5b22022-10-12 13:43:18 +0800139 static_assert(std::is_integral_v<T>, "T must be integral");
140 static_assert(std::is_unsigned_v<T>, "T must be unsigned");
Martin Stjernholmc15e7e42020-12-02 22:50:53 +0000141 return (val != 0) ? static_cast<T>(1u) << (BitSizeOf<T>() - CLZ(val) - 1u) : 0;
142}
143
144template<typename T>
145constexpr bool IsPowerOfTwo(T x) {
android-t13d2c5b22022-10-12 13:43:18 +0800146 static_assert(std::is_integral_v<T>, "T must be integral");
Martin Stjernholmc15e7e42020-12-02 22:50:53 +0000147 // TODO: assert unsigned. There is currently many uses with signed values.
148 return (x & (x - 1)) == 0;
149}
150
151template<typename T>
152constexpr int WhichPowerOf2(T x) {
android-t13d2c5b22022-10-12 13:43:18 +0800153 static_assert(std::is_integral_v<T>, "T must be integral");
Martin Stjernholmc15e7e42020-12-02 22:50:53 +0000154 // 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.
161template<typename T>
162constexpr T RoundDown(T x, typename Identity<T>::type n) WARN_UNUSED;
163
164template<typename T>
165constexpr T RoundDown(T x, typename Identity<T>::type n) {
166 DCHECK(IsPowerOfTwo(n));
167 return (x & -n);
168}
169
170template<typename T>
android-t13d2c5b22022-10-12 13:43:18 +0800171constexpr T RoundUp(T x, std::remove_reference_t<T> n) WARN_UNUSED;
Martin Stjernholmc15e7e42020-12-02 22:50:53 +0000172
173template<typename T>
android-t13d2c5b22022-10-12 13:43:18 +0800174constexpr T RoundUp(T x, std::remove_reference_t<T> n) {
Martin Stjernholmc15e7e42020-12-02 22:50:53 +0000175 return RoundDown(x + n - 1, n);
176}
177
178// For aligning pointers.
179template<typename T>
180inline T* AlignDown(T* x, uintptr_t n) WARN_UNUSED;
181
182template<typename T>
183inline T* AlignDown(T* x, uintptr_t n) {
184 return reinterpret_cast<T*>(RoundDown(reinterpret_cast<uintptr_t>(x), n));
185}
186
187template<typename T>
188inline T* AlignUp(T* x, uintptr_t n) WARN_UNUSED;
189
190template<typename T>
191inline T* AlignUp(T* x, uintptr_t n) {
192 return reinterpret_cast<T*>(RoundUp(reinterpret_cast<uintptr_t>(x), n));
193}
194
195template<int n, typename T>
196constexpr 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
201template<int n, typename T>
202inline bool IsAligned(T* x) {
203 return IsAligned<n>(reinterpret_cast<const uintptr_t>(x));
204}
205
206template<typename T>
207inline bool IsAlignedParam(T x, int n) {
208 return (x & (n - 1)) == 0;
209}
210
211template<typename T>
212inline 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
228inline uint16_t Low16Bits(uint32_t value) {
229 return static_cast<uint16_t>(value);
230}
231
232inline uint16_t High16Bits(uint32_t value) {
233 return static_cast<uint16_t>(value >> 16);
234}
235
236inline uint32_t Low32Bits(uint64_t value) {
237 return static_cast<uint32_t>(value);
238}
239
240inline 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.
245template <typename T>
246inline 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
257template <typename T>
258constexpr 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
264template <size_t kBits, typename T>
265constexpr bool IsInt(T value) {
266 static_assert(kBits > 0, "kBits cannot be zero.");
267 static_assert(kBits <= BitSizeOf<T>(), "kBits must be <= max.");
android-t13d2c5b22022-10-12 13:43:18 +0800268 static_assert(std::is_signed_v<T>, "Needs a signed type.");
Martin Stjernholmc15e7e42020-12-02 22:50:53 +0000269 // 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>
277constexpr bool IsUint(T value) {
278 static_assert(kBits > 0, "kBits cannot be zero.");
279 static_assert(kBits <= BitSizeOf<T>(), "kBits must be <= max.");
android-t13d2c5b22022-10-12 13:43:18 +0800280 static_assert(std::is_integral_v<T>, "Needs an integral type.");
Martin Stjernholmc15e7e42020-12-02 22:50:53 +0000281 // 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.
android-t13d2c5b22022-10-12 13:43:18 +0800285 using unsigned_type = std::make_unsigned_t<T>;
Martin Stjernholmc15e7e42020-12-02 22:50:53 +0000286 return (0 <= value) &&
287 (kBits == BitSizeOf<T>() ||
288 (static_cast<unsigned_type>(value) <= GetIntLimit<unsigned_type>(kBits) * 2u - 1u));
289}
290
291template <size_t kBits, typename T>
292constexpr bool IsAbsoluteUint(T value) {
293 static_assert(kBits <= BitSizeOf<T>(), "kBits must be <= max.");
android-t13d2c5b22022-10-12 13:43:18 +0800294 static_assert(std::is_integral_v<T>, "Needs an integral type.");
295 using unsigned_type = std::make_unsigned_t<T>;
Martin Stjernholmc15e7e42020-12-02 22:50:53 +0000296 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
304template <typename T>
305constexpr T MaxInt(size_t bits) {
android-t13d2c5b22022-10-12 13:43:18 +0800306 DCHECK(std::is_unsigned_v<T> || bits > 0u) << "bits cannot be zero for signed.";
Martin Stjernholmc15e7e42020-12-02 22:50:53 +0000307 DCHECK_LE(bits, BitSizeOf<T>());
android-t13d2c5b22022-10-12 13:43:18 +0800308 using unsigned_type = std::make_unsigned_t<T>;
Martin Stjernholmc15e7e42020-12-02 22:50:53 +0000309 return bits == BitSizeOf<T>()
310 ? std::numeric_limits<T>::max()
android-t13d2c5b22022-10-12 13:43:18 +0800311 : std::is_signed_v<T>
Martin Stjernholmc15e7e42020-12-02 22:50:53 +0000312 ? ((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
316template <typename T>
317constexpr T MinInt(size_t bits) {
android-t13d2c5b22022-10-12 13:43:18 +0800318 DCHECK(std::is_unsigned_v<T> || bits > 0) << "bits cannot be zero for signed.";
Martin Stjernholmc15e7e42020-12-02 22:50:53 +0000319 DCHECK_LE(bits, BitSizeOf<T>());
320 return bits == BitSizeOf<T>()
321 ? std::numeric_limits<T>::min()
android-t13d2c5b22022-10-12 13:43:18 +0800322 : std::is_signed_v<T>
Martin Stjernholmc15e7e42020-12-02 22:50:53 +0000323 ? ((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).
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) {
android-t13d2c5b22022-10-12 13:43:18 +0800337 using unsigned_type = std::make_unsigned_t<T>;
Martin Stjernholmc15e7e42020-12-02 22:50:53 +0000338 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);
android-t13d2c5b22022-10-12 13:43:18 +0800354 using unsigned_type = std::make_unsigned_t<T>;
Martin Stjernholmc15e7e42020-12-02 22:50:53 +0000355 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
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
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 {
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
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));
android-t13d2c5b22022-10-12 13:43:18 +0800488 if (std::is_signed_v<T>) {
Martin Stjernholmc15e7e42020-12-02 22:50:53 +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
503inline 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_