Rizin
unix-like reverse engineering framework and cli tools
MathExtras.h File Reference

Go to the source code of this file.

Functions

static uint32_t Hi_32 (uint64_t Value)
 Hi_32 - This function returns the high 32 bits of a 64 bit value. More...
 
static uint32_t Lo_32 (uint64_t Value)
 Lo_32 - This function returns the low 32 bits of a 64 bit value. More...
 
static bool isUIntN (unsigned N, uint64_t x)
 
static bool isMask_32 (uint32_t Value)
 
static bool isMask_64 (uint64_t Value)
 
static bool isShiftedMask_32 (uint32_t Value)
 
static bool isShiftedMask_64 (uint64_t Value)
 
static bool isPowerOf2_32 (uint32_t Value)
 
static unsigned CountLeadingZeros_32 (uint32_t Value)
 
static unsigned CountLeadingOnes_32 (uint32_t Value)
 
static unsigned CountLeadingZeros_64 (uint64_t Value)
 
static unsigned CountLeadingOnes_64 (uint64_t Value)
 
static unsigned CountTrailingZeros_32 (uint32_t Value)
 
static unsigned CountTrailingOnes_32 (uint32_t Value)
 
static unsigned CountTrailingZeros_64 (uint64_t Value)
 
static unsigned CountTrailingOnes_64 (uint64_t Value)
 
static unsigned CountPopulation_32 (uint32_t Value)
 
static unsigned CountPopulation_64 (uint64_t Value)
 
static unsigned Log2_32 (uint32_t Value)
 
static unsigned Log2_64 (uint64_t Value)
 
static unsigned Log2_32_Ceil (uint32_t Value)
 
static unsigned Log2_64_Ceil (uint64_t Value)
 
static uint64_t GreatestCommonDivisor64 (uint64_t A, uint64_t B)
 
static double BitsToDouble (uint64_t Bits)
 
static float BitsToFloat (uint32_t Bits)
 
static uint64_t DoubleToBits (double Double)
 
static uint32_t FloatToBits (float Float)
 
static uint64_t MinAlign (uint64_t A, uint64_t B)
 
static uint64_t NextPowerOf2 (uint64_t A)
 
static uint64_t RoundUpToAlignment (uint64_t Value, uint64_t Align)
 
static uint64_t OffsetToAlignment (uint64_t Value, uint64_t Align)
 
static int64_t abs64 (int64_t x)
 
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. More...
 
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. More...
 
static unsigned int countLeadingZeros (int x)
 Count number of 0's from the most significant bit to the least stopping at the first 1. More...
 

Function Documentation

◆ abs64()

static int64_t abs64 ( int64_t  x)
inlinestatic

abs64 - absolute value of a 64-bit int. Not all environments support "abs" on whatever their name for the 64-bit int type is. The absolute value of the largest negative number is undefined, as with "abs".

Definition at line 401 of file MathExtras.h.

401  {
402  return (x < 0) ? -x : x;
403 }
int x
Definition: mipsasm.c:20

References x.

◆ BitsToDouble()

static double BitsToDouble ( uint64_t  Bits)
inlinestatic

BitsToDouble - This function takes a 64-bit integer and returns the bit equivalent double.

Definition at line 309 of file MathExtras.h.

309  {
310  union {
311  uint64_t L;
312  double D;
313  } T;
314  T.L = Bits;
315  return T.D;
316 }
#define T(op)
#define D
Definition: block.c:38
unsigned long uint64_t
Definition: sftypes.h:28
#define L
Definition: zip_err_str.c:7

References D, L, and T.

◆ BitsToFloat()

static float BitsToFloat ( uint32_t  Bits)
inlinestatic

BitsToFloat - This function takes a 32-bit integer and returns the bit equivalent float.

Definition at line 320 of file MathExtras.h.

320  {
321  union {
322  uint32_t I;
323  float F;
324  } T;
325  T.I = Bits;
326  return T.F;
327 }
#define I(x)
Definition: arc.h:164
unsigned int uint32_t
Definition: sftypes.h:29
#define F(x)
Definition: tricore.h:111

References F, I, and T.

◆ CountLeadingOnes_32()

static unsigned CountLeadingOnes_32 ( uint32_t  Value)
inlinestatic

CountLeadingOnes_32 - this function performs the operation of counting the number of ones from the most significant bit to the first zero bit. Ex. CountLeadingOnes_32(0xFF0FFF00) == 8. Returns 32 if the word is all ones.

Definition at line 124 of file MathExtras.h.

124  {
125  return CountLeadingZeros_32(~Value);
126 }
static unsigned CountLeadingZeros_32(uint32_t Value)
Definition: MathExtras.h:95

References CountLeadingZeros_32().

◆ CountLeadingOnes_64()

