KrisLibrary  1.0.0
fastmath.h
Go to the documentation of this file.
1 #ifndef MATH_FASTMATH_H
2 #define MATH_FASTMATH_H
3 
9 unsigned int fastlog2(unsigned int v)
10 {
11  const unsigned int b[] = {0x2, 0xC, 0xF0, 0xFF00, 0xFFFF0000};
12  const unsigned int S[] = {1, 2, 4, 8, 16};
13  int i;
14 
15  register unsigned int r = 0; // result of log2(v) will go here
16  for (i = 4; i >= 0; i--) { // unroll for speed...
17  if (v & b[i]) {
18  v >>= S[i];
19  r |= S[i];
20  }
21  }
22  return r;
23 }
24 
25 #endif