blob: d330b4ae41d90b36c51db0c1062ef9a0ee27c744 [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) {
Nguyen Anh Quynh714aa912014-03-07 13:50:13 +080090 unsigned Count; // result
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080091#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
Nguyen Anh Quynh714aa912014-03-07 13:50:13 +080098 unsigned Shift;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080099 if (!Value) return 32;
100 Count = 0;
101 // bisection method for count leading zeros
pancake8ef059f2014-03-07 03:47:58 +0100102 for (Shift = 32 >> 1; Shift; Shift >>= 1) {
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800103 uint32_t Tmp = Value >> Shift;
104 if (Tmp) {
105 Value = Tmp;
106 } else {
107 Count |= Shift;
108 }
109 }
110#endif
111 return Count;
112}
113
114/// CountLeadingOnes_32 - this function performs the operation of
115/// counting the number of ones from the most significant bit to the first zero
116/// bit. Ex. CountLeadingOnes_32(0xFF0FFF00) == 8.
117/// Returns 32 if the word is all ones.
118static inline unsigned CountLeadingOnes_32(uint32_t Value) {
119 return CountLeadingZeros_32(~Value);
120}
121
122/// CountLeadingZeros_64 - This function performs the platform optimal form
123/// of counting the number of zeros from the most significant bit to the first
124/// one bit (64 bit edition.)
125/// Returns 64 if the word is zero.
126static inline unsigned CountLeadingZeros_64(uint64_t Value) {
Nguyen Anh Quynh714aa912014-03-07 13:50:13 +0800127 unsigned Count; // result
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800128#if __GNUC__ >= 4
129 // PowerPC is defined for __builtin_clzll(0)
130#if !defined(__ppc__) && !defined(__ppc64__)
131 if (!Value) return 64;
132#endif
133 Count = __builtin_clzll(Value);
134#else
Alex Ionescu46018db2014-01-22 09:45:00 -0800135#ifndef _MSC_VER
Nguyen Anh Quynh714aa912014-03-07 13:50:13 +0800136 unsigned Shift;
Alex Ionescu46018db2014-01-22 09:45:00 -0800137 if (sizeof(long) == sizeof(int64_t))
Nguyen Anh Quynh2e79ba82014-01-23 22:22:45 +0800138 {
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800139 if (!Value) return 64;
140 Count = 0;
141 // bisection method for count leading zeros
pancake8ef059f2014-03-07 03:47:58 +0100142 for (Shift = 64 >> 1; Shift; Shift >>= 1) {
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800143 uint64_t Tmp = Value >> Shift;
144 if (Tmp) {
145 Value = Tmp;
146 } else {
147 Count |= Shift;
148 }
149 }
Alex Ionescu46018db2014-01-22 09:45:00 -0800150 }
Nguyen Anh Quynh2e79ba82014-01-23 22:22:45 +0800151 else
Alex Ionescu46018db2014-01-22 09:45:00 -0800152#endif
Nguyen Anh Quynh2e79ba82014-01-23 22:22:45 +0800153 {
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800154 // get hi portion
155 uint32_t Hi = Hi_32(Value);
156
157 // if some bits in hi portion
158 if (Hi) {
159 // leading zeros in hi portion plus all bits in lo portion
160 Count = CountLeadingZeros_32(Hi);
161 } else {
162 // get lo portion
163 uint32_t Lo = Lo_32(Value);
164 // same as 32 bit value
165 Count = CountLeadingZeros_32(Lo)+32;
166 }
167 }
168#endif
169 return Count;
170}
171
172/// CountLeadingOnes_64 - This function performs the operation
173/// of counting the number of ones from the most significant bit to the first
174/// zero bit (64 bit edition.)
175/// Returns 64 if the word is all ones.
176static inline unsigned CountLeadingOnes_64(uint64_t Value) {
177 return CountLeadingZeros_64(~Value);
178}
179
180/// CountTrailingZeros_32 - this function performs the platform optimal form of
181/// counting the number of zeros from the least significant bit to the first one
182/// bit. Ex. CountTrailingZeros_32(0xFF00FF00) == 8.
183/// Returns 32 if the word is zero.
184static inline unsigned CountTrailingZeros_32(uint32_t Value) {
185#if __GNUC__ >= 4
186 return Value ? __builtin_ctz(Value) : 32;
187#else
188 static const unsigned Mod37BitPosition[] = {
189 32, 0, 1, 26, 2, 23, 27, 0, 3, 16, 24, 30, 28, 11, 0, 13,
190 4, 7, 17, 0, 25, 22, 31, 15, 29, 10, 12, 6, 0, 21, 14, 9,
191 5, 20, 8, 19, 18
192 };
193 // Replace "-Value" by "1+~Value" in the following commented code to avoid
194 // MSVC warning C4146
195 // return Mod37BitPosition[(-Value & Value) % 37];
196 return Mod37BitPosition[((1 + ~Value) & Value) % 37];
197#endif
198}
199
200/// CountTrailingOnes_32 - this function performs the operation of
201/// counting the number of ones from the least significant bit to the first zero
202/// bit. Ex. CountTrailingOnes_32(0x00FF00FF) == 8.
203/// Returns 32 if the word is all ones.
204static inline unsigned CountTrailingOnes_32(uint32_t Value) {
205 return CountTrailingZeros_32(~Value);
206}
207
208/// CountTrailingZeros_64 - This function performs the platform optimal form
209/// of counting the number of zeros from the least significant bit to the first
210/// one bit (64 bit edition.)
211/// Returns 64 if the word is zero.
212static inline unsigned CountTrailingZeros_64(uint64_t Value) {
213#if __GNUC__ >= 4
214 return Value ? __builtin_ctzll(Value) : 64;
215#else
216 static const unsigned Mod67Position[] = {
217 64, 0, 1, 39, 2, 15, 40, 23, 3, 12, 16, 59, 41, 19, 24, 54,
218 4, 64, 13, 10, 17, 62, 60, 28, 42, 30, 20, 51, 25, 44, 55,
219 47, 5, 32, 65, 38, 14, 22, 11, 58, 18, 53, 63, 9, 61, 27,
220 29, 50, 43, 46, 31, 37, 21, 57, 52, 8, 26, 49, 45, 36, 56,
221 7, 48, 35, 6, 34, 33, 0
222 };
223 // Replace "-Value" by "1+~Value" in the following commented code to avoid
224 // MSVC warning C4146
225 // return Mod67Position[(-Value & Value) % 67];
226 return Mod67Position[((1 + ~Value) & Value) % 67];
227#endif
228}
229
230/// CountTrailingOnes_64 - This function performs the operation
231/// of counting the number of ones from the least significant bit to the first
232/// zero bit (64 bit edition.)
233/// Returns 64 if the word is all ones.
234static inline unsigned CountTrailingOnes_64(uint64_t Value) {
235 return CountTrailingZeros_64(~Value);
236}
237
238/// CountPopulation_32 - this function counts the number of set bits in a value.
239/// Ex. CountPopulation(0xF000F000) = 8
240/// Returns 0 if the word is zero.
241static inline unsigned CountPopulation_32(uint32_t Value) {
242#if __GNUC__ >= 4
243 return __builtin_popcount(Value);
244#else
245 uint32_t v = Value - ((Value >> 1) & 0x55555555);
246 v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
pancake8ef059f2014-03-07 03:47:58 +0100247 return (((v + (v >> 4)) & 0xF0F0F0F) * 0x1010101) >> 24;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800248#endif
249}
250
251/// CountPopulation_64 - this function counts the number of set bits in a value,
252/// (64 bit edition.)
253static inline unsigned CountPopulation_64(uint64_t Value) {
254#if __GNUC__ >= 4
255 return __builtin_popcountll(Value);
256#else
257 uint64_t v = Value - ((Value >> 1) & 0x5555555555555555ULL);
258 v = (v & 0x3333333333333333ULL) + ((v >> 2) & 0x3333333333333333ULL);
259 v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0FULL;
Alex Ionescu46018db2014-01-22 09:45:00 -0800260 return (uint64_t)((v * 0x0101010101010101ULL) >> 56);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800261#endif
262}
263
264/// Log2_32 - This function returns the floor log base 2 of the specified value,
265/// -1 if the value is zero. (32 bit edition.)
266/// Ex. Log2_32(32) == 5, Log2_32(1) == 0, Log2_32(0) == -1, Log2_32(6) == 2
267static inline unsigned Log2_32(uint32_t Value) {
268 return 31 - CountLeadingZeros_32(Value);
269}
270
271/// Log2_64 - This function returns the floor log base 2 of the specified value,
272/// -1 if the value is zero. (64 bit edition.)
273static inline unsigned Log2_64(uint64_t Value) {
274 return 63 - CountLeadingZeros_64(Value);
275}
276
277/// Log2_32_Ceil - This function returns the ceil log base 2 of the specified
278/// value, 32 if the value is zero. (32 bit edition).
279/// Ex. Log2_32_Ceil(32) == 5, Log2_32_Ceil(1) == 0, Log2_32_Ceil(6) == 3
280static inline unsigned Log2_32_Ceil(uint32_t Value) {
281 return 32-CountLeadingZeros_32(Value-1);
282}
283
284/// Log2_64_Ceil - This function returns the ceil log base 2 of the specified
285/// value, 64 if the value is zero. (64 bit edition.)
286static inline unsigned Log2_64_Ceil(uint64_t Value) {
287 return 64-CountLeadingZeros_64(Value-1);
288}
289
290/// GreatestCommonDivisor64 - Return the greatest common divisor of the two
291/// values using Euclid's algorithm.
292static inline uint64_t GreatestCommonDivisor64(uint64_t A, uint64_t B) {
293 while (B) {
294 uint64_t T = B;
295 B = A % B;
296 A = T;
297 }
298 return A;
299}
300
301/// BitsToDouble - This function takes a 64-bit integer and returns the bit
302/// equivalent double.
303static inline double BitsToDouble(uint64_t Bits) {
304 union {
305 uint64_t L;
306 double D;
307 } T;
308 T.L = Bits;
309 return T.D;
310}
311
312/// BitsToFloat - This function takes a 32-bit integer and returns the bit
313/// equivalent float.
314static inline float BitsToFloat(uint32_t Bits) {
315 union {
316 uint32_t I;
317 float F;
318 } T;
319 T.I = Bits;
320 return T.F;
321}
322
323/// DoubleToBits - This function takes a double and returns the bit
324/// equivalent 64-bit integer. Note that copying doubles around
325/// changes the bits of NaNs on some hosts, notably x86, so this
326/// routine cannot be used if these bits are needed.
327static inline uint64_t DoubleToBits(double Double) {
328 union {
329 uint64_t L;
330 double D;
331 } T;
332 T.D = Double;
333 return T.L;
334}
335
336/// FloatToBits - This function takes a float and returns the bit
337/// equivalent 32-bit integer. Note that copying floats around
338/// changes the bits of NaNs on some hosts, notably x86, so this
339/// routine cannot be used if these bits are needed.
340static inline uint32_t FloatToBits(float Float) {
341 union {
342 uint32_t I;
343 float F;
344 } T;
345 T.F = Float;
346 return T.I;
347}
348
349/// MinAlign - A and B are either alignments or offsets. Return the minimum
350/// alignment that may be assumed after adding the two together.
351static inline uint64_t MinAlign(uint64_t A, uint64_t B) {
352 // The largest power of 2 that divides both A and B.
353 //
354 // Replace "-Value" by "1+~Value" in the following commented code to avoid
355 // MSVC warning C4146
356 // return (A | B) & -(A | B);
357 return (A | B) & (1 + ~(A | B));
358}
359
360/// NextPowerOf2 - Returns the next power of two (in 64-bits)
361/// that is strictly greater than A. Returns zero on overflow.
362static inline uint64_t NextPowerOf2(uint64_t A) {
363 A |= (A >> 1);
364 A |= (A >> 2);
365 A |= (A >> 4);
366 A |= (A >> 8);
367 A |= (A >> 16);
368 A |= (A >> 32);
369 return A + 1;
370}
371
372/// Returns the next integer (mod 2**64) that is greater than or equal to
373/// \p Value and is a multiple of \p Align. \p Align must be non-zero.
374///
375/// Examples:
376/// \code
377/// RoundUpToAlignment(5, 8) = 8
378/// RoundUpToAlignment(17, 8) = 24
379/// RoundUpToAlignment(~0LL, 8) = 0
380/// \endcode
381static inline uint64_t RoundUpToAlignment(uint64_t Value, uint64_t Align) {
382 return ((Value + Align - 1) / Align) * Align;
383}
384
385/// Returns the offset to the next integer (mod 2**64) that is greater than
386/// or equal to \p Value and is a multiple of \p Align. \p Align must be
387/// non-zero.
388static inline uint64_t OffsetToAlignment(uint64_t Value, uint64_t Align) {
389 return RoundUpToAlignment(Value, Align) - Value;
390}
391
392/// abs64 - absolute value of a 64-bit int. Not all environments support
393/// "abs" on whatever their name for the 64-bit int type is. The absolute
394/// value of the largest negative number is undefined, as with "abs".
395static inline int64_t abs64(int64_t x) {
396 return (x < 0) ? -x : x;
397}
398
399/// \brief Sign extend number in the bottom B bits of X to a 32-bit int.
400/// Requires 0 < B <= 32.
401static inline int32_t SignExtend32(uint32_t X, unsigned B) {
402 return (int32_t)(X << (32 - B)) >> (32 - B);
403}
404
405/// \brief Sign extend number in the bottom B bits of X to a 64-bit int.
406/// Requires 0 < B <= 64.
407static inline int64_t SignExtend64(uint64_t X, unsigned B) {
408 return (int64_t)(X << (64 - B)) >> (64 - B);
409}
410
411#endif