#include <stdio.h>
#include <inttypes.h>
#include <sys/time.h>
/* Headers for the algorithms below */
uint64_t sm_s;
uint64_t xoro_s[2];
uint64_t next_xoro(void);
void jump_xoro(void);
uint64_t next_sm();
/* Just a little code to exercise the below methods. */
int main(void) {
// Initialise first seed using a semi-random input from the current wall
// clock.
struct timeval tv;
gettimeofday(&tv, NULL);
sm_s = tv.tv_sec;
printf("Generating random sequences for the SplitMix algorithm.\n"); uint64_t seeds[5];
for (int i = 0; i < 5; i++) { seeds[i] = next_sm(); }
for (int i = 0; i < 5; i++) {
printf("Initial seed: %"PRId64
"\n", seeds
[i
]); sm_s = seeds[i];
for (int j = 0; j < 20; j++) {
printf("%"PRId64
"\n", next_sm
()); }
}
printf("Generating random sequences for the xoroshiro128+ algorithm.\n"); for (int i = 0; i < 5; i++) {
xoro_s[0] = next_sm();
xoro_s[1] = next_sm();
printf("Initial seed: %"PRId64
" %"PRId64
"\n", xoro_s
[0], xoro_s
[1]); for (int j = 0; j < 20; j++) {
printf("%"PRId64
"\n", next_xoro
()); }
xoro_s[0] = next_sm();
xoro_s[1] = next_sm();
printf("Initial seed: %"PRId64
" %"PRId64
"\n", xoro_s
[0], xoro_s
[1]); for (int j = 0; j < 5; j++) {
jump_xoro();
printf("Jump %d: %"PRId64
"\n", j
+ 1, next_xoro
()); }
}
return 0;
}
/* Written in 2015-2016 by David Blackman and Sebastiano Vigna (vigna@acm.org)
To the extent possible under law, the author has dedicated all copyright
and related and neighboring rights to this software to the public domain
worldwide. This software is distributed without any warranty.
See <http://c...content-available-to-author-only...s.org/publicdomain/zero/1.0/>. */
static inline uint64_t rotl(const uint64_t x, int k) {
return (x << k) | (x >> (64 - k));
}
uint64_t next_xoro(void) {
const uint64_t s0 = xoro_s[0];
uint64_t s1 = xoro_s[1];
const uint64_t result = s0 + s1;
s1 ^= s0;
xoro_s[0] = rotl(s0, 55) ^ s1 ^ (s1 << 14); // a, b
xoro_s[1] = rotl(s1, 36); // c
return result;
}
/* This is the jump function for the generator. It is equivalent
to 2^64 calls to next(); it can be used to generate 2^64
non-overlapping subsequences for parallel computations. */
void jump_xoro(void) {
static const uint64_t JUMP[] = { 0xbeac0467eba5facb, 0xd86b048b86aa9922 };
uint64_t s0 = 0;
uint64_t s1 = 0;
for(int i = 0; i < sizeof JUMP / sizeof *JUMP; i++)
for(int b = 0; b < 64; b++) {
if (JUMP[i] & 1ULL << b) {
s0 ^= xoro_s[0];
s1 ^= xoro_s[1];
}
next_xoro();
}
xoro_s[0] = s0;
xoro_s[1] = s1;
}
uint64_t next_sm() {
uint64_t z = (sm_s += UINT64_C(0x9E3779B97F4A7C15));
z = (z ^ (z >> 30)) * UINT64_C(0xBF58476D1CE4E5B9);
z = (z ^ (z >> 27)) * UINT64_C(0x94D049BB133111EB);
return z ^ (z >> 31);
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxpbnR0eXBlcy5oPgojaW5jbHVkZSA8c3lzL3RpbWUuaD4KCi8qIEhlYWRlcnMgZm9yIHRoZSBhbGdvcml0aG1zIGJlbG93ICovCnVpbnQ2NF90IHNtX3M7CnVpbnQ2NF90IHhvcm9fc1syXTsKdWludDY0X3QgbmV4dF94b3JvKHZvaWQpOwp2b2lkIGp1bXBfeG9ybyh2b2lkKTsKdWludDY0X3QgbmV4dF9zbSgpOwoKLyogSnVzdCBhIGxpdHRsZSBjb2RlIHRvIGV4ZXJjaXNlIHRoZSBiZWxvdyBtZXRob2RzLiAqLwoKaW50IG1haW4odm9pZCkgewogICAgLy8gSW5pdGlhbGlzZSBmaXJzdCBzZWVkIHVzaW5nIGEgc2VtaS1yYW5kb20gaW5wdXQgZnJvbSB0aGUgY3VycmVudCB3YWxsIAogICAgLy8gY2xvY2suCiAgICBzdHJ1Y3QgdGltZXZhbCB0djsKICAgIGdldHRpbWVvZmRheSgmdHYsIE5VTEwpOwogICAgc21fcyA9IHR2LnR2X3NlYzsKICAgIAoJcHJpbnRmKCJHZW5lcmF0aW5nIHJhbmRvbSBzZ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