LLVM  10.0.0svn
MathExtras.h
Go to the documentation of this file.
1 //===-- llvm/Support/MathExtras.h - Useful math functions -------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains some functions that are useful for math stuff.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_SUPPORT_MATHEXTRAS_H
14 #define LLVM_SUPPORT_MATHEXTRAS_H
15 
16 #include "llvm/Support/Compiler.h"
18 #include <algorithm>
19 #include <cassert>
20 #include <climits>
21 #include <cstring>
22 #include <limits>
23 #include <type_traits>
24 
25 #ifdef __ANDROID_NDK__
26 #include <android/api-level.h>
27 #endif
28 
29 #ifdef _MSC_VER
30 // Declare these intrinsics manually rather including intrin.h. It's very
31 // expensive, and MathExtras.h is popular.
32 // #include <intrin.h>
33 extern "C" {
34 unsigned char _BitScanForward(unsigned long *_Index, unsigned long _Mask);
35 unsigned char _BitScanForward64(unsigned long *_Index, unsigned __int64 _Mask);
36 unsigned char _BitScanReverse(unsigned long *_Index, unsigned long _Mask);
37 unsigned char _BitScanReverse64(unsigned long *_Index, unsigned __int64 _Mask);
38 }
39 #endif
40 
41 namespace llvm {
42 /// The behavior an operation has on an input of 0.
44  /// The returned value is undefined.
46  /// The returned value is numeric_limits<T>::max()
48  /// The returned value is numeric_limits<T>::digits
50 };
51 
52 namespace detail {
53 template <typename T, std::size_t SizeOfT> struct TrailingZerosCounter {
54  static unsigned count(T Val, ZeroBehavior) {
55  if (!Val)
56  return std::numeric_limits<T>::digits;
57  if (Val & 0x1)
58  return 0;
59 
60  // Bisection method.
61  unsigned ZeroBits = 0;
62  T Shift = std::numeric_limits<T>::digits >> 1;
63  T Mask = std::numeric_limits<T>::max() >> Shift;
64  while (Shift) {
65  if ((Val & Mask) == 0) {
66  Val >>= Shift;
67  ZeroBits |= Shift;
68  }
69  Shift >>= 1;
70  Mask >>= Shift;
71  }
72  return ZeroBits;
73  }
74 };
75 
76 #if defined(__GNUC__) || defined(_MSC_VER)
77 template <typename T> struct TrailingZerosCounter<T, 4> {
78  static unsigned count(T Val, ZeroBehavior ZB) {
79  if (ZB != ZB_Undefined && Val == 0)
80  return 32;
81 
82 #if __has_builtin(__builtin_ctz) || defined(__GNUC__)
83  return __builtin_ctz(Val);
84 #elif defined(_MSC_VER)
85  unsigned long Index;
86  _BitScanForward(&Index, Val);
87  return Index;
88 #endif
89  }
90 };
91 
92 #if !defined(_MSC_VER) || defined(_M_X64)
93 template <typename T> struct TrailingZerosCounter<T, 8> {
94  static unsigned count(T Val, ZeroBehavior ZB) {
95  if (ZB != ZB_Undefined && Val == 0)
96  return 64;
97 
98 #if __has_builtin(__builtin_ctzll) || defined(__GNUC__)
99  return __builtin_ctzll(Val);
100 #elif defined(_MSC_VER)
101  unsigned long Index;
102  _BitScanForward64(&Index, Val);
103  return Index;
104 #endif
105  }
106 };
107 #endif
108 #endif
109 } // namespace detail
110 
111 /// Count number of 0's from the least significant bit to the most
112 /// stopping at the first 1.
113 ///
114 /// Only unsigned integral types are allowed.
115 ///
116 /// \param ZB the behavior on an input of 0. Only ZB_Width and ZB_Undefined are
117 /// valid arguments.
118 template <typename T>
120  static_assert(std::numeric_limits<T>::is_integer &&
121  !std::numeric_limits<T>::is_signed,
122  "Only unsigned integral types are allowed.");
124 }
125 
126 namespace detail {
127 template <typename T, std::size_t SizeOfT> struct LeadingZerosCounter {
128  static unsigned count(T Val, ZeroBehavior) {
129  if (!Val)
130  return std::numeric_limits<T>::digits;
131 
132  // Bisection method.
133  unsigned ZeroBits = 0;
134  for (T Shift = std::numeric_limits<T>::digits >> 1; Shift; Shift >>= 1) {
135  T Tmp = Val >> Shift;
136  if (Tmp)
137  Val = Tmp;
138  else
139  ZeroBits |= Shift;
140  }
141  return ZeroBits;
142  }
143 };
144 
145 #if defined(__GNUC__) || defined(_MSC_VER)
146 template <typename T> struct LeadingZerosCounter<T, 4> {
147  static unsigned count(T Val, ZeroBehavior ZB) {
148  if (ZB != ZB_Undefined && Val == 0)
149  return 32;
150 
151 #if __has_builtin(__builtin_clz) || defined(__GNUC__)
152  return __builtin_clz(Val);
153 #elif defined(_MSC_VER)
154  unsigned long Index;
155  _BitScanReverse(&Index, Val);
156  return Index ^ 31;
157 #endif
158  }
159 };
160 
161 #if !defined(_MSC_VER) || defined(_M_X64)
162 template <typename T> struct LeadingZerosCounter<T, 8> {
163  static unsigned count(T Val, ZeroBehavior ZB) {
164  if (ZB != ZB_Undefined && Val == 0)
165  return 64;
166 
167 #if __has_builtin(__builtin_clzll) || defined(__GNUC__)
168  return __builtin_clzll(Val);
169 #elif defined(_MSC_VER)
170  unsigned long Index;
171  _BitScanReverse64(&Index, Val);
172  return Index ^ 63;
173 #endif
174  }
175 };
176 #endif
177 #endif
178 } // namespace detail
179 
180 /// Count number of 0's from the most significant bit to the least
181 /// stopping at the first 1.
182 ///
183 /// Only unsigned integral types are allowed.
184 ///
185 /// \param ZB the behavior on an input of 0. Only ZB_Width and ZB_Undefined are
186 /// valid arguments.
187 template <typename T>
189  static_assert(std::numeric_limits<T>::is_integer &&
190  !std::numeric_limits<T>::is_signed,
191  "Only unsigned integral types are allowed.");
193 }
194 
195 /// Get the index of the first set bit starting from the least
196 /// significant bit.
197 ///
198 /// Only unsigned integral types are allowed.
199 ///
200 /// \param ZB the behavior on an input of 0. Only ZB_Max and ZB_Undefined are
201 /// valid arguments.
202 template <typename T> T findFirstSet(T Val, ZeroBehavior ZB = ZB_Max) {
203  if (ZB == ZB_Max && Val == 0)
205 
206  return countTrailingZeros(Val, ZB_Undefined);
207 }
208 
209 /// Create a bitmask with the N right-most bits set to 1, and all other
210 /// bits set to 0. Only unsigned types are allowed.
211 template <typename T> T maskTrailingOnes(unsigned N) {
212  static_assert(std::is_unsigned<T>::value, "Invalid type!");
213  const unsigned Bits = CHAR_BIT * sizeof(T);
214  assert(N <= Bits && "Invalid bit index");
215  return N == 0 ? 0 : (T(-1) >> (Bits - N));
216 }
217 
218 /// Create a bitmask with the N left-most bits set to 1, and all other
219 /// bits set to 0. Only unsigned types are allowed.
220 template <typename T> T maskLeadingOnes(unsigned N) {
221  return ~maskTrailingOnes<T>(CHAR_BIT * sizeof(T) - N);
222 }
223 
224 /// Create a bitmask with the N right-most bits set to 0, and all other
225 /// bits set to 1. Only unsigned types are allowed.
226 template <typename T> T maskTrailingZeros(unsigned N) {
227  return maskLeadingOnes<T>(CHAR_BIT * sizeof(T) - N);
228 }
229 
230 /// Create a bitmask with the N left-most bits set to 0, and all other
231 /// bits set to 1. Only unsigned types are allowed.
232 template <typename T> T maskLeadingZeros(unsigned N) {
233  return maskTrailingOnes<T>(CHAR_BIT * sizeof(T) - N);
234 }
235 
236 /// Get the index of the last set bit starting from the least
237 /// significant bit.
238 ///
239 /// Only unsigned integral types are allowed.
240 ///
241 /// \param ZB the behavior on an input of 0. Only ZB_Max and ZB_Undefined are
242 /// valid arguments.
243 template <typename T> T findLastSet(T Val, ZeroBehavior ZB = ZB_Max) {
244  if (ZB == ZB_Max && Val == 0)
246 
247  // Use ^ instead of - because both gcc and llvm can remove the associated ^
248  // in the __builtin_clz intrinsic on x86.
249  return countLeadingZeros(Val, ZB_Undefined) ^
250  (std::numeric_limits<T>::digits - 1);
251 }
252 
253 /// Macro compressed bit reversal table for 256 bits.
254 ///
255 /// http://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable
256 static const unsigned char BitReverseTable256[256] = {
257 #define R2(n) n, n + 2 * 64, n + 1 * 64, n + 3 * 64
258 #define R4(n) R2(n), R2(n + 2 * 16), R2(n + 1 * 16), R2(n + 3 * 16)
259 #define R6(n) R4(n), R4(n + 2 * 4), R4(n + 1 * 4), R4(n + 3 * 4)
260  R6(0), R6(2), R6(1), R6(3)
261 #undef R2
262 #undef R4
263 #undef R6
264 };
265 
266 /// Reverse the bits in \p Val.
267 template <typename T>
269  unsigned char in[sizeof(Val)];
270  unsigned char out[sizeof(Val)];
271  std::memcpy(in, &Val, sizeof(Val));
272  for (unsigned i = 0; i < sizeof(Val); ++i)
273  out[(sizeof(Val) - i) - 1] = BitReverseTable256[in[i]];
274  std::memcpy(&Val, out, sizeof(Val));
275  return Val;
276 }
277 
278 // NOTE: The following support functions use the _32/_64 extensions instead of
279 // type overloading so that signed and unsigned integers can be used without
280 // ambiguity.
281 
282 /// Return the high 32 bits of a 64 bit value.
283 constexpr inline uint32_t Hi_32(uint64_t Value) {
284  return static_cast<uint32_t>(Value >> 32);
285 }
286 
287 /// Return the low 32 bits of a 64 bit value.
288 constexpr inline uint32_t Lo_32(uint64_t Value) {
289  return static_cast<uint32_t>(Value);
290 }
291 
292 /// Make a 64-bit integer from a high / low pair of 32-bit integers.
293 constexpr inline uint64_t Make_64(uint32_t High, uint32_t Low) {
294  return ((uint64_t)High << 32) | (uint64_t)Low;
295 }
296 
297 /// Checks if an integer fits into the given bit width.
298 template <unsigned N> constexpr inline bool isInt(int64_t x) {
299  return N >= 64 || (-(INT64_C(1)<<(N-1)) <= x && x < (INT64_C(1)<<(N-1)));
300 }
301 // Template specializations to get better code for common cases.
302 template <> constexpr inline bool isInt<8>(int64_t x) {
303  return static_cast<int8_t>(x) == x;
304 }
305 template <> constexpr inline bool isInt<16>(int64_t x) {
306  return static_cast<int16_t>(x) == x;
307 }
308 template <> constexpr inline bool isInt<32>(int64_t x) {
309  return static_cast<int32_t>(x) == x;
310 }
311 
312 /// Checks if a signed integer is an N bit number shifted left by S.
313 template <unsigned N, unsigned S>
314 constexpr inline bool isShiftedInt(int64_t x) {
315  static_assert(
316  N > 0, "isShiftedInt<0> doesn't make sense (refers to a 0-bit number.");
317  static_assert(N + S <= 64, "isShiftedInt<N, S> with N + S > 64 is too wide.");
318  return isInt<N + S>(x) && (x % (UINT64_C(1) << S) == 0);
319 }
320 
321 /// Checks if an unsigned integer fits into the given bit width.
322 ///
323 /// This is written as two functions rather than as simply
324 ///
325 /// return N >= 64 || X < (UINT64_C(1) << N);
326 ///
327 /// to keep MSVC from (incorrectly) warning on isUInt<64> that we're shifting
328 /// left too many places.
329 template <unsigned N>
330 constexpr inline typename std::enable_if<(N < 64), bool>::type
331 isUInt(uint64_t X) {
332  static_assert(N > 0, "isUInt<0> doesn't make sense");
333  return X < (UINT64_C(1) << (N));
334 }
335 template <unsigned N>
336 constexpr inline typename std::enable_if<N >= 64, bool>::type
337 isUInt(uint64_t X) {
338  return true;
339 }
340 
341 // Template specializations to get better code for common cases.
342 template <> constexpr inline bool isUInt<8>(uint64_t x) {
343  return static_cast<uint8_t>(x) == x;
344 }
345 template <> constexpr inline bool isUInt<16>(uint64_t x) {
346  return static_cast<uint16_t>(x) == x;
347 }
348 template <> constexpr inline bool isUInt<32>(uint64_t x) {
349  return static_cast<uint32_t>(x) == x;
350 }
351 
352 /// Checks if a unsigned integer is an N bit number shifted left by S.
353 template <unsigned N, unsigned S>
354 constexpr inline bool isShiftedUInt(uint64_t x) {
355  static_assert(
356  N > 0, "isShiftedUInt<0> doesn't make sense (refers to a 0-bit number)");
357  static_assert(N + S <= 64,
358  "isShiftedUInt<N, S> with N + S > 64 is too wide.");
359  // Per the two static_asserts above, S must be strictly less than 64. So
360  // 1 << S is not undefined behavior.
361  return isUInt<N + S>(x) && (x % (UINT64_C(1) << S) == 0);
362 }
363 
364 /// Gets the maximum value for a N-bit unsigned integer.
365 inline uint64_t maxUIntN(uint64_t N) {
366  assert(N > 0 && N <= 64 && "integer width out of range");
367 
368  // uint64_t(1) << 64 is undefined behavior, so we can't do
369  // (uint64_t(1) << N) - 1
370  // without checking first that N != 64. But this works and doesn't have a
371  // branch.
372  return UINT64_MAX >> (64 - N);
373 }
374 
375 /// Gets the minimum value for a N-bit signed integer.
376 inline int64_t minIntN(int64_t N) {
377  assert(N > 0 && N <= 64 && "integer width out of range");
378 
379  return -(UINT64_C(1)<<(N-1));
380 }
381 
382 /// Gets the maximum value for a N-bit signed integer.
383 inline int64_t maxIntN(int64_t N) {
384  assert(N > 0 && N <= 64 && "integer width out of range");
385 
386  // This relies on two's complement wraparound when N == 64, so we convert to
387  // int64_t only at the very end to avoid UB.
388  return (UINT64_C(1) << (N - 1)) - 1;
389 }
390 
391 /// Checks if an unsigned integer fits into the given (dynamic) bit width.
392 inline bool isUIntN(unsigned N, uint64_t x) {
393  return N >= 64 || x <= maxUIntN(N);
394 }
395 
396 /// Checks if an signed integer fits into the given (dynamic) bit width.
397 inline bool isIntN(unsigned N, int64_t x) {
398  return N >= 64 || (minIntN(N) <= x && x <= maxIntN(N));
399 }
400 
401 /// Return true if the argument is a non-empty sequence of ones starting at the
402 /// least significant bit with the remainder zero (32 bit version).
403 /// Ex. isMask_32(0x0000FFFFU) == true.
404 constexpr inline bool isMask_32(uint32_t Value) {
405  return Value && ((Value + 1) & Value) == 0;
406 }
407 
408 /// Return true if the argument is a non-empty sequence of ones starting at the
409 /// least significant bit with the remainder zero (64 bit version).
410 constexpr inline bool isMask_64(uint64_t Value) {
411  return Value && ((Value + 1) & Value) == 0;
412 }
413 
414 /// Return true if the argument contains a non-empty sequence of ones with the
415 /// remainder zero (32 bit version.) Ex. isShiftedMask_32(0x0000FF00U) == true.
416 constexpr inline bool isShiftedMask_32(uint32_t Value) {
417  return Value && isMask_32((Value - 1) | Value);
418 }
419 
420 /// Return true if the argument contains a non-empty sequence of ones with the
421 /// remainder zero (64 bit version.)
422 constexpr inline bool isShiftedMask_64(uint64_t Value) {
423  return Value && isMask_64((Value - 1) | Value);
424 }
425 
426 /// Return true if the argument is a power of two > 0.
427 /// Ex. isPowerOf2_32(0x00100000U) == true (32 bit edition.)
428 constexpr inline bool isPowerOf2_32(uint32_t Value) {
429  return Value && !(Value & (Value - 1));
430 }
431 
432 /// Return true if the argument is a power of two > 0 (64 bit edition.)
433 constexpr inline bool isPowerOf2_64(uint64_t Value) {
434  return Value && !(Value & (Value - 1));
435 }
436 
437 /// Return a byte-swapped representation of the 16-bit argument.
438 inline uint16_t ByteSwap_16(uint16_t Value) {
439  return sys::SwapByteOrder_16(Value);
440 }
441 
442 /// Return a byte-swapped representation of the 32-bit argument.
444  return sys::SwapByteOrder_32(Value);
445 }
446 
447 /// Return a byte-swapped representation of the 64-bit argument.
448 inline uint64_t ByteSwap_64(uint64_t Value) {
449  return sys::SwapByteOrder_64(Value);
450 }
451 
452 /// Count the number of ones from the most significant bit to the first
453 /// zero bit.
454 ///
455 /// Ex. countLeadingOnes(0xFF0FFF00) == 8.
456 /// Only unsigned integral types are allowed.
457 ///
458 /// \param ZB the behavior on an input of all ones. Only ZB_Width and
459 /// ZB_Undefined are valid arguments.
460 template <typename T>
462  static_assert(std::numeric_limits<T>::is_integer &&
463  !std::numeric_limits<T>::is_signed,
464  "Only unsigned integral types are allowed.");
465  return countLeadingZeros<T>(~Value, ZB);
466 }
467 
468 /// Count the number of ones from the least significant bit to the first
469 /// zero bit.
470 ///
471 /// Ex. countTrailingOnes(0x00FF00FF) == 8.
472 /// Only unsigned integral types are allowed.
473 ///
474 /// \param ZB the behavior on an input of all ones. Only ZB_Width and
475 /// ZB_Undefined are valid arguments.
476 template <typename T>
478  static_assert(std::numeric_limits<T>::is_integer &&
479  !std::numeric_limits<T>::is_signed,
480  "Only unsigned integral types are allowed.");
481  return countTrailingZeros<T>(~Value, ZB);
482 }
483 
484 namespace detail {
485 template <typename T, std::size_t SizeOfT> struct PopulationCounter {
486  static unsigned count(T Value) {
487  // Generic version, forward to 32 bits.
488  static_assert(SizeOfT <= 4, "Not implemented!");
489 #if defined(__GNUC__)
490  return __builtin_popcount(Value);
491 #else
492  uint32_t v = Value;
493  v = v - ((v >> 1) & 0x55555555);
494  v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
495  return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
496 #endif
497  }
498 };
499 
500 template <typename T> struct PopulationCounter<T, 8> {
501  static unsigned count(T Value) {
502 #if defined(__GNUC__)
503  return __builtin_popcountll(Value);
504 #else
505  uint64_t v = Value;
506  v = v - ((v >> 1) & 0x5555555555555555ULL);
507  v = (v & 0x3333333333333333ULL) + ((v >> 2) & 0x3333333333333333ULL);
508  v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0FULL;
509  return unsigned((uint64_t)(v * 0x0101010101010101ULL) >> 56);
510 #endif
511  }
512 };
513 } // namespace detail
514 
515 /// Count the number of set bits in a value.
516 /// Ex. countPopulation(0xF000F000) = 8
517 /// Returns 0 if the word is zero.
518 template <typename T>
519 inline unsigned countPopulation(T Value) {
520  static_assert(std::numeric_limits<T>::is_integer &&
521  !std::numeric_limits<T>::is_signed,
522  "Only unsigned integral types are allowed.");
524 }
525 
526 /// Return the log base 2 of the specified value.
527 inline double Log2(double Value) {
528 #if defined(__ANDROID_API__) && __ANDROID_API__ < 18
529  return __builtin_log(Value) / __builtin_log(2.0);
530 #else
531  return log2(Value);
532 #endif
533 }
534 
535 /// Return the floor log base 2 of the specified value, -1 if the value is zero.
536 /// (32 bit edition.)
537 /// Ex. Log2_32(32) == 5, Log2_32(1) == 0, Log2_32(0) == -1, Log2_32(6) == 2
538 inline unsigned Log2_32(uint32_t Value) {
539  return 31 - countLeadingZeros(Value);
540 }
541 
542 /// Return the floor log base 2 of the specified value, -1 if the value is zero.
543 /// (64 bit edition.)
544 inline unsigned Log2_64(uint64_t Value) {
545  return 63 - countLeadingZeros(Value);
546 }
547 
548 /// Return the ceil log base 2 of the specified value, 32 if the value is zero.
549 /// (32 bit edition).
550 /// Ex. Log2_32_Ceil(32) == 5, Log2_32_Ceil(1) == 0, Log2_32_Ceil(6) == 3
551 inline unsigned Log2_32_Ceil(uint32_t Value) {
552  return 32 - countLeadingZeros(Value - 1);
553 }
554 
555 /// Return the ceil log base 2 of the specified value, 64 if the value is zero.
556 /// (64 bit edition.)
557 inline unsigned Log2_64_Ceil(uint64_t Value) {
558  return 64 - countLeadingZeros(Value - 1);
559 }
560 
561 /// Return the greatest common divisor of the values using Euclid's algorithm.
562 template <typename T>
564  while (B) {
565  T Tmp = B;
566  B = A % B;
567  A = Tmp;
568  }
569  return A;
570 }
571 
572 inline uint64_t GreatestCommonDivisor64(uint64_t A, uint64_t B) {
573  return greatestCommonDivisor<uint64_t>(A, B);
574 }
575 
576 /// This function takes a 64-bit integer and returns the bit equivalent double.
577 inline double BitsToDouble(uint64_t Bits) {
578  double D;
579  static_assert(sizeof(uint64_t) == sizeof(double), "Unexpected type sizes");
580  memcpy(&D, &Bits, sizeof(Bits));
581  return D;
582 }
583 
584 /// This function takes a 32-bit integer and returns the bit equivalent float.
585 inline float BitsToFloat(uint32_t Bits) {
586  float F;
587  static_assert(sizeof(uint32_t) == sizeof(float), "Unexpected type sizes");
588  memcpy(&F, &Bits, sizeof(Bits));
589  return F;
590 }
591 
592 /// This function takes a double and returns the bit equivalent 64-bit integer.
593 /// Note that copying doubles around changes the bits of NaNs on some hosts,
594 /// notably x86, so this routine cannot be used if these bits are needed.
595 inline uint64_t DoubleToBits(double Double) {
596  uint64_t Bits;
597  static_assert(sizeof(uint64_t) == sizeof(double), "Unexpected type sizes");
598  memcpy(&Bits, &Double, sizeof(Double));
599  return Bits;
600 }
601 
602 /// This function takes a float and returns the bit equivalent 32-bit integer.
603 /// Note that copying floats around changes the bits of NaNs on some hosts,
604 /// notably x86, so this routine cannot be used if these bits are needed.
605 inline uint32_t FloatToBits(float Float) {
606  uint32_t Bits;
607  static_assert(sizeof(uint32_t) == sizeof(float), "Unexpected type sizes");
608  memcpy(&Bits, &Float, sizeof(Float));
609  return Bits;
610 }
611 
612 /// A and B are either alignments or offsets. Return the minimum alignment that
613 /// may be assumed after adding the two together.
614 constexpr inline uint64_t MinAlign(uint64_t A, uint64_t B) {
615  // The largest power of 2 that divides both A and B.
616  //
617  // Replace "-Value" by "1+~Value" in the following commented code to avoid
618  // MSVC warning C4146
619  // return (A | B) & -(A | B);
620  return (A | B) & (1 + ~(A | B));
621 }
622 
623 /// Aligns \c Addr to \c Alignment bytes, rounding up.
624 ///
625 /// Alignment should be a power of two. This method rounds up, so
626 /// alignAddr(7, 4) == 8 and alignAddr(8, 4) == 8.
627 inline uintptr_t alignAddr(const void *Addr, size_t Alignment) {
628  assert(Alignment && isPowerOf2_64((uint64_t)Alignment) &&
629  "Alignment is not a power of two!");
630 
631  assert((uintptr_t)Addr + Alignment - 1 >= (uintptr_t)Addr);
632 
633  return (((uintptr_t)Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1));
634 }
635 
636 /// Returns the necessary adjustment for aligning \c Ptr to \c Alignment
637 /// bytes, rounding up.
638 inline size_t alignmentAdjustment(const void *Ptr, size_t Alignment) {
639  return alignAddr(Ptr, Alignment) - (uintptr_t)Ptr;
640 }
641 
642 /// Returns the next power of two (in 64-bits) that is strictly greater than A.
643 /// Returns zero on overflow.
644 inline uint64_t NextPowerOf2(uint64_t A) {
645  A |= (A >> 1);
646  A |= (A >> 2);
647  A |= (A >> 4);
648  A |= (A >> 8);
649  A |= (A >> 16);
650  A |= (A >> 32);
651  return A + 1;
652 }
653 
654 /// Returns the power of two which is less than or equal to the given value.
655 /// Essentially, it is a floor operation across the domain of powers of two.
656 inline uint64_t PowerOf2Floor(uint64_t A) {
657  if (!A) return 0;
658  return 1ull << (63 - countLeadingZeros(A, ZB_Undefined));
659 }
660 
661 /// Returns the power of two which is greater than or equal to the given value.
662 /// Essentially, it is a ceil operation across the domain of powers of two.
663 inline uint64_t PowerOf2Ceil(uint64_t A) {
664  if (!A)
665  return 0;
666  return NextPowerOf2(A - 1);
667 }
668 
669 /// Returns the next integer (mod 2**64) that is greater than or equal to
670 /// \p Value and is a multiple of \p Align. \p Align must be non-zero.
671 ///
672 /// If non-zero \p Skew is specified, the return value will be a minimal
673 /// integer that is greater than or equal to \p Value and equal to
674 /// \p Align * N + \p Skew for some integer N. If \p Skew is larger than
675 /// \p Align, its value is adjusted to '\p Skew mod \p Align'.
676 ///
677 /// Examples:
678 /// \code
679 /// alignTo(5, 8) = 8
680 /// alignTo(17, 8) = 24
681 /// alignTo(~0LL, 8) = 0
682 /// alignTo(321, 255) = 510
683 ///
684 /// alignTo(5, 8, 7) = 7
685 /// alignTo(17, 8, 1) = 17
686 /// alignTo(~0LL, 8, 3) = 3
687 /// alignTo(321, 255, 42) = 552
688 /// \endcode
689 inline uint64_t alignTo(uint64_t Value, uint64_t Align, uint64_t Skew = 0) {
690  assert(Align != 0u && "Align can't be 0.");
691  Skew %= Align;
692  return (Value + Align - 1 - Skew) / Align * Align + Skew;
693 }
694 
695 /// Returns the next integer (mod 2**64) that is greater than or equal to
696 /// \p Value and is a multiple of \c Align. \c Align must be non-zero.
697 template <uint64_t Align> constexpr inline uint64_t alignTo(uint64_t Value) {
698  static_assert(Align != 0u, "Align must be non-zero");
699  return (Value + Align - 1) / Align * Align;
700 }
701 
702 /// Returns the integer ceil(Numerator / Denominator).
703 inline uint64_t divideCeil(uint64_t Numerator, uint64_t Denominator) {
704  return alignTo(Numerator, Denominator) / Denominator;
705 }
706 
707 /// Returns the largest uint64_t less than or equal to \p Value and is
708 /// \p Skew mod \p Align. \p Align must be non-zero
709 inline uint64_t alignDown(uint64_t Value, uint64_t Align, uint64_t Skew = 0) {
710  assert(Align != 0u && "Align can't be 0.");
711  Skew %= Align;
712  return (Value - Skew) / Align * Align + Skew;
713 }
714 
715 /// Sign-extend the number in the bottom B bits of X to a 32-bit integer.
716 /// Requires 0 < B <= 32.
717 template <unsigned B> constexpr inline int32_t SignExtend32(uint32_t X) {
718  static_assert(B > 0, "Bit width can't be 0.");
719  static_assert(B <= 32, "Bit width out of range.");
720  return int32_t(X << (32 - B)) >> (32 - B);
721 }
722 
723 /// Sign-extend the number in the bottom B bits of X to a 32-bit integer.
724 /// Requires 0 < B < 32.
725 inline int32_t SignExtend32(uint32_t X, unsigned B) {
726  assert(B > 0 && "Bit width can't be 0.");
727  assert(B <= 32 && "Bit width out of range.");
728  return int32_t(X << (32 - B)) >> (32 - B);
729 }
730 
731 /// Sign-extend the number in the bottom B bits of X to a 64-bit integer.
732 /// Requires 0 < B < 64.
733 template <unsigned B> constexpr inline int64_t SignExtend64(uint64_t x) {
734  static_assert(B > 0, "Bit width can't be 0.");
735  static_assert(B <= 64, "Bit width out of range.");
736  return int64_t(x << (64 - B)) >> (64 - B);
737 }
738 
739 /// Sign-extend the number in the bottom B bits of X to a 64-bit integer.
740 /// Requires 0 < B < 64.
741 inline int64_t SignExtend64(uint64_t X, unsigned B) {
742  assert(B > 0 && "Bit width can't be 0.");
743  assert(B <= 64 && "Bit width out of range.");
744  return int64_t(X << (64 - B)) >> (64 - B);
745 }
746 
747 /// Subtract two unsigned integers, X and Y, of type T and return the absolute
748 /// value of the result.
749 template <typename T>
750 typename std::enable_if<std::is_unsigned<T>::value, T>::type
752  return std::max(X, Y) - std::min(X, Y);
753 }
754 
755 /// Add two unsigned integers, X and Y, of type T. Clamp the result to the
756 /// maximum representable value of T on overflow. ResultOverflowed indicates if
757 /// the result is larger than the maximum representable value of type T.
758 template <typename T>
759 typename std::enable_if<std::is_unsigned<T>::value, T>::type
760 SaturatingAdd(T X, T Y, bool *ResultOverflowed = nullptr) {
761  bool Dummy;
762  bool &Overflowed = ResultOverflowed ? *ResultOverflowed : Dummy;
763  // Hacker's Delight, p. 29
764  T Z = X + Y;
765  Overflowed = (Z < X || Z < Y);
766  if (Overflowed)
768  else
769  return Z;
770 }
771 
772 /// Multiply two unsigned integers, X and Y, of type T. Clamp the result to the
773 /// maximum representable value of T on overflow. ResultOverflowed indicates if
774 /// the result is larger than the maximum representable value of type T.
775 template <typename T>
776 typename std::enable_if<std::is_unsigned<T>::value, T>::type
777 SaturatingMultiply(T X, T Y, bool *ResultOverflowed = nullptr) {
778  bool Dummy;
779  bool &Overflowed = ResultOverflowed ? *ResultOverflowed : Dummy;
780 
781  // Hacker's Delight, p. 30 has a different algorithm, but we don't use that
782  // because it fails for uint16_t (where multiplication can have undefined
783  // behavior due to promotion to int), and requires a division in addition
784  // to the multiplication.
785 
786  Overflowed = false;
787 
788  // Log2(Z) would be either Log2Z or Log2Z + 1.
789  // Special case: if X or Y is 0, Log2_64 gives -1, and Log2Z
790  // will necessarily be less than Log2Max as desired.
791  int Log2Z = Log2_64(X) + Log2_64(Y);
792  const T Max = std::numeric_limits<T>::max();
793  int Log2Max = Log2_64(Max);
794  if (Log2Z < Log2Max) {
795  return X * Y;
796  }
797  if (Log2Z > Log2Max) {
798  Overflowed = true;
799  return Max;
800  }
801 
802  // We're going to use the top bit, and maybe overflow one
803  // bit past it. Multiply all but the bottom bit then add
804  // that on at the end.
805  T Z = (X >> 1) * Y;
806  if (Z & ~(Max >> 1)) {
807  Overflowed = true;
808  return Max;
809  }
810  Z <<= 1;
811  if (X & 1)
812  return SaturatingAdd(Z, Y, ResultOverflowed);
813 
814  return Z;
815 }
816 
817 /// Multiply two unsigned integers, X and Y, and add the unsigned integer, A to
818 /// the product. Clamp the result to the maximum representable value of T on
819 /// overflow. ResultOverflowed indicates if the result is larger than the
820 /// maximum representable value of type T.
821 template <typename T>
822 typename std::enable_if<std::is_unsigned<T>::value, T>::type
823 SaturatingMultiplyAdd(T X, T Y, T A, bool *ResultOverflowed = nullptr) {
824  bool Dummy;
825  bool &Overflowed = ResultOverflowed ? *ResultOverflowed : Dummy;
826 
827  T Product = SaturatingMultiply(X, Y, &Overflowed);
828  if (Overflowed)
829  return Product;
830 
831  return SaturatingAdd(A, Product, &Overflowed);
832 }
833 
834 /// Use this rather than HUGE_VALF; the latter causes warnings on MSVC.
835 extern const float huge_valf;
836 
837 
838 /// Add two signed integers, computing the two's complement truncated result,
839 /// returning true if overflow occured.
840 template <typename T>
841 typename std::enable_if<std::is_signed<T>::value, T>::type
842 AddOverflow(T X, T Y, T &Result) {
843 #if __has_builtin(__builtin_add_overflow)
844  return __builtin_add_overflow(X, Y, &Result);
845 #else
846  // Perform the unsigned addition.
847  using U = typename std::make_unsigned<T>::type;
848  const U UX = static_cast<U>(X);
849  const U UY = static_cast<U>(Y);
850  const U UResult = UX + UY;
851 
852  // Convert to signed.
853  Result = static_cast<T>(UResult);
854 
855  // Adding two positive numbers should result in a positive number.
856  if (X > 0 && Y > 0)
857  return Result <= 0;
858  // Adding two negatives should result in a negative number.
859  if (X < 0 && Y < 0)
860  return Result >= 0;
861  return false;
862 #endif
863 }
864 
865 /// Subtract two signed integers, computing the two's complement truncated
866 /// result, returning true if an overflow ocurred.
867 template <typename T>
868 typename std::enable_if<std::is_signed<T>::value, T>::type
869 SubOverflow(T X, T Y, T &Result) {
870 #if __has_builtin(__builtin_sub_overflow)
871  return __builtin_sub_overflow(X, Y, &Result);
872 #else
873  // Perform the unsigned addition.
874  using U = typename std::make_unsigned<T>::type;
875  const U UX = static_cast<U>(X);
876  const U UY = static_cast<U>(Y);
877  const U UResult = UX - UY;
878 
879  // Convert to signed.
880  Result = static_cast<T>(UResult);
881 
882  // Subtracting a positive number from a negative results in a negative number.
883  if (X <= 0 && Y > 0)
884  return Result >= 0;
885  // Subtracting a negative number from a positive results in a positive number.
886  if (X >= 0 && Y < 0)
887  return Result <= 0;
888  return false;
889 #endif
890 }
891 
892 
893 /// Multiply two signed integers, computing the two's complement truncated
894 /// result, returning true if an overflow ocurred.
895 template <typename T>
896 typename std::enable_if<std::is_signed<T>::value, T>::type
897 MulOverflow(T X, T Y, T &Result) {
898  // Perform the unsigned multiplication on absolute values.
899  using U = typename std::make_unsigned<T>::type;
900  const U UX = X < 0 ? (0 - static_cast<U>(X)) : static_cast<U>(X);
901  const U UY = Y < 0 ? (0 - static_cast<U>(Y)) : static_cast<U>(Y);
902  const U UResult = UX * UY;
903 
904  // Convert to signed.
905  const bool IsNegative = (X < 0) ^ (Y < 0);
906  Result = IsNegative ? (0 - UResult) : UResult;
907 
908  // If any of the args was 0, result is 0 and no overflow occurs.
909  if (UX == 0 || UY == 0)
910  return false;
911 
912  // UX and UY are in [1, 2^n], where n is the number of digits.
913  // Check how the max allowed absolute value (2^n for negative, 2^(n-1) for
914  // positive) divided by an argument compares to the other.
915  if (IsNegative)
916  return UX > (static_cast<U>(std::numeric_limits<T>::max()) + U(1)) / UY;
917  else
918  return UX > (static_cast<U>(std::numeric_limits<T>::max())) / UY;
919 }
920 
921 } // End llvm namespace
922 
923 #endif
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition: Alignment.h:150
constexpr bool isUInt< 32 >(uint64_t x)
Definition: MathExtras.h:348
unsigned Log2_32_Ceil(uint32_t Value)
Return the ceil log base 2 of the specified value, 32 if the value is zero.
Definition: MathExtras.h:551
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
T findLastSet(T Val, ZeroBehavior ZB=ZB_Max)
Get the index of the last set bit starting from the least significant bit.
Definition: MathExtras.h:243
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
uint64_t GreatestCommonDivisor64(uint64_t A, uint64_t B)
Definition: MathExtras.h:572
This class represents lattice values for constants.
Definition: AllocatorList.h:23
unsigned countLeadingOnes(T Value, ZeroBehavior ZB=ZB_Width)
Count the number of ones from the most significant bit to the first zero bit.
Definition: MathExtras.h:461
constexpr uint32_t Lo_32(uint64_t Value)
Return the low 32 bits of a 64 bit value.
Definition: MathExtras.h:288
static unsigned count(T Value)
Definition: MathExtras.h:486
float BitsToFloat(uint32_t Bits)
This function takes a 32-bit integer and returns the bit equivalent float.
Definition: MathExtras.h:585
constexpr bool isInt< 8 >(int64_t x)
Definition: MathExtras.h:302
ZeroBehavior
The behavior an operation has on an input of 0.
Definition: MathExtras.h:43
constexpr bool isInt< 16 >(int64_t x)
Definition: MathExtras.h:305
F(f)
std::enable_if< std::is_unsigned< T >::value, T >::type SaturatingMultiply(T X, T Y, bool *ResultOverflowed=nullptr)
Multiply two unsigned integers, X and Y, of type T.
Definition: MathExtras.h:777
uint64_t High
constexpr bool isMask_32(uint32_t Value)
Return true if the argument is a non-empty sequence of ones starting at the least significant bit wit...
Definition: MathExtras.h:404
std::enable_if< std::is_unsigned< T >::value, T >::type SaturatingAdd(T X, T Y, bool *ResultOverflowed=nullptr)
Add two unsigned integers, X and Y, of type T.
Definition: MathExtras.h:760
T maskTrailingOnes(unsigned N)
Create a bitmask with the N right-most bits set to 1, and all other bits set to 0.
Definition: MathExtras.h:211
unsigned countTrailingZeros(T Val, ZeroBehavior ZB=ZB_Width)
Count number of 0&#39;s from the least significant bit to the most stopping at the first 1...
Definition: MathExtras.h:119
static unsigned count(T Val, ZeroBehavior)
Definition: MathExtras.h:54
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
uint64_t alignDown(uint64_t Value, uint64_t Align, uint64_t Skew=0)
Returns the largest uint64_t less than or equal to Value and is Skew mod Align.
Definition: MathExtras.h:709
uint32_t SwapByteOrder_32(uint32_t value)
This function returns a byte-swapped representation of the 32-bit argument.
Definition: SwapByteOrder.h:43
The returned value is numeric_limits<T>::digits.
Definition: MathExtras.h:49
std::enable_if< std::is_signed< T >::value, T >::type MulOverflow(T X, T Y, T &Result)
Multiply two signed integers, computing the two&#39;s complement truncated result, returning true if an o...
Definition: MathExtras.h:897
The returned value is undefined.
Definition: MathExtras.h:45
static unsigned count(T Value)
Definition: MathExtras.h:501
uint32_t ByteSwap_32(uint32_t Value)
Return a byte-swapped representation of the 32-bit argument.
Definition: MathExtras.h:443
int64_t maxIntN(int64_t N)
Gets the maximum value for a N-bit signed integer.
Definition: MathExtras.h:383
static const unsigned char BitReverseTable256[256]
Macro compressed bit reversal table for 256 bits.
Definition: MathExtras.h:256
#define UINT64_MAX
Definition: DataTypes.h:83
constexpr bool isMask_64(uint64_t Value)
Return true if the argument is a non-empty sequence of ones starting at the least significant bit wit...
Definition: MathExtras.h:410
uint16_t ByteSwap_16(uint16_t Value)
Return a byte-swapped representation of the 16-bit argument.
Definition: MathExtras.h:438
constexpr bool isShiftedUInt(uint64_t x)
Checks if a unsigned integer is an N bit number shifted left by S.
Definition: MathExtras.h:354
int64_t minIntN(int64_t N)
Gets the minimum value for a N-bit signed integer.
Definition: MathExtras.h:376
const float huge_valf
Use this rather than HUGE_VALF; the latter causes warnings on MSVC.
Definition: MathExtras.cpp:28
static unsigned count(T Val, ZeroBehavior)
Definition: MathExtras.h:128
T maskTrailingZeros(unsigned N)
Create a bitmask with the N right-most bits set to 0, and all other bits set to 1.
Definition: MathExtras.h:226
uint32_t FloatToBits(float Float)
This function takes a float and returns the bit equivalent 32-bit integer.
Definition: MathExtras.h:605
uint16_t SwapByteOrder_16(uint16_t value)
SwapByteOrder_16 - This function returns a byte-swapped representation of the 16-bit argument...
Definition: SwapByteOrder.h:30
T greatestCommonDivisor(T A, T B)
Return the greatest common divisor of the values using Euclid&#39;s algorithm.
Definition: MathExtras.h:563
constexpr uint64_t MinAlign(uint64_t A, uint64_t B)
A and B are either alignments or offsets.
Definition: MathExtras.h:614
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
constexpr bool isUInt< 8 >(uint64_t x)
Definition: MathExtras.h:342
constexpr bool isShiftedInt(int64_t x)
Checks if a signed integer is an N bit number shifted left by S.
Definition: MathExtras.h:314
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition: MathExtras.h:428
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition: MathExtras.h:298
size_t alignmentAdjustment(const void *Ptr, size_t Alignment)
Returns the necessary adjustment for aligning Ptr to Alignment bytes, rounding up.
Definition: MathExtras.h:638
std::enable_if< std::is_signed< T >::value, T >::type AddOverflow(T X, T Y, T &Result)
Add two signed integers, computing the two&#39;s complement truncated result, returning true if overflow ...
Definition: MathExtras.h:842
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
Definition: MathExtras.h:433
The returned value is numeric_limits<T>::max()
Definition: MathExtras.h:47
static double log2(double V)
bool isIntN(unsigned N, int64_t x)
Checks if an signed integer fits into the given (dynamic) bit width.
Definition: MathExtras.h:397
uint64_t NextPowerOf2(uint64_t A)
Returns the next power of two (in 64-bits) that is strictly greater than A.
Definition: MathExtras.h:644
unsigned countLeadingZeros(T Val, ZeroBehavior ZB=ZB_Width)
Count number of 0&#39;s from the most significant bit to the least stopping at the first 1...
Definition: MathExtras.h:188
T findFirstSet(T Val, ZeroBehavior ZB=ZB_Max)
Get the index of the first set bit starting from the least significant bit.
Definition: MathExtras.h:202
uint64_t SwapByteOrder_64(uint64_t value)
This function returns a byte-swapped representation of the 64-bit argument.
Definition: SwapByteOrder.h:58
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:40
std::enable_if< std::is_unsigned< T >::value, T >::type SaturatingMultiplyAdd(T X, T Y, T A, bool *ResultOverflowed=nullptr)
Multiply two unsigned integers, X and Y, and add the unsigned integer, A to the product.
Definition: MathExtras.h:823
unsigned countPopulation(T Value)
Count the number of set bits in a value.
Definition: MathExtras.h:519
constexpr std::enable_if<(N< 64), bool >::type isUInt(uint64_t X)
Checks if an unsigned integer fits into the given bit width.
Definition: MathExtras.h:331
#define R6(n)
constexpr bool isInt< 32 >(int64_t x)
Definition: MathExtras.h:308
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
uint64_t DoubleToBits(double Double)
This function takes a double and returns the bit equivalent 64-bit integer.
Definition: MathExtras.h:595
double BitsToDouble(uint64_t Bits)
This function takes a 64-bit integer and returns the bit equivalent double.
Definition: MathExtras.h:577
unsigned Log2_64_Ceil(uint64_t Value)
Return the ceil log base 2 of the specified value, 64 if the value is zero.
Definition: MathExtras.h:557
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition: MathExtras.h:538
uintptr_t alignAddr(const void *Addr, size_t Alignment)
Aligns Addr to Alignment bytes, rounding up.
Definition: MathExtras.h:627
constexpr bool isShiftedMask_64(uint64_t Value)
Return true if the argument contains a non-empty sequence of ones with the remainder zero (64 bit ver...
Definition: MathExtras.h:422
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:133
uint64_t maxUIntN(uint64_t N)
Gets the maximum value for a N-bit unsigned integer.
Definition: MathExtras.h:365
T maskLeadingZeros(unsigned N)
Create a bitmask with the N left-most bits set to 0, and all other bits set to 1. ...
Definition: MathExtras.h:232
uint64_t ByteSwap_64(uint64_t Value)
Return a byte-swapped representation of the 64-bit argument.
Definition: MathExtras.h:448
#define N
std::enable_if< std::is_unsigned< T >::value, T >::type AbsoluteDifference(T X, T Y)
Subtract two unsigned integers, X and Y, of type T and return the absolute value of the result...
Definition: MathExtras.h:751
constexpr int32_t SignExtend32(uint32_t X)
Sign-extend the number in the bottom B bits of X to a 32-bit integer.
Definition: MathExtras.h:717
uint64_t divideCeil(uint64_t Numerator, uint64_t Denominator)
Returns the integer ceil(Numerator / Denominator).
Definition: MathExtras.h:703
constexpr bool isUInt< 16 >(uint64_t x)
Definition: MathExtras.h:345
constexpr int64_t SignExtend64(uint64_t x)
Sign-extend the number in the bottom B bits of X to a 64-bit integer.
Definition: MathExtras.h:733
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
uint64_t PowerOf2Floor(uint64_t A)
Returns the power of two which is less than or equal to the given value.
Definition: MathExtras.h:656
constexpr uint32_t Hi_32(uint64_t Value)
Return the high 32 bits of a 64 bit value.
Definition: MathExtras.h:283
LLVM Value Representation.
Definition: Value.h:73
unsigned countTrailingOnes(T Value, ZeroBehavior ZB=ZB_Width)
Count the number of ones from the least significant bit to the first zero bit.
Definition: MathExtras.h:477
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E&#39;s largest value.
Definition: BitmaskEnum.h:80
constexpr uint64_t Make_64(uint32_t High, uint32_t Low)
Make a 64-bit integer from a high / low pair of 32-bit integers.
Definition: MathExtras.h:293
std::enable_if< std::is_signed< T >::value, T >::type SubOverflow(T X, T Y, T &Result)
Subtract two signed integers, computing the two&#39;s complement truncated result, returning true if an o...
Definition: MathExtras.h:869
constexpr bool isShiftedMask_32(uint32_t Value)
Return true if the argument contains a non-empty sequence of ones with the remainder zero (32 bit ver...
Definition: MathExtras.h:416
bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
Definition: MathExtras.h:392
T reverseBits(T Val)
Reverse the bits in Val.
Definition: MathExtras.h:268
unsigned Log2_64(uint64_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition: MathExtras.h:544
uint64_t PowerOf2Ceil(uint64_t A)
Returns the power of two which is greater than or equal to the given value.
Definition: MathExtras.h:663
T maskLeadingOnes(unsigned N)
Create a bitmask with the N left-most bits set to 1, and all other bits set to 0. ...
Definition: MathExtras.h:220