11 DECLARE_THREAD_LOCAL(rx_, uint32_t);
12 DECLARE_THREAD_LOCAL(ry_, uint32_t);
13 DECLARE_THREAD_LOCAL(rz_, uint32_t);
14 DECLARE_THREAD_LOCAL(rc_, uint32_t);
16 void rnd_init (void) {
17 INIT_THREAD_LOCAL(rx_);
18 INIT_THREAD_LOCAL(ry_);
19 INIT_THREAD_LOCAL(rz_);
20 INIT_THREAD_LOCAL(rc_);
23 // TODO: put a lock around this so that multiple threads being initialize concurrently don't read
24 // the same values from /dev/urandom
25 void rnd_thread_init (void) {
26 int fd = open("/dev/urandom", O_RDONLY);
28 perror("Error opening /dev/urandom");
34 int n = read(fd, buf, sizeof(buf));
37 perror("Error reading from /dev/urandom");
39 fprintf(stderr, "Could not read enough bytes from /dev/urandom");
44 memcpy(&x, buf + 0, 4);
45 memcpy(&y, buf + 4, 4);
46 memcpy(&z, buf + 8, 4);
47 memcpy(&c, buf + 12, 4);
49 SET_THREAD_LOCAL(rx_, x);
50 SET_THREAD_LOCAL(ry_, y);
51 SET_THREAD_LOCAL(rz_, z);
52 SET_THREAD_LOCAL(rc_, z);
55 // George Marsaglia's KISS generator
57 // Even though this returns 64 bits, this algorithm was only designed to generate 32 bits.
58 // The upper 32 bits is going to be highly correlated with the lower 32 bits of the next call.
59 uint64_t nbd_rand (void) {
60 LOCALIZE_THREAD_LOCAL(rx_, unsigned);
61 LOCALIZE_THREAD_LOCAL(ry_, unsigned);
62 LOCALIZE_THREAD_LOCAL(rz_, unsigned);
63 LOCALIZE_THREAD_LOCAL(rc_, unsigned);
65 uint32_t rx = 69069 * rx_ + 12345;
70 uint64_t t = rz_ * 698769069LL + rc_;
71 uint64_t r = rx + ry + t;
73 SET_THREAD_LOCAL(rx_, rx);
74 SET_THREAD_LOCAL(ry_, ry);
75 SET_THREAD_LOCAL(rz_, t);
76 SET_THREAD_LOCAL(rc_, t >> 32);