-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathIntMath.cs
36 lines (33 loc) · 902 Bytes
/
IntMath.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
namespace GeodesicGrid
{
internal static class IntMath
{
// From: http://stackoverflow.com/a/383596/303422
public static int Pow(int x, uint pow)
{
int ret = 1;
while (pow != 0)
{
if ((pow & 1) == 1)
ret *= x;
x *= x;
pow >>= 1;
}
return ret;
}
private static int[] MultiplyDeBruijnBitPosition = {
0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30,
8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31
};
public static int LogBase2(uint v)
{
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
return MultiplyDeBruijnBitPosition[(v * 0x7C4ACDDU) >> 27];
}
}
}