blob: 185b679e7c1365956cd80e40d433dc8fa41a951e [file] [log] [blame]
Jens Axboe79c94bd2010-03-25 23:13:32 +01001/*
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
Jens Axboe9c426842012-03-02 21:02:12 +010036#include <string.h>
Jens Axboefd1583f2014-12-03 19:55:33 -070037#include <assert.h>
Jens Axboe1fbbf722010-03-25 23:03:18 +010038#include "rand.h"
Jens Axboe637ef8d2010-06-21 20:39:38 +020039#include "../hash.h"
Jens Axboe1fbbf722010-03-25 23:03:18 +010040
Jens Axboe1fbbf722010-03-25 23:03:18 +010041static inline int __seed(unsigned int x, unsigned int m)
42{
43 return (x < m) ? x + m : x;
44}
45
Jens Axboe2615cc42011-03-28 09:35:09 +020046static void __init_rand(struct frand_state *state, unsigned int seed)
47{
48 int cranks = 6;
49
50#define LCG(x, seed) ((x) * 69069 ^ (seed))
51
52 state->s1 = __seed(LCG((2^31) + (2^17) + (2^7), seed), 1);
53 state->s2 = __seed(LCG(state->s1, seed), 7);
54 state->s3 = __seed(LCG(state->s2, seed), 15);
55
56 while (cranks--)
57 __rand(state);
58}
59
Jens Axboe1fbbf722010-03-25 23:03:18 +010060void init_rand(struct frand_state *state)
61{
Jens Axboe2615cc42011-03-28 09:35:09 +020062 __init_rand(state, 1);
63}
Jens Axboe1fbbf722010-03-25 23:03:18 +010064
Jens Axboe2615cc42011-03-28 09:35:09 +020065void init_rand_seed(struct frand_state *state, unsigned int seed)
66{
67 __init_rand(state, seed);
Jens Axboe1fbbf722010-03-25 23:03:18 +010068}
Jens Axboe637ef8d2010-06-21 20:39:38 +020069
Jens Axboe7d9fb452011-01-11 22:16:49 +010070void __fill_random_buf(void *buf, unsigned int len, unsigned long seed)
Jens Axboe637ef8d2010-06-21 20:39:38 +020071{
Jens Axboe2ed0dfa2014-12-05 10:48:01 -070072 void *ptr = buf;
Jens Axboe637ef8d2010-06-21 20:39:38 +020073
Jens Axboe2ed0dfa2014-12-05 10:48:01 -070074 while (len) {
Jens Axboe57a49c02014-12-05 11:04:19 -070075 int this_len;
76
Jens Axboe2ed0dfa2014-12-05 10:48:01 -070077 if (len >= sizeof(int64_t)) {
78 *((int64_t *) ptr) = seed;
Jens Axboe57a49c02014-12-05 11:04:19 -070079 this_len = sizeof(int64_t);
Jens Axboe2ed0dfa2014-12-05 10:48:01 -070080 } else if (len >= sizeof(int32_t)) {
81 *((int32_t *) ptr) = seed;
Jens Axboe57a49c02014-12-05 11:04:19 -070082 this_len = sizeof(int32_t);
Jens Axboe2ed0dfa2014-12-05 10:48:01 -070083 } else if (len >= sizeof(int16_t)) {
84 *((int16_t *) ptr) = seed;
Jens Axboe57a49c02014-12-05 11:04:19 -070085 this_len = sizeof(int16_t);
Jens Axboe2ed0dfa2014-12-05 10:48:01 -070086 } else {
87 *((int8_t *) ptr) = seed;
Jens Axboe57a49c02014-12-05 11:04:19 -070088 this_len = sizeof(int8_t);
Jens Axboe2ed0dfa2014-12-05 10:48:01 -070089 }
Jens Axboe57a49c02014-12-05 11:04:19 -070090 ptr += this_len;
91 len -= this_len;
Jens Axboe7d9fb452011-01-11 22:16:49 +010092 seed *= GOLDEN_RATIO_PRIME;
93 seed >>= 3;
94 }
95}
96
Jens Axboe3545a102011-08-31 15:20:15 -060097unsigned long fill_random_buf(struct frand_state *fs, void *buf,
98 unsigned int len)
Jens Axboe7d9fb452011-01-11 22:16:49 +010099{
Jens Axboe3545a102011-08-31 15:20:15 -0600100 unsigned long r = __rand(fs);
Jens Axboe7d9fb452011-01-11 22:16:49 +0100101
102 if (sizeof(int) != sizeof(long *))
Jens Axboe3545a102011-08-31 15:20:15 -0600103 r *= (unsigned long) __rand(fs);
Jens Axboe637ef8d2010-06-21 20:39:38 +0200104
Jens Axboe7d9fb452011-01-11 22:16:49 +0100105 __fill_random_buf(buf, len, r);
106 return r;
Jens Axboe637ef8d2010-06-21 20:39:38 +0200107}
Jens Axboe9c426842012-03-02 21:02:12 +0100108
Jens Axboefd1583f2014-12-03 19:55:33 -0700109void fill_pattern(void *p, unsigned int len, char *pattern,
110 unsigned int pattern_bytes)
111{
112 switch (pattern_bytes) {
113 case 0:
114 assert(0);
115 break;
116 case 1:
117 memset(p, pattern[0], len);
118 break;
119 default: {
120 unsigned int i = 0, size = 0;
121 unsigned char *b = p;
122
123 while (i < len) {
124 size = pattern_bytes;
125 if (size > (len - i))
126 size = len - i;
127 memcpy(b+i, pattern, size);
128 i += size;
129 }
130 break;
131 }
132 }
133}
134
Jens Axboee1457292014-12-04 15:40:32 -0700135void __fill_random_buf_percentage(unsigned long seed, void *buf,
136 unsigned int percentage,
137 unsigned int segment, unsigned int len,
138 char *pattern, unsigned int pbytes)
Jens Axboe9c426842012-03-02 21:02:12 +0100139{
Jens Axboe811ac502012-03-02 22:23:36 +0100140 unsigned int this_len;
Jens Axboe9c426842012-03-02 21:02:12 +0100141
Jens Axboe811ac502012-03-02 22:23:36 +0100142 if (percentage == 100) {
Jens Axboefd1583f2014-12-03 19:55:33 -0700143 if (pbytes)
144 fill_pattern(buf, len, pattern, pbytes);
145 else
146 memset(buf, 0, len);
Jens Axboee1457292014-12-04 15:40:32 -0700147 return;
Jens Axboe811ac502012-03-02 22:23:36 +0100148 }
149
150 if (segment > len)
151 segment = len;
Jens Axboe9c426842012-03-02 21:02:12 +0100152
Jens Axboe9c426842012-03-02 21:02:12 +0100153 while (len) {
154 /*
155 * Fill random chunk
156 */
157 this_len = (segment * (100 - percentage)) / 100;
158 if (this_len > len)
159 this_len = len;
160
Jens Axboee1457292014-12-04 15:40:32 -0700161 __fill_random_buf(buf, this_len, seed);
Jens Axboe9c426842012-03-02 21:02:12 +0100162
163 len -= this_len;
Jens Axboe2ed0dfa2014-12-05 10:48:01 -0700164 if (!len)
165 break;
Jens Axboe9c426842012-03-02 21:02:12 +0100166 buf += this_len;
167
Jens Axboe811ac502012-03-02 22:23:36 +0100168 if (this_len > len)
169 this_len = len;
Jens Axboe2ed0dfa2014-12-05 10:48:01 -0700170 else if (len - this_len <= sizeof(long))
171 this_len = len;
Jens Axboe9c426842012-03-02 21:02:12 +0100172
Jens Axboefd1583f2014-12-03 19:55:33 -0700173 if (pbytes)
174 fill_pattern(buf, this_len, pattern, pbytes);
175 else
176 memset(buf, 0, this_len);
Jens Axboee1457292014-12-04 15:40:32 -0700177
Jens Axboe98836532012-03-09 19:00:31 +0100178 len -= this_len;
179 buf += this_len;
Jens Axboe9c426842012-03-02 21:02:12 +0100180 }
Jens Axboee1457292014-12-04 15:40:32 -0700181}
Jens Axboe9c426842012-03-02 21:02:12 +0100182
Jens Axboee1457292014-12-04 15:40:32 -0700183unsigned long fill_random_buf_percentage(struct frand_state *fs, void *buf,
184 unsigned int percentage,
185 unsigned int segment, unsigned int len,
186 char *pattern, unsigned int pbytes)
187{
188 unsigned long r = __rand(fs);
189
190 if (sizeof(int) != sizeof(long *))
191 r *= (unsigned long) __rand(fs);
192
193 __fill_random_buf_percentage(r, buf, percentage, segment, len,
194 pattern, pbytes);
Jens Axboe9c426842012-03-02 21:02:12 +0100195 return r;
196}