WPILibC++  2019.1.1-beta-2-17-g6f0c185
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
MathExtras.h
1 //===-- llvm/Support/MathExtras.h - Useful math functions -------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains some functions that are useful for math stuff.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef WPIUTIL_WPI_MATHEXTRAS_H
15 #define WPIUTIL_WPI_MATHEXTRAS_H
16 
17 #include "wpi/Compiler.h"
18 #include <cstdint>
19 #include <algorithm>
20 #include <cassert>
21 #include <climits>
22 #include <cmath>
23 #include <cstring>
24 #include <limits>
25 #include <type_traits>
26 
27 #ifdef _MSC_VER
28 #include <intrin.h>
29 #endif
30 
31 namespace wpi {
40 };
41 
42 namespace detail {
43 template <typename T, std::size_t SizeOfT> struct TrailingZerosCounter {
44  static std::size_t count(T Val, ZeroBehavior) {
45  if (!Val)
46  return std::numeric_limits<T>::digits;
47  if (Val & 0x1)
48  return 0;
49 
50  // Bisection method.
51  std::size_t ZeroBits = 0;
52  T Shift = std::numeric_limits<T>::digits >> 1;
53  T Mask = std::numeric_limits<T>::max() >> Shift;
54  while (Shift) {
55  if ((Val & Mask) == 0) {
56  Val >>= Shift;
57  ZeroBits |= Shift;
58  }
59  Shift >>= 1;
60  Mask >>= Shift;
61  }
62  return ZeroBits;
63  }
64 };
65 
66 #if __GNUC__ >= 4 || defined(_MSC_VER)
67 template <typename T> struct TrailingZerosCounter<T, 4> {
68  static std::size_t count(T Val, ZeroBehavior ZB) {
69  if (ZB != ZB_Undefined && Val == 0)
70  return 32;
71 
72 #if __has_builtin(__builtin_ctz) || LLVM_GNUC_PREREQ(4, 0, 0)
73  return __builtin_ctz(Val);
74 #elif defined(_MSC_VER)
75  unsigned long Index;
76  _BitScanForward(&Index, Val);
77  return Index;
78 #endif
79  }
80 };
81 
82 #if !defined(_MSC_VER) || defined(_M_X64)
83 template <typename T> struct TrailingZerosCounter<T, 8> {
84  static std::size_t count(T Val, ZeroBehavior ZB) {
85  if (ZB != ZB_Undefined && Val == 0)
86  return 64;
87 
88 #if __has_builtin(__builtin_ctzll) || LLVM_GNUC_PREREQ(4, 0, 0)
89  return __builtin_ctzll(Val);
90 #elif defined(_MSC_VER)
91  unsigned long Index;
92  _BitScanForward64(&Index, Val);
93  return Index;
94 #endif
95  }
96 };
97 #endif
98 #endif
99 } // namespace detail
100 
108 template <typename T>
109 std::size_t countTrailingZeros(T Val, ZeroBehavior ZB = ZB_Width) {
110  static_assert(std::numeric_limits<T>::is_integer &&
111  !std::numeric_limits<T>::is_signed,
112  "Only unsigned integral types are allowed.");
114 }
115 
116 namespace detail {
117 template <typename T, std::size_t SizeOfT> struct LeadingZerosCounter {
118  static std::size_t count(T Val, ZeroBehavior) {
119  if (!Val)
120  return std::numeric_limits<T>::digits;
121 
122  // Bisection method.
123  std::size_t ZeroBits = 0;
124  for (T Shift = std::numeric_limits<T>::digits >> 1; Shift; Shift >>= 1) {
125  T Tmp = Val >> Shift;
126  if (Tmp)
127  Val = Tmp;
128  else
129  ZeroBits |= Shift;
130  }
131  return ZeroBits;
132  }
133 };
134 
135 #if __GNUC__ >= 4 || defined(_MSC_VER)
136 template <typename T> struct LeadingZerosCounter<T, 4> {
137  static std::size_t count(T Val, ZeroBehavior ZB) {
138  if (ZB != ZB_Undefined && Val == 0)
139  return 32;
140 
141 #if __has_builtin(__builtin_clz) || LLVM_GNUC_PREREQ(4, 0, 0)
142  return __builtin_clz(Val);
143 #elif defined(_MSC_VER)
144  unsigned long Index;
145  _BitScanReverse(&Index, Val);
146  return Index ^ 31;
147 #endif
148  }
149 };
150 
151 #if !defined(_MSC_VER) || defined(_M_X64)
152 template <typename T> struct LeadingZerosCounter<T, 8> {
153  static std::size_t count(T Val, ZeroBehavior ZB) {
154  if (ZB != ZB_Undefined && Val == 0)
155  return 64;
156 
157 #if __has_builtin(__builtin_clzll) || LLVM_GNUC_PREREQ(4, 0, 0)
158  return __builtin_clzll(Val);
159 #elif defined(_MSC_VER)
160  unsigned long Index;
161  _BitScanReverse64(&Index, Val);
162  return Index ^ 63;
163 #endif
164  }
165 };
166 #endif
167 #endif
168 } // namespace detail
169 
177 template <typename T>
178 std::size_t countLeadingZeros(T Val, ZeroBehavior ZB = ZB_Width) {
179  static_assert(std::numeric_limits<T>::is_integer &&
180  !std::numeric_limits<T>::is_signed,
181  "Only unsigned integral types are allowed.");
183 }
184 
192 template <typename T> T findFirstSet(T Val, ZeroBehavior ZB = ZB_Max) {
193  if (ZB == ZB_Max && Val == 0)
194  return std::numeric_limits<T>::max();
195 
196  return countTrailingZeros(Val, ZB_Undefined);
197 }
198 
201 template <typename T> T maskTrailingOnes(unsigned N) {
202  static_assert(std::is_unsigned<T>::value, "Invalid type!");
203  const unsigned Bits = CHAR_BIT * sizeof(T);
204  assert(N <= Bits && "Invalid bit index");
205  return N == 0 ? 0 : (T(-1) >> (Bits - N));
206 }
207 
210 template <typename T> T maskLeadingOnes(unsigned N) {
211  return ~maskTrailingOnes<T>(CHAR_BIT * sizeof(T) - N);
212 }
213 
216 template <typename T> T maskTrailingZeros(unsigned N) {
217  return maskLeadingOnes<T>(CHAR_BIT * sizeof(T) - N);
218 }
219 
222 template <typename T> T maskLeadingZeros(unsigned N) {
223  return maskTrailingOnes<T>(CHAR_BIT * sizeof(T) - N);
224 }
225 
233 template <typename T> T findLastSet(T Val, ZeroBehavior ZB = ZB_Max) {
234  if (ZB == ZB_Max && Val == 0)
235  return std::numeric_limits<T>::max();
236 
237  // Use ^ instead of - because both gcc and llvm can remove the associated ^
238  // in the __builtin_clz intrinsic on x86.
239  return countLeadingZeros(Val, ZB_Undefined) ^
240  (std::numeric_limits<T>::digits - 1);
241 }
242 
246 static const unsigned char BitReverseTable256[256] = {
247 #define R2(n) n, n + 2 * 64, n + 1 * 64, n + 3 * 64
248 #define R4(n) R2(n), R2(n + 2 * 16), R2(n + 1 * 16), R2(n + 3 * 16)
249 #define R6(n) R4(n), R4(n + 2 * 4), R4(n + 1 * 4), R4(n + 3 * 4)
250  R6(0), R6(2), R6(1), R6(3)
251 #undef R2
252 #undef R4
253 #undef R6
254 };
255 
257 template <typename T>
258 T reverseBits(T Val) {
259  unsigned char in[sizeof(Val)];
260  unsigned char out[sizeof(Val)];
261  std::memcpy(in, &Val, sizeof(Val));
262  for (unsigned i = 0; i < sizeof(Val); ++i)
263  out[(sizeof(Val) - i) - 1] = BitReverseTable256[in[i]];
264  std::memcpy(&Val, out, sizeof(Val));
265  return Val;
266 }
267 
268 // NOTE: The following support functions use the _32/_64 extensions instead of
269 // type overloading so that signed and unsigned integers can be used without
270 // ambiguity.
271 
273 constexpr inline uint32_t Hi_32(uint64_t Value) {
274  return static_cast<uint32_t>(Value >> 32);
275 }
276 
278 constexpr inline uint32_t Lo_32(uint64_t Value) {
279  return static_cast<uint32_t>(Value);
280 }
281 
283 constexpr inline uint64_t Make_64(uint32_t High, uint32_t Low) {
284  return ((uint64_t)High << 32) | (uint64_t)Low;
285 }
286 
288 template <unsigned N> constexpr inline bool isInt(int64_t x) {
289  return N >= 64 || (-(INT64_C(1)<<(N-1)) <= x && x < (INT64_C(1)<<(N-1)));
290 }
291 // Template specializations to get better code for common cases.
292 template <> constexpr inline bool isInt<8>(int64_t x) {
293  return static_cast<int8_t>(x) == x;
294 }
295 template <> constexpr inline bool isInt<16>(int64_t x) {
296  return static_cast<int16_t>(x) == x;
297 }
298 template <> constexpr inline bool isInt<32>(int64_t x) {
299  return static_cast<int32_t>(x) == x;
300 }
301 
303 template <unsigned N, unsigned S>
304 constexpr inline bool isShiftedInt(int64_t x) {
305  static_assert(
306  N > 0, "isShiftedInt<0> doesn't make sense (refers to a 0-bit number.");
307  static_assert(N + S <= 64, "isShiftedInt<N, S> with N + S > 64 is too wide.");
308  return isInt<N + S>(x) && (x % (UINT64_C(1) << S) == 0);
309 }
310 
319 template <unsigned N>
320 constexpr inline typename std::enable_if<(N < 64), bool>::type
321 isUInt(uint64_t X) {
322  static_assert(N > 0, "isUInt<0> doesn't make sense");
323  return X < (UINT64_C(1) << (N));
324 }
325 template <unsigned N>
326 constexpr inline typename std::enable_if<N >= 64, bool>::type
327 isUInt(uint64_t X) {
328  return true;
329 }
330 
331 // Template specializations to get better code for common cases.
332 template <> constexpr inline bool isUInt<8>(uint64_t x) {
333  return static_cast<uint8_t>(x) == x;
334 }
335 template <> constexpr inline bool isUInt<16>(uint64_t x) {
336  return static_cast<uint16_t>(x) == x;
337 }
338 template <> constexpr inline bool isUInt<32>(uint64_t x) {
339  return static_cast<uint32_t>(x) == x;
340 }
341 
343 template <unsigned N, unsigned S>
344 constexpr inline bool isShiftedUInt(uint64_t x) {
345  static_assert(
346  N > 0, "isShiftedUInt<0> doesn't make sense (refers to a 0-bit number)");
347  static_assert(N + S <= 64,
348  "isShiftedUInt<N, S> with N + S > 64 is too wide.");
349  // Per the two static_asserts above, S must be strictly less than 64. So
350  // 1 << S is not undefined behavior.
351  return isUInt<N + S>(x) && (x % (UINT64_C(1) << S) == 0);
352 }
353 
355 inline uint64_t maxUIntN(uint64_t N) {
356  assert(N > 0 && N <= 64 && "integer width out of range");
357 
358  // uint64_t(1) << 64 is undefined behavior, so we can't do
359  // (uint64_t(1) << N) - 1
360  // without checking first that N != 64. But this works and doesn't have a
361  // branch.
362  return UINT64_MAX >> (64 - N);
363 }
364 
366 inline int64_t minIntN(int64_t N) {
367  assert(N > 0 && N <= 64 && "integer width out of range");
368 
369  return -(UINT64_C(1)<<(N-1));
370 }
371 
373 inline int64_t maxIntN(int64_t N) {
374  assert(N > 0 && N <= 64 && "integer width out of range");
375 
376  // This relies on two's complement wraparound when N == 64, so we convert to
377  // int64_t only at the very end to avoid UB.
378  return (UINT64_C(1) << (N - 1)) - 1;
379 }
380 
382 inline bool isUIntN(unsigned N, uint64_t x) {
383  return N >= 64 || x <= maxUIntN(N);
384 }
385 
387 inline bool isIntN(unsigned N, int64_t x) {
388  return N >= 64 || (minIntN(N) <= x && x <= maxIntN(N));
389 }
390 
394 constexpr inline bool isMask_32(uint32_t Value) {
395  return Value && ((Value + 1) & Value) == 0;
396 }
397 
400 constexpr inline bool isMask_64(uint64_t Value) {
401  return Value && ((Value + 1) & Value) == 0;
402 }
403 
406 constexpr inline bool isShiftedMask_32(uint32_t Value) {
407  return Value && isMask_32((Value - 1) | Value);
408 }
409 
412 constexpr inline bool isShiftedMask_64(uint64_t Value) {
413  return Value && isMask_64((Value - 1) | Value);
414 }
415 
418 constexpr inline bool isPowerOf2_32(uint32_t Value) {
419  return Value && !(Value & (Value - 1));
420 }
421 
423 constexpr inline bool isPowerOf2_64(uint64_t Value) {
424  return Value && !(Value & (Value - 1));
425 }
426 
435 template <typename T>
436 std::size_t countLeadingOnes(T Value, ZeroBehavior ZB = ZB_Width) {
437  static_assert(std::numeric_limits<T>::is_integer &&
438  !std::numeric_limits<T>::is_signed,
439  "Only unsigned integral types are allowed.");
440  return countLeadingZeros<T>(~Value, ZB);
441 }
442 
451 template <typename T>
452 std::size_t countTrailingOnes(T Value, ZeroBehavior ZB = ZB_Width) {
453  static_assert(std::numeric_limits<T>::is_integer &&
454  !std::numeric_limits<T>::is_signed,
455  "Only unsigned integral types are allowed.");
456  return countTrailingZeros<T>(~Value, ZB);
457 }
458 
459 namespace detail {
460 template <typename T, std::size_t SizeOfT> struct PopulationCounter {
461  static unsigned count(T Value) {
462  // Generic version, forward to 32 bits.
463  static_assert(SizeOfT <= 4, "Not implemented!");
464 #if __GNUC__ >= 4
465  return __builtin_popcount(Value);
466 #else
467  uint32_t v = Value;
468  v = v - ((v >> 1) & 0x55555555);
469  v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
470  return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
471 #endif
472  }
473 };
474 
475 template <typename T> struct PopulationCounter<T, 8> {
476  static unsigned count(T Value) {
477 #if __GNUC__ >= 4
478  return __builtin_popcountll(Value);
479 #else
480  uint64_t v = Value;
481  v = v - ((v >> 1) & 0x5555555555555555ULL);
482  v = (v & 0x3333333333333333ULL) + ((v >> 2) & 0x3333333333333333ULL);
483  v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0FULL;
484  return unsigned((uint64_t)(v * 0x0101010101010101ULL) >> 56);
485 #endif
486  }
487 };
488 } // namespace detail
489 
493 template <typename T>
494 inline unsigned countPopulation(T Value) {
495  static_assert(std::numeric_limits<T>::is_integer &&
496  !std::numeric_limits<T>::is_signed,
497  "Only unsigned integral types are allowed.");
499 }
500 
502 inline double Log2(double Value) {
503 #if defined(__ANDROID_API__) && __ANDROID_API__ < 18
504  return __builtin_log(Value) / __builtin_log(2.0);
505 #else
506  return std::log2(Value);
507 #endif
508 }
509 
513 inline unsigned Log2_32(uint32_t Value) {
514  return 31 - countLeadingZeros(Value);
515 }
516 
519 inline unsigned Log2_64(uint64_t Value) {
520  return 63 - countLeadingZeros(Value);
521 }
522 
526 inline unsigned Log2_32_Ceil(uint32_t Value) {
527  return 32 - countLeadingZeros(Value - 1);
528 }
529 
532 inline unsigned Log2_64_Ceil(uint64_t Value) {
533  return 64 - countLeadingZeros(Value - 1);
534 }
535 
537 inline uint64_t GreatestCommonDivisor64(uint64_t A, uint64_t B) {
538  while (B) {
539  uint64_t T = B;
540  B = A % B;
541  A = T;
542  }
543  return A;
544 }
545 
547 inline double BitsToDouble(uint64_t Bits) {
548  double D;
549  static_assert(sizeof(uint64_t) == sizeof(double), "Unexpected type sizes");
550  memcpy(&D, &Bits, sizeof(Bits));
551  return D;
552 }
553 
555 inline float BitsToFloat(uint32_t Bits) {
556  float F;
557  static_assert(sizeof(uint32_t) == sizeof(float), "Unexpected type sizes");
558  memcpy(&F, &Bits, sizeof(Bits));
559  return F;
560 }
561 
565 inline uint64_t DoubleToBits(double Double) {
566  uint64_t Bits;
567  static_assert(sizeof(uint64_t) == sizeof(double), "Unexpected type sizes");
568  memcpy(&Bits, &Double, sizeof(Double));
569  return Bits;
570 }
571 
575 inline uint32_t FloatToBits(float Float) {
576  uint32_t Bits;
577  static_assert(sizeof(uint32_t) == sizeof(float), "Unexpected type sizes");
578  memcpy(&Bits, &Float, sizeof(Float));
579  return Bits;
580 }
581 
584 constexpr inline uint64_t MinAlign(uint64_t A, uint64_t B) {
585  // The largest power of 2 that divides both A and B.
586  //
587  // Replace "-Value" by "1+~Value" in the following commented code to avoid
588  // MSVC warning C4146
589  // return (A | B) & -(A | B);
590  return (A | B) & (1 + ~(A | B));
591 }
592 
597 inline uintptr_t alignAddr(const void *Addr, size_t Alignment) {
598  assert(Alignment && isPowerOf2_64((uint64_t)Alignment) &&
599  "Alignment is not a power of two!");
600 
601  assert((uintptr_t)Addr + Alignment - 1 >= (uintptr_t)Addr);
602 
603  return (((uintptr_t)Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1));
604 }
605 
608 inline size_t alignmentAdjustment(const void *Ptr, size_t Alignment) {
609  return alignAddr(Ptr, Alignment) - (uintptr_t)Ptr;
610 }
611 
614 inline uint64_t NextPowerOf2(uint64_t A) {
615  A |= (A >> 1);
616  A |= (A >> 2);
617  A |= (A >> 4);
618  A |= (A >> 8);
619  A |= (A >> 16);
620  A |= (A >> 32);
621  return A + 1;
622 }
623 
626 inline uint64_t PowerOf2Floor(uint64_t A) {
627  if (!A) return 0;
628  return 1ull << (63 - countLeadingZeros(A, ZB_Undefined));
629 }
630 
633 inline uint64_t PowerOf2Ceil(uint64_t A) {
634  if (!A)
635  return 0;
636  return NextPowerOf2(A - 1);
637 }
638 
659 inline uint64_t alignTo(uint64_t Value, uint64_t Align, uint64_t Skew = 0) {
660  assert(Align != 0u && "Align can't be 0.");
661  Skew %= Align;
662  return (Value + Align - 1 - Skew) / Align * Align + Skew;
663 }
664 
667 template <uint64_t Align> constexpr inline uint64_t alignTo(uint64_t Value) {
668  static_assert(Align != 0u, "Align must be non-zero");
669  return (Value + Align - 1) / Align * Align;
670 }
671 
673 inline uint64_t divideCeil(uint64_t Numerator, uint64_t Denominator) {
674  return alignTo(Numerator, Denominator) / Denominator;
675 }
676 
681 template <uint64_t Align>
682 struct AlignTo {
683  static_assert(Align != 0u, "Align must be non-zero");
684  template <uint64_t Value>
685  struct from_value {
686  static const uint64_t value = (Value + Align - 1) / Align * Align;
687  };
688 };
689 
692 inline uint64_t alignDown(uint64_t Value, uint64_t Align, uint64_t Skew = 0) {
693  assert(Align != 0u && "Align can't be 0.");
694  Skew %= Align;
695  return (Value - Skew) / Align * Align + Skew;
696 }
697 
701 inline uint64_t OffsetToAlignment(uint64_t Value, uint64_t Align) {
702  return alignTo(Value, Align) - Value;
703 }
704 
707 template <unsigned B> constexpr inline int32_t SignExtend32(uint32_t X) {
708  static_assert(B > 0, "Bit width can't be 0.");
709  static_assert(B <= 32, "Bit width out of range.");
710  return int32_t(X << (32 - B)) >> (32 - B);
711 }
712 
715 inline int32_t SignExtend32(uint32_t X, unsigned B) {
716  assert(B > 0 && "Bit width can't be 0.");
717  assert(B <= 32 && "Bit width out of range.");
718  return int32_t(X << (32 - B)) >> (32 - B);
719 }
720 
723 template <unsigned B> constexpr inline int64_t SignExtend64(uint64_t x) {
724  static_assert(B > 0, "Bit width can't be 0.");
725  static_assert(B <= 64, "Bit width out of range.");
726  return int64_t(x << (64 - B)) >> (64 - B);
727 }
728 
731 inline int64_t SignExtend64(uint64_t X, unsigned B) {
732  assert(B > 0 && "Bit width can't be 0.");
733  assert(B <= 64 && "Bit width out of range.");
734  return int64_t(X << (64 - B)) >> (64 - B);
735 }
736 
739 template <typename T>
740 typename std::enable_if<std::is_unsigned<T>::value, T>::type
742  return std::max(X, Y) - std::min(X, Y);
743 }
744 
748 template <typename T>
749 typename std::enable_if<std::is_unsigned<T>::value, T>::type
750 SaturatingAdd(T X, T Y, bool *ResultOverflowed = nullptr) {
751  bool Dummy;
752  bool &Overflowed = ResultOverflowed ? *ResultOverflowed : Dummy;
753  // Hacker's Delight, p. 29
754  T Z = X + Y;
755  Overflowed = (Z < X || Z < Y);
756  if (Overflowed)
757  return std::numeric_limits<T>::max();
758  else
759  return Z;
760 }
761 
765 template <typename T>
766 typename std::enable_if<std::is_unsigned<T>::value, T>::type
767 SaturatingMultiply(T X, T Y, bool *ResultOverflowed = nullptr) {
768  bool Dummy;
769  bool &Overflowed = ResultOverflowed ? *ResultOverflowed : Dummy;
770 
771  // Hacker's Delight, p. 30 has a different algorithm, but we don't use that
772  // because it fails for uint16_t (where multiplication can have undefined
773  // behavior due to promotion to int), and requires a division in addition
774  // to the multiplication.
775 
776  Overflowed = false;
777 
778  // Log2(Z) would be either Log2Z or Log2Z + 1.
779  // Special case: if X or Y is 0, Log2_64 gives -1, and Log2Z
780  // will necessarily be less than Log2Max as desired.
781  int Log2Z = Log2_64(X) + Log2_64(Y);
782  const T Max = std::numeric_limits<T>::max();
783  int Log2Max = Log2_64(Max);
784  if (Log2Z < Log2Max) {
785  return X * Y;
786  }
787  if (Log2Z > Log2Max) {
788  Overflowed = true;
789  return Max;
790  }
791 
792  // We're going to use the top bit, and maybe overflow one
793  // bit past it. Multiply all but the bottom bit then add
794  // that on at the end.
795  T Z = (X >> 1) * Y;
796  if (Z & ~(Max >> 1)) {
797  Overflowed = true;
798  return Max;
799  }
800  Z <<= 1;
801  if (X & 1)
802  return SaturatingAdd(Z, Y, ResultOverflowed);
803 
804  return Z;
805 }
806 
811 template <typename T>
812 typename std::enable_if<std::is_unsigned<T>::value, T>::type
813 SaturatingMultiplyAdd(T X, T Y, T A, bool *ResultOverflowed = nullptr) {
814  bool Dummy;
815  bool &Overflowed = ResultOverflowed ? *ResultOverflowed : Dummy;
816 
817  T Product = SaturatingMultiply(X, Y, &Overflowed);
818  if (Overflowed)
819  return Product;
820 
821  return SaturatingAdd(A, Product, &Overflowed);
822 }
823 
824 } // namespace wpi
825 
826 #endif
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:412
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:526
std::size_t countLeadingZeros(T Val, ZeroBehavior ZB=ZB_Width)
Count number of 0's from the most significant bit to the least stopping at the first 1...
Definition: MathExtras.h:178
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:723
std::size_t countTrailingZeros(T Val, ZeroBehavior ZB=ZB_Width)
Count number of 0's from the least significant bit to the most stopping at the first 1...
Definition: MathExtras.h:109
bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
Definition: MathExtras.h:382
uint64_t divideCeil(uint64_t Numerator, uint64_t Denominator)
Returns the integer ceil(Numerator / Denominator).
Definition: MathExtras.h:673
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:192
Definition: MathExtras.h:460
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition: MathExtras.h:418
constexpr bool isShiftedUInt(uint64_t x)
Checks if a unsigned integer is an N bit number shifted left by S.
Definition: MathExtras.h:344
std::size_t 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:436
constexpr uint64_t MinAlign(uint64_t A, uint64_t B)
A and B are either alignments or offsets.
Definition: MathExtras.h:584
The returned value is undefined.
Definition: MathExtras.h:35
int64_t maxIntN(int64_t N)
Gets the maximum value for a N-bit signed integer.
Definition: MathExtras.h:373
ZeroBehavior
The behavior an operation has on an input of 0.
Definition: MathExtras.h:33
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:750
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:400
The returned value is numeric_limits::digits.
Definition: MathExtras.h:39
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:692
The returned value is numeric_limits::max()
Definition: MathExtras.h:37
Definition: MathExtras.h:43
WPILib C++ utilities (wpiutil) namespace.
Definition: SmallString.h:21
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:741
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:767
alignTo for contexts where a constant expression is required.
Definition: MathExtras.h:682
double Log2(double Value)
Return the log base 2 of the specified value.
Definition: MathExtras.h:502
uint64_t maxUIntN(uint64_t N)
Gets the maximum value for a N-bit unsigned integer.
Definition: MathExtras.h:355
uint64_t NextPowerOf2(uint64_t A)
Returns the next power of two (in 64-bits) that is strictly greater than A.
Definition: MathExtras.h:614
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:216
bool isIntN(unsigned N, int64_t x)
Checks if an signed integer fits into the given (dynamic) bit width.
Definition: MathExtras.h:387
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:394
float BitsToFloat(uint32_t Bits)
This function takes a 32-bit integer and returns the bit equivalent float.
Definition: MathExtras.h:555
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:513
constexpr bool isShiftedInt(int64_t x)
Checks if a signed integer is an N bit number shifted left by S.
Definition: MathExtras.h:304
std::size_t 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:452
uint64_t alignTo(uint64_t Value, uint64_t Align, uint64_t Skew=0)
Returns the next integer (mod 2**64) that is greater than or equal to Value and is a multiple of Alig...
Definition: MathExtras.h:659
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:201
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:321
uint64_t PowerOf2Ceil(uint64_t A)
Returns the power of two which is greater than or equal to the given value.
Definition: MathExtras.h:633
size_t alignmentAdjustment(const void *Ptr, size_t Alignment)
Returns the necessary adjustment for aligning Ptr to Alignment bytes, rounding up.
Definition: MathExtras.h:608
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:283
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition: MathExtras.h:288
constexpr uint32_t Hi_32(uint64_t Value)
Return the high 32 bits of a 64 bit value.
Definition: MathExtras.h:273
constexpr uint32_t Lo_32(uint64_t Value)
Return the low 32 bits of a 64 bit value.
Definition: MathExtras.h:278
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:519
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:210
Definition: MathExtras.h:117
uint64_t PowerOf2Floor(uint64_t A)
Returns the power of two which is less than or equal to the given value.
Definition: MathExtras.h:626
uint64_t OffsetToAlignment(uint64_t Value, uint64_t Align)
Returns the offset to the next integer (mod 2**64) that is greater than or equal to Value and is a mu...
Definition: MathExtras.h:701
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:813
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:707
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:233
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
Definition: MathExtras.h:423
double BitsToDouble(uint64_t Bits)
This function takes a 64-bit integer and returns the bit equivalent double.
Definition: MathExtras.h:547
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:222
uintptr_t alignAddr(const void *Addr, size_t Alignment)
Aligns Addr to Alignment bytes, rounding up.
Definition: MathExtras.h:597
int64_t minIntN(int64_t N)
Gets the minimum value for a N-bit signed integer.
Definition: MathExtras.h:366
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:406
uint64_t DoubleToBits(double Double)
This function takes a double and returns the bit equivalent 64-bit integer.
Definition: MathExtras.h:565
unsigned countPopulation(T Value)
Count the number of set bits in a value.
Definition: MathExtras.h:494
T reverseBits(T Val)
Reverse the bits in Val.
Definition: MathExtras.h:258
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:532
uint32_t FloatToBits(float Float)
This function takes a float and returns the bit equivalent 32-bit integer.
Definition: MathExtras.h:575
uint64_t GreatestCommonDivisor64(uint64_t A, uint64_t B)
Return the greatest common divisor of the values using Euclid's algorithm.
Definition: MathExtras.h:537