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