MathExtras: Parametrize count(Trailing|Leading)Zeros on the type size.
[oota-llvm.git] / include / llvm / Support / MathExtras.h
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 sequence of ones
322 /// starting at the least significant bit with the remainder zero (32 bit
323 /// 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 sequence of ones
329 /// starting at the least significant bit with the remainder zero (64 bit
330 /// 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 /// 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 isMask_32((Value - 1) | Value);
340 }
341
342 /// isShiftedMask_64 - This function returns true if the argument contains a
343 /// sequence of ones with the remainder zero (64 bit version.)
344 inline bool isShiftedMask_64(uint64_t Value) {
345   return 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 /// CountLeadingOnes_32 - this function performs the operation of
379 /// counting the number of ones from the most significant bit to the first zero
380 /// bit.  Ex. CountLeadingOnes_32(0xFF0FFF00) == 8.
381 /// Returns 32 if the word is all ones.
382 inline unsigned CountLeadingOnes_32(uint32_t Value) {
383   return countLeadingZeros(~Value);
384 }
385
386 /// CountLeadingOnes_64 - This function performs the operation
387 /// of counting the number of ones from the most significant bit to the first
388 /// zero bit (64 bit edition.)
389 /// Returns 64 if the word is all ones.
390 inline unsigned CountLeadingOnes_64(uint64_t Value) {
391   return countLeadingZeros(~Value);
392 }
393
394 /// CountTrailingOnes_32 - this function performs the operation of
395 /// counting the number of ones from the least significant bit to the first zero
396 /// bit.  Ex. CountTrailingOnes_32(0x00FF00FF) == 8.
397 /// Returns 32 if the word is all ones.
398 inline unsigned CountTrailingOnes_32(uint32_t Value) {
399   return countTrailingZeros(~Value);
400 }
401
402 /// CountTrailingOnes_64 - This function performs the operation
403 /// of counting the number of ones from the least significant bit to the first
404 /// zero bit (64 bit edition.)
405 /// Returns 64 if the word is all ones.
406 inline unsigned CountTrailingOnes_64(uint64_t Value) {
407   return countTrailingZeros(~Value);
408 }
409
410 /// CountPopulation_32 - this function counts the number of set bits in a value.
411 /// Ex. CountPopulation(0xF000F000) = 8
412 /// Returns 0 if the word is zero.
413 inline unsigned CountPopulation_32(uint32_t Value) {
414 #if __GNUC__ >= 4
415   return __builtin_popcount(Value);
416 #else
417   uint32_t v = Value - ((Value >> 1) & 0x55555555);
418   v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
419   return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
420 #endif
421 }
422
423 /// CountPopulation_64 - this function counts the number of set bits in a value,
424 /// (64 bit edition.)
425 inline unsigned CountPopulation_64(uint64_t Value) {
426 #if __GNUC__ >= 4
427   return __builtin_popcountll(Value);
428 #else
429   uint64_t v = Value - ((Value >> 1) & 0x5555555555555555ULL);
430   v = (v & 0x3333333333333333ULL) + ((v >> 2) & 0x3333333333333333ULL);
431   v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0FULL;
432   return unsigned((uint64_t)(v * 0x0101010101010101ULL) >> 56);
433 #endif
434 }
435
436 /// Log2_32 - This function returns the floor log base 2 of the specified value,
437 /// -1 if the value is zero. (32 bit edition.)
438 /// Ex. Log2_32(32) == 5, Log2_32(1) == 0, Log2_32(0) == -1, Log2_32(6) == 2
439 inline unsigned Log2_32(uint32_t Value) {
440   return 31 - countLeadingZeros(Value);
441 }
442
443 /// Log2_64 - This function returns the floor log base 2 of the specified value,
444 /// -1 if the value is zero. (64 bit edition.)
445 inline unsigned Log2_64(uint64_t Value) {
446   return 63 - countLeadingZeros(Value);
447 }
448
449 /// Log2_32_Ceil - This function returns the ceil log base 2 of the specified
450 /// value, 32 if the value is zero. (32 bit edition).
451 /// Ex. Log2_32_Ceil(32) == 5, Log2_32_Ceil(1) == 0, Log2_32_Ceil(6) == 3
452 inline unsigned Log2_32_Ceil(uint32_t Value) {
453   return 32 - countLeadingZeros(Value - 1);
454 }
455
456 /// Log2_64_Ceil - This function returns the ceil log base 2 of the specified
457 /// value, 64 if the value is zero. (64 bit edition.)
458 inline unsigned Log2_64_Ceil(uint64_t Value) {
459   return 64 - countLeadingZeros(Value - 1);
460 }
461
462 /// GreatestCommonDivisor64 - Return the greatest common divisor of the two
463 /// values using Euclid's algorithm.
464 inline uint64_t GreatestCommonDivisor64(uint64_t A, uint64_t B) {
465   while (B) {
466     uint64_t T = B;
467     B = A % B;
468     A = T;
469   }
470   return A;
471 }
472
473 /// BitsToDouble - This function takes a 64-bit integer and returns the bit
474 /// equivalent double.
475 inline double BitsToDouble(uint64_t Bits) {
476   union {
477     uint64_t L;
478     double D;
479   } T;
480   T.L = Bits;
481   return T.D;
482 }
483
484 /// BitsToFloat - This function takes a 32-bit integer and returns the bit
485 /// equivalent float.
486 inline float BitsToFloat(uint32_t Bits) {
487   union {
488     uint32_t I;
489     float F;
490   } T;
491   T.I = Bits;
492   return T.F;
493 }
494
495 /// DoubleToBits - This function takes a double and returns the bit
496 /// equivalent 64-bit integer.  Note that copying doubles around
497 /// changes the bits of NaNs on some hosts, notably x86, so this
498 /// routine cannot be used if these bits are needed.
499 inline uint64_t DoubleToBits(double Double) {
500   union {
501     uint64_t L;
502     double D;
503   } T;
504   T.D = Double;
505   return T.L;
506 }
507
508 /// FloatToBits - This function takes a float and returns the bit
509 /// equivalent 32-bit integer.  Note that copying floats around
510 /// changes the bits of NaNs on some hosts, notably x86, so this
511 /// routine cannot be used if these bits are needed.
512 inline uint32_t FloatToBits(float Float) {
513   union {
514     uint32_t I;
515     float F;
516   } T;
517   T.F = Float;
518   return T.I;
519 }
520
521 /// Platform-independent wrappers for the C99 isnan() function.
522 int IsNAN(float f);
523 int IsNAN(double d);
524
525 /// Platform-independent wrappers for the C99 isinf() function.
526 int IsInf(float f);
527 int IsInf(double d);
528
529 /// MinAlign - A and B are either alignments or offsets.  Return the minimum
530 /// alignment that may be assumed after adding the two together.
531 inline uint64_t MinAlign(uint64_t A, uint64_t B) {
532   // The largest power of 2 that divides both A and B.
533   //
534   // Replace "-Value" by "1+~Value" in the following commented code to avoid 
535   // MSVC warning C4146
536   //    return (A | B) & -(A | B);
537   return (A | B) & (1 + ~(A | B));
538 }
539
540 /// \brief Aligns \c Addr to \c Alignment bytes, rounding up.
541 ///
542 /// Alignment should be a power of two.  This method rounds up, so
543 /// alignAddr(7, 4) == 8 and alignAddr(8, 4) == 8.
544 inline uintptr_t alignAddr(void *Addr, size_t Alignment) {
545   assert(Alignment && isPowerOf2_64((uint64_t)Alignment) &&
546          "Alignment is not a power of two!");
547
548   assert((uintptr_t)Addr + Alignment - 1 >= (uintptr_t)Addr);
549
550   return (((uintptr_t)Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1));
551 }
552
553 /// \brief Returns the necessary adjustment for aligning \c Ptr to \c Alignment
554 /// bytes, rounding up.
555 inline size_t alignmentAdjustment(void *Ptr, size_t Alignment) {
556   return alignAddr(Ptr, Alignment) - (uintptr_t)Ptr;
557 }
558
559 /// NextPowerOf2 - Returns the next power of two (in 64-bits)
560 /// that is strictly greater than A.  Returns zero on overflow.
561 inline uint64_t NextPowerOf2(uint64_t A) {
562   A |= (A >> 1);
563   A |= (A >> 2);
564   A |= (A >> 4);
565   A |= (A >> 8);
566   A |= (A >> 16);
567   A |= (A >> 32);
568   return A + 1;
569 }
570
571 /// Returns the power of two which is less than or equal to the given value.
572 /// Essentially, it is a floor operation across the domain of powers of two.
573 inline uint64_t PowerOf2Floor(uint64_t A) {
574   if (!A) return 0;
575   return 1ull << (63 - countLeadingZeros(A, ZB_Undefined));
576 }
577
578 /// Returns the next integer (mod 2**64) that is greater than or equal to
579 /// \p Value and is a multiple of \p Align. \p Align must be non-zero.
580 ///
581 /// Examples:
582 /// \code
583 ///   RoundUpToAlignment(5, 8) = 8
584 ///   RoundUpToAlignment(17, 8) = 24
585 ///   RoundUpToAlignment(~0LL, 8) = 0
586 ///   RoundUpToAlignment(321, 255) = 510
587 /// \endcode
588 inline uint64_t RoundUpToAlignment(uint64_t Value, uint64_t Align) {
589   return (Value + Align - 1) / Align * Align;
590 }
591
592 /// Returns the offset to the next integer (mod 2**64) that is greater than
593 /// or equal to \p Value and is a multiple of \p Align. \p Align must be
594 /// non-zero.
595 inline uint64_t OffsetToAlignment(uint64_t Value, uint64_t Align) {
596   return RoundUpToAlignment(Value, Align) - Value;
597 }
598
599 /// abs64 - absolute value of a 64-bit int.  Not all environments support
600 /// "abs" on whatever their name for the 64-bit int type is.  The absolute
601 /// value of the largest negative number is undefined, as with "abs".
602 inline int64_t abs64(int64_t x) {
603   return (x < 0) ? -x : x;
604 }
605
606 /// SignExtend32 - Sign extend B-bit number x to 32-bit int.
607 /// Usage int32_t r = SignExtend32<5>(x);
608 template <unsigned B> inline int32_t SignExtend32(uint32_t x) {
609   return int32_t(x << (32 - B)) >> (32 - B);
610 }
611
612 /// \brief Sign extend number in the bottom B bits of X to a 32-bit int.
613 /// Requires 0 < B <= 32.
614 inline int32_t SignExtend32(uint32_t X, unsigned B) {
615   return int32_t(X << (32 - B)) >> (32 - B);
616 }
617
618 /// SignExtend64 - Sign extend B-bit number x to 64-bit int.
619 /// Usage int64_t r = SignExtend64<5>(x);
620 template <unsigned B> inline int64_t SignExtend64(uint64_t x) {
621   return int64_t(x << (64 - B)) >> (64 - B);
622 }
623
624 /// \brief Sign extend number in the bottom B bits of X to a 64-bit int.
625 /// Requires 0 < B <= 64.
626 inline int64_t SignExtend64(uint64_t X, unsigned B) {
627   return int64_t(X << (64 - B)) >> (64 - B);
628 }
629
630 extern const float huge_valf;
631 } // End llvm namespace
632
633 #endif