8 #define FRAND32_MAX (-1U)
9 #define FRAND64_MAX (-1ULL)
12 unsigned int s1, s2, s3;
15 struct taus258_state {
16 uint64_t s1, s2, s3, s4, s5;
22 struct taus88_state state32;
23 struct taus258_state state64;
27 static inline uint64_t rand_max(struct frand_state *state)
35 static inline void __frand32_copy(struct taus88_state *dst,
36 struct taus88_state *src)
43 static inline void __frand64_copy(struct taus258_state *dst,
44 struct taus258_state *src)
53 static inline void frand_copy(struct frand_state *dst, struct frand_state *src)
56 __frand64_copy(&dst->state64, &src->state64);
58 __frand32_copy(&dst->state32, &src->state32);
60 dst->use64 = src->use64;
63 static inline unsigned int __rand32(struct taus88_state *state)
65 #define TAUSWORTHE(s,a,b,c,d) ((s&c)<<d) ^ (((s <<a) ^ s)>>b)
67 state->s1 = TAUSWORTHE(state->s1, 13, 19, 4294967294UL, 12);
68 state->s2 = TAUSWORTHE(state->s2, 2, 25, 4294967288UL, 4);
69 state->s3 = TAUSWORTHE(state->s3, 3, 11, 4294967280UL, 17);
71 return (state->s1 ^ state->s2 ^ state->s3);
74 static inline uint64_t __rand64(struct taus258_state *state)
78 xval = ((state->s1 << 1) ^ state->s1) >> 53;
79 state->s1 = ((state->s1 & 18446744073709551614ULL) << 10) ^ xval;
81 xval = ((state->s2 << 24) ^ state->s2) >> 50;
82 state->s2 = ((state->s2 & 18446744073709551104ULL) << 5) ^ xval;
84 xval = ((state->s3 << 3) ^ state->s3) >> 23;
85 state->s3 = ((state->s3 & 18446744073709547520ULL) << 29) ^ xval;
87 xval = ((state->s4 << 5) ^ state->s4) >> 24;
88 state->s4 = ((state->s4 & 18446744073709420544ULL) << 23) ^ xval;
90 xval = ((state->s5 << 3) ^ state->s5) >> 33;
91 state->s5 = ((state->s5 & 18446744073701163008ULL) << 8) ^ xval;
93 return (state->s1 ^ state->s2 ^ state->s3 ^ state->s4 ^ state->s5);
96 static inline uint64_t __rand(struct frand_state *state)
99 return __rand64(&state->state64);
101 return __rand32(&state->state32);
104 static inline double __rand_0_1(struct frand_state *state)
107 uint64_t val = __rand64(&state->state64);
109 return (val + 1.0) / (FRAND64_MAX + 1.0);
111 uint32_t val = __rand32(&state->state32);
113 return (val + 1.0) / (FRAND32_MAX + 1.0);
117 static inline uint32_t rand32_upto(struct frand_state *state, uint32_t end)
121 assert(!state->use64);
123 r = __rand32(&state->state32);
125 return (int) ((double)end * (r / (FRAND32_MAX + 1.0)));
128 static inline uint64_t rand64_upto(struct frand_state *state, uint64_t end)
132 assert(state->use64);
134 r = __rand64(&state->state64);
136 return (uint64_t) ((double)end * (r / (FRAND64_MAX + 1.0)));
140 * Generate a random value between 'start' and 'end', both inclusive
142 static inline uint64_t rand_between(struct frand_state *state, uint64_t start,
146 return start + rand64_upto(state, end - start);
148 return start + rand32_upto(state, end - start);
151 extern void init_rand(struct frand_state *, bool);
152 extern void init_rand_seed(struct frand_state *, unsigned int seed, bool);
153 extern void __fill_random_buf(void *buf, unsigned int len, unsigned long seed);
154 extern unsigned long fill_random_buf(struct frand_state *, void *buf, unsigned int len);
155 extern void __fill_random_buf_percentage(unsigned long, void *, unsigned int, unsigned int, unsigned int, char *, unsigned int);
156 extern unsigned long fill_random_buf_percentage(struct frand_state *, void *, unsigned int, unsigned int, unsigned int, char *, unsigned int);