LLVM API Documentation
00001 //===-- llvm/Support/MathExtras.h - Useful math functions -------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file was developed by the LLVM research group and is distributed under 00006 // the University of Illinois Open Source 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 00026 // Hi_32 - This function returns the high 32 bits of a 64 bit value. 00027 inline unsigned Hi_32(uint64_t Value) { 00028 return (unsigned)(Value >> 32); 00029 } 00030 00031 // Lo_32 - This function returns the low 32 bits of a 64 bit value. 00032 inline unsigned Lo_32(uint64_t Value) { 00033 return (unsigned)Value; 00034 } 00035 00036 // is?Type - these functions produce optimal testing for integer data types. 00037 inline bool isInt8 (int Value) { return ( signed char )Value == Value; } 00038 inline bool isUInt8 (int Value) { return (unsigned char )Value == Value; } 00039 inline bool isInt16 (int Value) { return ( signed short)Value == Value; } 00040 inline bool isUInt16(int Value) { return (unsigned short)Value == Value; } 00041 inline bool isInt32 (int64_t Value) { return ( signed int )Value == Value; } 00042 inline bool isUInt32(int64_t Value) { return (unsigned int )Value == Value; } 00043 00044 // isMask_32 - This function returns true if the argument is a sequence of ones 00045 // starting at the least significant bit with the remainder zero (32 bit version.) 00046 // Ex. isMask_32(0x0000FFFFU) == true. 00047 inline const bool isMask_32(unsigned Value) { 00048 return Value && ((Value + 1) & Value) == 0; 00049 } 00050 00051 // isMask_64 - This function returns true if the argument is a sequence of ones 00052 // starting at the least significant bit with the remainder zero (64 bit version.) 00053 inline const bool isMask_64(uint64_t Value) { 00054 return Value && ((Value + 1) & Value) == 0; 00055 } 00056 00057 // isShiftedMask_32 - This function returns true if the argument contains a 00058 // sequence of ones with the remainder zero (32 bit version.) 00059 // Ex. isShiftedMask_32(0x0000FF00U) == true. 00060 inline const bool isShiftedMask_32(unsigned Value) { 00061 return isMask_32((Value - 1) | Value); 00062 } 00063 00064 // isShiftedMask_64 - This function returns true if the argument contains a 00065 // sequence of ones with the remainder zero (64 bit version.) 00066 inline const bool isShiftedMask_64(uint64_t Value) { 00067 return isMask_64((Value - 1) | Value); 00068 } 00069 00070 // isPowerOf2_32 - This function returns true if the argument is a power of 00071 // two > 0. Ex. isPowerOf2_32(0x00100000U) == true (32 bit edition.) 00072 inline bool isPowerOf2_32(unsigned Value) { 00073 return Value && !(Value & (Value - 1)); 00074 } 00075 00076 // isPowerOf2_64 - This function returns true if the argument is a power of two 00077 // > 0 (64 bit edition.) 00078 inline bool isPowerOf2_64(uint64_t Value) { 00079 return Value && !(Value & (Value - 1LL)); 00080 } 00081 00082 // ByteSwap_16 - This function returns a byte-swapped representation of the 00083 // 16-bit argument, Value. 00084 inline unsigned short ByteSwap_16(unsigned short Value) { 00085 unsigned short Hi = Value << 8; 00086 unsigned short Lo = Value >> 8; 00087 return Hi | Lo; 00088 } 00089 00090 // ByteSwap_32 - This function returns a byte-swapped representation of the 00091 // 32-bit argument, Value. 00092 inline unsigned ByteSwap_32(unsigned Value) { 00093 unsigned Byte0 = Value & 0x000000FF; 00094 unsigned Byte1 = Value & 0x0000FF00; 00095 unsigned Byte2 = Value & 0x00FF0000; 00096 unsigned Byte3 = Value & 0xFF000000; 00097 return (Byte0 << 24) | (Byte1 << 8) | (Byte2 >> 8) | (Byte3 >> 24); 00098 } 00099 00100 // ByteSwap_64 - This function returns a byte-swapped representation of the 00101 // 64-bit argument, Value. 00102 inline uint64_t ByteSwap_64(uint64_t Value) { 00103 uint64_t Hi = ByteSwap_32(unsigned(Value)); 00104 uint64_t Lo = ByteSwap_32(unsigned(Value >> 32)); 00105 return (Hi << 32) | Lo; 00106 } 00107 00108 // CountLeadingZeros_32 - this function performs the platform optimal form of 00109 // counting the number of zeros from the most significant bit to the first one 00110 // bit. Ex. CountLeadingZeros_32(0x00F000FF) == 8. 00111 // Returns 32 if the word is zero. 00112 inline unsigned CountLeadingZeros_32(unsigned Value) { 00113 unsigned Count; // result 00114 #if __GNUC__ >= 4 00115 // PowerPC is defined for __builtin_clz(0) 00116 #if !defined(__ppc__) && !defined(__ppc64__) 00117 if (!Value) return 32; 00118 #endif 00119 Count = __builtin_clz(Value); 00120 #else 00121 if (!Value) return 32; 00122 Count = 0; 00123 // bisecton method for count leading zeros 00124 for (unsigned Shift = 32 >> 1; Shift; Shift >>= 1) { 00125 unsigned Tmp = Value >> Shift; 00126 if (Tmp) { 00127 Value = Tmp; 00128 } else { 00129 Count |= Shift; 00130 } 00131 } 00132 #endif 00133 return Count; 00134 } 00135 00136 // CountLeadingZeros_64 - This function performs the platform optimal form 00137 // of counting the number of zeros from the most significant bit to the first 00138 // one bit (64 bit edition.) 00139 // Returns 64 if the word is zero. 00140 inline unsigned CountLeadingZeros_64(uint64_t Value) { 00141 unsigned Count; // result 00142 #if __GNUC__ >= 4 00143 // PowerPC is defined for __builtin_clzll(0) 00144 #if !defined(__ppc__) && !defined(__ppc64__) 00145 if (!Value) return 64; 00146 #endif 00147 Count = __builtin_clzll(Value); 00148 #else 00149 if (sizeof(long) == sizeof(int64_t)) { 00150 if (!Value) return 64; 00151 Count = 0; 00152 // bisecton method for count leading zeros 00153 for (uint64_t Shift = 64 >> 1; Shift; Shift >>= 1) { 00154 uint64_t Tmp = Value >> Shift; 00155 if (Tmp) { 00156 Value = Tmp; 00157 } else { 00158 Count |= Shift; 00159 } 00160 } 00161 } else { 00162 // get hi portion 00163 unsigned Hi = Hi_32(Value); 00164 00165 // if some bits in hi portion 00166 if (Hi) { 00167 // leading zeros in hi portion plus all bits in lo portion 00168 Count = CountLeadingZeros_32(Hi); 00169 } else { 00170 // get lo portion 00171 unsigned Lo = Lo_32(Value); 00172 // same as 32 bit value 00173 Count = CountLeadingZeros_32(Lo)+32; 00174 } 00175 } 00176 #endif 00177 return Count; 00178 } 00179 00180 // CountTrailingZeros_32 - this function performs the platform optimal form of 00181 // counting the number of zeros from the least significant bit to the first one 00182 // bit. Ex. CountTrailingZeros_32(0xFF00FF00) == 8. 00183 // Returns 32 if the word is zero. 00184 inline unsigned CountTrailingZeros_32(unsigned Value) { 00185 return 32 - CountLeadingZeros_32(~Value & (Value - 1)); 00186 } 00187 00188 // CountTrailingZeros_64 - This function performs the platform optimal form 00189 // of counting the number of zeros from the least significant bit to the first 00190 // one bit (64 bit edition.) 00191 // Returns 64 if the word is zero. 00192 inline unsigned CountTrailingZeros_64(uint64_t Value) { 00193 return 64 - CountLeadingZeros_64(~Value & (Value - 1)); 00194 } 00195 00196 // CountPopulation_32 - this function counts the number of set bits in a value. 00197 // Ex. CountPopulation(0xF000F000) = 8 00198 // Returns 0 if the word is zero. 00199 inline unsigned CountPopulation_32(unsigned Value) { 00200 unsigned x, t; 00201 x = Value - ((Value >> 1) & 0x55555555); 00202 t = ((x >> 2) & 0x33333333); 00203 x = (x & 0x33333333) + t; 00204 x = (x + (x >> 4)) & 0x0F0F0F0F; 00205 x = x + (x << 8); 00206 x = x + (x << 16); 00207 return x >> 24; 00208 } 00209 00210 // CountPopulation_64 - this function counts the number of set bits in a value, 00211 // (64 bit edition.) 00212 inline unsigned CountPopulation_64(uint64_t Value) { 00213 return CountPopulation_32(unsigned(Value >> 32)) + 00214 CountPopulation_32(unsigned(Value)); 00215 } 00216 00217 // Log2_32 - This function returns the floor log base 2 of the specified value, 00218 // -1 if the value is zero. (32 bit edition.) 00219 // Ex. Log2_32(32) == 5, Log2_32(1) == 0, Log2_32(0) == -1 00220 inline unsigned Log2_32(unsigned Value) { 00221 return 31 - CountLeadingZeros_32(Value); 00222 } 00223 00224 // Log2_64 - This function returns the floor log base 2 of the specified value, 00225 // -1 if the value is zero. (64 bit edition.) 00226 inline unsigned Log2_64(uint64_t Value) { 00227 return 63 - CountLeadingZeros_64(Value); 00228 } 00229 00230 // BitsToDouble - This function takes a 64-bit integer and returns the bit 00231 // equivalent double. 00232 inline double BitsToDouble(uint64_t Bits) { 00233 union { 00234 uint64_t L; 00235 double D; 00236 } T; 00237 T.L = Bits; 00238 return T.D; 00239 } 00240 00241 // BitsToFloat - This function takes a 32-bit integer and returns the bit 00242 // equivalent float. 00243 inline float BitsToFloat(uint32_t Bits) { 00244 union { 00245 uint32_t I; 00246 float F; 00247 } T; 00248 T.I = Bits; 00249 return T.F; 00250 } 00251 00252 // DoubleToBits - This function takes a double and returns the bit 00253 // equivalent 64-bit integer. 00254 inline uint64_t DoubleToBits(double Double) { 00255 union { 00256 uint64_t L; 00257 double D; 00258 } T; 00259 T.D = Double; 00260 return T.L; 00261 } 00262 00263 // FloatToBits - This function takes a float and returns the bit 00264 // equivalent 32-bit integer. 00265 inline uint32_t FloatToBits(float Float) { 00266 union { 00267 uint32_t I; 00268 float F; 00269 } T; 00270 T.F = Float; 00271 return T.I; 00272 } 00273 00274 // Platform-independent wrappers for the C99 isnan() function. 00275 int IsNAN (float f); 00276 int IsNAN (double d); 00277 00278 // Platform-independent wrappers for the C99 isinf() function. 00279 int IsInf (float f); 00280 int IsInf (double d); 00281 00282 } // End llvm namespace 00283 00284 #endif