static unsigned CountLeadingOnes_64 ( uint64_t  Value)
inlinestatic

CountLeadingOnes_64 - This function performs the operation of counting the number of ones from the most significant bit to the first zero bit (64 bit edition.) Returns 64 if the word is all ones.

Definition at line 182 of file MathExtras.h.

182  {
183  return CountLeadingZeros_64(~Value);
184 }
static unsigned CountLeadingZeros_64(uint64_t Value)
Definition: MathExtras.h:132

References CountLeadingZeros_64().

◆ countLeadingZeros()

static unsigned int countLeadingZeros ( int  x)
inlinestatic

Count number of 0's from the most significant bit to the least stopping at the first 1.

Only unsigned integral types are allowed.

Parameters
ZBthe behavior on an input of 0. Only ZB_Width and ZB_Undefined are valid arguments.

Definition at line 424 of file MathExtras.h.

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 }
lzma_index ** i
Definition: index.h:629
int bits(struct state *s, int need)
Definition: blast.c:72
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

References bits(), count, i, and x.

◆ CountLeadingZeros_32()

static unsigned CountLeadingZeros_32 ( uint32_t  Value)
inlinestatic

CountLeadingZeros_32 - this function performs the platform optimal form of counting the number of zeros from the most significant bit to the first one bit. Ex. CountLeadingZeros_32(0x00F000FF) == 8. Returns 32 if the word is zero.

Definition at line 95 of file MathExtras.h.

95  {
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 }

Referenced by CountLeadingOnes_32(), CountLeadingZeros_64(), getT2SOImmValRotateVal(), Log2_32(), and Log2_32_Ceil().

◆ CountLeadingZeros_64()

static unsigned CountLeadingZeros_64 ( uint64_t  Value)
inlinestatic

CountLeadingZeros_64 - This function performs the platform optimal form of counting the number of zeros from the most significant bit to the first one bit (64 bit edition.) Returns 64 if the word is zero.

Definition at line 132 of file MathExtras.h.

132  {
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 }
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 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
long int64_t
Definition: sftypes.h:32

References CountLeadingZeros_32(), Hi_32(), and Lo_32().

Referenced by CountLeadingOnes_64(), Log2_64(), and Log2_64_Ceil().

◆ CountPopulation_32()

static unsigned CountPopulation_32 ( uint32_t  Value)
inlinestatic

CountPopulation_32 - this function counts the number of set bits in a value. Ex. CountPopulation(0xF000F000) = 8 Returns 0 if the word is zero.

Definition at line 247 of file MathExtras.h.

247  {
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 }
const char * v
Definition: dsignal.c:12

References v.

◆ CountPopulation_64()

static unsigned CountPopulation_64 ( uint64_t  Value)
inlinestatic

CountPopulation_64 - this function counts the number of set bits in a value, (64 bit edition.)

Definition at line 259 of file MathExtras.h.

259  {
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 }

References v.

◆ CountTrailingOnes_32()

static unsigned CountTrailingOnes_32 ( uint32_t  Value)
inlinestatic

CountTrailingOnes_32 - this function performs the operation of counting the number of ones from the least significant bit to the first zero bit. Ex. CountTrailingOnes_32(0x00FF00FF) == 8. Returns 32 if the word is all ones.

Definition at line 210 of file MathExtras.h.

210  {
211  return CountTrailingZeros_32(~Value);
212 }
static unsigned CountTrailingZeros_32(uint32_t Value)
Definition: MathExtras.h:190

References CountTrailingZeros_32().

◆ CountTrailingOnes_64()

static unsigned CountTrailingOnes_64 ( uint64_t  Value)
inlinestatic

CountTrailingOnes_64 - This function performs the operation of counting the number of ones from the least significant bit to the first zero bit (64 bit edition.) Returns 64 if the word is all ones.

Definition at line 240 of file MathExtras.h.

240  {
241  return CountTrailingZeros_64(~Value);
242 }
static unsigned CountTrailingZeros_64(uint64_t Value)
Definition: MathExtras.h:218

References CountTrailingZeros_64().

◆ CountTrailingZeros_32()

static unsigned CountTrailingZeros_32 ( uint32_t  Value)
inlinestatic

CountTrailingZeros_32 - this function performs the platform optimal form of counting the number of zeros from the least significant bit to the first one bit. Ex. CountTrailingZeros_32(0xFF00FF00) == 8. Returns 32 if the word is zero.

Definition at line 190 of file MathExtras.h.

190  {
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 }

Referenced by CountTrailingOnes_32(), getSOImmValRotate(), getT2SOImmValRotate(), getThumbImm16ValShift(), and getThumbImmValShift().

◆ CountTrailingZeros_64()

static unsigned CountTrailingZeros_64 ( uint64_t  Value)
inlinestatic

