Add Intel rdrand support
[fio.git] / lib / rand.c
1 /*
2   This is a maximally equidistributed combined Tausworthe generator
3   based on code from GNU Scientific Library 1.5 (30 Jun 2004)
4
5    x_n = (s1_n ^ s2_n ^ s3_n)
6
7    s1_{n+1} = (((s1_n & 4294967294) <<12) ^ (((s1_n <<13) ^ s1_n) >>19))
8    s2_{n+1} = (((s2_n & 4294967288) << 4) ^ (((s2_n << 2) ^ s2_n) >>25))
9    s3_{n+1} = (((s3_n & 4294967280) <<17) ^ (((s3_n << 3) ^ s3_n) >>11))
10
11    The period of this generator is about 2^88.
12
13    From: P. L'Ecuyer, "Maximally Equidistributed Combined Tausworthe
14    Generators", Mathematics of Computation, 65, 213 (1996), 203--213.
15
16    This is available on the net from L'Ecuyer's home page,
17
18    http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps
19    ftp://ftp.iro.umontreal.ca/pub/simulation/lecuyer/papers/tausme.ps
20
21    There is an erratum in the paper "Tables of Maximally
22    Equidistributed Combined LFSR Generators", Mathematics of
23    Computation, 68, 225 (1999), 261--269:
24    http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps
25
26         ... the k_j most significant bits of z_j must be non-
27         zero, for each j. (Note: this restriction also applies to the
28         computer code given in [4], but was mistakenly not mentioned in
29         that paper.)
30
31    This affects the seeding procedure by imposing the requirement
32    s1 > 1, s2 > 7, s3 > 15.
33
34 */
35
36 #include <string.h>
37 #include <assert.h>
38 #include "rand.h"
39 #include "../hash.h"
40
41 int arch_random;
42
43 static inline int __seed(unsigned int x, unsigned int m)
44 {
45         return (x < m) ? x + m : x;
46 }
47
48 static void __init_rand(struct frand_state *state, unsigned int seed)
49 {
50         int cranks = 6;
51
52 #define LCG(x, seed)  ((x) * 69069 ^ (seed))
53
54         state->s1 = __seed(LCG((2^31) + (2^17) + (2^7), seed), 1);
55         state->s2 = __seed(LCG(state->s1, seed), 7);
56         state->s3 = __seed(LCG(state->s2, seed), 15);
57
58         while (cranks--)
59                 __rand(state);
60 }
61
62 void init_rand(struct frand_state *state)
63 {
64         __init_rand(state, 1);
65 }
66
67 void init_rand_seed(struct frand_state *state, unsigned int seed)
68 {
69         __init_rand(state, seed);
70 }
71
72 void __fill_random_buf(void *buf, unsigned int len, unsigned long seed)
73 {
74         void *ptr = buf;
75
76         while (len) {
77                 int this_len;
78
79                 if (len >= sizeof(int64_t)) {
80                         *((int64_t *) ptr) = seed;
81                         this_len = sizeof(int64_t);
82                 } else if (len >= sizeof(int32_t)) {
83                         *((int32_t *) ptr) = seed;
84                         this_len = sizeof(int32_t);
85                 } else if (len >= sizeof(int16_t)) {
86                         *((int16_t *) ptr) = seed;
87                         this_len = sizeof(int16_t);
88                 } else {
89                         *((int8_t *) ptr) = seed;
90                         this_len = sizeof(int8_t);
91                 }
92                 ptr += this_len;
93                 len -= this_len;
94                 seed *= GOLDEN_RATIO_PRIME;
95                 seed >>= 3;
96         }
97 }
98
99 unsigned long fill_random_buf(struct frand_state *fs, void *buf,
100                               unsigned int len)
101 {
102         unsigned long r = __rand(fs);
103
104         if (sizeof(int) != sizeof(long *))
105                 r *= (unsigned long) __rand(fs);
106
107         __fill_random_buf(buf, len, r);
108         return r;
109 }
110
111 void fill_pattern(void *p, unsigned int len, char *pattern,
112                   unsigned int pattern_bytes)
113 {
114         switch (pattern_bytes) {
115         case 0:
116                 assert(0);
117                 break;
118         case 1:
119                 memset(p, pattern[0], len);
120                 break;
121         default: {
122                 unsigned int i = 0, size = 0;
123                 unsigned char *b = p;
124
125                 while (i < len) {
126                         size = pattern_bytes;
127                         if (size > (len - i))
128                                 size = len - i;
129                         memcpy(b+i, pattern, size);
130                         i += size;
131                 }
132                 break;
133                 }
134         }
135 }
136
137 void __fill_random_buf_percentage(unsigned long seed, void *buf,
138                                   unsigned int percentage,
139                                   unsigned int segment, unsigned int len,
140                                   char *pattern, unsigned int pbytes)
141 {
142         unsigned int this_len;
143
144         if (percentage == 100) {
145                 if (pbytes)
146                         fill_pattern(buf, len, pattern, pbytes);
147                 else
148                         memset(buf, 0, len);
149                 return;
150         }
151
152         if (segment > len)
153                 segment = len;
154
155         while (len) {
156                 /*
157                  * Fill random chunk
158                  */
159                 this_len = (segment * (100 - percentage)) / 100;
160                 if (this_len > len)
161                         this_len = len;
162
163                 __fill_random_buf(buf, this_len, seed);
164
165                 len -= this_len;
166                 if (!len)
167                         break;
168                 buf += this_len;
169
170                 if (this_len > len)
171                         this_len = len;
172                 else if (len - this_len <= sizeof(long))
173                         this_len = len;
174
175                 if (pbytes)
176                         fill_pattern(buf, this_len, pattern, pbytes);
177                 else
178                         memset(buf, 0, this_len);
179
180                 len -= this_len;
181                 buf += this_len;
182         }
183 }
184
185 unsigned long fill_random_buf_percentage(struct frand_state *fs, void *buf,
186                                          unsigned int percentage,
187                                          unsigned int segment, unsigned int len,
188                                          char *pattern, unsigned int pbytes)
189 {
190         unsigned long r = __rand(fs);
191
192         if (sizeof(int) != sizeof(long *))
193                 r *= (unsigned long) __rand(fs);
194
195         __fill_random_buf_percentage(r, buf, percentage, segment, len,
196                                         pattern, pbytes);
197         return r;
198 }