LLVM API Documentation

MathExtras.h
Go to the documentation of this file.
00001 //===-- llvm/Support/MathExtras.h - Useful math functions -------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file contains some functions that are useful for math stuff.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_SUPPORT_MATHEXTRAS_H
00015 #define LLVM_SUPPORT_MATHEXTRAS_H
00016 
00017 #include "llvm/Support/Compiler.h"
00018 #include "llvm/Support/SwapByteOrder.h"
00019 #include <cassert>
00020 #include <cstring>
00021 #include <type_traits>
00022 
00023 #ifdef _MSC_VER
00024 #include <intrin.h>
00025 #include <limits>
00026 #endif
00027 
00028 namespace llvm {
00029 /// \brief The behavior an operation has on an input of 0.
00030 enum ZeroBehavior {
00031   /// \brief The returned value is undefined.
00032   ZB_Undefined,
00033   /// \brief The returned value is numeric_limits<T>::max()
00034   ZB_Max,
00035   /// \brief The returned value is numeric_limits<T>::digits
00036   ZB_Width
00037 };
00038 
00039 /// \brief Count number of 0's from the least significant bit to the most
00040 ///   stopping at the first 1.
00041 ///
00042 /// Only unsigned integral types are allowed.
00043 ///
00044 /// \param ZB the behavior on an input of 0. Only ZB_Width and ZB_Undefined are
00045 ///   valid arguments.
00046 template <typename T>
00047 typename std::enable_if<std::numeric_limits<T>::is_integer &&
00048                         !std::numeric_limits<T>::is_signed, std::size_t>::type
00049 countTrailingZeros(T Val, ZeroBehavior ZB = ZB_Width) {
00050   (void)ZB;
00051 
00052   if (!Val)
00053     return std::numeric_limits<T>::digits;
00054   if (Val & 0x1)
00055     return 0;
00056 
00057   // Bisection method.
00058   std::size_t ZeroBits = 0;
00059   T Shift = std::numeric_limits<T>::digits >> 1;
00060   T Mask = std::numeric_limits<T>::max() >> Shift;
00061   while (Shift) {
00062     if ((Val & Mask) == 0) {
00063       Val >>= Shift;
00064       ZeroBits |= Shift;
00065     }
00066     Shift >>= 1;
00067     Mask >>= Shift;
00068   }
00069   return ZeroBits;
00070 }
00071 
00072 // Disable signed.
00073 template <typename T>
00074 typename std::enable_if<std::numeric_limits<T>::is_integer &&
00075                         std::numeric_limits<T>::is_signed, std::size_t>::type
00076 countTrailingZeros(T Val, ZeroBehavior ZB = ZB_Width) LLVM_DELETED_FUNCTION;
00077 
00078 #if __GNUC__ >= 4 || _MSC_VER
00079 template <>
00080 inline std::size_t countTrailingZeros<uint32_t>(uint32_t Val, ZeroBehavior ZB) {
00081   if (ZB != ZB_Undefined && Val == 0)
00082     return 32;
00083 
00084 #if __has_builtin(__builtin_ctz) || __GNUC_PREREQ(4, 0)
00085   return __builtin_ctz(Val);
00086 #elif _MSC_VER
00087   unsigned long Index;
00088   _BitScanForward(&Index, Val);
00089   return Index;
00090 #endif
00091 }
00092 
00093 #if !defined(_MSC_VER) || defined(_M_X64)
00094 template <>
00095 inline std::size_t countTrailingZeros<uint64_t>(uint64_t Val, ZeroBehavior ZB) {
00096   if (ZB != ZB_Undefined && Val == 0)
00097     return 64;
00098 
00099 #if __has_builtin(__builtin_ctzll) || __GNUC_PREREQ(4, 0)
00100   return __builtin_ctzll(Val);
00101 #elif _MSC_VER
00102   unsigned long Index;
00103   _BitScanForward64(&Index, Val);
00104   return Index;
00105 #endif
00106 }
00107 #endif
00108 #endif
00109 
00110 /// \brief Count number of 0's from the most significant bit to the least
00111 ///   stopping at the first 1.
00112 ///
00113 /// Only unsigned integral types are allowed.
00114 ///
00115 /// \param ZB the behavior on an input of 0. Only ZB_Width and ZB_Undefined are
00116 ///   valid arguments.
00117 template <typename T>
00118 typename std::enable_if<std::numeric_limits<T>::is_integer &&
00119                         !std::numeric_limits<T>::is_signed, std::size_t>::type
00120 countLeadingZeros(T Val, ZeroBehavior ZB = ZB_Width) {
00121   (void)ZB;
00122 
00123   if (!Val)
00124     return std::numeric_limits<T>::digits;
00125 
00126   // Bisection method.
00127   std::size_t ZeroBits = 0;
00128   for (T Shift = std::numeric_limits<T>::digits >> 1; Shift; Shift >>= 1) {
00129     T Tmp = Val >> Shift;
00130     if (Tmp)
00131       Val = Tmp;
00132     else
00133       ZeroBits |= Shift;
00134   }
00135   return ZeroBits;
00136 }
00137 
00138 // Disable signed.
00139 template <typename T>
00140 typename std::enable_if<std::numeric_limits<T>::is_integer &&
00141                         std::numeric_limits<T>::is_signed, std::size_t>::type
00142 countLeadingZeros(T Val, ZeroBehavior ZB = ZB_Width) LLVM_DELETED_FUNCTION;
00143 
00144 #if __GNUC__ >= 4 || _MSC_VER
00145 template <>
00146 inline std::size_t countLeadingZeros<uint32_t>(uint32_t Val, ZeroBehavior ZB) {
00147   if (ZB != ZB_Undefined && Val == 0)
00148     return 32;
00149 
00150 #if __has_builtin(__builtin_clz) || __GNUC_PREREQ(4, 0)
00151   return __builtin_clz(Val);
00152 #elif _MSC_VER
00153   unsigned long Index;
00154   _BitScanReverse(&Index, Val);
00155   return Index ^ 31;
00156 #endif
00157 }
00158 
00159 #if !defined(_MSC_VER) || defined(_M_X64)
00160 template <>
00161 inline std::size_t countLeadingZeros<uint64_t>(uint64_t Val, ZeroBehavior ZB) {
00162   if (ZB != ZB_Undefined && Val == 0)
00163     return 64;
00164 
00165 #if __has_builtin(__builtin_clzll) || __GNUC_PREREQ(4, 0)
00166   return __builtin_clzll(Val);
00167 #elif _MSC_VER
00168   unsigned long Index;
00169   _BitScanReverse64(&Index, Val);
00170   return Index ^ 63;
00171 #endif
00172 }
00173 #endif
00174 #endif
00175 
00176 /// \brief Get the index of the first set bit starting from the least
00177 ///   significant bit.
00178 ///
00179 /// Only unsigned integral types are allowed.
00180 ///
00181 /// \param ZB the behavior on an input of 0. Only ZB_Max and ZB_Undefined are
00182 ///   valid arguments.
00183 template <typename T>
00184 typename std::enable_if<std::numeric_limits<T>::is_integer &&
00185                        !std::numeric_limits<T>::is_signed, T>::type
00186 findFirstSet(T Val, ZeroBehavior ZB = ZB_Max) {
00187   if (ZB == ZB_Max && Val == 0)
00188     return std::numeric_limits<T>::max();
00189 
00190   return countTrailingZeros(Val, ZB_Undefined);
00191 }
00192 
00193 // Disable signed.
00194 template <typename T>
00195 typename std::enable_if<std::numeric_limits<T>::is_integer &&
00196                         std::numeric_limits<T>::is_signed, T>::type
00197 findFirstSet(T Val, ZeroBehavior ZB = ZB_Max) LLVM_DELETED_FUNCTION;
00198 
00199 /// \brief Get the index of the last set bit starting from the least
00200 ///   significant bit.
00201 ///
00202 /// Only unsigned integral types are allowed.
00203 ///
00204 /// \param ZB the behavior on an input of 0. Only ZB_Max and ZB_Undefined are
00205 ///   valid arguments.
00206 template <typename T>
00207 typename std::enable_if<std::numeric_limits<T>::is_integer &&
00208                         !std::numeric_limits<T>::is_signed, T>::type
00209 findLastSet(T Val, ZeroBehavior ZB = ZB_Max) {
00210   if (ZB == ZB_Max && Val == 0)
00211     return std::numeric_limits<T>::max();
00212 
00213   // Use ^ instead of - because both gcc and llvm can remove the associated ^
00214   // in the __builtin_clz intrinsic on x86.
00215   return countLeadingZeros(Val, ZB_Undefined) ^
00216          (std::numeric_limits<T>::digits - 1);
00217 }
00218 
00219 // Disable signed.
00220 template <typename T>
00221 typename std::enable_if<std::numeric_limits<T>::is_integer &&
00222                         std::numeric_limits<T>::is_signed, T>::type
00223 findLastSet(T Val, ZeroBehavior ZB = ZB_Max) LLVM_DELETED_FUNCTION;
00224 
00225 /// \brief Macro compressed bit reversal table for 256 bits.
00226 ///
00227 /// http://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable
00228 static const unsigned char BitReverseTable256[256] = {
00229 #define R2(n) n, n + 2 * 64, n + 1 * 64, n + 3 * 64
00230 #define R4(n) R2(n), R2(n + 2 * 16), R2(n + 1 * 16), R2(n + 3 * 16)
00231 #define R6(n) R4(n), R4(n + 2 * 4), R4(n + 1 * 4), R4(n + 3 * 4)
00232   R6(0), R6(2), R6(1), R6(3)
00233 #undef R2
00234 #undef R4
00235 #undef R6
00236 };
00237 
00238 /// \brief Reverse the bits in \p Val.
00239 template <typename T>
00240 T reverseBits(T Val) {
00241   unsigned char in[sizeof(Val)];
00242   unsigned char out[sizeof(Val)];
00243   std::memcpy(in, &Val, sizeof(Val));
00244   for (unsigned i = 0; i < sizeof(Val); ++i)
00245     out[(sizeof(Val) - i) - 1] = BitReverseTable256[in[i]];
00246   std::memcpy(&Val, out, sizeof(Val));
00247   return Val;
00248 }
00249 
00250 // NOTE: The following support functions use the _32/_64 extensions instead of
00251 // type overloading so that signed and unsigned integers can be used without
00252 // ambiguity.
00253 
00254 /// Hi_32 - This function returns the high 32 bits of a 64 bit value.
00255 inline uint32_t Hi_32(uint64_t Value) {
00256   return static_cast<uint32_t>(Value >> 32);
00257 }
00258 
00259 /// Lo_32 - This function returns the low 32 bits of a 64 bit value.
00260 inline uint32_t Lo_32(uint64_t Value) {
00261   return static_cast<uint32_t>(Value);
00262 }
00263 
00264 /// Make_64 - This functions makes a 64-bit integer from a high / low pair of
00265 ///           32-bit integers.
00266 inline uint64_t Make_64(uint32_t High, uint32_t Low) {
00267   return ((uint64_t)High << 32) | (uint64_t)Low;
00268 }
00269 
00270 /// isInt - Checks if an integer fits into the given bit width.
00271 template<unsigned N>
00272 inline bool isInt(int64_t x) {
00273   return N >= 64 || (-(INT64_C(1)<<(N-1)) <= x && x < (INT64_C(1)<<(N-1)));
00274 }
00275 // Template specializations to get better code for common cases.
00276 template<>
00277 inline bool isInt<8>(int64_t x) {
00278   return static_cast<int8_t>(x) == x;
00279 }
00280 template<>
00281 inline bool isInt<16>(int64_t x) {
00282   return static_cast<int16_t>(x) == x;
00283 }
00284 template<>
00285 inline bool isInt<32>(int64_t x) {
00286   return static_cast<int32_t>(x) == x;
00287 }
00288 
00289 /// isShiftedInt<N,S> - Checks if a signed integer is an N bit number shifted
00290 ///                     left by S.
00291 template<unsigned N, unsigned S>
00292 inline bool isShiftedInt(int64_t x) {
00293   return isInt<N+S>(x) && (x % (1<<S) == 0);
00294 }
00295 
00296 /// isUInt - Checks if an unsigned integer fits into the given bit width.
00297 template<unsigned N>
00298 inline bool isUInt(uint64_t x) {
00299   return N >= 64 || x < (UINT64_C(1)<<(N));
00300 }
00301 // Template specializations to get better code for common cases.
00302 template<>
00303 inline bool isUInt<8>(uint64_t x) {
00304   return static_cast<uint8_t>(x) == x;
00305 }
00306 template<>
00307 inline bool isUInt<16>(uint64_t x) {
00308   return static_cast<uint16_t>(x) == x;
00309 }
00310 template<>
00311 inline bool isUInt<32>(uint64_t x) {
00312   return static_cast<uint32_t>(x) == x;
00313 }
00314 
00315 /// isShiftedUInt<N,S> - Checks if a unsigned integer is an N bit number shifted
00316 ///                     left by S.
00317 template<unsigned N, unsigned S>
00318 inline bool isShiftedUInt(uint64_t x) {
00319   return isUInt<N+S>(x) && (x % (1<<S) == 0);
00320 }
00321 
00322 /// isUIntN - Checks if an unsigned integer fits into the given (dynamic)
00323 /// bit width.
00324 inline bool isUIntN(unsigned N, uint64_t x) {
00325   return x == (x & (~0ULL >> (64 - N)));
00326 }
00327 
00328 /// isIntN - Checks if an signed integer fits into the given (dynamic)
00329 /// bit width.
00330 inline bool isIntN(unsigned N, int64_t x) {
00331   return N >= 64 || (-(INT64_C(1)<<(N-1)) <= x && x < (INT64_C(1)<<(N-1)));
00332 }
00333 
00334 /// isMask_32 - This function returns true if the argument is a sequence of ones
00335 /// starting at the least significant bit with the remainder zero (32 bit
00336 /// version).   Ex. isMask_32(0x0000FFFFU) == true.
00337 inline bool isMask_32(uint32_t Value) {
00338   return Value && ((Value + 1) & Value) == 0;
00339 }
00340 
00341 /// isMask_64 - This function returns true if the argument is a sequence of ones
00342 /// starting at the least significant bit with the remainder zero (64 bit
00343 /// version).
00344 inline bool isMask_64(uint64_t Value) {
00345   return Value && ((Value + 1) & Value) == 0;
00346 }
00347 
00348 /// isShiftedMask_32 - This function returns true if the argument contains a
00349 /// sequence of ones with the remainder zero (32 bit version.)
00350 /// Ex. isShiftedMask_32(0x0000FF00U) == true.
00351 inline bool isShiftedMask_32(uint32_t Value) {
00352   return isMask_32((Value - 1) | Value);
00353 }
00354 
00355 /// isShiftedMask_64 - This function returns true if the argument contains a
00356 /// sequence of ones with the remainder zero (64 bit version.)
00357 inline bool isShiftedMask_64(uint64_t Value) {
00358   return isMask_64((Value - 1) | Value);
00359 }
00360 
00361 /// isPowerOf2_32 - This function returns true if the argument is a power of
00362 /// two > 0. Ex. isPowerOf2_32(0x00100000U) == true (32 bit edition.)
00363 inline bool isPowerOf2_32(uint32_t Value) {
00364   return Value && !(Value & (Value - 1));
00365 }
00366 
00367 /// isPowerOf2_64 - This function returns true if the argument is a power of two
00368 /// > 0 (64 bit edition.)
00369 inline bool isPowerOf2_64(uint64_t Value) {
00370   return Value && !(Value & (Value - int64_t(1L)));
00371 }
00372 
00373 /// ByteSwap_16 - This function returns a byte-swapped representation of the
00374 /// 16-bit argument, Value.
00375 inline uint16_t ByteSwap_16(uint16_t Value) {
00376   return sys::SwapByteOrder_16(Value);
00377 }
00378 
00379 /// ByteSwap_32 - This function returns a byte-swapped representation of the
00380 /// 32-bit argument, Value.
00381 inline uint32_t ByteSwap_32(uint32_t Value) {
00382   return sys::SwapByteOrder_32(Value);
00383 }
00384 
00385 /// ByteSwap_64 - This function returns a byte-swapped representation of the
00386 /// 64-bit argument, Value.
00387 inline uint64_t ByteSwap_64(uint64_t Value) {
00388   return sys::SwapByteOrder_64(Value);
00389 }
00390 
00391 /// CountLeadingOnes_32 - this function performs the operation of
00392 /// counting the number of ones from the most significant bit to the first zero
00393 /// bit.  Ex. CountLeadingOnes_32(0xFF0FFF00) == 8.
00394 /// Returns 32 if the word is all ones.
00395 inline unsigned CountLeadingOnes_32(uint32_t Value) {
00396   return countLeadingZeros(~Value);
00397 }
00398 
00399 /// CountLeadingOnes_64 - This function performs the operation
00400 /// of counting the number of ones from the most significant bit to the first
00401 /// zero bit (64 bit edition.)
00402 /// Returns 64 if the word is all ones.
00403 inline unsigned CountLeadingOnes_64(uint64_t Value) {
00404   return countLeadingZeros(~Value);
00405 }
00406 
00407 /// CountTrailingOnes_32 - this function performs the operation of
00408 /// counting the number of ones from the least significant bit to the first zero
00409 /// bit.  Ex. CountTrailingOnes_32(0x00FF00FF) == 8.
00410 /// Returns 32 if the word is all ones.
00411 inline unsigned CountTrailingOnes_32(uint32_t Value) {
00412   return countTrailingZeros(~Value);
00413 }
00414 
00415 /// CountTrailingOnes_64 - This function performs the operation
00416 /// of counting the number of ones from the least significant bit to the first
00417 /// zero bit (64 bit edition.)
00418 /// Returns 64 if the word is all ones.
00419 inline unsigned CountTrailingOnes_64(uint64_t Value) {
00420   return countTrailingZeros(~Value);
00421 }
00422 
00423 /// CountPopulation_32 - this function counts the number of set bits in a value.
00424 /// Ex. CountPopulation(0xF000F000) = 8
00425 /// Returns 0 if the word is zero.
00426 inline unsigned CountPopulation_32(uint32_t Value) {
00427 #if __GNUC__ >= 4
00428   return __builtin_popcount(Value);
00429 #else
00430   uint32_t v = Value - ((Value >> 1) & 0x55555555);
00431   v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
00432   return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
00433 #endif
00434 }
00435 
00436 /// CountPopulation_64 - this function counts the number of set bits in a value,
00437 /// (64 bit edition.)
00438 inline unsigned CountPopulation_64(uint64_t Value) {
00439 #if __GNUC__ >= 4
00440   return __builtin_popcountll(Value);
00441 #else
00442   uint64_t v = Value - ((Value >> 1) & 0x5555555555555555ULL);
00443   v = (v & 0x3333333333333333ULL) + ((v >> 2) & 0x3333333333333333ULL);
00444   v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0FULL;
00445   return unsigned((uint64_t)(v * 0x0101010101010101ULL) >> 56);
00446 #endif
00447 }
00448 
00449 /// Log2_32 - This function returns the floor log base 2 of the specified value,
00450 /// -1 if the value is zero. (32 bit edition.)
00451 /// Ex. Log2_32(32) == 5, Log2_32(1) == 0, Log2_32(0) == -1, Log2_32(6) == 2
00452 inline unsigned Log2_32(uint32_t Value) {
00453   return 31 - countLeadingZeros(Value);
00454 }
00455 
00456 /// Log2_64 - This function returns the floor log base 2 of the specified value,
00457 /// -1 if the value is zero. (64 bit edition.)
00458 inline unsigned Log2_64(uint64_t Value) {
00459   return 63 - countLeadingZeros(Value);
00460 }
00461 
00462 /// Log2_32_Ceil - This function returns the ceil log base 2 of the specified
00463 /// value, 32 if the value is zero. (32 bit edition).
00464 /// Ex. Log2_32_Ceil(32) == 5, Log2_32_Ceil(1) == 0, Log2_32_Ceil(6) == 3
00465 inline unsigned Log2_32_Ceil(uint32_t Value) {
00466   return 32 - countLeadingZeros(Value - 1);
00467 }
00468 
00469 /// Log2_64_Ceil - This function returns the ceil log base 2 of the specified
00470 /// value, 64 if the value is zero. (64 bit edition.)
00471 inline unsigned Log2_64_Ceil(uint64_t Value) {
00472   return 64 - countLeadingZeros(Value - 1);
00473 }
00474 
00475 /// GreatestCommonDivisor64 - Return the greatest common divisor of the two
00476 /// values using Euclid's algorithm.
00477 inline uint64_t GreatestCommonDivisor64(uint64_t A, uint64_t B) {
00478   while (B) {
00479     uint64_t T = B;
00480     B = A % B;
00481     A = T;
00482   }
00483   return A;
00484 }
00485 
00486 /// BitsToDouble - This function takes a 64-bit integer and returns the bit
00487 /// equivalent double.
00488 inline double BitsToDouble(uint64_t Bits) {
00489   union {
00490     uint64_t L;
00491     double D;
00492   } T;
00493   T.L = Bits;
00494   return T.D;
00495 }
00496 
00497 /// BitsToFloat - This function takes a 32-bit integer and returns the bit
00498 /// equivalent float.
00499 inline float BitsToFloat(uint32_t Bits) {
00500   union {
00501     uint32_t I;
00502     float F;
00503   } T;
00504   T.I = Bits;
00505   return T.F;
00506 }
00507 
00508 /// DoubleToBits - This function takes a double and returns the bit
00509 /// equivalent 64-bit integer.  Note that copying doubles around
00510 /// changes the bits of NaNs on some hosts, notably x86, so this
00511 /// routine cannot be used if these bits are needed.
00512 inline uint64_t DoubleToBits(double Double) {
00513   union {
00514     uint64_t L;
00515     double D;
00516   } T;
00517   T.D = Double;
00518   return T.L;
00519 }
00520 
00521 /// FloatToBits - This function takes a float and returns the bit
00522 /// equivalent 32-bit integer.  Note that copying floats around
00523 /// changes the bits of NaNs on some hosts, notably x86, so this
00524 /// routine cannot be used if these bits are needed.
00525 inline uint32_t FloatToBits(float Float) {
00526   union {
00527     uint32_t I;
00528     float F;
00529   } T;
00530   T.F = Float;
00531   return T.I;
00532 }
00533 
00534 /// Platform-independent wrappers for the C99 isnan() function.
00535 int IsNAN(float f);
00536 int IsNAN(double d);
00537 
00538 /// Platform-independent wrappers for the C99 isinf() function.
00539 int IsInf(float f);
00540 int IsInf(double d);
00541 
00542 /// MinAlign - A and B are either alignments or offsets.  Return the minimum
00543 /// alignment that may be assumed after adding the two together.
00544 inline uint64_t MinAlign(uint64_t A, uint64_t B) {
00545   // The largest power of 2 that divides both A and B.
00546   //
00547   // Replace "-Value" by "1+~Value" in the following commented code to avoid 
00548   // MSVC warning C4146
00549   //    return (A | B) & -(A | B);
00550   return (A | B) & (1 + ~(A | B));
00551 }
00552 
00553 /// \brief Aligns \c Addr to \c Alignment bytes, rounding up.
00554 ///
00555 /// Alignment should be a power of two.  This method rounds up, so
00556 /// alignAddr(7, 4) == 8 and alignAddr(8, 4) == 8.
00557 inline uintptr_t alignAddr(void *Addr, size_t Alignment) {
00558   assert(Alignment && isPowerOf2_64((uint64_t)Alignment) &&
00559          "Alignment is not a power of two!");
00560 
00561   assert((uintptr_t)Addr + Alignment - 1 >= (uintptr_t)Addr);
00562 
00563   return (((uintptr_t)Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1));
00564 }
00565 
00566 /// \brief Returns the necessary adjustment for aligning \c Ptr to \c Alignment
00567 /// bytes, rounding up.
00568 inline size_t alignmentAdjustment(void *Ptr, size_t Alignment) {
00569   return alignAddr(Ptr, Alignment) - (uintptr_t)Ptr;
00570 }
00571 
00572 /// NextPowerOf2 - Returns the next power of two (in 64-bits)
00573 /// that is strictly greater than A.  Returns zero on overflow.
00574 inline uint64_t NextPowerOf2(uint64_t A) {
00575   A |= (A >> 1);
00576   A |= (A >> 2);
00577   A |= (A >> 4);
00578   A |= (A >> 8);
00579   A |= (A >> 16);
00580   A |= (A >> 32);
00581   return A + 1;
00582 }
00583 
00584 /// Returns the power of two which is less than or equal to the given value.
00585 /// Essentially, it is a floor operation across the domain of powers of two.
00586 inline uint64_t PowerOf2Floor(uint64_t A) {
00587   if (!A) return 0;
00588   return 1ull << (63 - countLeadingZeros(A, ZB_Undefined));
00589 }
00590 
00591 /// Returns the next integer (mod 2**64) that is greater than or equal to
00592 /// \p Value and is a multiple of \p Align. \p Align must be non-zero.
00593 ///
00594 /// Examples:
00595 /// \code
00596 ///   RoundUpToAlignment(5, 8) = 8
00597 ///   RoundUpToAlignment(17, 8) = 24
00598 ///   RoundUpToAlignment(~0LL, 8) = 0
00599 /// \endcode
00600 inline uint64_t RoundUpToAlignment(uint64_t Value, uint64_t Align) {
00601   return ((Value + Align - 1) / Align) * Align;
00602 }
00603 
00604 /// Returns the offset to the next integer (mod 2**64) that is greater than
00605 /// or equal to \p Value and is a multiple of \p Align. \p Align must be
00606 /// non-zero.
00607 inline uint64_t OffsetToAlignment(uint64_t Value, uint64_t Align) {
00608   return RoundUpToAlignment(Value, Align) - Value;
00609 }
00610 
00611 /// abs64 - absolute value of a 64-bit int.  Not all environments support
00612 /// "abs" on whatever their name for the 64-bit int type is.  The absolute
00613 /// value of the largest negative number is undefined, as with "abs".
00614 inline int64_t abs64(int64_t x) {
00615   return (x < 0) ? -x : x;
00616 }
00617 
00618 /// SignExtend32 - Sign extend B-bit number x to 32-bit int.
00619 /// Usage int32_t r = SignExtend32<5>(x);
00620 template <unsigned B> inline int32_t SignExtend32(uint32_t x) {
00621   return int32_t(x << (32 - B)) >> (32 - B);
00622 }
00623 
00624 /// \brief Sign extend number in the bottom B bits of X to a 32-bit int.
00625 /// Requires 0 < B <= 32.
00626 inline int32_t SignExtend32(uint32_t X, unsigned B) {
00627   return int32_t(X << (32 - B)) >> (32 - B);
00628 }
00629 
00630 /// SignExtend64 - Sign extend B-bit number x to 64-bit int.
00631 /// Usage int64_t r = SignExtend64<5>(x);
00632 template <unsigned B> inline int64_t SignExtend64(uint64_t x) {
00633   return int64_t(x << (64 - B)) >> (64 - B);
00634 }
00635 
00636 /// \brief Sign extend number in the bottom B bits of X to a 64-bit int.
00637 /// Requires 0 < B <= 64.
00638 inline int64_t SignExtend64(uint64_t X, unsigned B) {
00639   return int64_t(X << (64 - B)) >> (64 - B);
00640 }
00641 
00642 #if defined(_MSC_VER)
00643   // Visual Studio defines the HUGE_VAL class of macros using purposeful
00644   // constant arithmetic overflow, which it then warns on when encountered.
00645   const float huge_valf = std::numeric_limits<float>::infinity();
00646 #else
00647   const float huge_valf = HUGE_VALF;
00648 #endif
00649 } // End llvm namespace
00650 
00651 #endif