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 };
00234 
00235 /// \brief Reverse the bits in \p Val.
00236 template <typename T>
00237 T reverseBits(T Val) {
00238   unsigned char in[sizeof(Val)];
00239   unsigned char out[sizeof(Val)];
00240   std::memcpy(in, &Val, sizeof(Val));
00241   for (unsigned i = 0; i < sizeof(Val); ++i)
00242     out[(sizeof(Val) - i) - 1] = BitReverseTable256[in[i]];
00243   std::memcpy(&Val, out, sizeof(Val));
00244   return Val;
00245 }
00246 
00247 // NOTE: The following support functions use the _32/_64 extensions instead of
00248 // type overloading so that signed and unsigned integers can be used without
00249 // ambiguity.
00250 
00251 /// Hi_32 - This function returns the high 32 bits of a 64 bit value.
00252 inline uint32_t Hi_32(uint64_t Value) {
00253   return static_cast<uint32_t>(Value >> 32);
00254 }
00255 
00256 /// Lo_32 - This function returns the low 32 bits of a 64 bit value.
00257 inline uint32_t Lo_32(uint64_t Value) {
00258   return static_cast<uint32_t>(Value);
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 sequence of ones
00326 /// starting at the least significant bit with the remainder zero (32 bit
00327 /// 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 sequence of ones
00333 /// starting at the least significant bit with the remainder zero (64 bit
00334 /// 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 /// 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 isMask_32((Value - 1) | Value);
00344 }
00345 
00346 /// isShiftedMask_64 - This function returns true if the argument contains a
00347 /// sequence of ones with the remainder zero (64 bit version.)
00348 inline bool isShiftedMask_64(uint64_t Value) {
00349   return 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 /// CountLeadingOnes_32 - this function performs the operation of
00383 /// counting the number of ones from the most significant bit to the first zero
00384 /// bit.  Ex. CountLeadingOnes_32(0xFF0FFF00) == 8.
00385 /// Returns 32 if the word is all ones.
00386 inline unsigned CountLeadingOnes_32(uint32_t Value) {
00387   return countLeadingZeros(~Value);
00388 }
00389 
00390 /// CountLeadingOnes_64 - This function performs the operation
00391 /// of counting the number of ones from the most significant bit to the first
00392 /// zero bit (64 bit edition.)
00393 /// Returns 64 if the word is all ones.
00394 inline unsigned CountLeadingOnes_64(uint64_t Value) {
00395   return countLeadingZeros(~Value);
00396 }
00397 
00398 /// CountTrailingOnes_32 - this function performs the operation of
00399 /// counting the number of ones from the least significant bit to the first zero
00400 /// bit.  Ex. CountTrailingOnes_32(0x00FF00FF) == 8.
00401 /// Returns 32 if the word is all ones.
00402 inline unsigned CountTrailingOnes_32(uint32_t Value) {
00403   return countTrailingZeros(~Value);
00404 }
00405 
00406 /// CountTrailingOnes_64 - This function performs the operation
00407 /// of counting the number of ones from the least significant bit to the first
00408 /// zero bit (64 bit edition.)
00409 /// Returns 64 if the word is all ones.
00410 inline unsigned CountTrailingOnes_64(uint64_t Value) {
00411   return countTrailingZeros(~Value);
00412 }
00413 
00414 /// CountPopulation_32 - this function counts the number of set bits in a value.
00415 /// Ex. CountPopulation(0xF000F000) = 8
00416 /// Returns 0 if the word is zero.
00417 inline unsigned CountPopulation_32(uint32_t Value) {
00418 #if __GNUC__ >= 4
00419   return __builtin_popcount(Value);
00420 #else
00421   uint32_t v = Value - ((Value >> 1) & 0x55555555);
00422   v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
00423   return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
00424 #endif
00425 }
00426 
00427 /// CountPopulation_64 - this function counts the number of set bits in a value,
00428 /// (64 bit edition.)
00429 inline unsigned CountPopulation_64(uint64_t Value) {
00430 #if __GNUC__ >= 4
00431   return __builtin_popcountll(Value);
00432 #else
00433   uint64_t v = Value - ((Value >> 1) & 0x5555555555555555ULL);
00434   v = (v & 0x3333333333333333ULL) + ((v >> 2) & 0x3333333333333333ULL);
00435   v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0FULL;
00436   return unsigned((uint64_t)(v * 0x0101010101010101ULL) >> 56);
00437 #endif
00438 }
00439 
00440 /// Log2_32 - This function returns the floor log base 2 of the specified value,
00441 /// -1 if the value is zero. (32 bit edition.)
00442 /// Ex. Log2_32(32) == 5, Log2_32(1) == 0, Log2_32(0) == -1, Log2_32(6) == 2
00443 inline unsigned Log2_32(uint32_t Value) {
00444   return 31 - countLeadingZeros(Value);
00445 }
00446 
00447 /// Log2_64 - This function returns the floor log base 2 of the specified value,
00448 /// -1 if the value is zero. (64 bit edition.)
00449 inline unsigned Log2_64(uint64_t Value) {
00450   return 63 - countLeadingZeros(Value);
00451 }
00452 
00453 /// Log2_32_Ceil - This function returns the ceil log base 2 of the specified
00454 /// value, 32 if the value is zero. (32 bit edition).
00455 /// Ex. Log2_32_Ceil(32) == 5, Log2_32_Ceil(1) == 0, Log2_32_Ceil(6) == 3
00456 inline unsigned Log2_32_Ceil(uint32_t Value) {
00457   return 32 - countLeadingZeros(Value - 1);
00458 }
00459 
00460 /// Log2_64_Ceil - This function returns the ceil log base 2 of the specified
00461 /// value, 64 if the value is zero. (64 bit edition.)
00462 inline unsigned Log2_64_Ceil(uint64_t Value) {
00463   return 64 - countLeadingZeros(Value - 1);
00464 }
00465 
00466 /// GreatestCommonDivisor64 - Return the greatest common divisor of the two
00467 /// values using Euclid's algorithm.
00468 inline uint64_t GreatestCommonDivisor64(uint64_t A, uint64_t B) {
00469   while (B) {
00470     uint64_t T = B;
00471     B = A % B;
00472     A = T;
00473   }
00474   return A;
00475 }
00476 
00477 /// BitsToDouble - This function takes a 64-bit integer and returns the bit
00478 /// equivalent double.
00479 inline double BitsToDouble(uint64_t Bits) {
00480   union {
00481     uint64_t L;
00482     double D;
00483   } T;
00484   T.L = Bits;
00485   return T.D;
00486 }
00487 
00488 /// BitsToFloat - This function takes a 32-bit integer and returns the bit
00489 /// equivalent float.
00490 inline float BitsToFloat(uint32_t Bits) {
00491   union {
00492     uint32_t I;
00493     float F;
00494   } T;
00495   T.I = Bits;
00496   return T.F;
00497 }
00498 
00499 /// DoubleToBits - This function takes a double and returns the bit
00500 /// equivalent 64-bit integer.  Note that copying doubles around
00501 /// changes the bits of NaNs on some hosts, notably x86, so this
00502 /// routine cannot be used if these bits are needed.
00503 inline uint64_t DoubleToBits(double Double) {
00504   union {
00505     uint64_t L;
00506     double D;
00507   } T;
00508   T.D = Double;
00509   return T.L;
00510 }
00511 
00512 /// FloatToBits - This function takes a float and returns the bit
00513 /// equivalent 32-bit integer.  Note that copying floats around
00514 /// changes the bits of NaNs on some hosts, notably x86, so this
00515 /// routine cannot be used if these bits are needed.
00516 inline uint32_t FloatToBits(float Float) {
00517   union {
00518     uint32_t I;
00519     float F;
00520   } T;
00521   T.F = Float;
00522   return T.I;
00523 }
00524 
00525 /// Platform-independent wrappers for the C99 isnan() function.
00526 int IsNAN(float f);
00527 int IsNAN(double d);
00528 
00529 /// Platform-independent wrappers for the C99 isinf() function.
00530 int IsInf(float f);
00531 int IsInf(double d);
00532 
00533 /// MinAlign - A and B are either alignments or offsets.  Return the minimum
00534 /// alignment that may be assumed after adding the two together.
00535 inline uint64_t MinAlign(uint64_t A, uint64_t B) {
00536   // The largest power of 2 that divides both A and B.
00537   //
00538   // Replace "-Value" by "1+~Value" in the following commented code to avoid 
00539   // MSVC warning C4146
00540   //    return (A | B) & -(A | B);
00541   return (A | B) & (1 + ~(A | B));
00542 }
00543 
00544 /// \brief Aligns \c Ptr to \c Alignment bytes, rounding up.
00545 ///
00546 /// Alignment should be a power of two.  This method rounds up, so
00547 /// AlignPtr(7, 4) == 8 and AlignPtr(8, 4) == 8.
00548 inline char *alignPtr(char *Ptr, size_t Alignment) {
00549   assert(Alignment && isPowerOf2_64((uint64_t)Alignment) &&
00550          "Alignment is not a power of two!");
00551 
00552   return (char *)(((uintptr_t)Ptr + Alignment - 1) &
00553                   ~(uintptr_t)(Alignment - 1));
00554 }
00555 
00556 /// NextPowerOf2 - Returns the next power of two (in 64-bits)
00557 /// that is strictly greater than A.  Returns zero on overflow.
00558 inline uint64_t NextPowerOf2(uint64_t A) {
00559   A |= (A >> 1);
00560   A |= (A >> 2);
00561   A |= (A >> 4);
00562   A |= (A >> 8);
00563   A |= (A >> 16);
00564   A |= (A >> 32);
00565   return A + 1;
00566 }
00567 
00568 /// Returns the power of two which is less than or equal to the given value.
00569 /// Essentially, it is a floor operation across the domain of powers of two.
00570 inline uint64_t PowerOf2Floor(uint64_t A) {
00571   if (!A) return 0;
00572   return 1ull << (63 - countLeadingZeros(A, ZB_Undefined));
00573 }
00574 
00575 /// Returns the next integer (mod 2**64) that is greater than or equal to
00576 /// \p Value and is a multiple of \p Align. \p Align must be non-zero.
00577 ///
00578 /// Examples:
00579 /// \code
00580 ///   RoundUpToAlignment(5, 8) = 8
00581 ///   RoundUpToAlignment(17, 8) = 24
00582 ///   RoundUpToAlignment(~0LL, 8) = 0
00583 /// \endcode
00584 inline uint64_t RoundUpToAlignment(uint64_t Value, uint64_t Align) {
00585   return ((Value + Align - 1) / Align) * Align;
00586 }
00587 
00588 /// Returns the offset to the next integer (mod 2**64) that is greater than
00589 /// or equal to \p Value and is a multiple of \p Align. \p Align must be
00590 /// non-zero.
00591 inline uint64_t OffsetToAlignment(uint64_t Value, uint64_t Align) {
00592   return RoundUpToAlignment(Value, Align) - Value;
00593 }
00594 
00595 /// abs64 - absolute value of a 64-bit int.  Not all environments support
00596 /// "abs" on whatever their name for the 64-bit int type is.  The absolute
00597 /// value of the largest negative number is undefined, as with "abs".
00598 inline int64_t abs64(int64_t x) {
00599   return (x < 0) ? -x : x;
00600 }
00601 
00602 /// SignExtend32 - Sign extend B-bit number x to 32-bit int.
00603 /// Usage int32_t r = SignExtend32<5>(x);
00604 template <unsigned B> inline int32_t SignExtend32(uint32_t x) {
00605   return int32_t(x << (32 - B)) >> (32 - B);
00606 }
00607 
00608 /// \brief Sign extend number in the bottom B bits of X to a 32-bit int.
00609 /// Requires 0 < B <= 32.
00610 inline int32_t SignExtend32(uint32_t X, unsigned B) {
00611   return int32_t(X << (32 - B)) >> (32 - B);
00612 }
00613 
00614 /// SignExtend64 - Sign extend B-bit number x to 64-bit int.
00615 /// Usage int64_t r = SignExtend64<5>(x);
00616 template <unsigned B> inline int64_t SignExtend64(uint64_t x) {
00617   return int64_t(x << (64 - B)) >> (64 - B);
00618 }
00619 
00620 /// \brief Sign extend number in the bottom B bits of X to a 64-bit int.
00621 /// Requires 0 < B <= 64.
00622 inline int64_t SignExtend64(uint64_t X, unsigned B) {
00623   return int64_t(X << (64 - B)) >> (64 - B);
00624 }
00625 
00626 #if defined(_MSC_VER)
00627   // Visual Studio defines the HUGE_VAL class of macros using purposeful
00628   // constant arithmetic overflow, which it then warns on when encountered.
00629   const float huge_valf = std::numeric_limits<float>::infinity();
00630 #else
00631   const float huge_valf = HUGE_VALF;
00632 #endif
00633 } // End llvm namespace
00634 
00635 #endif