6 /* Simple Park-Miller */
8 #ifndef NO_THREAD_LOCAL
10 static __thread
unsigned long pmseed
= 29264;
13 fast_srandom(unsigned long seed_
)
25 fast_random(unsigned int max
)
28 lo
= 16807 * (pmseed
& 0xffff);
29 hi
= 16807 * (pmseed
>> 16);
30 lo
+= (hi
& 0x7fff) << 16;
32 pmseed
= (lo
& 0x7fffffff) + (lo
>> 31);
33 return ((pmseed
& 0xffff) * max
) >> 16;
39 /* Construct (1,2) IEEE float from our random integer */
40 /* http://rgba.org/articles/sfrand/sfrand.htm */
41 union { unsigned long ul
; float f
; } p
;
42 p
.ul
= (((pmseed
*= 16807) & 0x007fffff) - 1) | 0x3f800000;
48 /* Thread local storage not supported through __thread,
49 * use pthread_getspecific() instead. */
53 static pthread_key_t seed_key
;
55 static void __attribute__((constructor
))
58 pthread_key_create(&seed_key
, NULL
);
59 fast_srandom(29264UL);
63 fast_srandom(unsigned long seed_
)
65 pthread_setspecific(seed_key
, (void *)seed_
);
71 return (unsigned long)pthread_getspecific(seed_key
);
75 fast_random(unsigned int max
)
77 unsigned long pmseed
= (unsigned long)pthread_getspecific(seed_key
);
79 lo
= 16807 * (pmseed
& 0xffff);
80 hi
= 16807 * (pmseed
>> 16);
81 lo
+= (hi
& 0x7fff) << 16;
83 pmseed
= (lo
& 0x7fffffff) + (lo
>> 31);
84 pthread_setspecific(seed_key
, (void *)pmseed
);
85 return ((pmseed
& 0xffff) * max
) >> 16;
91 /* Construct (1,2) IEEE float from our random integer */
92 /* http://rgba.org/articles/sfrand/sfrand.htm */
93 unsigned long pmseed
= (unsigned long)pthread_getspecific(seed_key
);
95 union { unsigned long ul
; float f
; } p
;
96 p
.ul
= ((pmseed
& 0x007fffff) - 1) | 0x3f800000;
97 pthread_setspecific(seed_key
, (void *)pmseed
);