LLVM API Documentation

MathExtras.h
Go to the documentation of this file.
00001 //===-- llvm/Support/MathExtras.h - Useful math functions -------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file contains some functions that are useful for math stuff.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_SUPPORT_MATHEXTRAS_H
00015 #define LLVM_SUPPORT_MATHEXTRAS_H
00016 
00017 #include "llvm/Support/SwapByteOrder.h"
00018 
00019 #ifdef _MSC_VER
00020 # include <intrin.h>
00021 #endif
00022 
00023 namespace llvm {
00024 
00025 // NOTE: The following support functions use the _32/_64 extensions instead of
00026 // type overloading so that signed and unsigned integers can be used without
00027 // ambiguity.
00028 
00029 /// Hi_32 - This function returns the high 32 bits of a 64 bit value.
00030 inline uint32_t Hi_32(uint64_t Value) {
00031   return static_cast<uint32_t>(Value >> 32);
00032 }
00033 
00034 /// Lo_32 - This function returns the low 32 bits of a 64 bit value.
00035 inline uint32_t Lo_32(uint64_t Value) {
00036   return static_cast<uint32_t>(Value);
00037 }
00038 
00039 /// isInt - Checks if an integer fits into the given bit width.
00040 template<unsigned N>
00041 inline bool isInt(int64_t x) {
00042   return N >= 64 || (-(INT64_C(1)<<(N-1)) <= x && x < (INT64_C(1)<<(N-1)));
00043 }
00044 // Template specializations to get better code for common cases.
00045 template<>
00046 inline bool isInt<8>(int64_t x) {
00047   return static_cast<int8_t>(x) == x;
00048 }
00049 template<>
00050 inline bool isInt<16>(int64_t x) {
00051   return static_cast<int16_t>(x) == x;
00052 }
00053 template<>
00054 inline bool isInt<32>(int64_t x) {
00055   return static_cast<int32_t>(x) == x;
00056 }
00057 
00058 /// isShiftedInt<N,S> - Checks if a signed integer is an N bit number shifted
00059 ///                     left by S.
00060 template<unsigned N, unsigned S>
00061 inline bool isShiftedInt(int64_t x) {
00062   return isInt<N+S>(x) && (x % (1<<S) == 0);
00063 }
00064 
00065 /// isUInt - Checks if an unsigned integer fits into the given bit width.
00066 template<unsigned N>
00067 inline bool isUInt(uint64_t x) {
00068   return N >= 64 || x < (UINT64_C(1)<<(N));
00069 }
00070 // Template specializations to get better code for common cases.
00071 template<>
00072 inline bool isUInt<8>(uint64_t x) {
00073   return static_cast<uint8_t>(x) == x;
00074 }
00075 template<>
00076 inline bool isUInt<16>(uint64_t x) {
00077   return static_cast<uint16_t>(x) == x;
00078 }
00079 template<>
00080 inline bool isUInt<32>(uint64_t x) {
00081   return static_cast<uint32_t>(x) == x;
00082 }
00083 
00084 /// isShiftedUInt<N,S> - Checks if a unsigned integer is an N bit number shifted
00085 ///                     left by S.
00086 template<unsigned N, unsigned S>
00087 inline bool isShiftedUInt(uint64_t x) {
00088   return isUInt<N+S>(x) && (x % (1<<S) == 0);
00089 }
00090 
00091 /// isUIntN - Checks if an unsigned integer fits into the given (dynamic)
00092 /// bit width.
00093 inline bool isUIntN(unsigned N, uint64_t x) {
00094   return x == (x & (~0ULL >> (64 - N)));
00095 }
00096 
00097 /// isIntN - Checks if an signed integer fits into the given (dynamic)
00098 /// bit width.
00099 inline bool isIntN(unsigned N, int64_t x) {
00100   return N >= 64 || (-(INT64_C(1)<<(N-1)) <= x && x < (INT64_C(1)<<(N-1)));
00101 }
00102 
00103 /// isMask_32 - This function returns true if the argument is a sequence of ones
00104 /// starting at the least significant bit with the remainder zero (32 bit
00105 /// version).   Ex. isMask_32(0x0000FFFFU) == true.
00106 inline bool isMask_32(uint32_t Value) {
00107   return Value && ((Value + 1) & Value) == 0;
00108 }
00109 
00110 /// isMask_64 - This function returns true if the argument is a sequence of ones
00111 /// starting at the least significant bit with the remainder zero (64 bit
00112 /// version).
00113 inline bool isMask_64(uint64_t Value) {
00114   return Value && ((Value + 1) & Value) == 0;
00115 }
00116 
00117 /// isShiftedMask_32 - This function returns true if the argument contains a
00118 /// sequence of ones with the remainder zero (32 bit version.)
00119 /// Ex. isShiftedMask_32(0x0000FF00U) == true.
00120 inline bool isShiftedMask_32(uint32_t Value) {
00121   return isMask_32((Value - 1) | Value);
00122 }
00123 
00124 /// isShiftedMask_64 - This function returns true if the argument contains a
00125 /// sequence of ones with the remainder zero (64 bit version.)
00126 inline bool isShiftedMask_64(uint64_t Value) {
00127   return isMask_64((Value - 1) | Value);
00128 }
00129 
00130 /// isPowerOf2_32 - This function returns true if the argument is a power of
00131 /// two > 0. Ex. isPowerOf2_32(0x00100000U) == true (32 bit edition.)
00132 inline bool isPowerOf2_32(uint32_t Value) {
00133   return Value && !(Value & (Value - 1));
00134 }
00135 
00136 /// isPowerOf2_64 - This function returns true if the argument is a power of two
00137 /// > 0 (64 bit edition.)
00138 inline bool isPowerOf2_64(uint64_t Value) {
00139   return Value && !(Value & (Value - int64_t(1L)));
00140 }
00141 
00142 /// ByteSwap_16 - This function returns a byte-swapped representation of the
00143 /// 16-bit argument, Value.
00144 inline uint16_t ByteSwap_16(uint16_t Value) {
00145   return sys::SwapByteOrder_16(Value);
00146 }
00147 
00148 /// ByteSwap_32 - This function returns a byte-swapped representation of the
00149 /// 32-bit argument, Value.
00150 inline uint32_t ByteSwap_32(uint32_t Value) {
00151   return sys::SwapByteOrder_32(Value);
00152 }
00153 
00154 /// ByteSwap_64 - This function returns a byte-swapped representation of the
00155 /// 64-bit argument, Value.
00156 inline uint64_t ByteSwap_64(uint64_t Value) {
00157   return sys::SwapByteOrder_64(Value);
00158 }
00159 
00160 /// CountLeadingZeros_32 - this function performs the platform optimal form of
00161 /// counting the number of zeros from the most significant bit to the first one
00162 /// bit.  Ex. CountLeadingZeros_32(0x00F000FF) == 8.
00163 /// Returns 32 if the word is zero.
00164 inline unsigned CountLeadingZeros_32(uint32_t Value) {
00165   unsigned Count; // result
00166 #if __GNUC__ >= 4
00167   // PowerPC is defined for __builtin_clz(0)
00168 #if !defined(__ppc__) && !defined(__ppc64__)
00169   if (!Value) return 32;
00170 #endif
00171   Count = __builtin_clz(Value);
00172 #else
00173   if (!Value) return 32;
00174   Count = 0;
00175   // bisection method for count leading zeros
00176   for (unsigned Shift = 32 >> 1; Shift; Shift >>= 1) {
00177     uint32_t Tmp = Value >> Shift;
00178     if (Tmp) {
00179       Value = Tmp;
00180     } else {
00181       Count |= Shift;
00182     }
00183   }
00184 #endif
00185   return Count;
00186 }
00187 
00188 /// CountLeadingOnes_32 - this function performs the operation of
00189 /// counting the number of ones from the most significant bit to the first zero
00190 /// bit.  Ex. CountLeadingOnes_32(0xFF0FFF00) == 8.
00191 /// Returns 32 if the word is all ones.
00192 inline unsigned CountLeadingOnes_32(uint32_t Value) {
00193   return CountLeadingZeros_32(~Value);
00194 }
00195 
00196 /// CountLeadingZeros_64 - This function performs the platform optimal form
00197 /// of counting the number of zeros from the most significant bit to the first
00198 /// one bit (64 bit edition.)
00199 /// Returns 64 if the word is zero.
00200 inline unsigned CountLeadingZeros_64(uint64_t Value) {
00201   unsigned Count; // result
00202 #if __GNUC__ >= 4
00203   // PowerPC is defined for __builtin_clzll(0)
00204 #if !defined(__ppc__) && !defined(__ppc64__)
00205   if (!Value) return 64;
00206 #endif
00207   Count = __builtin_clzll(Value);
00208 #else
00209   if (sizeof(long) == sizeof(int64_t)) {
00210     if (!Value) return 64;
00211     Count = 0;
00212     // bisection method for count leading zeros
00213     for (unsigned Shift = 64 >> 1; Shift; Shift >>= 1) {
00214       uint64_t Tmp = Value >> Shift;
00215       if (Tmp) {
00216         Value = Tmp;
00217       } else {
00218         Count |= Shift;
00219       }
00220     }
00221   } else {
00222     // get hi portion
00223     uint32_t Hi = Hi_32(Value);
00224 
00225     // if some bits in hi portion
00226     if (Hi) {
00227         // leading zeros in hi portion plus all bits in lo portion
00228         Count = CountLeadingZeros_32(Hi);
00229     } else {
00230         // get lo portion
00231         uint32_t Lo = Lo_32(Value);
00232         // same as 32 bit value
00233         Count = CountLeadingZeros_32(Lo)+32;
00234     }
00235   }
00236 #endif
00237   return Count;
00238 }
00239 
00240 /// CountLeadingOnes_64 - This function performs the operation
00241 /// of counting the number of ones from the most significant bit to the first
00242 /// zero bit (64 bit edition.)
00243 /// Returns 64 if the word is all ones.
00244 inline unsigned CountLeadingOnes_64(uint64_t Value) {
00245   return CountLeadingZeros_64(~Value);
00246 }
00247 
00248 /// CountTrailingZeros_32 - this function performs the platform optimal form of
00249 /// counting the number of zeros from the least significant bit to the first one
00250 /// bit.  Ex. CountTrailingZeros_32(0xFF00FF00) == 8.
00251 /// Returns 32 if the word is zero.
00252 inline unsigned CountTrailingZeros_32(uint32_t Value) {
00253 #if __GNUC__ >= 4
00254   return Value ? __builtin_ctz(Value) : 32;
00255 #else
00256   static const unsigned Mod37BitPosition[] = {
00257     32, 0, 1, 26, 2, 23, 27, 0, 3, 16, 24, 30, 28, 11, 0, 13,
00258     4, 7, 17, 0, 25, 22, 31, 15, 29, 10, 12, 6, 0, 21, 14, 9,
00259     5, 20, 8, 19, 18
00260   };
00261   // Replace "-Value" by "1+~Value" in the following commented code to avoid 
00262   // MSVC warning C4146
00263   //    return Mod37BitPosition[(-Value & Value) % 37];
00264   return Mod37BitPosition[((1 + ~Value) & Value) % 37];
00265 #endif
00266 }
00267 
00268 /// CountTrailingOnes_32 - this function performs the operation of
00269 /// counting the number of ones from the least significant bit to the first zero
00270 /// bit.  Ex. CountTrailingOnes_32(0x00FF00FF) == 8.
00271 /// Returns 32 if the word is all ones.
00272 inline unsigned CountTrailingOnes_32(uint32_t Value) {
00273   return CountTrailingZeros_32(~Value);
00274 }
00275 
00276 /// CountTrailingZeros_64 - This function performs the platform optimal form
00277 /// of counting the number of zeros from the least significant bit to the first
00278 /// one bit (64 bit edition.)
00279 /// Returns 64 if the word is zero.
00280 inline unsigned CountTrailingZeros_64(uint64_t Value) {
00281 #if __GNUC__ >= 4
00282   return Value ? __builtin_ctzll(Value) : 64;
00283 #else
00284   static const unsigned Mod67Position[] = {
00285     64, 0, 1, 39, 2, 15, 40, 23, 3, 12, 16, 59, 41, 19, 24, 54,
00286     4, 64, 13, 10, 17, 62, 60, 28, 42, 30, 20, 51, 25, 44, 55,
00287     47, 5, 32, 65, 38, 14, 22, 11, 58, 18, 53, 63, 9, 61, 27,
00288     29, 50, 43, 46, 31, 37, 21, 57, 52, 8, 26, 49, 45, 36, 56,
00289     7, 48, 35, 6, 34, 33, 0
00290   };
00291   // Replace "-Value" by "1+~Value" in the following commented code to avoid 
00292   // MSVC warning C4146
00293   //    return Mod67Position[(-Value & Value) % 67];
00294   return Mod67Position[((1 + ~Value) & Value) % 67];
00295 #endif
00296 }
00297 
00298 /// CountTrailingOnes_64 - This function performs the operation
00299 /// of counting the number of ones from the least significant bit to the first
00300 /// zero bit (64 bit edition.)
00301 /// Returns 64 if the word is all ones.
00302 inline unsigned CountTrailingOnes_64(uint64_t Value) {
00303   return CountTrailingZeros_64(~Value);
00304 }
00305 
00306 /// CountPopulation_32 - this function counts the number of set bits in a value.
00307 /// Ex. CountPopulation(0xF000F000) = 8
00308 /// Returns 0 if the word is zero.
00309 inline unsigned CountPopulation_32(uint32_t Value) {
00310 #if __GNUC__ >= 4
00311   return __builtin_popcount(Value);
00312 #else
00313   uint32_t v = Value - ((Value >> 1) & 0x55555555);
00314   v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
00315   return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
00316 #endif
00317 }
00318 
00319 /// CountPopulation_64 - this function counts the number of set bits in a value,
00320 /// (64 bit edition.)
00321 inline unsigned CountPopulation_64(uint64_t Value) {
00322 #if __GNUC__ >= 4
00323   return __builtin_popcountll(Value);
00324 #else
00325   uint64_t v = Value - ((Value >> 1) & 0x5555555555555555ULL);
00326   v = (v & 0x3333333333333333ULL) + ((v >> 2) & 0x3333333333333333ULL);
00327   v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0FULL;
00328   return unsigned((uint64_t)(v * 0x0101010101010101ULL) >> 56);
00329 #endif
00330 }
00331 
00332 /// Log2_32 - This function returns the floor log base 2 of the specified value,
00333 /// -1 if the value is zero. (32 bit edition.)
00334 /// Ex. Log2_32(32) == 5, Log2_32(1) == 0, Log2_32(0) == -1, Log2_32(6) == 2
00335 inline unsigned Log2_32(uint32_t Value) {
00336   return 31 - CountLeadingZeros_32(Value);
00337 }
00338 
00339 /// Log2_64 - This function returns the floor log base 2 of the specified value,
00340 /// -1 if the value is zero. (64 bit edition.)
00341 inline unsigned Log2_64(uint64_t Value) {
00342   return 63 - CountLeadingZeros_64(Value);
00343 }
00344 
00345 /// Log2_32_Ceil - This function returns the ceil log base 2 of the specified
00346 /// value, 32 if the value is zero. (32 bit edition).
00347 /// Ex. Log2_32_Ceil(32) == 5, Log2_32_Ceil(1) == 0, Log2_32_Ceil(6) == 3
00348 inline unsigned Log2_32_Ceil(uint32_t Value) {
00349   return 32-CountLeadingZeros_32(Value-1);
00350 }
00351 
00352 /// Log2_64_Ceil - This function returns the ceil log base 2 of the specified
00353 /// value, 64 if the value is zero. (64 bit edition.)
00354 inline unsigned Log2_64_Ceil(uint64_t Value) {
00355   return 64-CountLeadingZeros_64(Value-1);
00356 }
00357 
00358 /// GreatestCommonDivisor64 - Return the greatest common divisor of the two
00359 /// values using Euclid's algorithm.
00360 inline uint64_t GreatestCommonDivisor64(uint64_t A, uint64_t B) {
00361   while (B) {
00362     uint64_t T = B;
00363     B = A % B;
00364     A = T;
00365   }
00366   return A;
00367 }
00368 
00369 /// BitsToDouble - This function takes a 64-bit integer and returns the bit
00370 /// equivalent double.
00371 inline double BitsToDouble(uint64_t Bits) {
00372   union {
00373     uint64_t L;
00374     double D;
00375   } T;
00376   T.L = Bits;
00377   return T.D;
00378 }
00379 
00380 /// BitsToFloat - This function takes a 32-bit integer and returns the bit
00381 /// equivalent float.
00382 inline float BitsToFloat(uint32_t Bits) {
00383   union {
00384     uint32_t I;
00385     float F;
00386   } T;
00387   T.I = Bits;
00388   return T.F;
00389 }
00390 
00391 /// DoubleToBits - This function takes a double and returns the bit
00392 /// equivalent 64-bit integer.  Note that copying doubles around
00393 /// changes the bits of NaNs on some hosts, notably x86, so this
00394 /// routine cannot be used if these bits are needed.
00395 inline uint64_t DoubleToBits(double Double) {
00396   union {
00397     uint64_t L;
00398     double D;
00399   } T;
00400   T.D = Double;
00401   return T.L;
00402 }
00403 
00404 /// FloatToBits - This function takes a float and returns the bit
00405 /// equivalent 32-bit integer.  Note that copying floats around
00406 /// changes the bits of NaNs on some hosts, notably x86, so this
00407 /// routine cannot be used if these bits are needed.
00408 inline uint32_t FloatToBits(float Float) {
00409   union {
00410     uint32_t I;
00411     float F;
00412   } T;
00413   T.F = Float;
00414   return T.I;
00415 }
00416 
00417 /// Platform-independent wrappers for the C99 isnan() function.
00418 int IsNAN(float f);
00419 int IsNAN(double d);
00420 
00421 /// Platform-independent wrappers for the C99 isinf() function.
00422 int IsInf(float f);
00423 int IsInf(double d);
00424 
00425 /// MinAlign - A and B are either alignments or offsets.  Return the minimum
00426 /// alignment that may be assumed after adding the two together.
00427 inline uint64_t MinAlign(uint64_t A, uint64_t B) {
00428   // The largest power of 2 that divides both A and B.
00429   //
00430   // Replace "-Value" by "1+~Value" in the following commented code to avoid 
00431   // MSVC warning C4146
00432   //    return (A | B) & -(A | B);
00433   return (A | B) & (1 + ~(A | B));
00434 }
00435 
00436 /// NextPowerOf2 - Returns the next power of two (in 64-bits)
00437 /// that is strictly greater than A.  Returns zero on overflow.
00438 inline uint64_t NextPowerOf2(uint64_t A) {
00439   A |= (A >> 1);
00440   A |= (A >> 2);
00441   A |= (A >> 4);
00442   A |= (A >> 8);
00443   A |= (A >> 16);
00444   A |= (A >> 32);
00445   return A + 1;
00446 }
00447 
00448 /// Returns the next integer (mod 2**64) that is greater than or equal to
00449 /// \p Value and is a multiple of \p Align. \p Align must be non-zero.
00450 ///
00451 /// Examples:
00452 /// \code
00453 ///   RoundUpToAlignment(5, 8) = 8
00454 ///   RoundUpToAlignment(17, 8) = 24
00455 ///   RoundUpToAlignment(~0LL, 8) = 0
00456 /// \endcode
00457 inline uint64_t RoundUpToAlignment(uint64_t Value, uint64_t Align) {
00458   return ((Value + Align - 1) / Align) * Align;
00459 }
00460 
00461 /// Returns the offset to the next integer (mod 2**64) that is greater than
00462 /// or equal to \p Value and is a multiple of \p Align. \p Align must be
00463 /// non-zero.
00464 inline uint64_t OffsetToAlignment(uint64_t Value, uint64_t Align) {
00465   return RoundUpToAlignment(Value, Align) - Value;
00466 }
00467 
00468 /// abs64 - absolute value of a 64-bit int.  Not all environments support
00469 /// "abs" on whatever their name for the 64-bit int type is.  The absolute
00470 /// value of the largest negative number is undefined, as with "abs".
00471 inline int64_t abs64(int64_t x) {
00472   return (x < 0) ? -x : x;
00473 }
00474 
00475 /// SignExtend32 - Sign extend B-bit number x to 32-bit int.
00476 /// Usage int32_t r = SignExtend32<5>(x);
00477 template <unsigned B> inline int32_t SignExtend32(uint32_t x) {
00478   return int32_t(x << (32 - B)) >> (32 - B);
00479 }
00480 
00481 /// \brief Sign extend number in the bottom B bits of X to a 32-bit int.
00482 /// Requires 0 < B <= 32.
00483 inline int32_t SignExtend32(uint32_t X, unsigned B) {
00484   return int32_t(X << (32 - B)) >> (32 - B);
00485 }
00486 
00487 /// SignExtend64 - Sign extend B-bit number x to 64-bit int.
00488 /// Usage int64_t r = SignExtend64<5>(x);
00489 template <unsigned B> inline int64_t SignExtend64(uint64_t x) {
00490   return int64_t(x << (64 - B)) >> (64 - B);
00491 }
00492 
00493 /// \brief Sign extend number in the bottom B bits of X to a 64-bit int.
00494 /// Requires 0 < B <= 64.
00495 inline int64_t SignExtend64(uint64_t X, unsigned B) {
00496   return int64_t(X << (64 - B)) >> (64 - B);
00497 }
00498 
00499 } // End llvm namespace
00500 
00501 #endif