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