Revision 35afde18 xseg/peers/user/bench-lfsr.c
b/xseg/peers/user/bench-lfsr.c | ||
---|---|---|
44 | 44 |
#include <sys/util.h> |
45 | 45 |
#include <signal.h> |
46 | 46 |
#include <bench-xseg.h> |
47 |
#include <limits.h> |
|
47 | 48 |
|
48 | 49 |
#include <math.h> |
49 | 50 |
|
... | ... | |
57 | 58 |
|
58 | 59 |
#define MAX_TAPS 6 |
59 | 60 |
|
61 |
#ifdef STAND_ALONE |
|
62 |
uint64_t global_seed; |
|
63 |
#endif |
|
64 |
|
|
60 | 65 |
/* |
61 | 66 |
* LFSRs are pseudo-random number generators. They are deterministic (meaning |
62 | 67 |
* that the same seed will produce the same number sequence) and extremely |
... | ... | |
188 | 193 |
* have (n+1) bits. |
189 | 194 |
* NOTE3: If an LFSR has n bits, the seed must not be all ones (= 2^(n+1) - 1) |
190 | 195 |
*/ |
196 |
/* |
|
191 | 197 |
int lfsr_init(struct lfsr *lfsr, uint64_t size, uint64_t seed) |
192 | 198 |
{ |
193 | 199 |
uint8_t i; |
... | ... | |
210 | 216 |
lfsr->state = seed; |
211 | 217 |
return 0; |
212 | 218 |
} |
219 |
*/ |
|
220 |
int lfsr_init(struct lfsr *lfsr, uint64_t size, uint64_t seed) |
|
221 |
{ |
|
222 |
uint8_t i; |
|
223 |
|
|
224 |
lfsr->limit = size; |
|
225 |
|
|
226 |
//i has number of bits of size |
|
227 |
for (i = 0; size; i++) |
|
228 |
size = size >> 1; |
|
213 | 229 |
|
230 |
//Too small or too big size to create an LFSR out of it |
|
231 |
if (i < 3 || i > 63) |
|
232 |
return -1; |
|
214 | 233 |
|
234 |
//The all ones state is illegal. Due to the fact that our seed is |
|
235 |
//nanoseconds taken from clock_gettime, we are sure that the 31st bit will |
|
236 |
//always be 0. The following codes has that in mind and creates a seed |
|
237 |
//that has at least one 0. |
|
238 |
if (seed == UINT64_MAX) { |
|
239 |
if (i < 32) |
|
240 |
lfsr->state = global_seed >> (31 - i); |
|
241 |
else |
|
242 |
lfsr->state = global_seed << (i - 31); |
|
243 |
} |
|
244 |
else { |
|
245 |
lfsr->state = seed; |
|
246 |
} |
|
247 |
|
|
248 |
lfsr->length = i; |
|
249 |
lfsr->xnormask = lfsr_create_xnormask(taps[i]); |
|
250 |
|
|
251 |
return 0; |
|
252 |
} |
|
253 |
|
|
254 |
#ifdef STAND_ALONE |
|
215 | 255 |
/* |
216 | 256 |
* Sanity-check every LFSR for wrong tap positions. |
217 | 257 |
*/ |
... | ... | |
249 | 289 |
return 0; |
250 | 290 |
} |
251 | 291 |
|
252 |
|
|
253 | 292 |
int main() |
254 | 293 |
{ |
255 | 294 |
int r; |
... | ... | |
258 | 297 |
|
259 | 298 |
return r; |
260 | 299 |
} |
300 |
#endif |
|
261 | 301 |
|
Also available in: Unified diff