blob: f0811b020b90a58efdaad119cde3390cf2193be1 [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
17#ifndef ART_RUNTIME_BASE_BIT_UTILS_H_
18#define ART_RUNTIME_BASE_BIT_UTILS_H_
19
20#include <iterator>
21#include <limits>
22#include <type_traits>
23
Vladimir Marko80afd022015-05-19 18:08:00 +010024#include "base/iteration_range.h"
Andreas Gampe58246a12016-09-26 12:51:53 -070025#include "base/logging.h"
Vladimir Marko88b2b802015-12-04 14:19:04 +000026#include "base/stl_util.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010027
28namespace art {
29
30template<typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +010031constexpr int CLZ(T x) {
Vladimir Marko80afd022015-05-19 18:08:00 +010032 static_assert(std::is_integral<T>::value, "T must be integral");
Andreas Gampe151ab8d2015-08-14 23:01:49 +000033 static_assert(std::is_unsigned<T>::value, "T must be unsigned");
Vladimir Marko80afd022015-05-19 18:08:00 +010034 static_assert(sizeof(T) <= sizeof(long long), // NOLINT [runtime/int] [4]
35 "T too large, must be smaller than long long");
Vladimir Markof04cf542016-08-31 15:25:25 +010036 DCHECK_NE(x, 0u);
37 return (sizeof(T) == sizeof(uint32_t)) ? __builtin_clz(x) : __builtin_clzll(x);
Vladimir Marko80afd022015-05-19 18:08:00 +010038}
39
40template<typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +010041constexpr int CTZ(T x) {
Vladimir Marko80afd022015-05-19 18:08:00 +010042 static_assert(std::is_integral<T>::value, "T must be integral");
Andreas Gampe151ab8d2015-08-14 23:01:49 +000043 // It is not unreasonable to ask for trailing zeros in a negative number. As such, do not check
44 // that T is an unsigned type.
45 static_assert(sizeof(T) <= sizeof(long long), // NOLINT [runtime/int] [4]
46 "T too large, must be smaller than long long");
Vladimir Markof04cf542016-08-31 15:25:25 +010047 DCHECK_NE(x, static_cast<T>(0));
48 return (sizeof(T) == sizeof(uint32_t)) ? __builtin_ctz(x) : __builtin_ctzll(x);
Vladimir Marko80afd022015-05-19 18:08:00 +010049}
50
Roland Levillain0d5a2812015-11-13 10:07:31 +000051// Return the number of 1-bits in `x`.
Vladimir Marko80afd022015-05-19 18:08:00 +010052template<typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +010053constexpr int POPCOUNT(T x) {
54 return (sizeof(T) == sizeof(uint32_t)) ? __builtin_popcount(x) : __builtin_popcountll(x);
Vladimir Marko80afd022015-05-19 18:08:00 +010055}
56
57// Find the bit position of the most significant bit (0-based), or -1 if there were no bits set.
58template <typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +010059constexpr ssize_t MostSignificantBit(T value) {
Vladimir Marko80afd022015-05-19 18:08:00 +010060 static_assert(std::is_integral<T>::value, "T must be integral");
61 static_assert(std::is_unsigned<T>::value, "T must be unsigned");
62 static_assert(std::numeric_limits<T>::radix == 2, "Unexpected radix!");
63 return (value == 0) ? -1 : std::numeric_limits<T>::digits - 1 - CLZ(value);
64}
65
66// Find the bit position of the least significant bit (0-based), or -1 if there were no bits set.
67template <typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +010068constexpr ssize_t LeastSignificantBit(T value) {
Vladimir Marko80afd022015-05-19 18:08:00 +010069 static_assert(std::is_integral<T>::value, "T must be integral");
70 static_assert(std::is_unsigned<T>::value, "T must be unsigned");
71 return (value == 0) ? -1 : CTZ(value);
72}
73
74// How many bits (minimally) does it take to store the constant 'value'? i.e. 1 for 1, 3 for 5, etc.
75template <typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +010076constexpr size_t MinimumBitsToStore(T value) {
Vladimir Marko80afd022015-05-19 18:08:00 +010077 return static_cast<size_t>(MostSignificantBit(value) + 1);
78}
79
80template <typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +010081constexpr T RoundUpToPowerOfTwo(T x) {
Vladimir Marko80afd022015-05-19 18:08:00 +010082 static_assert(std::is_integral<T>::value, "T must be integral");
83 static_assert(std::is_unsigned<T>::value, "T must be unsigned");
84 // NOTE: Undefined if x > (1 << (std::numeric_limits<T>::digits - 1)).
85 return (x < 2u) ? x : static_cast<T>(1u) << (std::numeric_limits<T>::digits - CLZ(x - 1u));
86}
87
88template<typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +010089constexpr bool IsPowerOfTwo(T x) {
Vladimir Marko80afd022015-05-19 18:08:00 +010090 static_assert(std::is_integral<T>::value, "T must be integral");
91 // TODO: assert unsigned. There is currently many uses with signed values.
92 return (x & (x - 1)) == 0;
93}
94
95template<typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +010096constexpr int WhichPowerOf2(T x) {
Vladimir Marko80afd022015-05-19 18:08:00 +010097 static_assert(std::is_integral<T>::value, "T must be integral");
98 // TODO: assert unsigned. There is currently many uses with signed values.
99 DCHECK((x != 0) && IsPowerOfTwo(x));
100 return CTZ(x);
101}
102
103// For rounding integers.
Vladimir Marko88b2b802015-12-04 14:19:04 +0000104// Note: Omit the `n` from T type deduction, deduce only from the `x` argument.
Vladimir Marko80afd022015-05-19 18:08:00 +0100105template<typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +0100106constexpr T RoundDown(T x, typename Identity<T>::type n) WARN_UNUSED;
Vladimir Marko80afd022015-05-19 18:08:00 +0100107
108template<typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +0100109constexpr T RoundDown(T x, typename Identity<T>::type n) {
110 DCHECK(IsPowerOfTwo(n));
111 return (x & -n);
Vladimir Marko80afd022015-05-19 18:08:00 +0100112}
113
114template<typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +0100115constexpr T RoundUp(T x, typename std::remove_reference<T>::type n) WARN_UNUSED;
Vladimir Marko80afd022015-05-19 18:08:00 +0100116
117template<typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +0100118constexpr T RoundUp(T x, typename std::remove_reference<T>::type n) {
Vladimir Marko80afd022015-05-19 18:08:00 +0100119 return RoundDown(x + n - 1, n);
120}
121
122// For aligning pointers.
123template<typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +0100124inline T* AlignDown(T* x, uintptr_t n) WARN_UNUSED;
Vladimir Marko80afd022015-05-19 18:08:00 +0100125
126template<typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +0100127inline T* AlignDown(T* x, uintptr_t n) {
Vladimir Marko80afd022015-05-19 18:08:00 +0100128 return reinterpret_cast<T*>(RoundDown(reinterpret_cast<uintptr_t>(x), n));
129}
130
131template<typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +0100132inline T* AlignUp(T* x, uintptr_t n) WARN_UNUSED;
Vladimir Marko80afd022015-05-19 18:08:00 +0100133
134template<typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +0100135inline T* AlignUp(T* x, uintptr_t n) {
Vladimir Marko80afd022015-05-19 18:08:00 +0100136 return reinterpret_cast<T*>(RoundUp(reinterpret_cast<uintptr_t>(x), n));
137}
138
139template<int n, typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +0100140constexpr bool IsAligned(T x) {
Vladimir Marko80afd022015-05-19 18:08:00 +0100141 static_assert((n & (n - 1)) == 0, "n is not a power of two");
142 return (x & (n - 1)) == 0;
143}
144
145template<int n, typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +0100146inline bool IsAligned(T* x) {
Vladimir Marko80afd022015-05-19 18:08:00 +0100147 return IsAligned<n>(reinterpret_cast<const uintptr_t>(x));
148}
149
150template<typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +0100151inline bool IsAlignedParam(T x, int n) {
Vladimir Marko80afd022015-05-19 18:08:00 +0100152 return (x & (n - 1)) == 0;
153}
154
155#define CHECK_ALIGNED(value, alignment) \
156 CHECK(::art::IsAligned<alignment>(value)) << reinterpret_cast<const void*>(value)
157
158#define DCHECK_ALIGNED(value, alignment) \
159 DCHECK(::art::IsAligned<alignment>(value)) << reinterpret_cast<const void*>(value)
160
Vladimir Markocf36d492015-08-12 19:27:26 +0100161#define CHECK_ALIGNED_PARAM(value, alignment) \
162 CHECK(::art::IsAlignedParam(value, alignment)) << reinterpret_cast<const void*>(value)
163
Vladimir Marko80afd022015-05-19 18:08:00 +0100164#define DCHECK_ALIGNED_PARAM(value, alignment) \
165 DCHECK(::art::IsAlignedParam(value, alignment)) << reinterpret_cast<const void*>(value)
166
167// Like sizeof, but count how many bits a type takes. Pass type explicitly.
168template <typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +0100169constexpr size_t BitSizeOf() {
Vladimir Marko80afd022015-05-19 18:08:00 +0100170 static_assert(std::is_integral<T>::value, "T must be integral");
Vladimir Markof04cf542016-08-31 15:25:25 +0100171 using unsigned_type = typename std::make_unsigned<T>::type;
Vladimir Marko80afd022015-05-19 18:08:00 +0100172 static_assert(sizeof(T) == sizeof(unsigned_type), "Unexpected type size mismatch!");
173 static_assert(std::numeric_limits<unsigned_type>::radix == 2, "Unexpected radix!");
174 return std::numeric_limits<unsigned_type>::digits;
175}
176
177// Like sizeof, but count how many bits a type takes. Infers type from parameter.
178template <typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +0100179constexpr size_t BitSizeOf(T /*x*/) {
Vladimir Marko80afd022015-05-19 18:08:00 +0100180 return BitSizeOf<T>();
181}
182
Vladimir Markof04cf542016-08-31 15:25:25 +0100183inline uint16_t Low16Bits(uint32_t value) {
Vladimir Marko80afd022015-05-19 18:08:00 +0100184 return static_cast<uint16_t>(value);
185}
186
Vladimir Markof04cf542016-08-31 15:25:25 +0100187inline uint16_t High16Bits(uint32_t value) {
Vladimir Marko80afd022015-05-19 18:08:00 +0100188 return static_cast<uint16_t>(value >> 16);
189}
190
Vladimir Markof04cf542016-08-31 15:25:25 +0100191inline uint32_t Low32Bits(uint64_t value) {
Vladimir Marko80afd022015-05-19 18:08:00 +0100192 return static_cast<uint32_t>(value);
193}
194
Vladimir Markof04cf542016-08-31 15:25:25 +0100195inline uint32_t High32Bits(uint64_t value) {
Vladimir Marko80afd022015-05-19 18:08:00 +0100196 return static_cast<uint32_t>(value >> 32);
197}
198
199// Check whether an N-bit two's-complement representation can hold value.
200template <typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +0100201inline bool IsInt(size_t N, T value) {
Vladimir Marko80afd022015-05-19 18:08:00 +0100202 if (N == BitSizeOf<T>()) {
203 return true;
204 } else {
205 CHECK_LT(0u, N);
206 CHECK_LT(N, BitSizeOf<T>());
207 T limit = static_cast<T>(1) << (N - 1u);
208 return (-limit <= value) && (value < limit);
209 }
210}
211
212template <typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +0100213constexpr T GetIntLimit(size_t bits) {
214 DCHECK_NE(bits, 0u);
215 DCHECK_LT(bits, BitSizeOf<T>());
216 return static_cast<T>(1) << (bits - 1);
Vladimir Marko80afd022015-05-19 18:08:00 +0100217}
218
219template <size_t kBits, typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +0100220constexpr bool IsInt(T value) {
Vladimir Marko80afd022015-05-19 18:08:00 +0100221 static_assert(kBits > 0, "kBits cannot be zero.");
222 static_assert(kBits <= BitSizeOf<T>(), "kBits must be <= max.");
223 static_assert(std::is_signed<T>::value, "Needs a signed type.");
224 // Corner case for "use all bits." Can't use the limits, as they would overflow, but it is
225 // trivially true.
226 return (kBits == BitSizeOf<T>()) ?
227 true :
228 (-GetIntLimit<T>(kBits) <= value) && (value < GetIntLimit<T>(kBits));
229}
230
231template <size_t kBits, typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +0100232constexpr bool IsUint(T value) {
Vladimir Marko80afd022015-05-19 18:08:00 +0100233 static_assert(kBits > 0, "kBits cannot be zero.");
234 static_assert(kBits <= BitSizeOf<T>(), "kBits must be <= max.");
235 static_assert(std::is_integral<T>::value, "Needs an integral type.");
236 // Corner case for "use all bits." Can't use the limits, as they would overflow, but it is
237 // trivially true.
238 // NOTE: To avoid triggering assertion in GetIntLimit(kBits+1) if kBits+1==BitSizeOf<T>(),
239 // use GetIntLimit(kBits)*2u. The unsigned arithmetic works well for us if it overflows.
Vladimir Markof04cf542016-08-31 15:25:25 +0100240 using unsigned_type = typename std::make_unsigned<T>::type;
Vladimir Marko80afd022015-05-19 18:08:00 +0100241 return (0 <= value) &&
242 (kBits == BitSizeOf<T>() ||
Vladimir Markof04cf542016-08-31 15:25:25 +0100243 (static_cast<unsigned_type>(value) <= GetIntLimit<unsigned_type>(kBits) * 2u - 1u));
Vladimir Marko80afd022015-05-19 18:08:00 +0100244}
245
246template <size_t kBits, typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +0100247constexpr bool IsAbsoluteUint(T value) {
Vladimir Marko80afd022015-05-19 18:08:00 +0100248 static_assert(kBits <= BitSizeOf<T>(), "kBits must be <= max.");
249 static_assert(std::is_integral<T>::value, "Needs an integral type.");
Vladimir Markof04cf542016-08-31 15:25:25 +0100250 using unsigned_type = typename std::make_unsigned<T>::type;
Vladimir Marko80afd022015-05-19 18:08:00 +0100251 return (kBits == BitSizeOf<T>())
252 ? true
253 : IsUint<kBits>(value < 0
254 ? static_cast<unsigned_type>(-1 - value) + 1u // Avoid overflow.
255 : static_cast<unsigned_type>(value));
256}
257
Chris Larsendbce0d72015-09-17 13:34:00 -0700258// Generate maximum/minimum values for signed/unsigned n-bit integers
259template <typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +0100260constexpr T MaxInt(size_t bits) {
261 DCHECK(std::is_unsigned<T>::value || bits > 0u) << "bits cannot be zero for signed.";
262 DCHECK_LE(bits, BitSizeOf<T>());
263 using unsigned_type = typename std::make_unsigned<T>::type;
264 return bits == BitSizeOf<T>()
265 ? std::numeric_limits<T>::max()
266 : std::is_signed<T>::value
267 ? ((bits == 1u) ? 0 : static_cast<T>(MaxInt<unsigned_type>(bits - 1)))
268 : static_cast<T>(UINT64_C(1) << bits) - static_cast<T>(1);
Chris Larsendbce0d72015-09-17 13:34:00 -0700269}
270
271template <typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +0100272constexpr T MinInt(size_t bits) {
273 DCHECK(std::is_unsigned<T>::value || bits > 0) << "bits cannot be zero for signed.";
274 DCHECK_LE(bits, BitSizeOf<T>());
275 return bits == BitSizeOf<T>()
276 ? std::numeric_limits<T>::min()
277 : std::is_signed<T>::value
278 ? ((bits == 1u) ? -1 : static_cast<T>(-1) - MaxInt<T>(bits))
279 : static_cast<T>(0);
Chris Larsendbce0d72015-09-17 13:34:00 -0700280}
281
Vladimir Marko80afd022015-05-19 18:08:00 +0100282// Using the Curiously Recurring Template Pattern to implement everything shared
283// by LowToHighBitIterator and HighToLowBitIterator, i.e. everything but operator*().
284template <typename T, typename Iter>
285class BitIteratorBase
286 : public std::iterator<std::forward_iterator_tag, uint32_t, ptrdiff_t, void, void> {
287 static_assert(std::is_integral<T>::value, "T must be integral");
288 static_assert(std::is_unsigned<T>::value, "T must be unsigned");
289
290 static_assert(sizeof(T) == sizeof(uint32_t) || sizeof(T) == sizeof(uint64_t), "Unsupported size");
291
292 public:
293 BitIteratorBase() : bits_(0u) { }
294 explicit BitIteratorBase(T bits) : bits_(bits) { }
295
296 Iter& operator++() {
297 DCHECK_NE(bits_, 0u);
298 uint32_t bit = *static_cast<Iter&>(*this);
299 bits_ &= ~(static_cast<T>(1u) << bit);
300 return static_cast<Iter&>(*this);
301 }
302
303 Iter& operator++(int) {
304 Iter tmp(static_cast<Iter&>(*this));
305 ++*this;
306 return tmp;
307 }
308
309 protected:
310 T bits_;
311
312 template <typename U, typename I>
313 friend bool operator==(const BitIteratorBase<U, I>& lhs, const BitIteratorBase<U, I>& rhs);
314};
315
316template <typename T, typename Iter>
317bool operator==(const BitIteratorBase<T, Iter>& lhs, const BitIteratorBase<T, Iter>& rhs) {
318 return lhs.bits_ == rhs.bits_;
319}
320
321template <typename T, typename Iter>
322bool operator!=(const BitIteratorBase<T, Iter>& lhs, const BitIteratorBase<T, Iter>& rhs) {
323 return !(lhs == rhs);
324}
325
326template <typename T>
327class LowToHighBitIterator : public BitIteratorBase<T, LowToHighBitIterator<T>> {
328 public:
329 using BitIteratorBase<T, LowToHighBitIterator<T>>::BitIteratorBase;
330
331 uint32_t operator*() const {
332 DCHECK_NE(this->bits_, 0u);
333 return CTZ(this->bits_);
334 }
335};
336
337template <typename T>
338class HighToLowBitIterator : public BitIteratorBase<T, HighToLowBitIterator<T>> {
339 public:
340 using BitIteratorBase<T, HighToLowBitIterator<T>>::BitIteratorBase;
341
342 uint32_t operator*() const {
343 DCHECK_NE(this->bits_, 0u);
344 static_assert(std::numeric_limits<T>::radix == 2, "Unexpected radix!");
345 return std::numeric_limits<T>::digits - 1u - CLZ(this->bits_);
346 }
347};
348
349template <typename T>
350IterationRange<LowToHighBitIterator<T>> LowToHighBits(T bits) {
351 return IterationRange<LowToHighBitIterator<T>>(
352 LowToHighBitIterator<T>(bits), LowToHighBitIterator<T>());
353}
354
355template <typename T>
356IterationRange<HighToLowBitIterator<T>> HighToLowBits(T bits) {
357 return IterationRange<HighToLowBitIterator<T>>(
358 HighToLowBitIterator<T>(bits), HighToLowBitIterator<T>());
359}
360
361} // namespace art
362
363#endif // ART_RUNTIME_BASE_BIT_UTILS_H_