1 //===-- llvm/Support/MathExtras.h - Useful math functions -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains some functions that are useful for math stuff.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_SUPPORT_MATHEXTRAS_H
15 #define LLVM_SUPPORT_MATHEXTRAS_H
17 #include "llvm/Support/DataTypes.h"
18 #include "llvm/System/IncludeFile.h"
22 // NOTE: The following support functions use the _32/_64 extensions instead of
23 // type overloading so that signed and unsigned integers can be used without
27 // Hi_32 - This function returns the high 32 bits of a 64 bit value.
28 inline unsigned Hi_32(uint64_t Value) {
29 return static_cast<unsigned>(Value >> 32);
32 // Lo_32 - This function returns the low 32 bits of a 64 bit value.
33 inline unsigned Lo_32(uint64_t Value) {
34 return static_cast<unsigned>(Value);
37 // is?Type - these functions produce optimal testing for integer data types.
38 inline bool isInt8 (int Value) {
39 return static_cast<signed char>(Value) == Value;
41 inline bool isUInt8 (int Value) {
42 return static_cast<unsigned char>(Value) == Value;
44 inline bool isInt16 (int Value) {
45 return static_cast<signed short>(Value) == Value;
47 inline bool isUInt16(int Value) {
48 return static_cast<unsigned short>(Value) == Value;
50 inline bool isInt32 (int64_t Value) {
51 return static_cast<signed int>(Value) == Value;
53 inline bool isUInt32(int64_t Value) {
54 return static_cast<unsigned int>(Value) == Value;
57 // isMask_32 - This function returns true if the argument is a sequence of ones
58 // starting at the least significant bit with the remainder zero (32 bit version.)
59 // Ex. isMask_32(0x0000FFFFU) == true.
60 inline const bool isMask_32(unsigned Value) {
61 return Value && ((Value + 1) & Value) == 0;
64 // isMask_64 - This function returns true if the argument is a sequence of ones
65 // starting at the least significant bit with the remainder zero (64 bit version.)
66 inline const bool isMask_64(uint64_t Value) {
67 return Value && ((Value + 1) & Value) == 0;
70 // isShiftedMask_32 - This function returns true if the argument contains a
71 // sequence of ones with the remainder zero (32 bit version.)
72 // Ex. isShiftedMask_32(0x0000FF00U) == true.
73 inline const bool isShiftedMask_32(unsigned Value) {
74 return isMask_32((Value - 1) | Value);
77 // isShiftedMask_64 - This function returns true if the argument contains a
78 // sequence of ones with the remainder zero (64 bit version.)
79 inline const bool isShiftedMask_64(uint64_t Value) {
80 return isMask_64((Value - 1) | Value);
83 // isPowerOf2_32 - This function returns true if the argument is a power of
84 // two > 0. Ex. isPowerOf2_32(0x00100000U) == true (32 bit edition.)
85 inline bool isPowerOf2_32(unsigned Value) {
86 return Value && !(Value & (Value - 1));
89 // isPowerOf2_64 - This function returns true if the argument is a power of two
90 // > 0 (64 bit edition.)
91 inline bool isPowerOf2_64(uint64_t Value) {
92 return Value && !(Value & (Value - int64_t(1L)));
95 // ByteSwap_16 - This function returns a byte-swapped representation of the
96 // 16-bit argument, Value.
97 inline unsigned short ByteSwap_16(unsigned short Value) {
98 unsigned short Hi = Value << 8;
99 unsigned short Lo = Value >> 8;
103 // ByteSwap_32 - This function returns a byte-swapped representation of the
104 // 32-bit argument, Value.
105 inline unsigned ByteSwap_32(unsigned Value) {
106 unsigned Byte0 = Value & 0x000000FF;
107 unsigned Byte1 = Value & 0x0000FF00;
108 unsigned Byte2 = Value & 0x00FF0000;
109 unsigned Byte3 = Value & 0xFF000000;
110 return (Byte0 << 24) | (Byte1 << 8) | (Byte2 >> 8) | (Byte3 >> 24);
113 // ByteSwap_64 - This function returns a byte-swapped representation of the
114 // 64-bit argument, Value.
115 inline uint64_t ByteSwap_64(uint64_t Value) {
116 uint64_t Hi = ByteSwap_32(unsigned(Value));
117 uint64_t Lo = ByteSwap_32(unsigned(Value >> 32));
118 return (Hi << 32) | Lo;
121 // CountLeadingZeros_32 - this function performs the platform optimal form of
122 // counting the number of zeros from the most significant bit to the first one
123 // bit. Ex. CountLeadingZeros_32(0x00F000FF) == 8.
124 // Returns 32 if the word is zero.
125 inline unsigned CountLeadingZeros_32(unsigned Value) {
126 unsigned Count; // result
128 // PowerPC is defined for __builtin_clz(0)
129 #if !defined(__ppc__) && !defined(__ppc64__)
130 if (!Value) return 32;
132 Count = __builtin_clz(Value);
134 if (!Value) return 32;
136 // bisecton method for count leading zeros
137 for (unsigned Shift = 32 >> 1; Shift; Shift >>= 1) {
138 unsigned Tmp = Value >> Shift;
149 // CountLeadingZeros_64 - This function performs the platform optimal form
150 // of counting the number of zeros from the most significant bit to the first
151 // one bit (64 bit edition.)
152 // Returns 64 if the word is zero.
153 inline unsigned CountLeadingZeros_64(uint64_t Value) {
154 unsigned Count; // result
156 // PowerPC is defined for __builtin_clzll(0)
157 #if !defined(__ppc__) && !defined(__ppc64__)
158 if (!Value) return 64;
160 Count = __builtin_clzll(Value);
162 if (sizeof(long) == sizeof(int64_t)) {
163 if (!Value) return 64;
165 // bisecton method for count leading zeros
166 for (uint64_t Shift = 64 >> 1; Shift; Shift >>= 1) {
167 uint64_t Tmp = Value >> Shift;
176 unsigned Hi = Hi_32(Value);
178 // if some bits in hi portion
180 // leading zeros in hi portion plus all bits in lo portion
181 Count = CountLeadingZeros_32(Hi);
184 unsigned Lo = Lo_32(Value);
185 // same as 32 bit value
186 Count = CountLeadingZeros_32(Lo)+32;
193 // CountTrailingZeros_32 - this function performs the platform optimal form of
194 // counting the number of zeros from the least significant bit to the first one
195 // bit. Ex. CountTrailingZeros_32(0xFF00FF00) == 8.
196 // Returns 32 if the word is zero.
197 inline unsigned CountTrailingZeros_32(unsigned Value) {
198 return 32 - CountLeadingZeros_32(~Value & (Value - 1));
201 // CountTrailingZeros_64 - This function performs the platform optimal form
202 // of counting the number of zeros from the least significant bit to the first
203 // one bit (64 bit edition.)
204 // Returns 64 if the word is zero.
205 inline unsigned CountTrailingZeros_64(uint64_t Value) {
206 return 64 - CountLeadingZeros_64(~Value & (Value - 1));
209 // CountPopulation_32 - this function counts the number of set bits in a value.
210 // Ex. CountPopulation(0xF000F000) = 8
211 // Returns 0 if the word is zero.
212 inline unsigned CountPopulation_32(unsigned Value) {
214 x = Value - ((Value >> 1) & 0x55555555);
215 t = ((x >> 2) & 0x33333333);
216 x = (x & 0x33333333) + t;
217 x = (x + (x >> 4)) & 0x0F0F0F0F;
223 // CountPopulation_64 - this function counts the number of set bits in a value,
225 inline unsigned CountPopulation_64(uint64_t Value) {
226 return CountPopulation_32(unsigned(Value >> 32)) +
227 CountPopulation_32(unsigned(Value));
230 // Log2_32 - This function returns the floor log base 2 of the specified value,
231 // -1 if the value is zero. (32 bit edition.)
232 // Ex. Log2_32(32) == 5, Log2_32(1) == 0, Log2_32(0) == -1, Log2_32(6) == 2
233 inline unsigned Log2_32(unsigned Value) {
234 return 31 - CountLeadingZeros_32(Value);
237 // Log2_64 - This function returns the floor log base 2 of the specified value,
238 // -1 if the value is zero. (64 bit edition.)
239 inline unsigned Log2_64(uint64_t Value) {
240 return 63 - CountLeadingZeros_64(Value);
243 // Log2_32_Ceil - This function returns the ceil log base 2 of the specified
244 // value, 32 if the value is zero. (32 bit edition).
245 // Ex. Log2_32_Ceil(32) == 5, Log2_32_Ceil(1) == 0, Log2_32_Ceil(6) == 3
246 inline unsigned Log2_32_Ceil(unsigned Value) {
247 return 32-CountLeadingZeros_32(Value-1);
250 // Log2_64 - This function returns the ceil log base 2 of the specified value,
251 // 64 if the value is zero. (64 bit edition.)
252 inline unsigned Log2_64_Ceil(uint64_t Value) {
253 return 64-CountLeadingZeros_64(Value-1);
256 // BitsToDouble - This function takes a 64-bit integer and returns the bit
257 // equivalent double.
258 inline double BitsToDouble(uint64_t Bits) {
267 // BitsToFloat - This function takes a 32-bit integer and returns the bit
269 inline float BitsToFloat(uint32_t Bits) {
278 // DoubleToBits - This function takes a double and returns the bit
279 // equivalent 64-bit integer.
280 inline uint64_t DoubleToBits(double Double) {
289 // FloatToBits - This function takes a float and returns the bit
290 // equivalent 32-bit integer.
291 inline uint32_t FloatToBits(float Float) {
300 // Platform-independent wrappers for the C99 isnan() function.
302 int IsNAN (double d);
304 // Platform-independent wrappers for the C99 isinf() function.
306 int IsInf (double d);
308 } // End llvm namespace
310 FORCE_DEFINING_FILE_TO_BE_LINKED(SupportIsInf)
311 FORCE_DEFINING_FILE_TO_BE_LINKED(SupportIsNAN)