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