Skip to content

Instantly share code, notes, and snippets.

@rygorous
Created November 16, 2015 05:13
Embed
What would you like to do?
Oh ye of little faith.
uint64_t shuffle(uint64_t state)
{
uint64_t word = ((state >> ((state >> 59u) + 5u)) ^ state)
* 12605985483714917081ull;
return (word >> 43u) ^ word;
}
uint64_t unxorshift(uint64_t xs, uint32_t shamt)
{
// bits:
// xs[i] = x[i] ^ x[i+shamt]
// recover one by one, higher bits to low
uint64_t x = xs;
uint64_t bitmask;
for (bitmask = 1ull << (63 - shamt); bitmask; bitmask >>= 1)
x ^= (x >> shamt) & bitmask;
return x;
}
uint64_t unshuffle(uint64_t shuffled)
{
uint64_t word = shuffled;
word = unxorshift(word, 43u);
word *= 15009553638781119849ull; // modular inverse
word = unxorshift(word, (word >> 59u) + 5u);
return word;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment