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