123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- #define _GNU_SOURCE
- #include <features.h>
- #include <limits.h>
- #include <stddef.h>
- #include <stdlib.h>
- #ifdef __UCLIBC_HAS_THREADS__
- #include <pthread.h>
- static pthread_mutex_t lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
- #else
- #define __pthread_mutex_lock(x)
- #define __pthread_mutex_unlock(x)
- #endif
- #define TYPE_0 0
- #define BREAK_0 8
- #define DEG_0 0
- #define SEP_0 0
- #define TYPE_1 1
- #define BREAK_1 32
- #define DEG_1 7
- #define SEP_1 3
- #define TYPE_2 2
- #define BREAK_2 64
- #define DEG_2 15
- #define SEP_2 1
- #define TYPE_3 3
- #define BREAK_3 128
- #define DEG_3 31
- #define SEP_3 3
- #define TYPE_4 4
- #define BREAK_4 256
- #define DEG_4 63
- #define SEP_4 1
- #define MAX_TYPES 5
- static int32_t randtbl[DEG_3 + 1] =
- {
- TYPE_3,
- -1726662223, 379960547, 1735697613, 1040273694, 1313901226,
- 1627687941, -179304937, -2073333483, 1780058412, -1989503057,
- -615974602, 344556628, 939512070, -1249116260, 1507946756,
- -812545463, 154635395, 1388815473, -1926676823, 525320961,
- -1009028674, 968117788, -123449607, 1284210865, 435012392,
- -2017506339, -911064859, -370259173, 1132637927, 1398500161,
- -205601318,
- };
- static struct random_data unsafe_state =
- {
-
- fptr : &randtbl[SEP_3 + 1],
- rptr : &randtbl[1],
-
- state : &randtbl[1],
- rand_type : TYPE_3,
- rand_deg : DEG_3,
- rand_sep : SEP_3,
- end_ptr : &randtbl[sizeof (randtbl) / sizeof (randtbl[0])]
- };
- void srandom (unsigned int x)
- {
- __pthread_mutex_lock(&lock);
- srandom_r (x, &unsafe_state);
- __pthread_mutex_unlock(&lock);
- }
- weak_alias (srandom, srand)
- char * initstate (unsigned int seed, char *arg_state, size_t n)
- {
- int32_t *ostate;
- __pthread_mutex_lock(&lock);
- ostate = &unsafe_state.state[-1];
- initstate_r (seed, arg_state, n, &unsafe_state);
- __pthread_mutex_unlock(&lock);
- return (char *) ostate;
- }
- char * setstate (char *arg_state)
- {
- int32_t *ostate;
- __pthread_mutex_lock(&lock);
- ostate = &unsafe_state.state[-1];
- if (setstate_r (arg_state, &unsafe_state) < 0)
- ostate = NULL;
- __pthread_mutex_unlock(&lock);
- return (char *) ostate;
- }
- long int random ()
- {
- int32_t retval;
- __pthread_mutex_lock(&lock);
- random_r (&unsafe_state, &retval);
- __pthread_mutex_unlock(&lock);
- return retval;
- }
|