6 /* Simple Park-Miller for floating point; LCG as used in glibc and other places */
8 #ifndef NO_THREAD_LOCAL
10 static __thread
unsigned long pmseed
= 29264;
13 fast_srandom(unsigned long seed_
)
25 fast_random(unsigned int max
)
27 pmseed
= ((pmseed
* 1103515245) + 12345) & 0x7fffffff;
28 return ((pmseed
& 0xffff) * max
) >> 16;
34 /* Construct (1,2) IEEE floating_t from our random integer */
35 /* http://rgba.org/articles/sfrand/sfrand.htm */
36 union { unsigned long ul
; floating_t f
; } p
;
37 p
.ul
= (((pmseed
*= 16807) & 0x007fffff) - 1) | 0x3f800000;
43 /* Thread local storage not supported through __thread,
44 * use pthread_getspecific() instead. */
48 static pthread_key_t seed_key
;
50 static void __attribute__((constructor
))
53 pthread_key_create(&seed_key
, NULL
);
54 fast_srandom(29264UL);
58 fast_srandom(unsigned long seed_
)
60 pthread_setspecific(seed_key
, (void *)seed_
);
66 return (unsigned long)pthread_getspecific(seed_key
);
70 fast_random(unsigned int max
)
72 unsigned long pmseed
= (unsigned long)pthread_getspecific(seed_key
);
73 pmseed
= ((pmseed
* 1103515245) + 12345) & 0x7fffffff;
74 pthread_setspecific(seed_key
, (void *)pmseed
);
75 return ((pmseed
& 0xffff) * max
) >> 16;
81 /* Construct (1,2) IEEE floating_t from our random integer */
82 /* http://rgba.org/articles/sfrand/sfrand.htm */
83 unsigned long pmseed
= (unsigned long)pthread_getspecific(seed_key
);
85 union { unsigned long ul
; floating_t f
; } p
;
86 p
.ul
= ((pmseed
& 0x007fffff) - 1) | 0x3f800000;
87 pthread_setspecific(seed_key
, (void *)pmseed
);