blob: 4041f5e1ed15bd746348fb6305767f92238a31a6 [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
Hiroshi Yamauchi3c3c4a12017-02-21 16:49:59 -0800155template<typename T>
156inline bool IsAlignedParam(T* x, int n) {
157 return IsAlignedParam(reinterpret_cast<const uintptr_t>(x), n);
158}
159
Vladimir Marko80afd022015-05-19 18:08:00 +0100160#define CHECK_ALIGNED(value, alignment) \
161 CHECK(::art::IsAligned<alignment>(value)) << reinterpret_cast<const void*>(value)
162
163#define DCHECK_ALIGNED(value, alignment) \
164 DCHECK(::art::IsAligned<alignment>(value)) << reinterpret_cast<const void*>(value)
165
Vladimir Markocf36d492015-08-12 19:27:26 +0100166#define CHECK_ALIGNED_PARAM(value, alignment) \
167 CHECK(::art::IsAlignedParam(value, alignment)) << reinterpret_cast<const void*>(value)
168
Vladimir Marko80afd022015-05-19 18:08:00 +0100169#define DCHECK_ALIGNED_PARAM(value, alignment) \
170 DCHECK(::art::IsAlignedParam(value, alignment)) << reinterpret_cast<const void*>(value)
171
172// Like sizeof, but count how many bits a type takes. Pass type explicitly.
173template <typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +0100174constexpr size_t BitSizeOf() {
Vladimir Marko80afd022015-05-19 18:08:00 +0100175 static_assert(std::is_integral<T>::value, "T must be integral");
Vladimir Markof04cf542016-08-31 15:25:25 +0100176 using unsigned_type = typename std::make_unsigned<T>::type;
Vladimir Marko80afd022015-05-19 18:08:00 +0100177 static_assert(sizeof(T) == sizeof(unsigned_type), "Unexpected type size mismatch!");
178 static_assert(std::numeric_limits<unsigned_type>::radix == 2, "Unexpected radix!");
179 return std::numeric_limits<unsigned_type>::digits;
180}
181
182// Like sizeof, but count how many bits a type takes. Infers type from parameter.
183template <typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +0100184constexpr size_t BitSizeOf(T /*x*/) {
Vladimir Marko80afd022015-05-19 18:08:00 +0100185 return BitSizeOf<T>();
186}
187
Vladimir Markof04cf542016-08-31 15:25:25 +0100188inline uint16_t Low16Bits(uint32_t value) {
Vladimir Marko80afd022015-05-19 18:08:00 +0100189 return static_cast<uint16_t>(value);
190}
191
Vladimir Markof04cf542016-08-31 15:25:25 +0100192inline uint16_t High16Bits(uint32_t value) {
Vladimir Marko80afd022015-05-19 18:08:00 +0100193 return static_cast<uint16_t>(value >> 16);
194}
195
Vladimir Markof04cf542016-08-31 15:25:25 +0100196inline uint32_t Low32Bits(uint64_t value) {
Vladimir Marko80afd022015-05-19 18:08:00 +0100197 return static_cast<uint32_t>(value);
198}
199
Vladimir Markof04cf542016-08-31 15:25:25 +0100200inline uint32_t High32Bits(uint64_t value) {
Vladimir Marko80afd022015-05-19 18:08:00 +0100201 return static_cast<uint32_t>(value >> 32);
202}
203
204// Check whether an N-bit two's-complement representation can hold value.
205template <typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +0100206inline bool IsInt(size_t N, T value) {
Vladimir Marko80afd022015-05-19 18:08:00 +0100207 if (N == BitSizeOf<T>()) {
208 return true;
209 } else {
210 CHECK_LT(0u, N);
211 CHECK_LT(N, BitSizeOf<T>());
212 T limit = static_cast<T>(1) << (N - 1u);
213 return (-limit <= value) && (value < limit);
214 }
215}
216
217template <typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +0100218constexpr T GetIntLimit(size_t bits) {
219 DCHECK_NE(bits, 0u);
220 DCHECK_LT(bits, BitSizeOf<T>());
221 return static_cast<T>(1) << (bits - 1);
Vladimir Marko80afd022015-05-19 18:08:00 +0100222}
223
224template <size_t kBits, typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +0100225constexpr bool IsInt(T value) {
Vladimir Marko80afd022015-05-19 18:08:00 +0100226 static_assert(kBits > 0, "kBits cannot be zero.");
227 static_assert(kBits <= BitSizeOf<T>(), "kBits must be <= max.");
228 static_assert(std::is_signed<T>::value, "Needs a signed type.");
229 // Corner case for "use all bits." Can't use the limits, as they would overflow, but it is
230 // trivially true.
231 return (kBits == BitSizeOf<T>()) ?
232 true :
233 (-GetIntLimit<T>(kBits) <= value) && (value < GetIntLimit<T>(kBits));
234}
235
236template <size_t kBits, typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +0100237constexpr bool IsUint(T value) {
Vladimir Marko80afd022015-05-19 18:08:00 +0100238 static_assert(kBits > 0, "kBits cannot be zero.");
239 static_assert(kBits <= BitSizeOf<T>(), "kBits must be <= max.");
240 static_assert(std::is_integral<T>::value, "Needs an integral type.");
241 // Corner case for "use all bits." Can't use the limits, as they would overflow, but it is
242 // trivially true.
243 // NOTE: To avoid triggering assertion in GetIntLimit(kBits+1) if kBits+1==BitSizeOf<T>(),
244 // use GetIntLimit(kBits)*2u. The unsigned arithmetic works well for us if it overflows.
Vladimir Markof04cf542016-08-31 15:25:25 +0100245 using unsigned_type = typename std::make_unsigned<T>::type;
Vladimir Marko80afd022015-05-19 18:08:00 +0100246 return (0 <= value) &&
247 (kBits == BitSizeOf<T>() ||
Vladimir Markof04cf542016-08-31 15:25:25 +0100248 (static_cast<unsigned_type>(value) <= GetIntLimit<unsigned_type>(kBits) * 2u - 1u));
Vladimir Marko80afd022015-05-19 18:08:00 +0100249}
250
251template <size_t kBits, typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +0100252constexpr bool IsAbsoluteUint(T value) {
Vladimir Marko80afd022015-05-19 18:08:00 +0100253 static_assert(kBits <= BitSizeOf<T>(), "kBits must be <= max.");
254 static_assert(std::is_integral<T>::value, "Needs an integral type.");
Vladimir Markof04cf542016-08-31 15:25:25 +0100255 using unsigned_type = typename std::make_unsigned<T>::type;
Vladimir Marko80afd022015-05-19 18:08:00 +0100256 return (kBits == BitSizeOf<T>())
257 ? true
258 : IsUint<kBits>(value < 0
259 ? static_cast<unsigned_type>(-1 - value) + 1u // Avoid overflow.
260 : static_cast<unsigned_type>(value));
261}
262
Chris Larsendbce0d72015-09-17 13:34:00 -0700263// Generate maximum/minimum values for signed/unsigned n-bit integers
264template <typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +0100265constexpr T MaxInt(size_t bits) {
266 DCHECK(std::is_unsigned<T>::value || bits > 0u) << "bits cannot be zero for signed.";
267 DCHECK_LE(bits, BitSizeOf<T>());
268 using unsigned_type = typename std::make_unsigned<T>::type;
269 return bits == BitSizeOf<T>()
270 ? std::numeric_limits<T>::max()
271 : std::is_signed<T>::value
272 ? ((bits == 1u) ? 0 : static_cast<T>(MaxInt<unsigned_type>(bits - 1)))
273 : static_cast<T>(UINT64_C(1) << bits) - static_cast<T>(1);
Chris Larsendbce0d72015-09-17 13:34:00 -0700274}
275
276template <typename T>
Vladimir Markof04cf542016-08-31 15:25:25 +0100277constexpr T MinInt(size_t bits) {
278 DCHECK(std::is_unsigned<T>::value || bits > 0) << "bits cannot be zero for signed.";
279 DCHECK_LE(bits, BitSizeOf<T>());
280 return bits == BitSizeOf<T>()
281 ? std::numeric_limits<T>::min()
282 : std::is_signed<T>::value
283 ? ((bits == 1u) ? -1 : static_cast<T>(-1) - MaxInt<T>(bits))
284 : static_cast<T>(0);
Chris Larsendbce0d72015-09-17 13:34:00 -0700285}
286
Vladimir Marko80afd022015-05-19 18:08:00 +0100287// Using the Curiously Recurring Template Pattern to implement everything shared
288// by LowToHighBitIterator and HighToLowBitIterator, i.e. everything but operator*().
289template <typename T, typename Iter>
290class BitIteratorBase
291 : public std::iterator<std::forward_iterator_tag, uint32_t, ptrdiff_t, void, void> {
292 static_assert(std::is_integral<T>::value, "T must be integral");
293 static_assert(std::is_unsigned<T>::value, "T must be unsigned");
294
295 static_assert(sizeof(T) == sizeof(uint32_t) || sizeof(T) == sizeof(uint64_t), "Unsupported size");
296
297 public:
298 BitIteratorBase() : bits_(0u) { }
299 explicit BitIteratorBase(T bits) : bits_(bits) { }
300
301 Iter& operator++() {
302 DCHECK_NE(bits_, 0u);
303 uint32_t bit = *static_cast<Iter&>(*this);
304 bits_ &= ~(static_cast<T>(1u) << bit);
305 return static_cast<Iter&>(*this);
306 }
307
308 Iter& operator++(int) {
309 Iter tmp(static_cast<Iter&>(*this));
310 ++*this;
311 return tmp;
312 }
313
314 protected:
315 T bits_;
316
317 template <typename U, typename I>
318 friend bool operator==(const BitIteratorBase<U, I>& lhs, const BitIteratorBase<U, I>& rhs);
319};
320
321template <typename T, typename Iter>
322bool operator==(const BitIteratorBase<T, Iter>& lhs, const BitIteratorBase<T, Iter>& rhs) {
323 return lhs.bits_ == rhs.bits_;
324}
325
326template <typename T, typename Iter>
327bool operator!=(const BitIteratorBase<T, Iter>& lhs, const BitIteratorBase<T, Iter>& rhs) {
328 return !(lhs == rhs);
329}
330
331template <typename T>
332class LowToHighBitIterator : public BitIteratorBase<T, LowToHighBitIterator<T>> {
333 public:
334 using BitIteratorBase<T, LowToHighBitIterator<T>>::BitIteratorBase;
335
336 uint32_t operator*() const {
337 DCHECK_NE(this->bits_, 0u);
338 return CTZ(this->bits_);
339 }
340};
341
342template <typename T>
343class HighToLowBitIterator : public BitIteratorBase<T, HighToLowBitIterator<T>> {
344 public:
345 using BitIteratorBase<T, HighToLowBitIterator<T>>::BitIteratorBase;
346
347 uint32_t operator*() const {
348 DCHECK_NE(this->bits_, 0u);
349 static_assert(std::numeric_limits<T>::radix == 2, "Unexpected radix!");
350 return std::numeric_limits<T>::digits - 1u - CLZ(this->bits_);
351 }
352};
353
354template <typename T>
355IterationRange<LowToHighBitIterator<T>> LowToHighBits(T bits) {
356 return IterationRange<LowToHighBitIterator<T>>(
357 LowToHighBitIterator<T>(bits), LowToHighBitIterator<T>());
358}
359
360template <typename T>
361IterationRange<HighToLowBitIterator<T>> HighToLowBits(T bits) {
362 return IterationRange<HighToLowBitIterator<T>>(
363 HighToLowBitIterator<T>(bits), HighToLowBitIterator<T>());
364}
365
366} // namespace art
367
368#endif // ART_RUNTIME_BASE_BIT_UTILS_H_