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