CountTrailingZeros_64 - This function performs the platform optimal form of counting the number of zeros from the least significant bit to the first one bit (64 bit edition.) Returns 64 if the word is zero.

Definition at line 218 of file MathExtras.h.

218  {
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 }

Referenced by CountTrailingOnes_64().

◆ DoubleToBits()

static uint64_t DoubleToBits ( double  Double)
inlinestatic

DoubleToBits - This function takes a double and returns the bit equivalent 64-bit integer. Note that copying doubles around changes the bits of NaNs on some hosts, notably x86, so this routine cannot be used if these bits are needed.

Definition at line 333 of file MathExtras.h.

333  {
334  union {
335  uint64_t L;
336  double D;
337  } T;
338  T.D = Double;
339  return T.L;
340 }

References D, L, and T.

◆ FloatToBits()

static uint32_t FloatToBits ( float  Float)
inlinestatic

FloatToBits - This function takes a float and returns the bit equivalent 32-bit integer. Note that copying floats around changes the bits of NaNs on some hosts, notably x86, so this routine cannot be used if these bits are needed.

Definition at line 346 of file MathExtras.h.

346  {
347  union {
348  uint32_t I;
349  float F;
350  } T;
351  T.F = Float;
352  return T.I;
353 }

References F, I, and T.

◆ GreatestCommonDivisor64()

static uint64_t GreatestCommonDivisor64 ( uint64_t  A,
uint64_t  B 
)
inlinestatic

GreatestCommonDivisor64 - Return the greatest common divisor of the two values using Euclid's algorithm.

Definition at line 298 of file MathExtras.h.

298  {
299  while (B) {
300  uint64_t T = B;
301  B = A % B;
302  A = T;
303  }
304  return A;
305 }
#define A(x)
Definition: arc.h:165
#define B(x)
Definition: arc.h:166

References A, B, and T.

◆ Hi_32()

static uint32_t Hi_32 ( uint64_t  Value)
inlinestatic

Hi_32 - This function returns the high 32 bits of a 64 bit value.

Definition at line 37 of file MathExtras.h.

37  {
38  return (uint32_t)(Value >> 32);
39 }

Referenced by CountLeadingZeros_64().

◆ isMask_32()

static bool isMask_32 ( uint32_t  Value)
inlinestatic

isIntN - Checks if an signed integer fits into the given (dynamic) bit width. isMask_32 - This function returns true if the argument is a sequence of ones starting at the least significant bit with the remainder zero (32 bit version). Ex. isMask_32(0x0000FFFFU) == true.

Definition at line 61 of file MathExtras.h.

61  {
62  return Value && ((Value + 1) & Value) == 0;
63 }

Referenced by isShiftedMask_32().

◆ isMask_64()

static bool isMask_64 ( uint64_t  Value)
inlinestatic

isMask_64 - This function returns true if the argument is a sequence of ones starting at the least significant bit with the remainder zero (64 bit version).

Definition at line 68 of file MathExtras.h.

68  {
69  return Value && ((Value + 1) & Value) == 0;
70 }

Referenced by isShiftedMask_64().

◆ isPowerOf2_32()

static bool isPowerOf2_32 ( uint32_t  Value)
inlinestatic

isPowerOf2_32 - This function returns true if the argument is a power of two > 0. Ex. isPowerOf2_32(0x00100000U) == true (32 bit edition.)

Definition at line 87 of file MathExtras.h.

87  {
88  return Value && !(Value & (Value - 1));
89 }

◆ isShiftedMask_32()

static bool isShiftedMask_32 ( uint32_t  Value)
inlinestatic

isShiftedMask_32 - This function returns true if the argument contains a sequence of ones with the remainder zero (32 bit version.) Ex. isShiftedMask_32(0x0000FF00U) == true.

Definition at line 75 of file MathExtras.h.

75  {
76  return isMask_32((Value - 1) | Value);
77 }
static bool isMask_32(uint32_t Value)
Definition: MathExtras.h:61

References isMask_32().

◆ isShiftedMask_64()

static bool isShiftedMask_64 ( uint64_t  Value)
inlinestatic

isShiftedMask_64 - This function returns true if the argument contains a sequence of ones with the remainder zero (64 bit version.)

Definition at line 81 of file MathExtras.h.

81  {
82  return isMask_64((Value - 1) | Value);
83 }
static bool isMask_64(uint64_t Value)
Definition: MathExtras.h:68

References isMask_64().

◆ isUIntN()

static bool isUIntN ( unsigned  N,
uint64_t  x 
)
inlinestatic

isUIntN - Checks if an unsigned integer fits into the given (dynamic) bit width.

Definition at line 48 of file MathExtras.h.

48  {
49  return x == (x & (~0ULL >> (64 - N)));
50 }
#define N
Definition: zip_err_str.c:8

