Rizin
unix-like reverse engineering framework and cli tools
MathExtras.h
Go to the documentation of this file.
1 //===-- 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 Disassembly Engine */
15 /* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2015 */
16 
17 #ifndef CS_LLVM_SUPPORT_MATHEXTRAS_H
18 #define CS_LLVM_SUPPORT_MATHEXTRAS_H
19 
20 #if defined(_WIN32_WCE) && (_WIN32_WCE < 0x800)
21 #include "windowsce/intrin.h"
22 #elif defined(_MSC_VER)
23 #include <intrin.h>
24 #endif
25 
26 #ifndef __cplusplus
27 #if defined (WIN32) || defined (WIN64) || defined (_WIN32) || defined (_WIN64)
28 #define inline /* inline */
29 #endif
30 #endif
31 
32 // NOTE: The following support functions use the _32/_64 extensions instead of
33 // type overloading so that signed and unsigned integers can be used without
34 // ambiguity.
35 
37 static inline uint32_t Hi_32(uint64_t Value) {
38  return (uint32_t)(Value >> 32);
39 }
40 
42 static inline uint32_t Lo_32(uint64_t Value) {
43  return (uint32_t)(Value);
44 }
45 
48 static inline bool isUIntN(unsigned N, uint64_t x) {
49  return x == (x & (~0ULL >> (64 - N)));
50 }
51 
54 //static inline bool isIntN(unsigned N, int64_t x) {
55 // return N >= 64 || (-(INT64_C(1)<<(N-1)) <= x && x < (INT64_C(1)<<(N-1)));
56 //}
57 
61 static inline bool isMask_32(uint32_t Value) {
62  return Value && ((Value + 1) & Value) == 0;
63 }
64 
68 static inline bool isMask_64(uint64_t Value) {
69  return Value && ((Value + 1) & Value) == 0;
70 }
71 
75 static inline bool isShiftedMask_32(uint32_t Value) {
76  return isMask_32((Value - 1) | Value);
77 }
78 
81 static inline bool isShiftedMask_64(uint64_t Value) {
82  return isMask_64((Value - 1) | Value);
83 }
84 
87 static inline bool isPowerOf2_32(uint32_t Value) {
88  return Value && !(Value & (Value - 1));
89 }
90 
95 static inline unsigned CountLeadingZeros_32(uint32_t Value) {
96  unsigned Count; // result
97 #if __GNUC__ >= 4
98  // PowerPC is defined for __builtin_clz(0)
99 #if !defined(__ppc__) && !defined(__ppc64__)
100  if (!Value) return 32;
101 #endif
102  Count = __builtin_clz(Value);
103 #else
104  unsigned Shift;
105  if (!Value) return 32;
106  Count = 0;
107  // bisection method for count leading zeros
108  for (Shift = 32 >> 1; Shift; Shift >>= 1) {
109  uint32_t Tmp = Value >> Shift;
110  if (Tmp) {
111  Value = Tmp;
112  } else {
113  Count |= Shift;
114  }
115  }
116 #endif
117  return Count;
118 }
119 
124 static inline unsigned CountLeadingOnes_32(uint32_t Value) {
125  return CountLeadingZeros_32(~Value);
126 }
127 
132 static inline unsigned CountLeadingZeros_64(uint64_t Value) {
133  unsigned Count; // result
134 #if __GNUC__ >= 4
135  // PowerPC is defined for __builtin_clzll(0)
136 #if !defined(__ppc__) && !defined(__ppc64__)
137  if (!Value) return 64;
138 #endif
139  Count = __builtin_clzll(Value);
140 #else
141 #ifndef _MSC_VER
142  unsigned Shift;
143  if (sizeof(long) == sizeof(int64_t))
144  {
145  if (!Value) return 64;
146  Count = 0;
147  // bisection method for count leading zeros
148  for (Shift = 64 >> 1; Shift; Shift >>= 1) {
149  uint64_t Tmp = Value >> Shift;
150  if (Tmp) {
151  Value = Tmp;
152  } else {
153  Count |= Shift;
154  }
155  }
156  }
157  else
158 #endif
159  {
160  // get hi portion
161  uint32_t Hi = Hi_32(Value);
162 
163  // if some bits in hi portion
164  if (Hi) {
165  // leading zeros in hi portion plus all bits in lo portion
166  Count = CountLeadingZeros_32(Hi);
167  } else {
168  // get lo portion
169  uint32_t Lo = Lo_32(Value);
170  // same as 32 bit value
171  Count = CountLeadingZeros_32(Lo)+32;
172  }
173  }
174 #endif
175  return Count;
176 }
177 
182 static inline unsigned CountLeadingOnes_64(uint64_t Value) {
183  return CountLeadingZeros_64(~Value);
184 }
185 
190 static inline unsigned CountTrailingZeros_32(uint32_t Value) {
191 #if __GNUC__ >= 4
192  return Value ? __builtin_ctz(Value) : 32;
193 #else
194  static const unsigned Mod37BitPosition[] = {
195  32, 0, 1, 26, 2, 23, 27, 0, 3, 16, 24, 30, 28, 11, 0, 13,
196  4, 7, 17, 0, 25, 22, 31, 15, 29, 10, 12, 6, 0, 21, 14, 9,
197  5, 20, 8, 19, 18
198  };
199  // Replace "-Value" by "1+~Value" in the following commented code to avoid
200  // MSVC warning C4146
201  // return Mod37BitPosition[(-Value & Value) % 37];
202  return Mod37BitPosition[((1 + ~Value) & Value) % 37];
203 #endif
204 }
205 
210 static inline unsigned CountTrailingOnes_32(uint32_t Value) {
211  return CountTrailingZeros_32(~Value);
212 }
213 
218 static inline unsigned CountTrailingZeros_64(uint64_t Value) {
219 #if __GNUC__ >= 4
220  return Value ? __builtin_ctzll(Value) : 64;
221 #else
222  static const unsigned Mod67Position[] = {
223  64, 0, 1, 39, 2, 15, 40, 23, 3, 12, 16, 59, 41, 19, 24, 54,
224  4, 64, 13, 10, 17, 62, 60, 28, 42, 30, 20, 51, 25, 44, 55,
225  47, 5, 32, 65, 38, 14, 22, 11, 58, 18, 53, 63, 9, 61, 27,
226  29, 50, 43, 46, 31, 37, 21, 57, 52, 8, 26, 49, 45, 36, 56,
227  7, 48, 35, 6, 34, 33, 0
228  };
229  // Replace "-Value" by "1+~Value" in the following commented code to avoid
230  // MSVC warning C4146
231  // return Mod67Position[(-Value & Value) % 67];
232  return Mod67Position[((1 + ~Value) & Value) % 67];
233 #endif
234 }
235 
240 static inline unsigned CountTrailingOnes_64(uint64_t Value) {
241  return CountTrailingZeros_64(~Value);
242 }
243 
247 static inline unsigned CountPopulation_32(uint32_t Value) {
248 #if __GNUC__ >= 4
249  return __builtin_popcount(Value);
250 #else
251  uint32_t v = Value - ((Value >> 1) & 0x55555555);
252  v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
253  return (((v + (v >> 4)) & 0xF0F0F0F) * 0x1010101) >> 24;
254 #endif
255 }
256 
259 static inline unsigned CountPopulation_64(uint64_t Value) {
260 #if __GNUC__ >= 4
261  return __builtin_popcountll(Value);
262 #else
263  uint64_t v = Value - ((Value >> 1) & 0x5555555555555555ULL);
264  v = (v & 0x3333333333333333ULL) + ((v >> 2) & 0x3333333333333333ULL);
265  v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0FULL;
266  return (uint64_t)((v * 0x0101010101010101ULL) >> 56);
267 #endif
268 }
269 
273 static inline unsigned Log2_32(uint32_t Value) {
274  return 31 - CountLeadingZeros_32(Value);
275 }
276 
279 static inline unsigned Log2_64(uint64_t Value) {
280  return 63 - CountLeadingZeros_64(Value);
281 }
282 
286 static inline unsigned Log2_32_Ceil(uint32_t Value) {
287  return 32-CountLeadingZeros_32(Value-1);
288 }
289 
292 static inline unsigned Log2_64_Ceil(uint64_t Value) {
293  return 64-CountLeadingZeros_64(Value-1);
294 }
295 
299  while (B) {
300  uint64_t T = B;
301  B = A % B;
302  A = T;
303  }
304  return A;
305 }
306 
309 static inline double BitsToDouble(uint64_t Bits) {
310  union {
311  uint64_t L;
312  double D;
313  } T;
314  T.L = Bits;
315  return T.D;
316 }
317 
320 static inline float BitsToFloat(uint32_t Bits) {
321  union {
322  uint32_t I;
323  float F;
324  } T;
325  T.I = Bits;
326  return T.F;
327 }
328 
333 static inline uint64_t DoubleToBits(double Double) {
334  union {
335  uint64_t L;
336  double D;
337  } T;
338  T.D = Double;
339  return T.L;
340 }
341 
346 static inline uint32_t FloatToBits(float Float) {
347  union {
348  uint32_t I;
349  float F;
350  } T;
351  T.F = Float;
352  return T.I;
353 }
354 
358  // The largest power of 2 that divides both A and B.
359  //
360  // Replace "-Value" by "1+~Value" in the following commented code to avoid
361  // MSVC warning C4146
362  // return (A | B) & -(A | B);
363  return (A | B) & (1 + ~(A | B));
364 }
365 
368 static inline uint64_t NextPowerOf2(uint64_t A) {
369  A |= (A >> 1);
370  A |= (A >> 2);
371  A |= (A >> 4);
372  A |= (A >> 8);
373  A |= (A >> 16);
374  A |= (A >> 32);
375  return A + 1;
376 }
377 
387 static inline uint64_t RoundUpToAlignment(uint64_t Value, uint64_t Align) {
388  return ((Value + Align - 1) / Align) * Align;
389 }
390 
394 static inline uint64_t OffsetToAlignment(uint64_t Value, uint64_t Align) {
395  return RoundUpToAlignment(Value, Align) - Value;
396 }
397 
401 static inline int64_t abs64(int64_t x) {
402  return (x < 0) ? -x : x;
403 }
404 
407 static inline int32_t SignExtend32(uint32_t X, unsigned B) {
408  return (int32_t)(X << (32 - B)) >> (32 - B);
409 }
410 
413 static inline int64_t SignExtend64(uint64_t X, unsigned B) {
414  return (int64_t)(X << (64 - B)) >> (64 - B);
415 }
416 
424 static inline unsigned int countLeadingZeros(int x)
425 {
426  int i;
427  const unsigned bits = sizeof(x) * 8;
428  unsigned count = bits;
429 
430  if (x < 0) {
431  return 0;
432  }
433  for (i = bits; --i; ) {
434  if (x == 0) break;
435  count--;
436  x >>= 1;
437  }
438 
439  return count;
440 }
441 
442 #endif
#define T(op)
static uint64_t RoundUpToAlignment(uint64_t Value, uint64_t Align)
Definition: MathExtras.h:387
static bool isUIntN(unsigned N, uint64_t x)
Definition: MathExtras.h:48
static bool isMask_32(uint32_t Value)
Definition: MathExtras.h:61
static unsigned Log2_32(uint32_t Value)
Definition: MathExtras.h:273
static uint64_t MinAlign(uint64_t A, uint64_t B)
Definition: MathExtras.h:357
static bool isShiftedMask_32(uint32_t Value)
Definition: MathExtras.h:75
static int64_t SignExtend64(uint64_t X, unsigned B)
Sign extend number in the bottom B bits of X to a 64-bit int. Requires 0 < B <= 64.
Definition: MathExtras.h:413
static double BitsToDouble(uint64_t Bits)
Definition: MathExtras.h:309
static unsigned int countLeadingZeros(int x)
Count number of 0's from the most significant bit to the least stopping at the first 1.
Definition: MathExtras.h:424
static int64_t abs64(int64_t x)
Definition: MathExtras.h:401
static unsigned CountLeadingOnes_64(uint64_t Value)
Definition: MathExtras.h:182
static bool isMask_64(uint64_t Value)
Definition: MathExtras.h:68
static uint64_t NextPowerOf2(uint64_t A)
Definition: MathExtras.h:368
static uint64_t OffsetToAlignment(uint64_t Value, uint64_t Align)
Definition: MathExtras.h:394
static float BitsToFloat(uint32_t Bits)
Definition: MathExtras.h:320
static unsigned CountLeadingZeros_32(uint32_t Value)
Definition: MathExtras.h:95
static unsigned Log2_64_Ceil(uint64_t Value)
Definition: MathExtras.h:292
static bool isShiftedMask_64(uint64_t Value)
Definition: MathExtras.h:81
static uint64_t DoubleToBits(double Double)
Definition: MathExtras.h:333
static unsigned CountTrailingOnes_32(uint32_t Value)
Definition: MathExtras.h:210
static bool isPowerOf2_32(uint32_t Value)
Definition: MathExtras.h:87
static unsigned Log2_32_Ceil(uint32_t Value)
Definition: MathExtras.h:286
static uint32_t Lo_32(uint64_t Value)
Lo_32 - This function returns the low 32 bits of a 64 bit value.
Definition: MathExtras.h:42
static unsigned CountLeadingZeros_64(uint64_t Value)
Definition: MathExtras.h:132
static unsigned CountTrailingZeros_32(uint32_t Value)
Definition: MathExtras.h:190
static int32_t SignExtend32(uint32_t X, unsigned B)
Sign extend number in the bottom B bits of X to a 32-bit int. Requires 0 < B <= 32.
Definition: MathExtras.h:407
static unsigned CountTrailingZeros_64(uint64_t Value)
Definition: MathExtras.h:218
static unsigned CountPopulation_64(uint64_t Value)
Definition: MathExtras.h:259
static unsigned CountPopulation_32(uint32_t Value)
Definition: MathExtras.h:247
static uint32_t FloatToBits(float Float)
Definition: MathExtras.h:346
static unsigned CountLeadingOnes_32(uint32_t Value)
Definition: MathExtras.h:124
static uint64_t GreatestCommonDivisor64(uint64_t A, uint64_t B)
Definition: MathExtras.h:298
static unsigned Log2_64(uint64_t Value)
Definition: MathExtras.h:279
static unsigned CountTrailingOnes_64(uint64_t Value)
Definition: MathExtras.h:240
static uint32_t Hi_32(uint64_t Value)
Hi_32 - This function returns the high 32 bits of a 64 bit value.
Definition: MathExtras.h:37
lzma_index ** i
Definition: index.h:629
#define X(x, b, m)
#define A(x)
Definition: arc.h:165
#define I(x)
Definition: arc.h:164
#define B(x)
Definition: arc.h:166
int bits(struct state *s, int need)
Definition: blast.c:72
#define D
Definition: block.c:38
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void count
Definition: sflib.h:98
const char * v
Definition: dsignal.c:12
int x
Definition: mipsasm.c:20
long int64_t
Definition: sftypes.h:32
int int32_t
Definition: sftypes.h:33
unsigned int uint32_t
Definition: sftypes.h:29
unsigned long uint64_t
Definition: sftypes.h:28
#define F(x)
Definition: tricore.h:111
#define N
Definition: zip_err_str.c:8
#define L
Definition: zip_err_str.c:7