CommitLineData
79c94bd3
JA
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
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
9c42684e 36#include <string.h>
d1af2894 37#include <assert.h>
1fbbf72e 38#include "rand.h"
637ef8d9 39#include "../hash.h"
1fbbf72e 40
16dc0710
JA
41int arch_random;
42
1fbbf72e
JA
43static inline int __seed(unsigned int x, unsigned int m)
44{
45 return (x < m) ? x + m : x;
46}
47
2615cc4b
JA
48static 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
1fbbf72e
JA
62void init_rand(struct frand_state *state)
63{
2615cc4b
JA
64 __init_rand(state, 1);
65}
66
67void init_rand_seed(struct frand_state *state, unsigned int seed)
68{
69 __init_rand(state, seed);
1fbbf72e 70}
637ef8d9 71
7d9fb455 72void __fill_random_buf(void *buf, unsigned int len, unsigned long seed)
637ef8d9 73{
6780906f 74 void *ptr = buf;
637ef8d9 75
6780906f 76 while (len) {
74a92cdd
JA
77 int this_len;
78
6780906f
JA
79 if (len >= sizeof(int64_t)) {
80 *((int64_t *) ptr) = seed;
74a92cdd 81 this_len = sizeof(int64_t);
6780906f
JA
82 } else if (len >= sizeof(int32_t)) {
83 *((int32_t *) ptr) = seed;
74a92cdd 84 this_len = sizeof(int32_t);
6780906f
JA
85 } else if (len >= sizeof(int16_t)) {
86 *((int16_t *) ptr) = seed;
74a92cdd 87 this_len = sizeof(int16_t);
6780906f
JA
88 } else {
89 *((int8_t *) ptr) = seed;
74a92cdd 90 this_len = sizeof(int8_t);
6780906f 91 }
74a92cdd
JA
92 ptr += this_len;
93 len -= this_len;
7d9fb455
JA
94 seed *= GOLDEN_RATIO_PRIME;
95 seed >>= 3;
637ef8d9
JA
96 }
97}
7d9fb455 98
3545a109
JA
99unsigned long fill_random_buf(struct frand_state *fs, void *buf,
100 unsigned int len)
7d9fb455 101{
3545a109 102 unsigned long r = __rand(fs);
7d9fb455
JA
103
104 if (sizeof(int) != sizeof(long *))
3545a109 105 r *= (unsigned long) __rand(fs);
7d9fb455
JA
106
107 __fill_random_buf(buf, len, r);
108 return r;
109}
9c42684e 110
d1af2894
JA
111void 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
bc769898
JA
137void __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)
9c42684e 141{
811ac503 142 unsigned int this_len;
9c42684e 143
811ac503 144 if (percentage == 100) {
d1af2894
JA
145 if (pbytes)
146 fill_pattern(buf, len, pattern, pbytes);
147 else
148 memset(buf, 0, len);
bc769898 149 return;
811ac503
JA
150 }
151
152 if (segment > len)
153 segment = len;
9c42684e 154
9c42684e
JA
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
bc769898 163 __fill_random_buf(buf, this_len, seed);
9c42684e
JA
164
165 len -= this_len;
6780906f
JA
166 if (!len)
167 break;
9c42684e
JA
168 buf += this_len;
169
811ac503
JA
170 if (this_len > len)
171 this_len = len;
6780906f
JA
172 else if (len - this_len <= sizeof(long))
173 this_len = len;
9c42684e 174
d1af2894
JA
175 if (pbytes)
176 fill_pattern(buf, this_len, pattern, pbytes);
177 else
178 memset(buf, 0, this_len);
bc769898 179
98836539
JA
180 len -= this_len;
181 buf += this_len;
9c42684e 182 }
bc769898
JA
183}
184
185unsigned 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);
9c42684e 194
bc769898
JA
195 __fill_random_buf_percentage(r, buf, percentage, segment, len,
196 pattern, pbytes);
9c42684e
JA
197 return r;
198}