| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
#ifndef VMMERSENNE_H |
| 16 |
#define VMMERSENNE_H |
| 17 |
|
| 18 |
#include "t3std.h" |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
class CVmMT19937 |
| 25 |
{ |
| 26 |
public: |
| 27 |
CVmMT19937() |
| 28 |
{ |
| 29 |
idx = 0; |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
void seed_random() |
| 34 |
{ |
| 35 |
|
| 36 |
os_gen_rand_bytes((unsigned char *)mt, sizeof(mt)); |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
void seed(uint32_t s) |
| 45 |
{ |
| 46 |
|
| 47 |
base_seed(s); |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
for (int i = 0 ; i < 37 ; ++i) |
| 56 |
rand(); |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
void seed(const char *buf, size_t len) |
| 65 |
{ |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
base_seed(19650218); |
| 72 |
|
| 73 |
|
| 74 |
size_t icnt = (len + 3)/4; |
| 75 |
|
| 76 |
|
| 77 |
uint32_t partial = 0; |
| 78 |
if (icnt*4 > len) |
| 79 |
{ |
| 80 |
int mul = 1; |
| 81 |
for (size_t i = (len/4)*4 ; i < len ; ++i, mul *= 256) |
| 82 |
partial += buf[i]*mul; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
int i = 1, j = 0, k = NMT > icnt ? NMT : icnt; |
| 91 |
for ( ; k != 0 ; --k) |
| 92 |
{ |
| 93 |
|
| 94 |
uint32_t seed = (j+1)*4 <= (int)len ? osrp4(buf + j*4) : partial; |
| 95 |
|
| 96 |
|
| 97 |
mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 0x19660D)) |
| 98 |
+ seed + j; |
| 99 |
|
| 100 |
|
| 101 |
if (++i >= NMT) |
| 102 |
{ |
| 103 |
mt[0] = mt[NMT-1]; |
| 104 |
i = 1; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
if (++j >= (int)icnt) |
| 109 |
j = 0; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
for (k = NMT-1 ; k != 0 ; --k) |
| 114 |
{ |
| 115 |
mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 0x5D588B65)) - i; |
| 116 |
if (++i >= NMT) |
| 117 |
{ |
| 118 |
mt[0] = mt[NMT-1]; |
| 119 |
i = 1; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
mt[0] = 0x80000000UL; |
| 125 |
|
| 126 |
|
| 127 |
idx = 0; |
| 128 |
for (i = 0 ; i <= NMT ; ++i) |
| 129 |
rand(); |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
uint32_t rand() |
| 134 |
{ |
| 135 |
|
| 136 |
if (idx >= NMT) |
| 137 |
{ |
| 138 |
generate_untempered(); |
| 139 |
idx = 0; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
uint32_t y = mt[idx]; |
| 144 |
y ^= (y >> 11); |
| 145 |
y ^= ((y << 7) & 0x9d2c5680); |
| 146 |
y ^= ((y << 15) & 0xefc60000); |
| 147 |
y ^= (y >> 18); |
| 148 |
|
| 149 |
|
| 150 |
++idx; |
| 151 |
|
| 152 |
|
| 153 |
return y; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
size_t get_state_size() const { return 2 + NMT*4; } |
| 158 |
|
| 159 |
|
| 160 |
void get_state(char *buf) |
| 161 |
{ |
| 162 |
oswp2(buf, (short)idx); |
| 163 |
for (int i = 0, dst = 2 ; i < NMT ; ++i, dst += 4) |
| 164 |
oswp4(buf + dst, mt[i]); |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
void put_state(const char *buf) |
| 169 |
{ |
| 170 |
idx = osrp2(buf) % NMT; |
| 171 |
for (int i = 0, src = 2 ; i < NMT ; ++i, src += 4) |
| 172 |
mt[i] = osrp4(buf + src); |
| 173 |
} |
| 174 |
|
| 175 |
protected: |
| 176 |
|
| 177 |
void base_seed(uint32_t s) |
| 178 |
{ |
| 179 |
mt[0] = s; |
| 180 |
for (idx = 1 ; idx < NMT ; ++idx) |
| 181 |
mt[idx] = 0x6c078965 * (mt[idx-1] ^ (mt[idx-1] >> 30)) + idx; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
void generate_untempered() |
| 186 |
{ |
| 187 |
for (int i = 0 ; i < NMT ; ++i) |
| 188 |
{ |
| 189 |
uint32_t y = (mt[i] & 0x80000000) + (mt[(i+1) % NMT] & 0x7fffffff); |
| 190 |
mt[i] = mt[(i + 397) % NMT] ^ (y >> 1); |
| 191 |
if ((y & 1) != 0) |
| 192 |
mt[i] ^= 0x9908b0df; |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
static const int NMT = 624; |
| 198 |
uint32_t mt[NMT]; |
| 199 |
|
| 200 |
|
| 201 |
int idx; |
| 202 |
}; |
| 203 |
|
| 204 |
|
| 205 |
#endif |