2 * GF-Complete: A Comprehensive Open Source Library for Galois Field Arithmetic
3 * James S. Plank, Ethan L. Miller, Kevin M. Greenan,
4 * Benjamin A. Arnold, John A. Burnum, Adam W. Disney, Allen C. McBride.
6 * gf_rand.c -- Random number generator.
14 /* Lifted the "Mother of All" random number generator from http://www.agner.org/random/ */
16 static uint32_t MOA_X[5];
18 uint32_t MOA_Random_32() {
20 sum = (uint64_t)2111111111UL * (uint64_t)MOA_X[3] +
21 (uint64_t)1492 * (uint64_t)(MOA_X[2]) +
22 (uint64_t)1776 * (uint64_t)(MOA_X[1]) +
23 (uint64_t)5115 * (uint64_t)(MOA_X[0]) +
25 MOA_X[3] = MOA_X[2]; MOA_X[2] = MOA_X[1]; MOA_X[1] = MOA_X[0];
26 MOA_X[4] = (uint32_t)(sum >> 32);
27 MOA_X[0] = (uint32_t)sum;
31 uint64_t MOA_Random_64() {
34 sum = MOA_Random_32();
36 sum |= MOA_Random_32();
40 void MOA_Random_128(uint64_t *x) {
41 x[0] = MOA_Random_64();
42 x[1] = MOA_Random_64();
46 uint32_t MOA_Random_W(int w, int zero_ok)
52 if (w == 31) b &= 0x7fffffff;
53 if (w < 31) b %= (1 << w);
54 } while (!zero_ok && b == 0);
58 void MOA_Seed(uint32_t seed) {
61 for (i = 0; i < 5; i++) {
65 for (i=0; i<19; i++) MOA_Random_32();
69 void MOA_Fill_Random_Region (void *reg, int size)
75 r32 = (uint32_t *) reg;
77 for (i = 0; i < size/4; i++) r32[i] = MOA_Random_32();
78 for (i *= 4; i < size; i++) r8[i] = MOA_Random_W(8, 1);