Skip to content

Instantly share code, notes, and snippets.

@tnayuki
Created July 27, 2018 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tnayuki/1ebbe2e272453102f5fb7ee5ee8a836a to your computer and use it in GitHub Desktop.
Save tnayuki/1ebbe2e272453102f5fb7ee5ee8a836a to your computer and use it in GitHub Desktop.
Xorshift64 for Burst
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