References N, and x.

◆ Lo_32()

static uint32_t Lo_32 ( uint64_t  Value)
inlinestatic

Lo_32 - This function returns the low 32 bits of a 64 bit value.

Definition at line 42 of file MathExtras.h.

42  {
43  return (uint32_t)(Value);
44 }

Referenced by CountLeadingZeros_64().

◆ Log2_32()

static unsigned Log2_32 ( uint32_t  Value)
inlinestatic

Log2_32 - This function returns the floor log base 2 of the specified value, -1 if the value is zero. (32 bit edition.) Ex. Log2_32(32) == 5, Log2_32(1) == 0, Log2_32(0) == -1, Log2_32(6) == 2

Definition at line 273 of file MathExtras.h.

273  {
274  return 31 - CountLeadingZeros_32(Value);
275 }

References CountLeadingZeros_32().

◆ Log2_32_Ceil()

static unsigned Log2_32_Ceil ( uint32_t  Value)
inlinestatic

Log2_32_Ceil - This function returns the ceil log base 2 of the specified value, 32 if the value is zero. (32 bit edition). Ex. Log2_32_Ceil(32) == 5, Log2_32_Ceil(1) == 0, Log2_32_Ceil(6) == 3

Definition at line 286 of file MathExtras.h.

286  {
287  return 32-CountLeadingZeros_32(Value-1);
288 }

References CountLeadingZeros_32().

◆ Log2_64()

static unsigned Log2_64 ( uint64_t  Value)
inlinestatic

Log2_64 - This function returns the floor log base 2 of the specified value, -1 if the value is zero. (64 bit edition.)

Definition at line 279 of file MathExtras.h.

279  {
280  return 63 - CountLeadingZeros_64(Value);
281 }

References CountLeadingZeros_64().

◆ Log2_64_Ceil()

static unsigned Log2_64_Ceil ( uint64_t  Value)
inlinestatic

Log2_64_Ceil - This function returns the ceil log base 2 of the specified value, 64 if the value is zero. (64 bit edition.)

Definition at line 292 of file MathExtras.h.

292  {
293  return 64-CountLeadingZeros_64(Value-1);
294 }

References CountLeadingZeros_64().

◆ MinAlign()

static uint64_t MinAlign ( uint64_t  A,
uint64_t  B 
)
inlinestatic

MinAlign - A and B are either alignments or offsets. Return the minimum alignment that may be assumed after adding the two together.

Definition at line 357 of file MathExtras.h.

357  {
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 }

References A, and B.

◆ NextPowerOf2()

static uint64_t NextPowerOf2 ( uint64_t  A)
inlinestatic

NextPowerOf2 - Returns the next power of two (in 64-bits) that is strictly greater than A. Returns zero on overflow.

Definition at line 368 of file MathExtras.h.

368  {
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 }

References A.

◆ OffsetToAlignment()

static uint64_t OffsetToAlignment ( uint64_t  Value,
uint64_t  Align 
)
inlinestatic

Returns the offset to the next integer (mod 2**64) that is greater than or equal to Value and is a multiple of Align. Align must be non-zero.

Definition at line 394 of file MathExtras.h.

394  {
395  return RoundUpToAlignment(Value, Align) - Value;
396 }
static uint64_t RoundUpToAlignment(uint64_t Value, uint64_t Align)
Definition: MathExtras.h:387

References RoundUpToAlignment().

◆ RoundUpToAlignment()

static uint64_t RoundUpToAlignment ( uint64_t  Value,
uint64_t  Align 
)
inlinestatic

Returns the next integer (mod 2**64) that is greater than or equal to Value and is a multiple of Align. Align must be non-zero.

Examples:

RoundUpToAlignment(17, 8) = 24
RoundUpToAlignment(~0LL, 8) = 0

Definition at line 387 of file MathExtras.h.

387  {
388  return ((Value + Align - 1) / Align) * Align;
389 }

Referenced by OffsetToAlignment().

◆ SignExtend32()

static int32_t SignExtend32 ( uint32_t  X,
unsigned  B 
)
inlinestatic

Sign extend number in the bottom B bits of X to a 32-bit int. Requires 0 < B <= 32.

Definition at line 407 of file MathExtras.h.

407  {
408  return (int32_t)(X << (32 - B)) >> (32 - B);
409 }
#define X(x, b, m)
int int32_t
Definition: sftypes.h:33

References B, and X.

◆ SignExtend64()

static int64_t SignExtend64 ( uint64_t  X,
unsigned  B 
)
inlinestatic

Sign extend number in the bottom B bits of X to a 64-bit int. Requires 0 < B <= 64.

Definition at line 413 of file MathExtras.h.

413  {
414  return (int64_t)(X << (64 - B)) >> (64 - B);
415 }

References B, and X.