blob: 98a6cb6fdb74442930904a71c74a8d99e9b9a588 [file] [log] [blame]
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001//===-- llvm/Support/MathExtras.h - Useful math functions -------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// 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/* Capstone Disassembler Engine */
15/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013> */
16
17#ifndef CS_LLVM_SUPPORT_MATHEXTRAS_H
18#define CS_LLVM_SUPPORT_MATHEXTRAS_H
19
20#include <stdint.h>
21
22#ifdef _MSC_VER
23# include <intrin.h>
24#endif
25
26// NOTE: The following support functions use the _32/_64 extensions instead of
27// type overloading so that signed and unsigned integers can be used without
28// ambiguity.
29
30/// Hi_32 - This function returns the high 32 bits of a 64 bit value.
31static inline uint32_t Hi_32(uint64_t Value) {
32 return (uint32_t)(Value >> 32);
33}
34
35/// Lo_32 - This function returns the low 32 bits of a 64 bit value.
36static inline uint32_t Lo_32(uint64_t Value) {
37 return (uint32_t)(Value);
38}
39
40/// isUIntN - Checks if an unsigned integer fits into the given (dynamic)
41/// bit width.
42static inline bool isUIntN(unsigned N, uint64_t x) {
43 return x == (x & (~0ULL >> (64 - N)));
44}
45
46/// isIntN - Checks if an signed integer fits into the given (dynamic)
47/// bit width.
48//static inline bool isIntN(unsigned N, int64_t x) {
49// return N >= 64 || (-(INT64_C(1)<<(N-1)) <= x && x < (INT64_C(1)<<(N-1)));
50//}
51
52/// isMask_32 - This function returns true if the argument is a sequence of ones
53/// starting at the least significant bit with the remainder zero (32 bit
54/// version). Ex. isMask_32(0x0000FFFFU) == true.
55static inline bool isMask_32(uint32_t Value) {
56 return Value && ((Value + 1) & Value) == 0;
57}
58
59/// isMask_64 - This function returns true if the argument is a sequence of ones
60/// starting at the least significant bit with the remainder zero (64 bit
61/// version).
62static inline bool isMask_64(uint64_t Value) {
63 return Value && ((Value + 1) & Value) == 0;
64}
65
66/// isShiftedMask_32 - This function returns true if the argument contains a
67/// sequence of ones with the remainder zero (32 bit version.)
68/// Ex. isShiftedMask_32(0x0000FF00U) == true.
69static inline bool isShiftedMask_32(uint32_t Value) {
70 return isMask_32((Value - 1) | Value);
71}
72
73/// isShiftedMask_64 - This function returns true if the argument contains a
74/// sequence of ones with the remainder zero (64 bit version.)
75static inline bool isShiftedMask_64(uint64_t Value) {
76 return isMask_64((Value - 1) | Value);
77}
78
79/// isPowerOf2_32 - This function returns true if the argument is a power of
80/// two > 0. Ex. isPowerOf2_32(0x00100000U) == true (32 bit edition.)
81static inline bool isPowerOf2_32(uint32_t Value) {
82 return Value && !(Value & (Value - 1));
83}
84
85/// CountLeadingZeros_32 - this function performs the platform optimal form of
86/// counting the number of zeros from the most significant bit to the first one
87/// bit. Ex. CountLeadingZeros_32(0x00F000FF) == 8.
88/// Returns 32 if the word is zero.
89static inline unsigned CountLeadingZeros_32(uint32_t Value) {
90 unsigned Count; // result
91#if __GNUC__ >= 4
92 // PowerPC is defined for __builtin_clz(0)
93#if !defined(__ppc__) && !defined(__ppc64__)
94 if (!Value) return 32;
95#endif
96 Count = __builtin_clz(Value);
97#else
98 if (!Value) return 32;
99 Count = 0;
100 // bisection method for count leading zeros
101 for (unsigned Shift = 32 >> 1; Shift; Shift >>= 1) {
102 uint32_t Tmp = Value >> Shift;
103 if (Tmp) {
104 Value = Tmp;
105 } else {
106 Count |= Shift;
107 }
108 }
109#endif
110 return Count;
111}
112
113/// CountLeadingOnes_32 - this function performs the operation of
114/// counting the number of ones from the most significant bit to the first zero
115/// bit. Ex. CountLeadingOnes_32(0xFF0FFF00) == 8.
116/// Returns 32 if the word is all ones.
117static inline unsigned CountLeadingOnes_32(uint32_t Value) {
118 return CountLeadingZeros_32(~Value);
119}
120
121/// CountLeadingZeros_64 - This function performs the platform optimal form
122/// of counting the number of zeros from the most significant bit to the first
123/// one bit (64 bit edition.)
124/// Returns 64 if the word is zero.
125static inline unsigned CountLeadingZeros_64(uint64_t Value) {
126 unsigned Count; // result
127#if __GNUC__ >= 4
128 // PowerPC is defined for __builtin_clzll(0)
129#if !defined(__ppc__) && !defined(__ppc64__)
130 if (!Value) return 64;
131#endif
132 Count = __builtin_clzll(Value);
133#else
134 if (sizeof(long) == sizeof(int64_t)) {
135 if (!Value) return 64;
136 Count = 0;
137 // bisection method for count leading zeros
138 for (unsigned Shift = 64 >> 1; Shift; Shift >>= 1) {
139 uint64_t Tmp = Value >> Shift;
140 if (Tmp) {
141 Value = Tmp;
142 } else {
143 Count |= Shift;
144 }
145 }
146 } else {
147 // get hi portion
148 uint32_t Hi = Hi_32(Value);
149
150 // if some bits in hi portion
151 if (Hi) {
152 // leading zeros in hi portion plus all bits in lo portion
153 Count = CountLeadingZeros_32(Hi);
154 } else {
155 // get lo portion
156 uint32_t Lo = Lo_32(Value);
157 // same as 32 bit value
158 Count = CountLeadingZeros_32(Lo)+32;
159 }
160 }
161#endif
162 return Count;
163}
164
165/// CountLeadingOnes_64 - This function performs the operation
166/// of counting the number of ones from the most significant bit to the first
167/// zero bit (64 bit edition.)
168/// Returns 64 if the word is all ones.
169static inline unsigned CountLeadingOnes_64(uint64_t Value) {
170 return CountLeadingZeros_64(~Value);
171}
172
173/// CountTrailingZeros_32 - this function performs the platform optimal form of
174/// counting the number of zeros from the least significant bit to the first one
175/// bit. Ex. CountTrailingZeros_32(0xFF00FF00) == 8.
176/// Returns 32 if the word is zero.
177static inline unsigned CountTrailingZeros_32(uint32_t Value) {
178#if __GNUC__ >= 4
179 return Value ? __builtin_ctz(Value) : 32;
180#else
181 static const unsigned Mod37BitPosition[] = {
182 32, 0, 1, 26, 2, 23, 27, 0, 3, 16, 24, 30, 28, 11, 0, 13,
183 4, 7, 17, 0, 25, 22, 31, 15, 29, 10, 12, 6, 0, 21, 14, 9,
184 5, 20, 8, 19, 18
185 };
186 // Replace "-Value" by "1+~Value" in the following commented code to avoid
187 // MSVC warning C4146
188 // return Mod37BitPosition[(-Value & Value) % 37];
189 return Mod37BitPosition[((1 + ~Value) & Value) % 37];
190#endif
191}
192
193/// CountTrailingOnes_32 - this function performs the operation of
194/// counting the number of ones from the least significant bit to the first zero
195/// bit. Ex. CountTrailingOnes_32(0x00FF00FF) == 8.
196/// Returns 32 if the word is all ones.
197static inline unsigned CountTrailingOnes_32(uint32_t Value) {
198 return CountTrailingZeros_32(~Value);
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.
205static inline unsigned CountTrailingZeros_64(uint64_t Value) {
206#if __GNUC__ >= 4
207 return Value ? __builtin_ctzll(Value) : 64;
208#else
209 static const unsigned Mod67Position[] = {
210 64, 0, 1, 39, 2, 15, 40, 23, 3, 12, 16, 59, 41, 19, 24, 54,
211 4, 64, 13, 10, 17, 62, 60, 28, 42, 30, 20, 51, 25, 44, 55,
212 47, 5, 32, 65, 38, 14, 22, 11, 58, 18, 53, 63, 9, 61, 27,
213 29, 50, 43, 46, 31, 37, 21, 57, 52, 8, 26, 49, 45, 36, 56,
214 7, 48, 35, 6, 34, 33, 0
215 };
216 // Replace "-Value" by "1+~Value" in the following commented code to avoid
217 // MSVC warning C4146
218 // return Mod67Position[(-Value & Value) % 67];
219 return Mod67Position[((1 + ~Value) & Value) % 67];
220#endif
221}
222
223/// CountTrailingOnes_64 - This function performs the operation
224/// of counting the number of ones from the least significant bit to the first
225/// zero bit (64 bit edition.)
226/// Returns 64 if the word is all ones.
227static inline unsigned CountTrailingOnes_64(uint64_t Value) {
228 return CountTrailingZeros_64(~Value);
229}
230
231/// CountPopulation_32 - this function counts the number of set bits in a value.
232/// Ex. CountPopulation(0xF000F000) = 8
233/// Returns 0 if the word is zero.
234static inline unsigned CountPopulation_32(uint32_t Value) {
235#if __GNUC__ >= 4
236 return __builtin_popcount(Value);
237#else
238 uint32_t v = Value - ((Value >> 1) & 0x55555555);
239 v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
240 return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
241#endif
242}
243
244/// CountPopulation_64 - this function counts the number of set bits in a value,
245/// (64 bit edition.)
246static inline unsigned CountPopulation_64(uint64_t Value) {
247#if __GNUC__ >= 4
248 return __builtin_popcountll(Value);
249#else
250 uint64_t v = Value - ((Value >> 1) & 0x5555555555555555ULL);
251 v = (v & 0x3333333333333333ULL) + ((v >> 2) & 0x3333333333333333ULL);
252 v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0FULL;
253 return unsigned((uint64_t)(v * 0x0101010101010101ULL) >> 56);
254#endif
255}
256
257/// Log2_32 - This function returns the floor log base 2 of the specified value,
258/// -1 if the value is zero. (32 bit edition.)
259/// Ex. Log2_32(32) == 5, Log2_32(1) == 0, Log2_32(0) == -1, Log2_32(6) == 2
260static inline unsigned Log2_32(uint32_t Value) {
261 return 31 - CountLeadingZeros_32(Value);
262}
263
264/// Log2_64 - This function returns the floor log base 2 of the specified value,
265/// -1 if the value is zero. (64 bit edition.)
266static inline unsigned Log2_64(uint64_t Value) {
267 return 63 - CountLeadingZeros_64(Value);
268}
269
270/// Log2_32_Ceil - This function returns the ceil log base 2 of the specified
271/// value, 32 if the value is zero. (32 bit edition).
272/// Ex. Log2_32_Ceil(32) == 5, Log2_32_Ceil(1) == 0, Log2_32_Ceil(6) == 3
273static inline unsigned Log2_32_Ceil(uint32_t Value) {
274 return 32-CountLeadingZeros_32(Value-1);
275}
276
277/// Log2_64_Ceil - This function returns the ceil log base 2 of the specified
278/// value, 64 if the value is zero. (64 bit edition.)
279static inline unsigned Log2_64_Ceil(uint64_t Value) {
280 return 64-CountLeadingZeros_64(Value-1);
281}
282
283/// GreatestCommonDivisor64 - Return the greatest common divisor of the two
284/// values using Euclid's algorithm.
285static inline uint64_t GreatestCommonDivisor64(uint64_t A, uint64_t B) {
286 while (B) {
287 uint64_t T = B;
288 B = A % B;
289 A = T;
290 }
291 return A;
292}
293
294/// BitsToDouble - This function takes a 64-bit integer and returns the bit
295/// equivalent double.
296static inline double BitsToDouble(uint64_t Bits) {
297 union {
298 uint64_t L;
299 double D;
300 } T;
301 T.L = Bits;
302 return T.D;
303}
304
305/// BitsToFloat - This function takes a 32-bit integer and returns the bit
306/// equivalent float.
307static inline float BitsToFloat(uint32_t Bits) {
308 union {
309 uint32_t I;
310 float F;
311 } T;
312 T.I = Bits;
313 return T.F;
314}
315
316/// DoubleToBits - This function takes a double and returns the bit
317/// equivalent 64-bit integer. Note that copying doubles around
318/// changes the bits of NaNs on some hosts, notably x86, so this
319/// routine cannot be used if these bits are needed.
320static inline uint64_t DoubleToBits(double Double) {
321 union {
322 uint64_t L;
323 double D;
324 } T;
325 T.D = Double;
326 return T.L;
327}
328
329/// FloatToBits - This function takes a float and returns the bit
330/// equivalent 32-bit integer. Note that copying floats around
331/// changes the bits of NaNs on some hosts, notably x86, so this
332/// routine cannot be used if these bits are needed.
333static inline uint32_t FloatToBits(float Float) {
334 union {
335 uint32_t I;
336 float F;
337 } T;
338 T.F = Float;
339 return T.I;
340}
341
342/// MinAlign - A and B are either alignments or offsets. Return the minimum
343/// alignment that may be assumed after adding the two together.
344static inline uint64_t MinAlign(uint64_t A, uint64_t B) {
345 // The largest power of 2 that divides both A and B.
346 //
347 // Replace "-Value" by "1+~Value" in the following commented code to avoid
348 // MSVC warning C4146
349 // return (A | B) & -(A | B);
350 return (A | B) & (1 + ~(A | B));
351}
352
353/// NextPowerOf2 - Returns the next power of two (in 64-bits)
354/// that is strictly greater than A. Returns zero on overflow.
355static inline uint64_t NextPowerOf2(uint64_t A) {
356 A |= (A >> 1);
357 A |= (A >> 2);
358 A |= (A >> 4);
359 A |= (A >> 8);
360 A |= (A >> 16);
361 A |= (A >> 32);
362 return A + 1;
363}
364
365/// Returns the next integer (mod 2**64) that is greater than or equal to
366/// \p Value and is a multiple of \p Align. \p Align must be non-zero.
367///
368/// Examples:
369/// \code
370/// RoundUpToAlignment(5, 8) = 8
371/// RoundUpToAlignment(17, 8) = 24
372/// RoundUpToAlignment(~0LL, 8) = 0
373/// \endcode
374static inline uint64_t RoundUpToAlignment(uint64_t Value, uint64_t Align) {
375 return ((Value + Align - 1) / Align) * Align;
376}
377
378/// Returns the offset to the next integer (mod 2**64) that is greater than
379/// or equal to \p Value and is a multiple of \p Align. \p Align must be
380/// non-zero.
381static inline uint64_t OffsetToAlignment(uint64_t Value, uint64_t Align) {
382 return RoundUpToAlignment(Value, Align) - Value;
383}
384
385/// abs64 - absolute value of a 64-bit int. Not all environments support
386/// "abs" on whatever their name for the 64-bit int type is. The absolute
387/// value of the largest negative number is undefined, as with "abs".
388static inline int64_t abs64(int64_t x) {
389 return (x < 0) ? -x : x;
390}
391
392/// \brief Sign extend number in the bottom B bits of X to a 32-bit int.
393/// Requires 0 < B <= 32.
394static inline int32_t SignExtend32(uint32_t X, unsigned B) {
395 return (int32_t)(X << (32 - B)) >> (32 - B);
396}
397
398/// \brief Sign extend number in the bottom B bits of X to a 64-bit int.
399/// Requires 0 < B <= 64.
400static inline int64_t SignExtend64(uint64_t X, unsigned B) {
401 return (int64_t)(X << (64 - B)) >> (64 - B);
402}
403
404#endif