Skip to content

Commit

Permalink
Move integer math functions to GeodesicGrid namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
Majiir committed Sep 1, 2014
1 parent 3423d46 commit 872ae4b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 36 deletions.
4 changes: 2 additions & 2 deletions Plugin/GeodesicGrid/Cell.cs
Expand Up @@ -26,7 +26,7 @@ public uint Index

public int Level
{
get { return (Misc.LogBase2((Math.Max(2, index) - 2) / 5) + 1) / 2; }
get { return (IntMath.LogBase2((Math.Max(2, index) - 2) / 5) + 1) / 2; }
}

public bool IsPentagon
Expand Down Expand Up @@ -542,7 +542,7 @@ private Cell approach(ChildType direction, int levels)
{
if (levels == 0) { return this; }
if (isPolar) { throw new ArgumentException("Cannot find child of a polar cell"); }
var a = (uint)Misc.IntPow(3, (uint)levels);
var a = (uint)IntMath.IntPow(3, (uint)levels);
return new Cell(this.index * a - (uint)((5 * (4 << (2 * this.Level)) * (a - (1 << (2 * levels))) - ((byte)direction - 4) * (a - 1)) / 2));
}

Expand Down
36 changes: 36 additions & 0 deletions Plugin/GeodesicGrid/IntMath.cs
@@ -0,0 +1,36 @@

namespace Kethane.GeodesicGrid
{
internal static class IntMath
{
// From: http://stackoverflow.com/a/383596/303422
public static int IntPow(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];
}
}
}
1 change: 1 addition & 0 deletions Plugin/Kethane.csproj
Expand Up @@ -53,6 +53,7 @@
<Compile Include="GeodesicGrid\CellMap.cs" />
<Compile Include="GeodesicGrid\CellSet.cs" />
<Compile Include="GeodesicGrid\ChildType.cs" />
<Compile Include="GeodesicGrid\IntMath.cs" />
<Compile Include="InstallCleanup.cs" />
<Compile Include="IWindowToggle.cs" />
<Compile Include="LegacyResourceGenerator.cs" />
Expand Down
34 changes: 0 additions & 34 deletions Plugin/Misc.cs
Expand Up @@ -184,40 +184,6 @@ public static string GetInformationalVersion(this System.Reflection.Assembly ass
return System.Diagnostics.FileVersionInfo.GetVersionInfo(assembly.Location).ProductVersion;
}

#region Bit hacks

// From: http://stackoverflow.com/a/383596/303422
public static int IntPow(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];
}

#endregion

#region Stream extensions

public static byte[] ReadFully(this Stream input)
Expand Down

0 comments on commit 872ae4b

Please sign in to comment.