LLVM API Documentation
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/DataTypes.h" 00018 00019 namespace llvm { 00020 00021 // NOTE: The following support functions use the _32/_64 extensions instead of 00022 // type overloading so that signed and unsigned integers can be used without 00023 // ambiguity. 00024 00025 /// Hi_32 - This function returns the high 32 bits of a 64 bit value. 00026 inline uint32_t Hi_32(uint64_t Value) { 00027 return static_cast<uint32_t>(Value >> 32); 00028 } 00029 00030 /// Lo_32 - This function returns the low 32 bits of a 64 bit value. 00031 inline uint32_t Lo_32(uint64_t Value) { 00032 return static_cast<uint32_t>(Value); 00033 } 00034 00035 /// is?Type - these functions produce optimal testing for integer data types. 00036 inline bool isInt8 (int64_t Value) { 00037 return static_cast<int8_t>(Value) == Value; 00038 } 00039 inline bool isUInt8 (int64_t Value) { 00040 return static_cast<uint8_t>(Value) == Value; 00041 } 00042 inline bool isInt16 (int64_t Value) { 00043 return static_cast<int16_t>(Value) == Value; 00044 } 00045 inline bool isUInt16(int64_t Value) { 00046 return static_cast<uint16_t>(Value) == Value; 00047 } 00048 inline bool isInt32 (int64_t Value) { 00049 return static_cast<int32_t>(Value) == Value; 00050 } 00051 inline bool isUInt32(int64_t Value) { 00052 return static_cast<uint32_t>(Value) == Value; 00053 } 00054 00055 /// isMask_32 - This function returns true if the argument is a sequence of ones 00056 /// starting at the least significant bit with the remainder zero (32 bit 00057 /// version). Ex. isMask_32(0x0000FFFFU) == true. 00058 inline bool isMask_32(uint32_t Value) { 00059 return Value && ((Value + 1) & Value) == 0; 00060 } 00061 00062 /// isMask_64 - This function returns true if the argument is a sequence of ones 00063 /// starting at the least significant bit with the remainder zero (64 bit 00064 /// version). 00065 inline bool isMask_64(uint64_t Value) { 00066 return Value && ((Value + 1) & Value) == 0; 00067 } 00068 00069 /// isShiftedMask_32 - This function returns true if the argument contains a 00070 /// sequence of ones with the remainder zero (32 bit version.) 00071 /// Ex. isShiftedMask_32(0x0000FF00U) == true. 00072 inline bool isShiftedMask_32(uint32_t Value) { 00073 return isMask_32((Value - 1) | Value); 00074 } 00075 00076 /// isShiftedMask_64 - This function returns true if the argument contains a 00077 /// sequence of ones with the remainder zero (64 bit version.) 00078 inline bool isShiftedMask_64(uint64_t Value) { 00079 return isMask_64((Value - 1) | Value); 00080 } 00081 00082 /// isPowerOf2_32 - This function returns true if the argument is a power of 00083 /// two > 0. Ex. isPowerOf2_32(0x00100000U) == true (32 bit edition.) 00084 inline bool isPowerOf2_32(uint32_t Value) { 00085 return Value && !(Value & (Value - 1)); 00086 } 00087 00088 /// isPowerOf2_64 - This function returns true if the argument is a power of two 00089 /// > 0 (64 bit edition.) 00090 inline bool isPowerOf2_64(uint64_t Value) { 00091 return Value && !(Value & (Value - int64_t(1L))); 00092 } 00093 00094 /// ByteSwap_16 - This function returns a byte-swapped representation of the 00095 /// 16-bit argument, Value. 00096 inline uint16_t ByteSwap_16(uint16_t Value) { 00097 #if defined(_MSC_VER) && !defined(_DEBUG) 00098 // The DLL version of the runtime lacks these functions (bug!?), but in a 00099 // release build they're replaced with BSWAP instructions anyway. 00100 return _byteswap_ushort(Value); 00101 #else 00102 uint16_t Hi = Value << 8; 00103 uint16_t Lo = Value >> 8; 00104 return Hi | Lo; 00105 #endif 00106 } 00107 00108 /// ByteSwap_32 - This function returns a byte-swapped representation of the 00109 /// 32-bit argument, Value. 00110 inline uint32_t ByteSwap_32(uint32_t Value) { 00111 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3) 00112 return __builtin_bswap32(Value); 00113 #elif defined(_MSC_VER) && !defined(_DEBUG) 00114 return _byteswap_ulong(Value); 00115 #else 00116 uint32_t Byte0 = Value & 0x000000FF; 00117 uint32_t Byte1 = Value & 0x0000FF00; 00118 uint32_t Byte2 = Value & 0x00FF0000; 00119 uint32_t Byte3 = Value & 0xFF000000; 00120 return (Byte0 << 24) | (Byte1 << 8) | (Byte2 >> 8) | (Byte3 >> 24); 00121 #endif 00122 } 00123 00124 /// ByteSwap_64 - This function returns a byte-swapped representation of the 00125 /// 64-bit argument, Value. 00126 inline uint64_t ByteSwap_64(uint64_t Value) { 00127 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3) 00128 return __builtin_bswap64(Value); 00129 #elif defined(_MSC_VER) && !defined(_DEBUG) 00130 return _byteswap_uint64(Value); 00131 #else 00132 uint64_t Hi = ByteSwap_32(uint32_t(Value)); 00133 uint32_t Lo = ByteSwap_32(uint32_t(Value >> 32)); 00134 return (Hi << 32) | Lo; 00135 #endif 00136 } 00137 00138 /// CountLeadingZeros_32 - this function performs the platform optimal form of 00139 /// counting the number of zeros from the most significant bit to the first one 00140 /// bit. Ex. CountLeadingZeros_32(0x00F000FF) == 8. 00141 /// Returns 32 if the word is zero. 00142 inline unsigned CountLeadingZeros_32(uint32_t Value) { 00143 unsigned Count; // result 00144 #if __GNUC__ >= 4 00145 // PowerPC is defined for __builtin_clz(0) 00146 #if !defined(__ppc__) && !defined(__ppc64__) 00147 if (!Value) return 32; 00148 #endif 00149 Count = __builtin_clz(Value); 00150 #else 00151 if (!Value) return 32; 00152 Count = 0; 00153 // bisecton method for count leading zeros 00154 for (unsigned Shift = 32 >> 1; Shift; Shift >>= 1) { 00155 uint32_t Tmp = Value >> Shift; 00156 if (Tmp) { 00157 Value = Tmp; 00158 } else { 00159 Count |= Shift; 00160 } 00161 } 00162 #endif 00163 return Count; 00164 } 00165 00166 /// CountLeadingOnes_32 - this function performs the operation of 00167 /// counting the number of ones from the most significant bit to the first zero 00168 /// bit. Ex. CountLeadingOnes_32(0xFF0FFF00) == 8. 00169 /// Returns 32 if the word is all ones. 00170 inline unsigned CountLeadingOnes_32(uint32_t Value) { 00171 return CountLeadingZeros_32(~Value); 00172 } 00173 00174 /// CountLeadingZeros_64 - This function performs the platform optimal form 00175 /// of counting the number of zeros from the most significant bit to the first 00176 /// one bit (64 bit edition.) 00177 /// Returns 64 if the word is zero. 00178 inline unsigned CountLeadingZeros_64(uint64_t Value) { 00179 unsigned Count; // result 00180 #if __GNUC__ >= 4 00181 // PowerPC is defined for __builtin_clzll(0) 00182 #if !defined(__ppc__) && !defined(__ppc64__) 00183 if (!Value) return 64; 00184 #endif 00185 Count = __builtin_clzll(Value); 00186 #else 00187 if (sizeof(long) == sizeof(int64_t)) { 00188 if (!Value) return 64; 00189 Count = 0; 00190 // bisecton method for count leading zeros 00191 for (unsigned Shift = 64 >> 1; Shift; Shift >>= 1) { 00192 uint64_t Tmp = Value >> Shift; 00193 if (Tmp) { 00194 Value = Tmp; 00195 } else { 00196 Count |= Shift; 00197 } 00198 } 00199 } else { 00200 // get hi portion 00201 uint32_t Hi = Hi_32(Value); 00202 00203 // if some bits in hi portion 00204 if (Hi) { 00205 // leading zeros in hi portion plus all bits in lo portion 00206 Count = CountLeadingZeros_32(Hi); 00207 } else { 00208 // get lo portion 00209 uint32_t Lo = Lo_32(Value); 00210 // same as 32 bit value 00211 Count = CountLeadingZeros_32(Lo)+32; 00212 } 00213 } 00214 #endif 00215 return Count; 00216 } 00217 00218 /// CountLeadingOnes_64 - This function performs the operation 00219 /// of counting the number of ones from the most significant bit to the first 00220 /// zero bit (64 bit edition.) 00221 /// Returns 64 if the word is all ones. 00222 inline unsigned CountLeadingOnes_64(uint64_t Value) { 00223 return CountLeadingZeros_64(~Value); 00224 } 00225 00226 /// CountTrailingZeros_32 - this function performs the platform optimal form of 00227 /// counting the number of zeros from the least significant bit to the first one 00228 /// bit. Ex. CountTrailingZeros_32(0xFF00FF00) == 8. 00229 /// Returns 32 if the word is zero. 00230 inline unsigned CountTrailingZeros_32(uint32_t Value) { 00231 #if __GNUC__ >= 4 00232 return Value ? __builtin_ctz(Value) : 32; 00233 #else 00234 static const unsigned Mod37BitPosition[] = { 00235 32, 0, 1, 26, 2, 23, 27, 0, 3, 16, 24, 30, 28, 11, 0, 13, 00236 4, 7, 17, 0, 25, 22, 31, 15, 29, 10, 12, 6, 0, 21, 14, 9, 00237 5, 20, 8, 19, 18 00238 }; 00239 return Mod37BitPosition[(-Value & Value) % 37]; 00240 #endif 00241 } 00242 00243 /// CountTrailingOnes_32 - this function performs the operation of 00244 /// counting the number of ones from the least significant bit to the first zero 00245 /// bit. Ex. CountTrailingOnes_32(0x00FF00FF) == 8. 00246 /// Returns 32 if the word is all ones. 00247 inline unsigned CountTrailingOnes_32(uint32_t Value) { 00248 return CountTrailingZeros_32(~Value); 00249 } 00250 00251 /// CountTrailingZeros_64 - This function performs the platform optimal form 00252 /// of counting the number of zeros from the least significant bit to the first 00253 /// one bit (64 bit edition.) 00254 /// Returns 64 if the word is zero. 00255 inline unsigned CountTrailingZeros_64(uint64_t Value) { 00256 #if __GNUC__ >= 4 00257 return Value ? __builtin_ctzll(Value) : 64; 00258 #else 00259 static const unsigned Mod67Position[] = { 00260 64, 0, 1, 39, 2, 15, 40, 23, 3, 12, 16, 59, 41, 19, 24, 54, 00261 4, 64, 13, 10, 17, 62, 60, 28, 42, 30, 20, 51, 25, 44, 55, 00262 47, 5, 32, 65, 38, 14, 22, 11, 58, 18, 53, 63, 9, 61, 27, 00263 29, 50, 43, 46, 31, 37, 21, 57, 52, 8, 26, 49, 45, 36, 56, 00264 7, 48, 35, 6, 34, 33, 0 00265 }; 00266 return Mod67Position[(-Value & Value) % 67]; 00267 #endif 00268 } 00269 00270 /// CountTrailingOnes_64 - This function performs the operation 00271 /// of counting the number of ones from the least significant bit to the first 00272 /// zero bit (64 bit edition.) 00273 /// Returns 64 if the word is all ones. 00274 inline unsigned CountTrailingOnes_64(uint64_t Value) { 00275 return CountTrailingZeros_64(~Value); 00276 } 00277 00278 /// CountPopulation_32 - this function counts the number of set bits in a value. 00279 /// Ex. CountPopulation(0xF000F000) = 8 00280 /// Returns 0 if the word is zero. 00281 inline unsigned CountPopulation_32(uint32_t Value) { 00282 #if __GNUC__ >= 4 00283 return __builtin_popcount(Value); 00284 #else 00285 uint32_t v = Value - ((Value >> 1) & 0x55555555); 00286 v = (v & 0x33333333) + ((v >> 2) & 0x33333333); 00287 return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24; 00288 #endif 00289 } 00290 00291 /// CountPopulation_64 - this function counts the number of set bits in a value, 00292 /// (64 bit edition.) 00293 inline unsigned CountPopulation_64(uint64_t Value) { 00294 #if __GNUC__ >= 4 00295 return __builtin_popcountll(Value); 00296 #else 00297 uint64_t v = Value - ((Value >> 1) & 0x5555555555555555ULL); 00298 v = (v & 0x3333333333333333ULL) + ((v >> 2) & 0x3333333333333333ULL); 00299 v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0FULL; 00300 return unsigned((uint64_t)(v * 0x0101010101010101ULL) >> 56); 00301 #endif 00302 } 00303 00304 /// Log2_32 - This function returns the floor log base 2 of the specified value, 00305 /// -1 if the value is zero. (32 bit edition.) 00306 /// Ex. Log2_32(32) == 5, Log2_32(1) == 0, Log2_32(0) == -1, Log2_32(6) == 2 00307 inline unsigned Log2_32(uint32_t Value) { 00308 return 31 - CountLeadingZeros_32(Value); 00309 } 00310 00311 /// Log2_64 - This function returns the floor log base 2 of the specified value, 00312 /// -1 if the value is zero. (64 bit edition.) 00313 inline unsigned Log2_64(uint64_t Value) { 00314 return 63 - CountLeadingZeros_64(Value); 00315 } 00316 00317 /// Log2_32_Ceil - This function returns the ceil log base 2 of the specified 00318 /// value, 32 if the value is zero. (32 bit edition). 00319 /// Ex. Log2_32_Ceil(32) == 5, Log2_32_Ceil(1) == 0, Log2_32_Ceil(6) == 3 00320 inline unsigned Log2_32_Ceil(uint32_t Value) { 00321 return 32-CountLeadingZeros_32(Value-1); 00322 } 00323 00324 /// Log2_64 - This function returns the ceil log base 2 of the specified value, 00325 /// 64 if the value is zero. (64 bit edition.) 00326 inline unsigned Log2_64_Ceil(uint64_t Value) { 00327 return 64-CountLeadingZeros_64(Value-1); 00328 } 00329 00330 /// GreatestCommonDivisor64 - Return the greatest common divisor of the two 00331 /// values using Euclid's algorithm. 00332 inline uint64_t GreatestCommonDivisor64(uint64_t A, uint64_t B) { 00333 while (B) { 00334 uint64_t T = B; 00335 B = A % B; 00336 A = T; 00337 } 00338 return A; 00339 } 00340 00341 /// BitsToDouble - This function takes a 64-bit integer and returns the bit 00342 /// equivalent double. 00343 inline double BitsToDouble(uint64_t Bits) { 00344 union { 00345 uint64_t L; 00346 double D; 00347 } T; 00348 T.L = Bits; 00349 return T.D; 00350 } 00351 00352 /// BitsToFloat - This function takes a 32-bit integer and returns the bit 00353 /// equivalent float. 00354 inline float BitsToFloat(uint32_t Bits) { 00355 union { 00356 uint32_t I; 00357 float F; 00358 } T; 00359 T.I = Bits; 00360 return T.F; 00361 } 00362 00363 /// DoubleToBits - This function takes a double and returns the bit 00364 /// equivalent 64-bit integer. Note that copying doubles around 00365 /// changes the bits of NaNs on some hosts, notably x86, so this 00366 /// routine cannot be used if these bits are needed. 00367 inline uint64_t DoubleToBits(double Double) { 00368 union { 00369 uint64_t L; 00370 double D; 00371 } T; 00372 T.D = Double; 00373 return T.L; 00374 } 00375 00376 /// FloatToBits - This function takes a float and returns the bit 00377 /// equivalent 32-bit integer. Note that copying floats around 00378 /// changes the bits of NaNs on some hosts, notably x86, so this 00379 /// routine cannot be used if these bits are needed. 00380 inline uint32_t FloatToBits(float Float) { 00381 union { 00382 uint32_t I; 00383 float F; 00384 } T; 00385 T.F = Float; 00386 return T.I; 00387 } 00388 00389 /// Platform-independent wrappers for the C99 isnan() function. 00390 int IsNAN(float f); 00391 int IsNAN(double d); 00392 00393 /// Platform-independent wrappers for the C99 isinf() function. 00394 int IsInf(float f); 00395 int IsInf(double d); 00396 00397 /// MinAlign - A and B are either alignments or offsets. Return the minimum 00398 /// alignment that may be assumed after adding the two together. 00399 static inline uint64_t MinAlign(uint64_t A, uint64_t B) { 00400 // The largest power of 2 that divides both A and B. 00401 return (A | B) & -(A | B); 00402 } 00403 00404 /// NextPowerOf2 - Returns the next power of two (in 64-bits) 00405 /// that is strictly greater than A. Returns zero on overflow. 00406 static inline uint64_t NextPowerOf2(uint64_t A) { 00407 A |= (A >> 1); 00408 A |= (A >> 2); 00409 A |= (A >> 4); 00410 A |= (A >> 8); 00411 A |= (A >> 16); 00412 A |= (A >> 32); 00413 return A + 1; 00414 } 00415 00416 } // End llvm namespace 00417 00418 #endif