MathExtras.h revision 88c606eb0cc5415ed367c24e073f7bb478501d34
1551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer//===-- llvm/Support/MathExtras.h - Useful math functions -------*- C++ -*-===// 263b3afa98460ce38a1c48d3c44ef6edfdaf37b77Misha Brukman// 3b2109ce97881269a610fa4afbcbca350e975174dJohn Criswell// The LLVM Compiler Infrastructure 4b2109ce97881269a610fa4afbcbca350e975174dJohn Criswell// 5b2109ce97881269a610fa4afbcbca350e975174dJohn Criswell// This file was developed by the LLVM research group and is distributed under 6b2109ce97881269a610fa4afbcbca350e975174dJohn Criswell// the University of Illinois Open Source License. See LICENSE.TXT for details. 763b3afa98460ce38a1c48d3c44ef6edfdaf37b77Misha Brukman// 8b2109ce97881269a610fa4afbcbca350e975174dJohn Criswell//===----------------------------------------------------------------------===// 954ea60c69e69b8e5a464a1d7688ceec5c68bacd5Chris Lattner// 1054ea60c69e69b8e5a464a1d7688ceec5c68bacd5Chris Lattner// This file contains some functions that are useful for math stuff. 1154ea60c69e69b8e5a464a1d7688ceec5c68bacd5Chris Lattner// 1254ea60c69e69b8e5a464a1d7688ceec5c68bacd5Chris Lattner//===----------------------------------------------------------------------===// 13cee8f9ae67104576b2028125b56e9ba4856a1d66Chris Lattner 14551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#ifndef LLVM_SUPPORT_MATHEXTRAS_H 15551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#define LLVM_SUPPORT_MATHEXTRAS_H 16cee8f9ae67104576b2028125b56e9ba4856a1d66Chris Lattner 17551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/Support/DataTypes.h" 18cee8f9ae67104576b2028125b56e9ba4856a1d66Chris Lattner 19d0fde30ce850b78371fd1386338350591f9ff494Brian Gaekenamespace llvm { 20d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke 2188c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner// NOTE: The following support functions use the _32/_64 extensions instead of type 2288c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner// overloading so that signed and unsigned integers can be used without ambiguity. 2388c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner 2488c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner 2588c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner// Hi_32 - This function returns the high 32 bits of a 64 bit value. 2688c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattnerinline unsigned Hi_32(uint64_t Value) { 2788c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner return (unsigned)(Value >> 32); 2888c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner} 2988c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner 3088c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner// Lo_32 - This function returns the low 32 bits of a 64 bit value. 3188c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattnerinline unsigned Lo_32(uint64_t Value) { 3288c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner return (unsigned)Value; 3388c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner} 3488c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner 3588c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner// is?Type - these functions produce optimal testing for integer data types. 3688c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattnerinline bool isInt8 (int Value) { return ( signed char )Value == Value; } 3788c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattnerinline bool isUInt8 (int Value) { return (unsigned char )Value == Value; } 3888c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattnerinline bool isInt16 (int Value) { return ( signed short)Value == Value; } 3988c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattnerinline bool isUInt16(int Value) { return (unsigned short)Value == Value; } 4088c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattnerinline bool isInt32 (int64_t Value) { return ( signed int )Value == Value; } 4188c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattnerinline bool isUInt32(int64_t Value) { return (unsigned int )Value == Value; } 4288c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner 4388c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner// isMask_32 - This function returns true if the argument is a sequence of ones starting 4488c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner// at the least significant bit with the remainder zero (32 bit version.) 4588c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner// Ex. isMask_32(0x0000FFFFU) == true. 4688c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattnerinline const bool isMask_32(unsigned Value) { 4788c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner return Value && ((Value + 1) & Value) == 0; 4888c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner} 4988c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner 5088c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner// isMask_64 - This function returns true if the argument is a sequence of ones starting 5188c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner// at the least significant bit with the remainder zero (64 bit version.) 5288c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattnerinline const bool isMask_64(uint64_t Value) { 5388c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner return Value && ((Value + 1) & Value) == 0; 5488c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner} 5588c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner 5688c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner// isShiftedMask_32 - This function returns true if the argument contains a sequence of ones 5788c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner// with the remainder zero (32 bit version.) 5888c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner// Ex. isShiftedMask_32(0x0000FF00U) == true. 5988c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattnerinline const bool isShiftedMask_32(unsigned Value) { 6088c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner return isMask_32((Value - 1) | Value); 6188c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner} 6288c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner 6388c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner// isShiftedMask_64 - This function returns true if the argument contains a sequence of ones 6488c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner// with the remainder zero (64 bit version.) 6588c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattnerinline const bool isShiftedMask_64(uint64_t Value) { 6688c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner return isMask_64((Value - 1) | Value); 6788c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner} 6888c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner 6988c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner// isPowerOf2_32 - This function returns true if the argument is a power of two > 0. 7088c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner// Ex. isPowerOf2_32(0x00100000U) == true (32 bit edition.) 7188c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattnerinline bool isPowerOf2_32(unsigned Value) { 7288c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner return Value && !(Value & (Value - 1)); 7388c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner} 7488c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner 7588c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner// isPowerOf2_64 - This function returns true if the argument is a power of two > 0 7688c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner// (64 bit edition.) 7788c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattnerinline bool isPowerOf2_64(uint64_t Value) { 7888c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner return Value && !(Value & (Value - 1LL)); 7988c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner} 8088c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner 8188c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner// CountLeadingZeros_32 - this function performs the platform optimal form 8288c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner// of counting the number of zeros from the most significant bit to the first one bit. 8388c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner// Ex. CountLeadingZeros_32(0x00F000FF) == 8. 8488c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner// Returns 32 if the word is zero. 8588c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattnerinline unsigned CountLeadingZeros_32(unsigned Value) { 8688c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner unsigned Count; // result 8788c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner #if __GNUC__ >= 4 8888c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner // PowerPC is defined for __builtin_clz(0) 8988c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner #if defined(__ppc__) || defined(__ppc64__) 9088c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner if (!Value) return 32; 91bb92f6fbf2c36b3530f33eb2e8d1842764ec9fddBrian Gaeke#endif 9288c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner Count = __builtin_clz(Value); 9388c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner #else 9488c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner if (!Value) return 32; 9588c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner Count = 0; 9688c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner // bisecton method for count leading zeros 9788c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner for (unsigned Shift = 32 >> 1; Shift; Shift >>= 1) { 9888c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner unsigned Tmp = Value >> Shift; 9988c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner if (Tmp) { 10088c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner Count |= Shift; 10188c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner Value = Tmp; 10288c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner } 10388c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner } 10488c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner #endif 10588c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner return Count; 10688c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner} 107bb92f6fbf2c36b3530f33eb2e8d1842764ec9fddBrian Gaeke 10888c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner// CountLeadingZeros_64 - This function performs the platform optimal form 10988c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner// of counting the number of zeros from the most significant bit to the first one bit 11088c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner// (64 bit edition.) 11188c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner// Returns 64 if the word is zero. 11288c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattnerinline unsigned CountLeadingZeros_64(uint64_t Value) { 11388c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner unsigned Count; // result 11488c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner #if __GNUC__ >= 4 11588c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner // PowerPC is defined for __builtin_clzll(0) 11688c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner #if defined(__ppc__) || defined(__ppc64__) 11788c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner if (!Value) return 64; 11888c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner #endif 11988c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner Count = __builtin_clzll(Value); 12088c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner #elif sizeof(long) == sizeof(int64_t) 12188c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner if (!Value) return 64; 12288c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner Count = 0; 12388c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner // bisecton method for count leading zeros 12488c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner for (uint64_t Shift = 64 >> 1; Shift; Shift >>= 1) { 12588c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner uint64_t Tmp = Value >> Shift; 12688c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner if (Tmp) { 12788c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner Count |= Shift; 12888c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner Value = Tmp; 12988c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner } 130bcfa423e3d1a2a385b56e3ec0f03137f0c33efc7Vikram S. Adve} 13188c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner #else 13288c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner // get hi portion 13388c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner unsigned Hi = Hi_32(Value); 134cee8f9ae67104576b2028125b56e9ba4856a1d66Chris Lattner 13588c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner // if some bits in hi portion 13688c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner if (Hi) { 13788c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner // leading zeros in hi portion plus all bits in lo portion 13888c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner Count = CountLeadingZeros_32(Hi) + 32; 13988c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner } else { 14088c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner // get lo portion 14188c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner unsigned Lo = Lo_32(Value); 14288c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner // same as 32 bit value 14388c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner Count = CountLeadingZeros_32(Lo); 14488c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner } 14588c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner #endif 14688c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner return Count; 147e265504e82310266271fa2329b6ef8115bbe8244Chris Lattner} 148e265504e82310266271fa2329b6ef8115bbe8244Chris Lattner 14988c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner// Log2_32 - This function returns the floor log base 2 of the specified value, -1 if the value is zero. 15088c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner// (32 bit edition.) 15188c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner// Ex. Log2_32(32) == 5, Log2_32(1) == 0, Log2_32(0) == -1 15288c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattnerinline unsigned Log2_32(unsigned Value) { 15388c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner return 31 - CountLeadingZeros_32(Value); 15454ea60c69e69b8e5a464a1d7688ceec5c68bacd5Chris Lattner } 15554ea60c69e69b8e5a464a1d7688ceec5c68bacd5Chris Lattner 15688c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner// Log2_64 - This function returns the floor log base 2 of the specified value, -1 if the value is zero. 15788c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner// (64 bit edition.) 15888c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattnerinline unsigned Log2_64(unsigned Value) { 15988c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner return 63 - CountLeadingZeros_64(Value); 160cee8f9ae67104576b2028125b56e9ba4856a1d66Chris Lattner} 161cee8f9ae67104576b2028125b56e9ba4856a1d66Chris Lattner 1627764f4b2e0e550dc23f3c536f236f9abf86879dcBrian Gaeke// Platform-independent wrappers for the C99 isnan() function. 1637764f4b2e0e550dc23f3c536f236f9abf86879dcBrian Gaekeint IsNAN (float f); 1647764f4b2e0e550dc23f3c536f236f9abf86879dcBrian Gaekeint IsNAN (double d); 1657764f4b2e0e550dc23f3c536f236f9abf86879dcBrian Gaeke 166a7d03b466a41e68e11480ae6ca275140fe4c4507Brian Gaeke// Platform-independent wrappers for the C99 isinf() function. 167a7d03b466a41e68e11480ae6ca275140fe4c4507Brian Gaekeint IsInf (float f); 168a7d03b466a41e68e11480ae6ca275140fe4c4507Brian Gaekeint IsInf (double d); 169a7d03b466a41e68e11480ae6ca275140fe4c4507Brian Gaeke 170d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke} // End llvm namespace 171d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke 17254ea60c69e69b8e5a464a1d7688ceec5c68bacd5Chris Lattner#endif 173