1 //===-- llvm/Support/MathExtras.h - Useful math functions -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains some functions that are useful for math stuff.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_SUPPORT_MATHEXTRAS_H
15 #define LLVM_SUPPORT_MATHEXTRAS_H
17 #include "llvm/Support/Compiler.h"
18 #include "llvm/Support/SwapByteOrder.h"
19 #include "llvm/Support/type_traits.h"
28 /// \brief The behavior an operation has on an input of 0.
30 /// \brief The returned value is undefined.
32 /// \brief The returned value is numeric_limits<T>::max()
34 /// \brief The returned value is numeric_limits<T>::digits
38 /// \brief Count number of 0's from the least significant bit to the most
39 /// stopping at the first 1.
41 /// Only unsigned integral types are allowed.
43 /// \param ZB the behavior on an input of 0. Only ZB_Width and ZB_Undefined are
46 typename enable_if_c<std::numeric_limits<T>::is_integer &&
47 !std::numeric_limits<T>::is_signed, std::size_t>::type
48 countTrailingZeros(T Val, ZeroBehavior ZB = ZB_Width) {
50 return std::numeric_limits<T>::digits;
55 std::size_t ZeroBits = 0;
56 T Shift = std::numeric_limits<T>::digits >> 1;
57 T Mask = std::numeric_limits<T>::max() >> Shift;
59 if ((Val & Mask) == 0) {
71 typename enable_if_c<std::numeric_limits<T>::is_integer &&
72 std::numeric_limits<T>::is_signed, std::size_t>::type
73 countTrailingZeros(T Val, ZeroBehavior ZB = ZB_Width) LLVM_DELETED_FUNCTION;
75 #if __GNUC__ >= 4 || _MSC_VER
77 inline std::size_t countTrailingZeros<uint32_t>(uint32_t Val, ZeroBehavior ZB) {
78 if (ZB != ZB_Undefined && Val == 0)
82 return __builtin_ctz(Val);
85 _BitScanForward(&Index, Val);
90 #if !defined(_MSC_VER) || defined(_M_X64)
92 inline std::size_t countTrailingZeros<uint64_t>(uint64_t Val, ZeroBehavior ZB) {
93 if (ZB != ZB_Undefined && Val == 0)
97 return __builtin_ctzll(Val);
100 _BitScanForward64(&Index, Val);
107 /// \brief Count number of 0's from the most significant bit to the least
108 /// stopping at the first 1.
110 /// Only unsigned integral types are allowed.
112 /// \param ZB the behavior on an input of 0. Only ZB_Width and ZB_Undefined are
114 template <typename T>
115 typename enable_if_c<std::numeric_limits<T>::is_integer &&
116 !std::numeric_limits<T>::is_signed, std::size_t>::type
117 countLeadingZeros(T Val, ZeroBehavior ZB = ZB_Width) {
119 return std::numeric_limits<T>::digits;
122 std::size_t ZeroBits = 0;
123 for (T Shift = std::numeric_limits<T>::digits >> 1; Shift; Shift >>= 1) {
124 T Tmp = Val >> Shift;
134 template <typename T>
135 typename enable_if_c<std::numeric_limits<T>::is_integer &&
136 std::numeric_limits<T>::is_signed, std::size_t>::type
137 countLeadingZeros(T Val, ZeroBehavior ZB = ZB_Width) LLVM_DELETED_FUNCTION;
139 #if __GNUC__ >= 4 || _MSC_VER
141 inline std::size_t countLeadingZeros<uint32_t>(uint32_t Val, ZeroBehavior ZB) {
142 if (ZB != ZB_Undefined && Val == 0)
146 return __builtin_clz(Val);
149 _BitScanReverse(&Index, Val);
154 #if !defined(_MSC_VER) || defined(_M_X64)
156 inline std::size_t countLeadingZeros<uint64_t>(uint64_t Val, ZeroBehavior ZB) {
157 if (ZB != ZB_Undefined && Val == 0)
161 return __builtin_clzll(Val);
164 _BitScanReverse64(&Index, Val);
171 /// \brief Get the index of the first set bit starting from the least
174 /// Only unsigned integral types are allowed.
176 /// \param ZB the behavior on an input of 0. Only ZB_Max and ZB_Undefined are
178 template <typename T>
179 typename enable_if_c<std::numeric_limits<T>::is_integer &&
180 !std::numeric_limits<T>::is_signed, T>::type
181 findFirstSet(T Val, ZeroBehavior ZB = ZB_Max) {
182 if (ZB == ZB_Max && Val == 0)
183 return std::numeric_limits<T>::max();
185 return countTrailingZeros(Val, ZB_Undefined);
189 template <typename T>
190 typename enable_if_c<std::numeric_limits<T>::is_integer &&
191 std::numeric_limits<T>::is_signed, T>::type
192 findFirstSet(T Val, ZeroBehavior ZB = ZB_Max) LLVM_DELETED_FUNCTION;
194 /// \brief Get the index of the last set bit starting from the least
197 /// Only unsigned integral types are allowed.
199 /// \param ZB the behavior on an input of 0. Only ZB_Max and ZB_Undefined are
201 template <typename T>
202 typename enable_if_c<std::numeric_limits<T>::is_integer &&
203 !std::numeric_limits<T>::is_signed, T>::type
204 findLastSet(T Val, ZeroBehavior ZB = ZB_Max) {
205 if (ZB == ZB_Max && Val == 0)
206 return std::numeric_limits<T>::max();
208 // Use ^ instead of - because both gcc and llvm can remove the associated ^
209 // in the __builtin_clz intrinsic on x86.
210 return countLeadingZeros(Val, ZB_Undefined) ^
211 (std::numeric_limits<T>::digits - 1);
215 template <typename T>
216 typename enable_if_c<std::numeric_limits<T>::is_integer &&
217 std::numeric_limits<T>::is_signed, T>::type
218 findLastSet(T Val, ZeroBehavior ZB = ZB_Max) LLVM_DELETED_FUNCTION;
220 /// \brief Macro compressed bit reversal table for 256 bits.
222 /// http://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable
223 static const unsigned char BitReverseTable256[256] = {
224 #define R2(n) n, n + 2 * 64, n + 1 * 64, n + 3 * 64
225 #define R4(n) R2(n), R2(n + 2 * 16), R2(n + 1 * 16), R2(n + 3 * 16)
226 #define R6(n) R4(n), R4(n + 2 * 4), R4(n + 1 * 4), R4(n + 3 * 4)
227 R6(0), R6(2), R6(1), R6(3)
230 /// \brief Reverse the bits in \p Val.
231 template <typename T>
232 T reverseBits(T Val) {
233 unsigned char in[sizeof(Val)];
234 unsigned char out[sizeof(Val)];
235 std::memcpy(in, &Val, sizeof(Val));
236 for (unsigned i = 0; i < sizeof(Val); ++i)
237 out[(sizeof(Val) - i) - 1] = BitReverseTable256[in[i]];
238 std::memcpy(&Val, out, sizeof(Val));
242 // NOTE: The following support functions use the _32/_64 extensions instead of
243 // type overloading so that signed and unsigned integers can be used without
246 /// Hi_32 - This function returns the high 32 bits of a 64 bit value.
247 inline uint32_t Hi_32(uint64_t Value) {
248 return static_cast<uint32_t>(Value >> 32);
251 /// Lo_32 - This function returns the low 32 bits of a 64 bit value.
252 inline uint32_t Lo_32(uint64_t Value) {
253 return static_cast<uint32_t>(Value);
256 /// isInt - Checks if an integer fits into the given bit width.
258 inline bool isInt(int64_t x) {
259 return N >= 64 || (-(INT64_C(1)<<(N-1)) <= x && x < (INT64_C(1)<<(N-1)));
261 // Template specializations to get better code for common cases.
263 inline bool isInt<8>(int64_t x) {
264 return static_cast<int8_t>(x) == x;
267 inline bool isInt<16>(int64_t x) {
268 return static_cast<int16_t>(x) == x;
271 inline bool isInt<32>(int64_t x) {
272 return static_cast<int32_t>(x) == x;
275 /// isShiftedInt<N,S> - Checks if a signed integer is an N bit number shifted
277 template<unsigned N, unsigned S>
278 inline bool isShiftedInt(int64_t x) {
279 return isInt<N+S>(x) && (x % (1<<S) == 0);
282 /// isUInt - Checks if an unsigned integer fits into the given bit width.
284 inline bool isUInt(uint64_t x) {
285 return N >= 64 || x < (UINT64_C(1)<<(N));
287 // Template specializations to get better code for common cases.
289 inline bool isUInt<8>(uint64_t x) {
290 return static_cast<uint8_t>(x) == x;
293 inline bool isUInt<16>(uint64_t x) {
294 return static_cast<uint16_t>(x) == x;
297 inline bool isUInt<32>(uint64_t x) {
298 return static_cast<uint32_t>(x) == x;
301 /// isShiftedUInt<N,S> - Checks if a unsigned integer is an N bit number shifted
303 template<unsigned N, unsigned S>
304 inline bool isShiftedUInt(uint64_t x) {
305 return isUInt<N+S>(x) && (x % (1<<S) == 0);
308 /// isUIntN - Checks if an unsigned integer fits into the given (dynamic)
310 inline bool isUIntN(unsigned N, uint64_t x) {
311 return x == (x & (~0ULL >> (64 - N)));
314 /// isIntN - Checks if an signed integer fits into the given (dynamic)
316 inline bool isIntN(unsigned N, int64_t x) {
317 return N >= 64 || (-(INT64_C(1)<<(N-1)) <= x && x < (INT64_C(1)<<(N-1)));
320 /// isMask_32 - This function returns true if the argument is a sequence of ones
321 /// starting at the least significant bit with the remainder zero (32 bit
322 /// version). Ex. isMask_32(0x0000FFFFU) == true.
323 inline bool isMask_32(uint32_t Value) {
324 return Value && ((Value + 1) & Value) == 0;
327 /// isMask_64 - This function returns true if the argument is a sequence of ones
328 /// starting at the least significant bit with the remainder zero (64 bit
330 inline bool isMask_64(uint64_t Value) {
331 return Value && ((Value + 1) & Value) == 0;
334 /// isShiftedMask_32 - This function returns true if the argument contains a
335 /// sequence of ones with the remainder zero (32 bit version.)
336 /// Ex. isShiftedMask_32(0x0000FF00U) == true.
337 inline bool isShiftedMask_32(uint32_t Value) {
338 return isMask_32((Value - 1) | Value);
341 /// isShiftedMask_64 - This function returns true if the argument contains a
342 /// sequence of ones with the remainder zero (64 bit version.)
343 inline bool isShiftedMask_64(uint64_t Value) {
344 return isMask_64((Value - 1) | Value);
347 /// isPowerOf2_32 - This function returns true if the argument is a power of
348 /// two > 0. Ex. isPowerOf2_32(0x00100000U) == true (32 bit edition.)
349 inline bool isPowerOf2_32(uint32_t Value) {
350 return Value && !(Value & (Value - 1));
353 /// isPowerOf2_64 - This function returns true if the argument is a power of two
354 /// > 0 (64 bit edition.)
355 inline bool isPowerOf2_64(uint64_t Value) {
356 return Value && !(Value & (Value - int64_t(1L)));
359 /// ByteSwap_16 - This function returns a byte-swapped representation of the
360 /// 16-bit argument, Value.
361 inline uint16_t ByteSwap_16(uint16_t Value) {
362 return sys::SwapByteOrder_16(Value);
365 /// ByteSwap_32 - This function returns a byte-swapped representation of the
366 /// 32-bit argument, Value.
367 inline uint32_t ByteSwap_32(uint32_t Value) {
368 return sys::SwapByteOrder_32(Value);
371 /// ByteSwap_64 - This function returns a byte-swapped representation of the
372 /// 64-bit argument, Value.
373 inline uint64_t ByteSwap_64(uint64_t Value) {
374 return sys::SwapByteOrder_64(Value);
377 /// CountLeadingOnes_32 - this function performs the operation of
378 /// counting the number of ones from the most significant bit to the first zero
379 /// bit. Ex. CountLeadingOnes_32(0xFF0FFF00) == 8.
380 /// Returns 32 if the word is all ones.
381 inline unsigned CountLeadingOnes_32(uint32_t Value) {
382 return countLeadingZeros(~Value);
385 /// CountLeadingOnes_64 - This function performs the operation
386 /// of counting the number of ones from the most significant bit to the first
387 /// zero bit (64 bit edition.)
388 /// Returns 64 if the word is all ones.
389 inline unsigned CountLeadingOnes_64(uint64_t Value) {
390 return countLeadingZeros(~Value);
393 /// CountTrailingOnes_32 - this function performs the operation of
394 /// counting the number of ones from the least significant bit to the first zero
395 /// bit. Ex. CountTrailingOnes_32(0x00FF00FF) == 8.
396 /// Returns 32 if the word is all ones.
397 inline unsigned CountTrailingOnes_32(uint32_t Value) {
398 return countTrailingZeros(~Value);
401 /// CountTrailingOnes_64 - This function performs the operation
402 /// of counting the number of ones from the least significant bit to the first
403 /// zero bit (64 bit edition.)
404 /// Returns 64 if the word is all ones.
405 inline unsigned CountTrailingOnes_64(uint64_t Value) {
406 return countTrailingZeros(~Value);
409 /// CountPopulation_32 - this function counts the number of set bits in a value.
410 /// Ex. CountPopulation(0xF000F000) = 8
411 /// Returns 0 if the word is zero.
412 inline unsigned CountPopulation_32(uint32_t Value) {
414 return __builtin_popcount(Value);
416 uint32_t v = Value - ((Value >> 1) & 0x55555555);
417 v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
418 return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
422 /// CountPopulation_64 - this function counts the number of set bits in a value,
423 /// (64 bit edition.)
424 inline unsigned CountPopulation_64(uint64_t Value) {
426 return __builtin_popcountll(Value);
428 uint64_t v = Value - ((Value >> 1) & 0x5555555555555555ULL);
429 v = (v & 0x3333333333333333ULL) + ((v >> 2) & 0x3333333333333333ULL);
430 v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0FULL;
431 return unsigned((uint64_t)(v * 0x0101010101010101ULL) >> 56);
435 /// Log2_32 - This function returns the floor log base 2 of the specified value,
436 /// -1 if the value is zero. (32 bit edition.)
437 /// Ex. Log2_32(32) == 5, Log2_32(1) == 0, Log2_32(0) == -1, Log2_32(6) == 2
438 inline unsigned Log2_32(uint32_t Value) {
439 return 31 - countLeadingZeros(Value);
442 /// Log2_64 - This function returns the floor log base 2 of the specified value,
443 /// -1 if the value is zero. (64 bit edition.)
444 inline unsigned Log2_64(uint64_t Value) {
445 return 63 - countLeadingZeros(Value);
448 /// Log2_32_Ceil - This function returns the ceil log base 2 of the specified
449 /// value, 32 if the value is zero. (32 bit edition).
450 /// Ex. Log2_32_Ceil(32) == 5, Log2_32_Ceil(1) == 0, Log2_32_Ceil(6) == 3
451 inline unsigned Log2_32_Ceil(uint32_t Value) {
452 return 32 - countLeadingZeros(Value - 1);
455 /// Log2_64_Ceil - This function returns the ceil log base 2 of the specified
456 /// value, 64 if the value is zero. (64 bit edition.)
457 inline unsigned Log2_64_Ceil(uint64_t Value) {
458 return 64 - countLeadingZeros(Value - 1);
461 /// GreatestCommonDivisor64 - Return the greatest common divisor of the two
462 /// values using Euclid's algorithm.
463 inline uint64_t GreatestCommonDivisor64(uint64_t A, uint64_t B) {
472 /// BitsToDouble - This function takes a 64-bit integer and returns the bit
473 /// equivalent double.
474 inline double BitsToDouble(uint64_t Bits) {
483 /// BitsToFloat - This function takes a 32-bit integer and returns the bit
484 /// equivalent float.
485 inline float BitsToFloat(uint32_t Bits) {
494 /// DoubleToBits - This function takes a double and returns the bit
495 /// equivalent 64-bit integer. Note that copying doubles around
496 /// changes the bits of NaNs on some hosts, notably x86, so this
497 /// routine cannot be used if these bits are needed.
498 inline uint64_t DoubleToBits(double Double) {
507 /// FloatToBits - This function takes a float and returns the bit
508 /// equivalent 32-bit integer. Note that copying floats around
509 /// changes the bits of NaNs on some hosts, notably x86, so this
510 /// routine cannot be used if these bits are needed.
511 inline uint32_t FloatToBits(float Float) {
520 /// Platform-independent wrappers for the C99 isnan() function.
524 /// Platform-independent wrappers for the C99 isinf() function.
528 /// MinAlign - A and B are either alignments or offsets. Return the minimum
529 /// alignment that may be assumed after adding the two together.
530 inline uint64_t MinAlign(uint64_t A, uint64_t B) {
531 // The largest power of 2 that divides both A and B.
533 // Replace "-Value" by "1+~Value" in the following commented code to avoid
534 // MSVC warning C4146
535 // return (A | B) & -(A | B);
536 return (A | B) & (1 + ~(A | B));
539 /// NextPowerOf2 - Returns the next power of two (in 64-bits)
540 /// that is strictly greater than A. Returns zero on overflow.
541 inline uint64_t NextPowerOf2(uint64_t A) {
551 /// Returns the next integer (mod 2**64) that is greater than or equal to
552 /// \p Value and is a multiple of \p Align. \p Align must be non-zero.
556 /// RoundUpToAlignment(5, 8) = 8
557 /// RoundUpToAlignment(17, 8) = 24
558 /// RoundUpToAlignment(~0LL, 8) = 0
560 inline uint64_t RoundUpToAlignment(uint64_t Value, uint64_t Align) {
561 return ((Value + Align - 1) / Align) * Align;
564 /// Returns the offset to the next integer (mod 2**64) that is greater than
565 /// or equal to \p Value and is a multiple of \p Align. \p Align must be
567 inline uint64_t OffsetToAlignment(uint64_t Value, uint64_t Align) {
568 return RoundUpToAlignment(Value, Align) - Value;
571 /// abs64 - absolute value of a 64-bit int. Not all environments support
572 /// "abs" on whatever their name for the 64-bit int type is. The absolute
573 /// value of the largest negative number is undefined, as with "abs".
574 inline int64_t abs64(int64_t x) {
575 return (x < 0) ? -x : x;
578 /// SignExtend32 - Sign extend B-bit number x to 32-bit int.
579 /// Usage int32_t r = SignExtend32<5>(x);
580 template <unsigned B> inline int32_t SignExtend32(uint32_t x) {
581 return int32_t(x << (32 - B)) >> (32 - B);
584 /// \brief Sign extend number in the bottom B bits of X to a 32-bit int.
585 /// Requires 0 < B <= 32.
586 inline int32_t SignExtend32(uint32_t X, unsigned B) {
587 return int32_t(X << (32 - B)) >> (32 - B);
590 /// SignExtend64 - Sign extend B-bit number x to 64-bit int.
591 /// Usage int64_t r = SignExtend64<5>(x);
592 template <unsigned B> inline int64_t SignExtend64(uint64_t x) {
593 return int64_t(x << (64 - B)) >> (64 - B);
596 /// \brief Sign extend number in the bottom B bits of X to a 64-bit int.
597 /// Requires 0 < B <= 64.
598 inline int64_t SignExtend64(uint64_t X, unsigned B) {
599 return int64_t(X << (64 - B)) >> (64 - B);
602 } // End llvm namespace