Created
July 27, 2018 14:39
-
-
Save tnayuki/1ebbe2e272453102f5fb7ee5ee8a836a to your computer and use it in GitHub Desktop.
Xorshift64 for Burst
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 UnityEngine; | |
using Unity.Burst; | |
using Unity.Collections; | |
using Unity.Collections.LowLevel.Unsafe; | |
using System.Threading; | |
public unsafe struct NativeXorshift64 { | |
[NativeDisableUnsafePtrRestriction] | |
public ulong *y; | |
public Allocator label; | |
public NativeXorshift64(ulong seed, Allocator allocator) { | |
label = allocator; | |
y = (ulong *)UnsafeUtility.Malloc(sizeof(ulong), UnsafeUtility.AlignOf<ulong>(), label); | |
*y = seed; | |
} | |
public void Dispose() { | |
UnsafeUtility.Free(y, label); | |
} | |
public ulong Next() { | |
ulong i, c; | |
do { | |
c = i = *y; | |
c = c ^ (c << 13); | |
c = c ^ (c >> 17); | |
c = c ^ (c << 15); | |
} while ((long)i != Interlocked.CompareExchange(ref *((long *)y), (long)c, (long)i)); | |
return c; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment