For PR780:
[oota-llvm.git] / include / llvm / Support / MathExtras.h
1 //===-- llvm/Support/MathExtras.h - Useful math functions -------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains some functions that are useful for math stuff.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_SUPPORT_MATHEXTRAS_H
15 #define LLVM_SUPPORT_MATHEXTRAS_H
16
17 #include "llvm/Support/DataTypes.h"
18 #include "llvm/System/IncludeFile.h"
19
20 namespace llvm {
21
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
24 // ambiguity.
25
26
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);
30 }
31
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);
35 }
36
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; 
40 }
41 inline bool isUInt8 (int Value)     { 
42   return static_cast<unsigned char>(Value) == Value; 
43 }
44 inline bool isInt16 (int Value)     { 
45   return static_cast<signed short>(Value) == Value; 
46 }
47 inline bool isUInt16(int Value)     { 
48   return static_cast<unsigned short>(Value) == Value; 
49 }
50 inline bool isInt32 (int64_t Value) { 
51   return static_cast<signed int>(Value) == Value; 
52 }
53 inline bool isUInt32(int64_t Value) { 
54   return static_cast<unsigned int>(Value) == Value; 
55 }
56
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;
62 }
63
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;
68 }
69
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);
75 }
76
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);
81 }
82
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));
87 }
88
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)));
93 }
94
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;
100   return Hi | Lo;
101 }
102
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);
111 }
112
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;
119 }
120
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
127 #if __GNUC__ >= 4
128   // PowerPC is defined for __builtin_clz(0)
129 #if !defined(__ppc__) && !defined(__ppc64__)
130   if (!Value) return 32;
131 #endif
132   Count = __builtin_clz(Value);
133 #else
134   if (!Value) return 32;
135   Count = 0;
136   // bisecton method for count leading zeros
137   for (unsigned Shift = 32 >> 1; Shift; Shift >>= 1) {
138     unsigned Tmp = Value >> Shift;
139     if (Tmp) {
140       Value = Tmp;
141     } else {
142       Count |= Shift;
143     }
144   }
145 #endif
146   return Count;
147 }
148
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
155 #if __GNUC__ >= 4
156   // PowerPC is defined for __builtin_clzll(0)
157 #if !defined(__ppc__) && !defined(__ppc64__)
158   if (!Value) return 64;
159 #endif
160   Count = __builtin_clzll(Value);
161 #else
162   if (sizeof(long) == sizeof(int64_t)) {
163     if (!Value) return 64;
164     Count = 0;
165     // bisecton method for count leading zeros
166     for (uint64_t Shift = 64 >> 1; Shift; Shift >>= 1) {
167       uint64_t Tmp = Value >> Shift;
168       if (Tmp) {
169         Value = Tmp;
170       } else {
171         Count |= Shift;
172       }
173     }
174   } else {
175     // get hi portion
176     unsigned Hi = Hi_32(Value);
177
178     // if some bits in hi portion
179     if (Hi) {
180         // leading zeros in hi portion plus all bits in lo portion
181         Count = CountLeadingZeros_32(Hi);
182     } else {
183         // get lo portion
184         unsigned Lo = Lo_32(Value);
185         // same as 32 bit value
186         Count = CountLeadingZeros_32(Lo)+32;
187     }
188   }
189 #endif
190   return Count;
191 }
192
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));
199 }
200
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));
207 }
208
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) {
213   unsigned x, t;
214   x = Value - ((Value >> 1) & 0x55555555);
215   t = ((x >> 2) & 0x33333333);
216   x = (x & 0x33333333) + t;
217   x = (x + (x >> 4)) & 0x0F0F0F0F;
218   x = x + (x << 8);
219   x = x + (x << 16);
220   return x >> 24;
221 }
222
223 // CountPopulation_64 - this function counts the number of set bits in a value,
224 // (64 bit edition.)
225 inline unsigned CountPopulation_64(uint64_t Value) {
226   return CountPopulation_32(unsigned(Value >> 32)) +
227          CountPopulation_32(unsigned(Value));
228 }
229
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);
235 }
236
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);
241 }
242
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);
248 }
249
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);
254 }
255
256 // BitsToDouble - This function takes a 64-bit integer and returns the bit
257 // equivalent double.
258 inline double BitsToDouble(uint64_t Bits) {
259   union {
260     uint64_t L;
261     double D;
262   } T;
263   T.L = Bits;
264   return T.D;
265 }
266
267 // BitsToFloat - This function takes a 32-bit integer and returns the bit
268 // equivalent float.
269 inline float BitsToFloat(uint32_t Bits) {
270   union {
271     uint32_t I;
272     float F;
273   } T;
274   T.I = Bits;
275   return T.F;
276 }
277
278 // DoubleToBits - This function takes a double and returns the bit
279 // equivalent 64-bit integer.
280 inline uint64_t DoubleToBits(double Double) {
281   union {
282     uint64_t L;
283     double D;
284   } T;
285   T.D = Double;
286   return T.L;
287 }
288
289 // FloatToBits - This function takes a float and returns the bit
290 // equivalent 32-bit integer.
291 inline uint32_t FloatToBits(float Float) {
292   union {
293     uint32_t I;
294     float F;
295   } T;
296   T.F = Float;
297   return T.I;
298 }
299
300 // Platform-independent wrappers for the C99 isnan() function.
301 int IsNAN (float f);
302 int IsNAN (double d);
303
304 // Platform-independent wrappers for the C99 isinf() function.
305 int IsInf (float f);
306 int IsInf (double d);
307
308 } // End llvm namespace
309
310 FORCE_DEFINING_FILE_TO_BE_LINKED(SupportIsInf)
311 FORCE_DEFINING_FILE_TO_BE_LINKED(SupportIsNAN)
312
313 #endif