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