MathExtras.h revision 261991
1193323Sed//===-- llvm/Support/MathExtras.h - Useful math functions -------*- C++ -*-===//
2193323Sed//
3193323Sed//                     The LLVM Compiler Infrastructure
4193323Sed//
5193323Sed// This file is distributed under the University of Illinois Open Source
6193323Sed// License. See LICENSE.TXT for details.
7193323Sed//
8193323Sed//===----------------------------------------------------------------------===//
9193323Sed//
10193323Sed// This file contains some functions that are useful for math stuff.
11193323Sed//
12193323Sed//===----------------------------------------------------------------------===//
13193323Sed
14193323Sed#ifndef LLVM_SUPPORT_MATHEXTRAS_H
15193323Sed#define LLVM_SUPPORT_MATHEXTRAS_H
16193323Sed
17261991Sdim#include "llvm/Support/Compiler.h"
18218893Sdim#include "llvm/Support/SwapByteOrder.h"
19261991Sdim#include "llvm/Support/type_traits.h"
20193323Sed
21261991Sdim#include <cstring>
22261991Sdim
23249423Sdim#ifdef _MSC_VER
24261991Sdim#include <intrin.h>
25261991Sdim#include <limits>
26249423Sdim#endif
27249423Sdim
28193323Sednamespace llvm {
29261991Sdim/// \brief The behavior an operation has on an input of 0.
30261991Sdimenum ZeroBehavior {
31261991Sdim  /// \brief The returned value is undefined.
32261991Sdim  ZB_Undefined,
33261991Sdim  /// \brief The returned value is numeric_limits<T>::max()
34261991Sdim  ZB_Max,
35261991Sdim  /// \brief The returned value is numeric_limits<T>::digits
36261991Sdim  ZB_Width
37261991Sdim};
38193323Sed
39261991Sdim/// \brief Count number of 0's from the least significant bit to the most
40261991Sdim///   stopping at the first 1.
41261991Sdim///
42261991Sdim/// Only unsigned integral types are allowed.
43261991Sdim///
44261991Sdim/// \param ZB the behavior on an input of 0. Only ZB_Width and ZB_Undefined are
45261991Sdim///   valid arguments.
46261991Sdimtemplate <typename T>
47261991Sdimtypename enable_if_c<std::numeric_limits<T>::is_integer &&
48261991Sdim                     !std::numeric_limits<T>::is_signed, std::size_t>::type
49261991SdimcountTrailingZeros(T Val, ZeroBehavior ZB = ZB_Width) {
50261991Sdim  (void)ZB;
51261991Sdim
52261991Sdim  if (!Val)
53261991Sdim    return std::numeric_limits<T>::digits;
54261991Sdim  if (Val & 0x1)
55261991Sdim    return 0;
56261991Sdim
57261991Sdim  // Bisection method.
58261991Sdim  std::size_t ZeroBits = 0;
59261991Sdim  T Shift = std::numeric_limits<T>::digits >> 1;
60261991Sdim  T Mask = std::numeric_limits<T>::max() >> Shift;
61261991Sdim  while (Shift) {
62261991Sdim    if ((Val & Mask) == 0) {
63261991Sdim      Val >>= Shift;
64261991Sdim      ZeroBits |= Shift;
65261991Sdim    }
66261991Sdim    Shift >>= 1;
67261991Sdim    Mask >>= Shift;
68261991Sdim  }
69261991Sdim  return ZeroBits;
70261991Sdim}
71261991Sdim
72261991Sdim// Disable signed.
73261991Sdimtemplate <typename T>
74261991Sdimtypename enable_if_c<std::numeric_limits<T>::is_integer &&
75261991Sdim                     std::numeric_limits<T>::is_signed, std::size_t>::type
76261991SdimcountTrailingZeros(T Val, ZeroBehavior ZB = ZB_Width) LLVM_DELETED_FUNCTION;
77261991Sdim
78261991Sdim#if __GNUC__ >= 4 || _MSC_VER
79261991Sdimtemplate <>
80261991Sdiminline std::size_t countTrailingZeros<uint32_t>(uint32_t Val, ZeroBehavior ZB) {
81261991Sdim  if (ZB != ZB_Undefined && Val == 0)
82261991Sdim    return 32;
83261991Sdim
84261991Sdim#if __has_builtin(__builtin_ctz) || __GNUC_PREREQ(4, 0)
85261991Sdim  return __builtin_ctz(Val);
86261991Sdim#elif _MSC_VER
87261991Sdim  unsigned long Index;
88261991Sdim  _BitScanForward(&Index, Val);
89261991Sdim  return Index;
90261991Sdim#endif
91261991Sdim}
92261991Sdim
93261991Sdim#if !defined(_MSC_VER) || defined(_M_X64)
94261991Sdimtemplate <>
95261991Sdiminline std::size_t countTrailingZeros<uint64_t>(uint64_t Val, ZeroBehavior ZB) {
96261991Sdim  if (ZB != ZB_Undefined && Val == 0)
97261991Sdim    return 64;
98261991Sdim
99261991Sdim#if __has_builtin(__builtin_ctzll) || __GNUC_PREREQ(4, 0)
100261991Sdim  return __builtin_ctzll(Val);
101261991Sdim#elif _MSC_VER
102261991Sdim  unsigned long Index;
103261991Sdim  _BitScanForward64(&Index, Val);
104261991Sdim  return Index;
105261991Sdim#endif
106261991Sdim}
107261991Sdim#endif
108261991Sdim#endif
109261991Sdim
110261991Sdim/// \brief Count number of 0's from the most significant bit to the least
111261991Sdim///   stopping at the first 1.
112261991Sdim///
113261991Sdim/// Only unsigned integral types are allowed.
114261991Sdim///
115261991Sdim/// \param ZB the behavior on an input of 0. Only ZB_Width and ZB_Undefined are
116261991Sdim///   valid arguments.
117261991Sdimtemplate <typename T>
118261991Sdimtypename enable_if_c<std::numeric_limits<T>::is_integer &&
119261991Sdim                     !std::numeric_limits<T>::is_signed, std::size_t>::type
120261991SdimcountLeadingZeros(T Val, ZeroBehavior ZB = ZB_Width) {
121261991Sdim  (void)ZB;
122261991Sdim
123261991Sdim  if (!Val)
124261991Sdim    return std::numeric_limits<T>::digits;
125261991Sdim
126261991Sdim  // Bisection method.
127261991Sdim  std::size_t ZeroBits = 0;
128261991Sdim  for (T Shift = std::numeric_limits<T>::digits >> 1; Shift; Shift >>= 1) {
129261991Sdim    T Tmp = Val >> Shift;
130261991Sdim    if (Tmp)
131261991Sdim      Val = Tmp;
132261991Sdim    else
133261991Sdim      ZeroBits |= Shift;
134261991Sdim  }
135261991Sdim  return ZeroBits;
136261991Sdim}
137261991Sdim
138261991Sdim// Disable signed.
139261991Sdimtemplate <typename T>
140261991Sdimtypename enable_if_c<std::numeric_limits<T>::is_integer &&
141261991Sdim                     std::numeric_limits<T>::is_signed, std::size_t>::type
142261991SdimcountLeadingZeros(T Val, ZeroBehavior ZB = ZB_Width) LLVM_DELETED_FUNCTION;
143261991Sdim
144261991Sdim#if __GNUC__ >= 4 || _MSC_VER
145261991Sdimtemplate <>
146261991Sdiminline std::size_t countLeadingZeros<uint32_t>(uint32_t Val, ZeroBehavior ZB) {
147261991Sdim  if (ZB != ZB_Undefined && Val == 0)
148261991Sdim    return 32;
149261991Sdim
150261991Sdim#if __has_builtin(__builtin_clz) || __GNUC_PREREQ(4, 0)
151261991Sdim  return __builtin_clz(Val);
152261991Sdim#elif _MSC_VER
153261991Sdim  unsigned long Index;
154261991Sdim  _BitScanReverse(&Index, Val);
155261991Sdim  return Index ^ 31;
156261991Sdim#endif
157261991Sdim}
158261991Sdim
159261991Sdim#if !defined(_MSC_VER) || defined(_M_X64)
160261991Sdimtemplate <>
161261991Sdiminline std::size_t countLeadingZeros<uint64_t>(uint64_t Val, ZeroBehavior ZB) {
162261991Sdim  if (ZB != ZB_Undefined && Val == 0)
163261991Sdim    return 64;
164261991Sdim
165261991Sdim#if __has_builtin(__builtin_clzll) || __GNUC_PREREQ(4, 0)
166261991Sdim  return __builtin_clzll(Val);
167261991Sdim#elif _MSC_VER
168261991Sdim  unsigned long Index;
169261991Sdim  _BitScanReverse64(&Index, Val);
170261991Sdim  return Index ^ 63;
171261991Sdim#endif
172261991Sdim}
173261991Sdim#endif
174261991Sdim#endif
175261991Sdim
176261991Sdim/// \brief Get the index of the first set bit starting from the least
177261991Sdim///   significant bit.
178261991Sdim///
179261991Sdim/// Only unsigned integral types are allowed.
180261991Sdim///
181261991Sdim/// \param ZB the behavior on an input of 0. Only ZB_Max and ZB_Undefined are
182261991Sdim///   valid arguments.
183261991Sdimtemplate <typename T>
184261991Sdimtypename enable_if_c<std::numeric_limits<T>::is_integer &&
185261991Sdim                     !std::numeric_limits<T>::is_signed, T>::type
186261991SdimfindFirstSet(T Val, ZeroBehavior ZB = ZB_Max) {
187261991Sdim  if (ZB == ZB_Max && Val == 0)
188261991Sdim    return std::numeric_limits<T>::max();
189261991Sdim
190261991Sdim  return countTrailingZeros(Val, ZB_Undefined);
191261991Sdim}
192261991Sdim
193261991Sdim// Disable signed.
194261991Sdimtemplate <typename T>
195261991Sdimtypename enable_if_c<std::numeric_limits<T>::is_integer &&
196261991Sdim                     std::numeric_limits<T>::is_signed, T>::type
197261991SdimfindFirstSet(T Val, ZeroBehavior ZB = ZB_Max) LLVM_DELETED_FUNCTION;
198261991Sdim
199261991Sdim/// \brief Get the index of the last set bit starting from the least
200261991Sdim///   significant bit.
201261991Sdim///
202261991Sdim/// Only unsigned integral types are allowed.
203261991Sdim///
204261991Sdim/// \param ZB the behavior on an input of 0. Only ZB_Max and ZB_Undefined are
205261991Sdim///   valid arguments.
206261991Sdimtemplate <typename T>
207261991Sdimtypename enable_if_c<std::numeric_limits<T>::is_integer &&
208261991Sdim                     !std::numeric_limits<T>::is_signed, T>::type
209261991SdimfindLastSet(T Val, ZeroBehavior ZB = ZB_Max) {
210261991Sdim  if (ZB == ZB_Max && Val == 0)
211261991Sdim    return std::numeric_limits<T>::max();
212261991Sdim
213261991Sdim  // Use ^ instead of - because both gcc and llvm can remove the associated ^
214261991Sdim  // in the __builtin_clz intrinsic on x86.
215261991Sdim  return countLeadingZeros(Val, ZB_Undefined) ^
216261991Sdim         (std::numeric_limits<T>::digits - 1);
217261991Sdim}
218261991Sdim
219261991Sdim// Disable signed.
220261991Sdimtemplate <typename T>
221261991Sdimtypename enable_if_c<std::numeric_limits<T>::is_integer &&
222261991Sdim                     std::numeric_limits<T>::is_signed, T>::type
223261991SdimfindLastSet(T Val, ZeroBehavior ZB = ZB_Max) LLVM_DELETED_FUNCTION;
224261991Sdim
225261991Sdim/// \brief Macro compressed bit reversal table for 256 bits.
226261991Sdim///
227261991Sdim/// http://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable
228261991Sdimstatic const unsigned char BitReverseTable256[256] = {
229261991Sdim#define R2(n) n, n + 2 * 64, n + 1 * 64, n + 3 * 64
230261991Sdim#define R4(n) R2(n), R2(n + 2 * 16), R2(n + 1 * 16), R2(n + 3 * 16)
231261991Sdim#define R6(n) R4(n), R4(n + 2 * 4), R4(n + 1 * 4), R4(n + 3 * 4)
232261991Sdim  R6(0), R6(2), R6(1), R6(3)
233261991Sdim};
234261991Sdim
235261991Sdim/// \brief Reverse the bits in \p Val.
236261991Sdimtemplate <typename T>
237261991SdimT reverseBits(T Val) {
238261991Sdim  unsigned char in[sizeof(Val)];
239261991Sdim  unsigned char out[sizeof(Val)];
240261991Sdim  std::memcpy(in, &Val, sizeof(Val));
241261991Sdim  for (unsigned i = 0; i < sizeof(Val); ++i)
242261991Sdim    out[(sizeof(Val) - i) - 1] = BitReverseTable256[in[i]];
243261991Sdim  std::memcpy(&Val, out, sizeof(Val));
244261991Sdim  return Val;
245261991Sdim}
246261991Sdim
247193323Sed// NOTE: The following support functions use the _32/_64 extensions instead of
248193323Sed// type overloading so that signed and unsigned integers can be used without
249193323Sed// ambiguity.
250193323Sed
251193323Sed/// Hi_32 - This function returns the high 32 bits of a 64 bit value.
252193323Sedinline uint32_t Hi_32(uint64_t Value) {
253193323Sed  return static_cast<uint32_t>(Value >> 32);
254193323Sed}
255193323Sed
256193323Sed/// Lo_32 - This function returns the low 32 bits of a 64 bit value.
257193323Sedinline uint32_t Lo_32(uint64_t Value) {
258193323Sed  return static_cast<uint32_t>(Value);
259193323Sed}
260193323Sed
261206083Srdivacky/// isInt - Checks if an integer fits into the given bit width.
262206083Srdivackytemplate<unsigned N>
263206083Srdivackyinline bool isInt(int64_t x) {
264206083Srdivacky  return N >= 64 || (-(INT64_C(1)<<(N-1)) <= x && x < (INT64_C(1)<<(N-1)));
265193323Sed}
266206083Srdivacky// Template specializations to get better code for common cases.
267206083Srdivackytemplate<>
268206083Srdivackyinline bool isInt<8>(int64_t x) {
269206083Srdivacky  return static_cast<int8_t>(x) == x;
270193323Sed}
271206083Srdivackytemplate<>
272206083Srdivackyinline bool isInt<16>(int64_t x) {
273206083Srdivacky  return static_cast<int16_t>(x) == x;
274193323Sed}
275206083Srdivackytemplate<>
276206083Srdivackyinline bool isInt<32>(int64_t x) {
277206083Srdivacky  return static_cast<int32_t>(x) == x;
278193323Sed}
279193323Sed
280234353Sdim/// isShiftedInt<N,S> - Checks if a signed integer is an N bit number shifted
281234353Sdim///                     left by S.
282234353Sdimtemplate<unsigned N, unsigned S>
283234353Sdiminline bool isShiftedInt(int64_t x) {
284234353Sdim  return isInt<N+S>(x) && (x % (1<<S) == 0);
285234353Sdim}
286234353Sdim
287206083Srdivacky/// isUInt - Checks if an unsigned integer fits into the given bit width.
288198090Srdivackytemplate<unsigned N>
289206083Srdivackyinline bool isUInt(uint64_t x) {
290249423Sdim  return N >= 64 || x < (UINT64_C(1)<<(N));
291198090Srdivacky}
292206083Srdivacky// Template specializations to get better code for common cases.
293206083Srdivackytemplate<>
294206083Srdivackyinline bool isUInt<8>(uint64_t x) {
295206083Srdivacky  return static_cast<uint8_t>(x) == x;
296206083Srdivacky}
297206083Srdivackytemplate<>
298206083Srdivackyinline bool isUInt<16>(uint64_t x) {
299206083Srdivacky  return static_cast<uint16_t>(x) == x;
300206083Srdivacky}
301206083Srdivackytemplate<>
302206083Srdivackyinline bool isUInt<32>(uint64_t x) {
303206083Srdivacky  return static_cast<uint32_t>(x) == x;
304206083Srdivacky}
305198090Srdivacky
306234353Sdim/// isShiftedUInt<N,S> - Checks if a unsigned integer is an N bit number shifted
307234353Sdim///                     left by S.
308234353Sdimtemplate<unsigned N, unsigned S>
309234353Sdiminline bool isShiftedUInt(uint64_t x) {
310234353Sdim  return isUInt<N+S>(x) && (x % (1<<S) == 0);
311234353Sdim}
312234353Sdim
313218893Sdim/// isUIntN - Checks if an unsigned integer fits into the given (dynamic)
314218893Sdim/// bit width.
315218893Sdiminline bool isUIntN(unsigned N, uint64_t x) {
316218893Sdim  return x == (x & (~0ULL >> (64 - N)));
317218893Sdim}
318218893Sdim
319218893Sdim/// isIntN - Checks if an signed integer fits into the given (dynamic)
320218893Sdim/// bit width.
321218893Sdiminline bool isIntN(unsigned N, int64_t x) {
322218893Sdim  return N >= 64 || (-(INT64_C(1)<<(N-1)) <= x && x < (INT64_C(1)<<(N-1)));
323218893Sdim}
324218893Sdim
325193323Sed/// isMask_32 - This function returns true if the argument is a sequence of ones
326193323Sed/// starting at the least significant bit with the remainder zero (32 bit
327193323Sed/// version).   Ex. isMask_32(0x0000FFFFU) == true.
328193323Sedinline bool isMask_32(uint32_t Value) {
329193323Sed  return Value && ((Value + 1) & Value) == 0;
330193323Sed}
331193323Sed
332193323Sed/// isMask_64 - This function returns true if the argument is a sequence of ones
333193323Sed/// starting at the least significant bit with the remainder zero (64 bit
334193323Sed/// version).
335193323Sedinline bool isMask_64(uint64_t Value) {
336193323Sed  return Value && ((Value + 1) & Value) == 0;
337193323Sed}
338193323Sed
339193323Sed/// isShiftedMask_32 - This function returns true if the argument contains a
340193323Sed/// sequence of ones with the remainder zero (32 bit version.)
341193323Sed/// Ex. isShiftedMask_32(0x0000FF00U) == true.
342193323Sedinline bool isShiftedMask_32(uint32_t Value) {
343193323Sed  return isMask_32((Value - 1) | Value);
344193323Sed}
345193323Sed
346193323Sed/// isShiftedMask_64 - This function returns true if the argument contains a
347193323Sed/// sequence of ones with the remainder zero (64 bit version.)
348193323Sedinline bool isShiftedMask_64(uint64_t Value) {
349193323Sed  return isMask_64((Value - 1) | Value);
350193323Sed}
351193323Sed
352193323Sed/// isPowerOf2_32 - This function returns true if the argument is a power of
353193323Sed/// two > 0. Ex. isPowerOf2_32(0x00100000U) == true (32 bit edition.)
354193323Sedinline bool isPowerOf2_32(uint32_t Value) {
355193323Sed  return Value && !(Value & (Value - 1));
356193323Sed}
357193323Sed
358193323Sed/// isPowerOf2_64 - This function returns true if the argument is a power of two
359193323Sed/// > 0 (64 bit edition.)
360193323Sedinline bool isPowerOf2_64(uint64_t Value) {
361193323Sed  return Value && !(Value & (Value - int64_t(1L)));
362193323Sed}
363193323Sed
364193323Sed/// ByteSwap_16 - This function returns a byte-swapped representation of the
365193323Sed/// 16-bit argument, Value.
366193323Sedinline uint16_t ByteSwap_16(uint16_t Value) {
367218893Sdim  return sys::SwapByteOrder_16(Value);
368193323Sed}
369193323Sed
370193323Sed/// ByteSwap_32 - This function returns a byte-swapped representation of the
371193323Sed/// 32-bit argument, Value.
372193323Sedinline uint32_t ByteSwap_32(uint32_t Value) {
373218893Sdim  return sys::SwapByteOrder_32(Value);
374193323Sed}
375193323Sed
376193323Sed/// ByteSwap_64 - This function returns a byte-swapped representation of the
377193323Sed/// 64-bit argument, Value.
378193323Sedinline uint64_t ByteSwap_64(uint64_t Value) {
379218893Sdim  return sys::SwapByteOrder_64(Value);
380193323Sed}
381193323Sed
382193323Sed/// CountLeadingOnes_32 - this function performs the operation of
383193323Sed/// counting the number of ones from the most significant bit to the first zero
384193323Sed/// bit.  Ex. CountLeadingOnes_32(0xFF0FFF00) == 8.
385193323Sed/// Returns 32 if the word is all ones.
386193323Sedinline unsigned CountLeadingOnes_32(uint32_t Value) {
387261991Sdim  return countLeadingZeros(~Value);
388193323Sed}
389193323Sed
390193323Sed/// CountLeadingOnes_64 - This function performs the operation
391193323Sed/// of counting the number of ones from the most significant bit to the first
392193323Sed/// zero bit (64 bit edition.)
393193323Sed/// Returns 64 if the word is all ones.
394193323Sedinline unsigned CountLeadingOnes_64(uint64_t Value) {
395261991Sdim  return countLeadingZeros(~Value);
396193323Sed}
397193323Sed
398193323Sed/// CountTrailingOnes_32 - this function performs the operation of
399193323Sed/// counting the number of ones from the least significant bit to the first zero
400193323Sed/// bit.  Ex. CountTrailingOnes_32(0x00FF00FF) == 8.
401193323Sed/// Returns 32 if the word is all ones.
402193323Sedinline unsigned CountTrailingOnes_32(uint32_t Value) {
403261991Sdim  return countTrailingZeros(~Value);
404193323Sed}
405193323Sed
406193323Sed/// CountTrailingOnes_64 - This function performs the operation
407193323Sed/// of counting the number of ones from the least significant bit to the first
408193323Sed/// zero bit (64 bit edition.)
409193323Sed/// Returns 64 if the word is all ones.
410193323Sedinline unsigned CountTrailingOnes_64(uint64_t Value) {
411261991Sdim  return countTrailingZeros(~Value);
412193323Sed}
413193323Sed
414193323Sed/// CountPopulation_32 - this function counts the number of set bits in a value.
415193323Sed/// Ex. CountPopulation(0xF000F000) = 8
416193323Sed/// Returns 0 if the word is zero.
417193323Sedinline unsigned CountPopulation_32(uint32_t Value) {
418193323Sed#if __GNUC__ >= 4
419193323Sed  return __builtin_popcount(Value);
420193323Sed#else
421193323Sed  uint32_t v = Value - ((Value >> 1) & 0x55555555);
422193323Sed  v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
423193323Sed  return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
424193323Sed#endif
425193323Sed}
426193323Sed
427193323Sed/// CountPopulation_64 - this function counts the number of set bits in a value,
428193323Sed/// (64 bit edition.)
429193323Sedinline unsigned CountPopulation_64(uint64_t Value) {
430193323Sed#if __GNUC__ >= 4
431193323Sed  return __builtin_popcountll(Value);
432193323Sed#else
433193323Sed  uint64_t v = Value - ((Value >> 1) & 0x5555555555555555ULL);
434193323Sed  v = (v & 0x3333333333333333ULL) + ((v >> 2) & 0x3333333333333333ULL);
435193323Sed  v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0FULL;
436193323Sed  return unsigned((uint64_t)(v * 0x0101010101010101ULL) >> 56);
437193323Sed#endif
438193323Sed}
439193323Sed
440193323Sed/// Log2_32 - This function returns the floor log base 2 of the specified value,
441193323Sed/// -1 if the value is zero. (32 bit edition.)
442193323Sed/// Ex. Log2_32(32) == 5, Log2_32(1) == 0, Log2_32(0) == -1, Log2_32(6) == 2
443193323Sedinline unsigned Log2_32(uint32_t Value) {
444261991Sdim  return 31 - countLeadingZeros(Value);
445193323Sed}
446193323Sed
447193323Sed/// Log2_64 - This function returns the floor log base 2 of the specified value,
448193323Sed/// -1 if the value is zero. (64 bit edition.)
449193323Sedinline unsigned Log2_64(uint64_t Value) {
450261991Sdim  return 63 - countLeadingZeros(Value);
451193323Sed}
452193323Sed
453193323Sed/// Log2_32_Ceil - This function returns the ceil log base 2 of the specified
454193323Sed/// value, 32 if the value is zero. (32 bit edition).
455193323Sed/// Ex. Log2_32_Ceil(32) == 5, Log2_32_Ceil(1) == 0, Log2_32_Ceil(6) == 3
456193323Sedinline unsigned Log2_32_Ceil(uint32_t Value) {
457261991Sdim  return 32 - countLeadingZeros(Value - 1);
458193323Sed}
459193323Sed
460193323Sed/// Log2_64_Ceil - This function returns the ceil log base 2 of the specified
461193323Sed/// value, 64 if the value is zero. (64 bit edition.)
462193323Sedinline unsigned Log2_64_Ceil(uint64_t Value) {
463261991Sdim  return 64 - countLeadingZeros(Value - 1);
464193323Sed}
465193323Sed
466193323Sed/// GreatestCommonDivisor64 - Return the greatest common divisor of the two
467193323Sed/// values using Euclid's algorithm.
468193323Sedinline uint64_t GreatestCommonDivisor64(uint64_t A, uint64_t B) {
469193323Sed  while (B) {
470193323Sed    uint64_t T = B;
471193323Sed    B = A % B;
472193323Sed    A = T;
473193323Sed  }
474193323Sed  return A;
475193323Sed}
476193323Sed
477193323Sed/// BitsToDouble - This function takes a 64-bit integer and returns the bit
478193323Sed/// equivalent double.
479193323Sedinline double BitsToDouble(uint64_t Bits) {
480193323Sed  union {
481193323Sed    uint64_t L;
482193323Sed    double D;
483193323Sed  } T;
484193323Sed  T.L = Bits;
485193323Sed  return T.D;
486193323Sed}
487193323Sed
488193323Sed/// BitsToFloat - This function takes a 32-bit integer and returns the bit
489193323Sed/// equivalent float.
490193323Sedinline float BitsToFloat(uint32_t Bits) {
491193323Sed  union {
492193323Sed    uint32_t I;
493193323Sed    float F;
494193323Sed  } T;
495193323Sed  T.I = Bits;
496193323Sed  return T.F;
497193323Sed}
498193323Sed
499193323Sed/// DoubleToBits - This function takes a double and returns the bit
500193323Sed/// equivalent 64-bit integer.  Note that copying doubles around
501193323Sed/// changes the bits of NaNs on some hosts, notably x86, so this
502193323Sed/// routine cannot be used if these bits are needed.
503193323Sedinline uint64_t DoubleToBits(double Double) {
504193323Sed  union {
505193323Sed    uint64_t L;
506193323Sed    double D;
507193323Sed  } T;
508193323Sed  T.D = Double;
509193323Sed  return T.L;
510193323Sed}
511193323Sed
512193323Sed/// FloatToBits - This function takes a float and returns the bit
513193323Sed/// equivalent 32-bit integer.  Note that copying floats around
514193323Sed/// changes the bits of NaNs on some hosts, notably x86, so this
515193323Sed/// routine cannot be used if these bits are needed.
516193323Sedinline uint32_t FloatToBits(float Float) {
517193323Sed  union {
518193323Sed    uint32_t I;
519193323Sed    float F;
520193323Sed  } T;
521193323Sed  T.F = Float;
522193323Sed  return T.I;
523193323Sed}
524193323Sed
525193323Sed/// Platform-independent wrappers for the C99 isnan() function.
526193323Sedint IsNAN(float f);
527193323Sedint IsNAN(double d);
528193323Sed
529193323Sed/// Platform-independent wrappers for the C99 isinf() function.
530193323Sedint IsInf(float f);
531193323Sedint IsInf(double d);
532193323Sed
533193323Sed/// MinAlign - A and B are either alignments or offsets.  Return the minimum
534193323Sed/// alignment that may be assumed after adding the two together.
535239462Sdiminline uint64_t MinAlign(uint64_t A, uint64_t B) {
536193323Sed  // The largest power of 2 that divides both A and B.
537249423Sdim  //
538249423Sdim  // Replace "-Value" by "1+~Value" in the following commented code to avoid
539249423Sdim  // MSVC warning C4146
540249423Sdim  //    return (A | B) & -(A | B);
541249423Sdim  return (A | B) & (1 + ~(A | B));
542193323Sed}
543193323Sed
544193323Sed/// NextPowerOf2 - Returns the next power of two (in 64-bits)
545193323Sed/// that is strictly greater than A.  Returns zero on overflow.
546239462Sdiminline uint64_t NextPowerOf2(uint64_t A) {
547193323Sed  A |= (A >> 1);
548193323Sed  A |= (A >> 2);
549193323Sed  A |= (A >> 4);
550193323Sed  A |= (A >> 8);
551193323Sed  A |= (A >> 16);
552193323Sed  A |= (A >> 32);
553193323Sed  return A + 1;
554193323Sed}
555193323Sed
556243830Sdim/// Returns the next integer (mod 2**64) that is greater than or equal to
557243830Sdim/// \p Value and is a multiple of \p Align. \p Align must be non-zero.
558193323Sed///
559193323Sed/// Examples:
560243830Sdim/// \code
561243830Sdim///   RoundUpToAlignment(5, 8) = 8
562243830Sdim///   RoundUpToAlignment(17, 8) = 24
563243830Sdim///   RoundUpToAlignment(~0LL, 8) = 0
564243830Sdim/// \endcode
565193323Sedinline uint64_t RoundUpToAlignment(uint64_t Value, uint64_t Align) {
566193323Sed  return ((Value + Align - 1) / Align) * Align;
567193323Sed}
568193323Sed
569243830Sdim/// Returns the offset to the next integer (mod 2**64) that is greater than
570243830Sdim/// or equal to \p Value and is a multiple of \p Align. \p Align must be
571243830Sdim/// non-zero.
572198090Srdivackyinline uint64_t OffsetToAlignment(uint64_t Value, uint64_t Align) {
573198090Srdivacky  return RoundUpToAlignment(Value, Align) - Value;
574198090Srdivacky}
575198090Srdivacky
576193323Sed/// abs64 - absolute value of a 64-bit int.  Not all environments support
577193323Sed/// "abs" on whatever their name for the 64-bit int type is.  The absolute
578193323Sed/// value of the largest negative number is undefined, as with "abs".
579193323Sedinline int64_t abs64(int64_t x) {
580193323Sed  return (x < 0) ? -x : x;
581193323Sed}
582193323Sed
583206124Srdivacky/// SignExtend32 - Sign extend B-bit number x to 32-bit int.
584206124Srdivacky/// Usage int32_t r = SignExtend32<5>(x);
585206274Srdivackytemplate <unsigned B> inline int32_t SignExtend32(uint32_t x) {
586206274Srdivacky  return int32_t(x << (32 - B)) >> (32 - B);
587206124Srdivacky}
588206124Srdivacky
589243830Sdim/// \brief Sign extend number in the bottom B bits of X to a 32-bit int.
590243830Sdim/// Requires 0 < B <= 32.
591243830Sdiminline int32_t SignExtend32(uint32_t X, unsigned B) {
592243830Sdim  return int32_t(X << (32 - B)) >> (32 - B);
593243830Sdim}
594243830Sdim
595206124Srdivacky/// SignExtend64 - Sign extend B-bit number x to 64-bit int.
596206124Srdivacky/// Usage int64_t r = SignExtend64<5>(x);
597206274Srdivackytemplate <unsigned B> inline int64_t SignExtend64(uint64_t x) {
598206274Srdivacky  return int64_t(x << (64 - B)) >> (64 - B);
599206124Srdivacky}
600206124Srdivacky
601243830Sdim/// \brief Sign extend number in the bottom B bits of X to a 64-bit int.
602243830Sdim/// Requires 0 < B <= 64.
603243830Sdiminline int64_t SignExtend64(uint64_t X, unsigned B) {
604243830Sdim  return int64_t(X << (64 - B)) >> (64 - B);
605243830Sdim}
606243830Sdim
607261991Sdim#if defined(_MSC_VER)
608261991Sdim  // Visual Studio defines the HUGE_VAL class of macros using purposeful
609261991Sdim  // constant arithmetic overflow, which it then warns on when encountered.
610261991Sdim  const float huge_valf = std::numeric_limits<float>::infinity();
611261991Sdim#else
612261991Sdim  const float huge_valf = HUGE_VALF;
613261991Sdim#endif
614193323Sed} // End llvm namespace
615193323Sed
616193323Sed#endif
617