2 * Written by Josh Dybnis and released to the public domain, as explained at
3 * http://creativecommons.org/licenses/publicdomain
5 #define _POSIX_C_SOURCE 1 // for rand_r()
14 DECLARE_THREAD_LOCAL(tid_, int);
15 DECLARE_THREAD_LOCAL(rx_, uint32_t);
16 DECLARE_THREAD_LOCAL(ry_, uint32_t);
17 DECLARE_THREAD_LOCAL(rz_, uint32_t);
18 DECLARE_THREAD_LOCAL(rc_, uint32_t);
20 typedef struct thread_info {
22 void *(*start_routine)(void *);
26 __attribute__ ((constructor)) void nbd_init (void) {
28 INIT_THREAD_LOCAL(tid_);
29 SET_THREAD_LOCAL(tid_, 0);
33 srand((uint32_t)rdtsc());
36 static void *worker (void *arg) {
37 thread_info_t *ti = (thread_info_t *)arg;
38 SET_THREAD_LOCAL(tid_, ti->thread_id);
39 LOCALIZE_THREAD_LOCAL(tid_, int);
41 SET_THREAD_LOCAL(rx_, rand());
42 SET_THREAD_LOCAL(ry_, rand());
43 SET_THREAD_LOCAL(rz_, rand());
44 SET_THREAD_LOCAL(rc_, rand());
46 lwt_thread_init(ti->thread_id);
47 rcu_thread_init(ti->thread_id);
49 void *ret = ti->start_routine(ti->arg);
54 int nbd_thread_create (pthread_t *restrict thread, int thread_id, void *(*start_routine)(void *), void *restrict arg) {
55 thread_info_t *ti = (thread_info_t *)nbd_malloc(sizeof(thread_info_t));
56 ti->thread_id = thread_id;
57 ti->start_routine = start_routine;
59 return pthread_create(thread, NULL, worker, ti);
62 // George Marsaglia's KISS generator
63 uint64_t nbd_rand (void) {
64 LOCALIZE_THREAD_LOCAL(rx_, unsigned);
65 LOCALIZE_THREAD_LOCAL(ry_, unsigned);
66 LOCALIZE_THREAD_LOCAL(rz_, unsigned);
67 LOCALIZE_THREAD_LOCAL(rc_, unsigned);
69 uint32_t rx = 69069 * rx_ + 12345;
75 uint64_t t = rz * 698769069LL + rc_;
76 uint64_t r = rx + ry + (rz = t);
78 SET_THREAD_LOCAL(rx_, rx);
79 SET_THREAD_LOCAL(ry_, ry);
80 SET_THREAD_LOCAL(rz_, rz);
81 SET_THREAD_LOCAL(rc_, t >> 32);
86 // Fairly fast random numbers
87 uint64_t nbd_rand_seed (int i) {
88 return rdtsc() + -715159705 + i * 129;
91 int nbd_next_rand (uint64_t *r) {
92 *r = (*r * 0x5DEECE66DLL + 0xBLL) & MASK(48);
93 return (*r >> 17) & 0x7FFFFFFF;