Home | History | Annotate | Download | only in Support
      1 //===-- llvm/Support/MathExtras.h - Useful math functions -------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file contains some functions that are useful for math stuff.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #ifndef LLVM_SUPPORT_MATHEXTRAS_H
     15 #define LLVM_SUPPORT_MATHEXTRAS_H
     16 
     17 #include "llvm/Support/Compiler.h"
     18 #include "llvm/Support/SwapByteOrder.h"
     19 #include <cassert>
     20 #include <cstring>
     21 #include <type_traits>
     22 
     23 #ifdef _MSC_VER
     24 #include <intrin.h>
     25 #endif
     26 
     27 namespace llvm {
     28 /// \brief The behavior an operation has on an input of 0.
     29 enum ZeroBehavior {
     30   /// \brief The returned value is undefined.
     31   ZB_Undefined,
     32   /// \brief The returned value is numeric_limits<T>::max()
     33   ZB_Max,
     34   /// \brief The returned value is numeric_limits<T>::digits
     35   ZB_Width
     36 };
     37 
     38 namespace detail {
     39 template <typename T, std::size_t SizeOfT> struct TrailingZerosCounter {
     40   static std::size_t count(T Val, ZeroBehavior) {
     41     if (!Val)
     42       return std::numeric_limits<T>::digits;
     43     if (Val & 0x1)
     44       return 0;
     45 
     46     // Bisection method.
     47     std::size_t ZeroBits = 0;
     48     T Shift = std::numeric_limits<T>::digits >> 1;
     49     T Mask = std::numeric_limits<T>::max() >> Shift;
     50     while (Shift) {
     51       if ((Val & Mask) == 0) {
     52         Val >>= Shift;
     53         ZeroBits |= Shift;
     54       }
     55       Shift >>= 1;
     56       Mask >>= Shift;
     57     }
     58     return ZeroBits;
     59   }
     60 };
     61 
     62 #if __GNUC__ >= 4 || _MSC_VER
     63 template <typename T> struct TrailingZerosCounter<T, 4> {
     64   static std::size_t count(T Val, ZeroBehavior ZB) {
     65     if (ZB != ZB_Undefined && Val == 0)
     66       return 32;
     67 
     68 #if __has_builtin(__builtin_ctz) || LLVM_GNUC_PREREQ(4, 0, 0)
     69     return __builtin_ctz(Val);
     70 #elif _MSC_VER
     71     unsigned long Index;
     72     _BitScanForward(&Index, Val);
     73     return Index;
     74 #endif
     75   }
     76 };
     77 
     78 #if !defined(_MSC_VER) || defined(_M_X64)
     79 template <typename T> struct TrailingZerosCounter<T, 8> {
     80   static std::size_t count(T Val, ZeroBehavior ZB) {
     81     if (ZB != ZB_Undefined && Val == 0)
     82       return 64;
     83 
     84 #if __has_builtin(__builtin_ctzll) || LLVM_GNUC_PREREQ(4, 0, 0)
     85     return __builtin_ctzll(Val);
     86 #elif _MSC_VER
     87     unsigned long Index;
     88     _BitScanForward64(&Index, Val);
     89     return Index;
     90 #endif
     91   }
     92 };
     93 #endif
     94 #endif
     95 } // namespace detail
     96 
     97 /// \brief Count number of 0's from the least significant bit to the most
     98 ///   stopping at the first 1.
     99 ///
    100 /// Only unsigned integral types are allowed.
    101 ///
    102 /// \param ZB the behavior on an input of 0. Only ZB_Width and ZB_Undefined are
    103 ///   valid arguments.
    104 template <typename T>
    105 std::size_t countTrailingZeros(T Val, ZeroBehavior ZB = ZB_Width) {
    106   static_assert(std::numeric_limits<T>::is_integer &&
    107                     !std::numeric_limits<T>::is_signed,
    108                 "Only unsigned integral types are allowed.");
    109   return detail::TrailingZerosCounter<T, sizeof(T)>::count(Val, ZB);
    110 }
    111 
    112 namespace detail {
    113 template <typename T, std::size_t SizeOfT> struct LeadingZerosCounter {
    114   static std::size_t count(T Val, ZeroBehavior) {
    115     if (!Val)
    116       return std::numeric_limits<T>::digits;
    117 
    118     // Bisection method.
    119     std::size_t ZeroBits = 0;
    120     for (T Shift = std::numeric_limits<T>::digits >> 1; Shift; Shift >>= 1) {
    121       T Tmp = Val >> Shift;
    122       if (Tmp)
    123         Val = Tmp;
    124       else
    125         ZeroBits |= Shift;
    126     }
    127     return ZeroBits;
    128   }
    129 };
    130 
    131 #if __GNUC__ >= 4 || _MSC_VER
    132 template <typename T> struct LeadingZerosCounter<T, 4> {
    133   static std::size_t count(T Val, ZeroBehavior ZB) {
    134     if (ZB != ZB_Undefined && Val == 0)
    135       return 32;
    136 
    137 #if __has_builtin(__builtin_clz) || LLVM_GNUC_PREREQ(4, 0, 0)
    138     return __builtin_clz(Val);
    139 #elif _MSC_VER
    140     unsigned long Index;
    141     _BitScanReverse(&Index, Val);
    142     return Index ^ 31;
    143 #endif
    144   }
    145 };
    146 
    147 #if !defined(_MSC_VER) || defined(_M_X64)
    148 template <typename T> struct LeadingZerosCounter<T, 8> {
    149   static std::size_t count(T Val, ZeroBehavior ZB) {
    150     if (ZB != ZB_Undefined && Val == 0)
    151       return 64;
    152 
    153 #if __has_builtin(__builtin_clzll) || LLVM_GNUC_PREREQ(4, 0, 0)
    154     return __builtin_clzll(Val);
    155 #elif _MSC_VER
    156     unsigned long Index;
    157     _BitScanReverse64(&Index, Val);
    158     return Index ^ 63;
    159 #endif
    160   }
    161 };
    162 #endif
    163 #endif
    164 } // namespace detail
    165 
    166 /// \brief Count number of 0's from the most significant bit to the least
    167 ///   stopping at the first 1.
    168 ///
    169 /// Only unsigned integral types are allowed.
    170 ///
    171 /// \param ZB the behavior on an input of 0. Only ZB_Width and ZB_Undefined are
    172 ///   valid arguments.
    173 template <typename T>
    174 std::size_t countLeadingZeros(T Val, ZeroBehavior ZB = ZB_Width) {
    175   static_assert(std::numeric_limits<T>::is_integer &&
    176                     !std::numeric_limits<T>::is_signed,
    177                 "Only unsigned integral types are allowed.");
    178   return detail::LeadingZerosCounter<T, sizeof(T)>::count(Val, ZB);
    179 }
    180 
    181 /// \brief Get the index of the first set bit starting from the least
    182 ///   significant bit.
    183 ///
    184 /// Only unsigned integral types are allowed.
    185 ///
    186 /// \param ZB the behavior on an input of 0. Only ZB_Max and ZB_Undefined are
    187 ///   valid arguments.
    188 template <typename T> T findFirstSet(T Val, ZeroBehavior ZB = ZB_Max) {
    189   if (ZB == ZB_Max && Val == 0)
    190     return std::numeric_limits<T>::max();
    191 
    192   return countTrailingZeros(Val, ZB_Undefined);
    193 }
    194 
    195 /// \brief Get the index of the last set bit starting from the least
    196 ///   significant bit.
    197 ///
    198 /// Only unsigned integral types are allowed.
    199 ///
    200 /// \param ZB the behavior on an input of 0. Only ZB_Max and ZB_Undefined are
    201 ///   valid arguments.
    202 template <typename T> T findLastSet(T Val, ZeroBehavior ZB = ZB_Max) {
    203   if (ZB == ZB_Max && Val == 0)
    204     return std::numeric_limits<T>::max();
    205 
    206   // Use ^ instead of - because both gcc and llvm can remove the associated ^
    207   // in the __builtin_clz intrinsic on x86.
    208   return countLeadingZeros(Val, ZB_Undefined) ^
    209          (std::numeric_limits<T>::digits - 1);
    210 }
    211 
    212 /// \brief Macro compressed bit reversal table for 256 bits.
    213 ///
    214 /// http://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable
    215 static const unsigned char BitReverseTable256[256] = {
    216 #define R2(n) n, n + 2 * 64, n + 1 * 64, n + 3 * 64
    217 #define R4(n) R2(n), R2(n + 2 * 16), R2(n + 1 * 16), R2(n + 3 * 16)
    218 #define R6(n) R4(n), R4(n + 2 * 4), R4(n + 1 * 4), R4(n + 3 * 4)
    219   R6(0), R6(2), R6(1), R6(3)
    220 #undef R2
    221 #undef R4
    222 #undef R6
    223 };
    224 
    225 /// \brief Reverse the bits in \p Val.
    226 template <typename T>
    227 T reverseBits(T Val) {
    228   unsigned char in[sizeof(Val)];
    229   unsigned char out[sizeof(Val)];
    230   std::memcpy(in, &Val, sizeof(Val));
    231   for (unsigned i = 0; i < sizeof(Val); ++i)
    232     out[(sizeof(Val) - i) - 1] = BitReverseTable256[in[i]];
    233   std::memcpy(&Val, out, sizeof(Val));
    234   return Val;
    235 }
    236 
    237 // NOTE: The following support functions use the _32/_64 extensions instead of
    238 // type overloading so that signed and unsigned integers can be used without
    239 // ambiguity.
    240 
    241 /// Hi_32 - This function returns the high 32 bits of a 64 bit value.
    242 inline uint32_t Hi_32(uint64_t Value) {
    243   return static_cast<uint32_t>(Value >> 32);
    244 }
    245 
    246 /// Lo_32 - This function returns the low 32 bits of a 64 bit value.
    247 inline uint32_t Lo_32(uint64_t Value) {
    248   return static_cast<uint32_t>(Value);
    249 }
    250 
    251 /// Make_64 - This functions makes a 64-bit integer from a high / low pair of
    252 ///           32-bit integers.
    253 inline uint64_t Make_64(uint32_t High, uint32_t Low) {
    254   return ((uint64_t)High << 32) | (uint64_t)Low;
    255 }
    256 
    257 /// isInt - Checks if an integer fits into the given bit width.
    258 template<unsigned N>
    259 inline bool isInt(int64_t x) {
    260   return N >= 64 || (-(INT64_C(1)<<(N-1)) <= x && x < (INT64_C(1)<<(N-1)));
    261 }
    262 // Template specializations to get better code for common cases.
    263 template<>
    264 inline bool isInt<8>(int64_t x) {
    265   return static_cast<int8_t>(x) == x;
    266 }
    267 template<>
    268 inline bool isInt<16>(int64_t x) {
    269   return static_cast<int16_t>(x) == x;
    270 }
    271 template<>
    272 inline bool isInt<32>(int64_t x) {
    273   return static_cast<int32_t>(x) == x;
    274 }
    275 
    276 /// isShiftedInt<N,S> - Checks if a signed integer is an N bit number shifted
    277 ///                     left by S.
    278 template<unsigned N, unsigned S>
    279 inline bool isShiftedInt(int64_t x) {
    280   return isInt<N+S>(x) && (x % (1<<S) == 0);
    281 }
    282 
    283 /// isUInt - Checks if an unsigned integer fits into the given bit width.
    284 template<unsigned N>
    285 inline bool isUInt(uint64_t x) {
    286   return N >= 64 || x < (UINT64_C(1)<<(N));
    287 }
    288 // Template specializations to get better code for common cases.
    289 template<>
    290 inline bool isUInt<8>(uint64_t x) {
    291   return static_cast<uint8_t>(x) == x;
    292 }
    293 template<>
    294 inline bool isUInt<16>(uint64_t x) {
    295   return static_cast<uint16_t>(x) == x;
    296 }
    297 template<>
    298 inline bool isUInt<32>(uint64_t x) {
    299   return static_cast<uint32_t>(x) == x;
    300 }
    301 
    302 /// isShiftedUInt<N,S> - Checks if a unsigned integer is an N bit number shifted
    303 ///                     left by S.
    304 template<unsigned N, unsigned S>
    305 inline bool isShiftedUInt(uint64_t x) {
    306   return isUInt<N+S>(x) && (x % (1<<S) == 0);
    307 }
    308 
    309 /// isUIntN - Checks if an unsigned integer fits into the given (dynamic)
    310 /// bit width.
    311 inline bool isUIntN(unsigned N, uint64_t x) {
    312   return x == (x & (~0ULL >> (64 - N)));
    313 }
    314 
    315 /// isIntN - Checks if an signed integer fits into the given (dynamic)
    316 /// bit width.
    317 inline bool isIntN(unsigned N, int64_t x) {
    318   return N >= 64 || (-(INT64_C(1)<<(N-1)) <= x && x < (INT64_C(1)<<(N-1)));
    319 }
    320 
    321 /// isMask_32 - This function returns true if the argument is a non-empty
    322 /// sequence of ones starting at the least significant bit with the remainder
    323 /// zero (32 bit version).  Ex. isMask_32(0x0000FFFFU) == true.
    324 inline bool isMask_32(uint32_t Value) {
    325   return Value && ((Value + 1) & Value) == 0;
    326 }
    327 
    328 /// isMask_64 - This function returns true if the argument is a non-empty
    329 /// sequence of ones starting at the least significant bit with the remainder
    330 /// zero (64 bit version).
    331 inline bool isMask_64(uint64_t Value) {
    332   return Value && ((Value + 1) & Value) == 0;
    333 }
    334 
    335 /// isShiftedMask_32 - This function returns true if the argument contains a
    336 /// non-empty sequence of ones with the remainder zero (32 bit version.)
    337 /// Ex. isShiftedMask_32(0x0000FF00U) == true.
    338 inline bool isShiftedMask_32(uint32_t Value) {
    339   return Value && isMask_32((Value - 1) | Value);
    340 }
    341 
    342 /// isShiftedMask_64 - This function returns true if the argument contains a
    343 /// non-empty sequence of ones with the remainder zero (64 bit version.)
    344 inline bool isShiftedMask_64(uint64_t Value) {
    345   return Value && isMask_64((Value - 1) | Value);
    346 }
    347 
    348 /// isPowerOf2_32 - This function returns true if the argument is a power of
    349 /// two > 0. Ex. isPowerOf2_32(0x00100000U) == true (32 bit edition.)
    350 inline bool isPowerOf2_32(uint32_t Value) {
    351   return Value && !(Value & (Value - 1));
    352 }
    353 
    354 /// isPowerOf2_64 - This function returns true if the argument is a power of two
    355 /// > 0 (64 bit edition.)
    356 inline bool isPowerOf2_64(uint64_t Value) {
    357   return Value && !(Value & (Value - int64_t(1L)));
    358 }
    359 
    360 /// ByteSwap_16 - This function returns a byte-swapped representation of the
    361 /// 16-bit argument, Value.
    362 inline uint16_t ByteSwap_16(uint16_t Value) {
    363   return sys::SwapByteOrder_16(Value);
    364 }
    365 
    366 /// ByteSwap_32 - This function returns a byte-swapped representation of the
    367 /// 32-bit argument, Value.
    368 inline uint32_t ByteSwap_32(uint32_t Value) {
    369   return sys::SwapByteOrder_32(Value);
    370 }
    371 
    372 /// ByteSwap_64 - This function returns a byte-swapped representation of the
    373 /// 64-bit argument, Value.
    374 inline uint64_t ByteSwap_64(uint64_t Value) {
    375   return sys::SwapByteOrder_64(Value);
    376 }
    377 
    378 /// \brief Count the number of ones from the most significant bit to the first
    379 /// zero bit.
    380 ///
    381 /// Ex. CountLeadingOnes(0xFF0FFF00) == 8.
    382 /// Only unsigned integral types are allowed.
    383 ///
    384 /// \param ZB the behavior on an input of all ones. Only ZB_Width and
    385 /// ZB_Undefined are valid arguments.
    386 template <typename T>
    387 std::size_t countLeadingOnes(T Value, ZeroBehavior ZB = ZB_Width) {
    388   static_assert(std::numeric_limits<T>::is_integer &&
    389                     !std::numeric_limits<T>::is_signed,
    390                 "Only unsigned integral types are allowed.");
    391   return countLeadingZeros(~Value, ZB);
    392 }
    393 
    394 /// \brief Count the number of ones from the least significant bit to the first
    395 /// zero bit.
    396 ///
    397 /// Ex. countTrailingOnes(0x00FF00FF) == 8.
    398 /// Only unsigned integral types are allowed.
    399 ///
    400 /// \param ZB the behavior on an input of all ones. Only ZB_Width and
    401 /// ZB_Undefined are valid arguments.
    402 template <typename T>
    403 std::size_t countTrailingOnes(T Value, ZeroBehavior ZB = ZB_Width) {
    404   static_assert(std::numeric_limits<T>::is_integer &&
    405                     !std::numeric_limits<T>::is_signed,
    406                 "Only unsigned integral types are allowed.");
    407   return countTrailingZeros(~Value, ZB);
    408 }
    409 
    410 namespace detail {
    411 template <typename T, std::size_t SizeOfT> struct PopulationCounter {
    412   static unsigned count(T Value) {
    413     // Generic version, forward to 32 bits.
    414     static_assert(SizeOfT <= 4, "Not implemented!");
    415 #if __GNUC__ >= 4
    416     return __builtin_popcount(Value);
    417 #else
    418     uint32_t v = Value;
    419     v = v - ((v >> 1) & 0x55555555);
    420     v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
    421     return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
    422 #endif
    423   }
    424 };
    425 
    426 template <typename T> struct PopulationCounter<T, 8> {
    427   static unsigned count(T Value) {
    428 #if __GNUC__ >= 4
    429     return __builtin_popcountll(Value);
    430 #else
    431     uint64_t v = Value;
    432     v = v - ((v >> 1) & 0x5555555555555555ULL);
    433     v = (v & 0x3333333333333333ULL) + ((v >> 2) & 0x3333333333333333ULL);
    434     v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0FULL;
    435     return unsigned((uint64_t)(v * 0x0101010101010101ULL) >> 56);
    436 #endif
    437   }
    438 };
    439 } // namespace detail
    440 
    441 /// \brief Count the number of set bits in a value.
    442 /// Ex. countPopulation(0xF000F000) = 8
    443 /// Returns 0 if the word is zero.
    444 template <typename T>
    445 inline unsigned countPopulation(T Value) {
    446   static_assert(std::numeric_limits<T>::is_integer &&
    447                     !std::numeric_limits<T>::is_signed,
    448                 "Only unsigned integral types are allowed.");
    449   return detail::PopulationCounter<T, sizeof(T)>::count(Value);
    450 }
    451 
    452 /// Log2_32 - This function returns the floor log base 2 of the specified value,
    453 /// -1 if the value is zero. (32 bit edition.)
    454 /// Ex. Log2_32(32) == 5, Log2_32(1) == 0, Log2_32(0) == -1, Log2_32(6) == 2
    455 inline unsigned Log2_32(uint32_t Value) {
    456   return 31 - countLeadingZeros(Value);
    457 }
    458 
    459 /// Log2_64 - This function returns the floor log base 2 of the specified value,
    460 /// -1 if the value is zero. (64 bit edition.)
    461 inline unsigned Log2_64(uint64_t Value) {
    462   return 63 - countLeadingZeros(Value);
    463 }
    464 
    465 /// Log2_32_Ceil - This function returns the ceil log base 2 of the specified
    466 /// value, 32 if the value is zero. (32 bit edition).
    467 /// Ex. Log2_32_Ceil(32) == 5, Log2_32_Ceil(1) == 0, Log2_32_Ceil(6) == 3
    468 inline unsigned Log2_32_Ceil(uint32_t Value) {
    469   return 32 - countLeadingZeros(Value - 1);
    470 }
    471 
    472 /// Log2_64_Ceil - This function returns the ceil log base 2 of the specified
    473 /// value, 64 if the value is zero. (64 bit edition.)
    474 inline unsigned Log2_64_Ceil(uint64_t Value) {
    475   return 64 - countLeadingZeros(Value - 1);
    476 }
    477 
    478 /// GreatestCommonDivisor64 - Return the greatest common divisor of the two
    479 /// values using Euclid's algorithm.
    480 inline uint64_t GreatestCommonDivisor64(uint64_t A, uint64_t B) {
    481   while (B) {
    482     uint64_t T = B;
    483     B = A % B;
    484     A = T;
    485   }
    486   return A;
    487 }
    488 
    489 /// BitsToDouble - This function takes a 64-bit integer and returns the bit
    490 /// equivalent double.
    491 inline double BitsToDouble(uint64_t Bits) {
    492   union {
    493     uint64_t L;
    494     double D;
    495   } T;
    496   T.L = Bits;
    497   return T.D;
    498 }
    499 
    500 /// BitsToFloat - This function takes a 32-bit integer and returns the bit
    501 /// equivalent float.
    502 inline float BitsToFloat(uint32_t Bits) {
    503   union {
    504     uint32_t I;
    505     float F;
    506   } T;
    507   T.I = Bits;
    508   return T.F;
    509 }
    510 
    511 /// DoubleToBits - This function takes a double and returns the bit
    512 /// equivalent 64-bit integer.  Note that copying doubles around
    513 /// changes the bits of NaNs on some hosts, notably x86, so this
    514 /// routine cannot be used if these bits are needed.
    515 inline uint64_t DoubleToBits(double Double) {
    516   union {
    517     uint64_t L;
    518     double D;
    519   } T;
    520   T.D = Double;
    521   return T.L;
    522 }
    523 
    524 /// FloatToBits - This function takes a float and returns the bit
    525 /// equivalent 32-bit integer.  Note that copying floats around
    526 /// changes the bits of NaNs on some hosts, notably x86, so this
    527 /// routine cannot be used if these bits are needed.
    528 inline uint32_t FloatToBits(float Float) {
    529   union {
    530     uint32_t I;
    531     float F;
    532   } T;
    533   T.F = Float;
    534   return T.I;
    535 }
    536 
    537 /// MinAlign - A and B are either alignments or offsets.  Return the minimum
    538 /// alignment that may be assumed after adding the two together.
    539 inline uint64_t MinAlign(uint64_t A, uint64_t B) {
    540   // The largest power of 2 that divides both A and B.
    541   //
    542   // Replace "-Value" by "1+~Value" in the following commented code to avoid
    543   // MSVC warning C4146
    544   //    return (A | B) & -(A | B);
    545   return (A | B) & (1 + ~(A | B));
    546 }
    547 
    548 /// \brief Aligns \c Addr to \c Alignment bytes, rounding up.
    549 ///
    550 /// Alignment should be a power of two.  This method rounds up, so
    551 /// alignAddr(7, 4) == 8 and alignAddr(8, 4) == 8.
    552 inline uintptr_t alignAddr(void *Addr, size_t Alignment) {
    553   assert(Alignment && isPowerOf2_64((uint64_t)Alignment) &&
    554          "Alignment is not a power of two!");
    555 
    556   assert((uintptr_t)Addr + Alignment - 1 >= (uintptr_t)Addr);
    557 
    558   return (((uintptr_t)Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1));
    559 }
    560 
    561 /// \brief Returns the necessary adjustment for aligning \c Ptr to \c Alignment
    562 /// bytes, rounding up.
    563 inline size_t alignmentAdjustment(void *Ptr, size_t Alignment) {
    564   return alignAddr(Ptr, Alignment) - (uintptr_t)Ptr;
    565 }
    566 
    567 /// NextPowerOf2 - Returns the next power of two (in 64-bits)
    568 /// that is strictly greater than A.  Returns zero on overflow.
    569 inline uint64_t NextPowerOf2(uint64_t A) {
    570   A |= (A >> 1);
    571   A |= (A >> 2);
    572   A |= (A >> 4);
    573   A |= (A >> 8);
    574   A |= (A >> 16);
    575   A |= (A >> 32);
    576   return A + 1;
    577 }
    578 
    579 /// Returns the power of two which is less than or equal to the given value.
    580 /// Essentially, it is a floor operation across the domain of powers of two.
    581 inline uint64_t PowerOf2Floor(uint64_t A) {
    582   if (!A) return 0;
    583   return 1ull << (63 - countLeadingZeros(A, ZB_Undefined));
    584 }
    585 
    586 /// Returns the next integer (mod 2**64) that is greater than or equal to
    587 /// \p Value and is a multiple of \p Align. \p Align must be non-zero.
    588 ///
    589 /// Examples:
    590 /// \code
    591 ///   RoundUpToAlignment(5, 8) = 8
    592 ///   RoundUpToAlignment(17, 8) = 24
    593 ///   RoundUpToAlignment(~0LL, 8) = 0
    594 ///   RoundUpToAlignment(321, 255) = 510
    595 /// \endcode
    596 inline uint64_t RoundUpToAlignment(uint64_t Value, uint64_t Align) {
    597   return (Value + Align - 1) / Align * Align;
    598 }
    599 
    600 /// Returns the offset to the next integer (mod 2**64) that is greater than
    601 /// or equal to \p Value and is a multiple of \p Align. \p Align must be
    602 /// non-zero.
    603 inline uint64_t OffsetToAlignment(uint64_t Value, uint64_t Align) {
    604   return RoundUpToAlignment(Value, Align) - Value;
    605 }
    606 
    607 /// SignExtend32 - Sign extend B-bit number x to 32-bit int.
    608 /// Usage int32_t r = SignExtend32<5>(x);
    609 template <unsigned B> inline int32_t SignExtend32(uint32_t x) {
    610   return int32_t(x << (32 - B)) >> (32 - B);
    611 }
    612 
    613 /// \brief Sign extend number in the bottom B bits of X to a 32-bit int.
    614 /// Requires 0 < B <= 32.
    615 inline int32_t SignExtend32(uint32_t X, unsigned B) {
    616   return int32_t(X << (32 - B)) >> (32 - B);
    617 }
    618 
    619 /// SignExtend64 - Sign extend B-bit number x to 64-bit int.
    620 /// Usage int64_t r = SignExtend64<5>(x);
    621 template <unsigned B> inline int64_t SignExtend64(uint64_t x) {
    622   return int64_t(x << (64 - B)) >> (64 - B);
    623 }
    624 
    625 /// \brief Sign extend number in the bottom B bits of X to a 64-bit int.
    626 /// Requires 0 < B <= 64.
    627 inline int64_t SignExtend64(uint64_t X, unsigned B) {
    628   return int64_t(X << (64 - B)) >> (64 - B);
    629 }
    630 
    631 extern const float huge_valf;
    632 } // End llvm namespace
    633 
    634 #endif
    635