Created
November 13, 2018 15:51
-
-
Save IJzerbaard/2ddba72c613b73a9c3663770d7a7ba23 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using System.Runtime.CompilerServices; | |
namespace CSTest | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Console.WriteLine("Latency test:"); | |
for (int j = 0; j < 10; j++) | |
{ | |
bool b = (j & 1) == 1; | |
var sw = Stopwatch.StartNew(); | |
ulong x = 0; | |
if (b) | |
{ | |
for (int i = 0; i < 1000000; i++) | |
{ | |
x = Transpose(x); | |
} | |
} | |
else | |
{ | |
for (int i = 0; i < 1000000; i++) | |
{ | |
x = Transpose2(x); | |
} | |
} | |
sw.Stop(); | |
sink = (uint)x; | |
Console.WriteLine("{0}: {1}", b ? "plain" : "mul ", sw.ElapsedTicks); | |
} | |
Console.WriteLine("Throughput test:"); | |
for (int j = 0; j < 10; j++) | |
{ | |
bool b = (j & 1) == 1; | |
var sw = Stopwatch.StartNew(); | |
ulong x = 0; | |
if (b) | |
{ | |
for (int i = 0; i < 1000000; i++) | |
{ | |
sink = (uint)Transpose(0); | |
} | |
} | |
else | |
{ | |
for (int i = 0; i < 1000000; i++) | |
{ | |
sink = (uint)Transpose2(0); | |
} | |
} | |
sw.Stop(); | |
Console.WriteLine("{0}: {1}", b ? "plain" : "mul ", sw.ElapsedTicks); | |
} | |
} | |
volatile static uint sink; | |
[MethodImpl(MethodImplOptions.NoInlining)] | |
public static ulong Transpose2(ulong state) | |
{ | |
ulong r0 = state & 0xF0000F0000F0000FL; //unchanged diagonals | |
ulong t = (state & 0x0000F000FF000FF0UL) * ((1UL << 12) + (1UL << 24)); | |
ulong r1 = (state & 0x0F0000F0000F0000L) >> 12; | |
r0 |= (state & 0x00F0000F00000000L) >> 24; | |
r1 |= (state & 0x000F000000000000L) >> 36; | |
r0 |= (state & 0x000000000000F000L) << 36; | |
r1 |= t & 0x0FF000FF000F0000UL; | |
return r0 | r1; | |
} | |
[MethodImpl(MethodImplOptions.NoInlining)] | |
public static ulong Transpose(ulong state) | |
{ | |
return state & 0xF0000F0000F0000FL //unchanged diagonals | |
| (state & 0x0F0000F0000F0000L) >> 12 | |
| (state & 0x0000F0000F0000F0L) << 12 | |
| (state & 0x00F0000F00000000L) >> 24 | |
| (state & 0x00000000F0000F00L) << 24 | |
| (state & 0x000F000000000000L) >> 36 | |
| (state & 0x000000000000F000L) << 36; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment