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