1//////////////////////////////////////////////////////////////////////
2// This file is part of Remere's Map Editor
3//////////////////////////////////////////////////////////////////////
4// Remere's Map Editor is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Remere's Map Editor is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16//////////////////////////////////////////////////////////////////////
17
18#include "main.h"
19
20static inline unsigned long int mt_get(void* vstate);
21static double mt_get_double(void* vstate);
22static void mt_set(void* state, unsigned long int s);
23
24#define N 624 /* Period parameters */
25#define M 397
26
27/* most significant w-r bits */
28static const unsigned long UPPER_MASK = 0x80000000UL;
29
30/* least significant r bits */
31static const unsigned long LOWER_MASK = 0x7fffffffUL;
32
33typedef struct
34{
35 unsigned long mt[N];
36 int mti;
37} mt_state_t;
38
39static inline unsigned long
40mt_get(void* vstate) {
41 mt_state_t* state = (mt_state_t*)vstate;
42
43 unsigned long k;
44 unsigned long int* const mt = state->mt;
45
46#define MAGIC(y) (((y) & 0x1) ? 0x9908b0dfUL : 0)
47
48 if (state->mti >= N) { /* generate N words at one time */
49 int kk;
50
51 for (kk = 0; kk < N - M; kk++) {
52 unsigned long y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
53 mt[kk] = mt[kk + M] ^ (y >> 1) ^ MAGIC(y);
54 }
55 for (; kk < N - 1; kk++) {
56 unsigned long y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
57 mt[kk] = mt[kk + (M - N)] ^ (y >> 1) ^ MAGIC(y);
58 }
59
60 {
61 unsigned long y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK);
62 mt[N - 1] = mt[M - 1] ^ (y >> 1) ^ MAGIC(y);
63 }
64
65 state->mti = 0;
66 }
67
68 /* Tempering */
69
70 k = mt[state->mti];
71 k ^= (k >> 11);
72 k ^= (k << 7) & 0x9d2c5680UL;
73 k ^= (k << 15) & 0xefc60000UL;
74 k ^= (k >> 18);
75
76 state->mti++;
77
78 return k;
79}
80
81static double
82mt_get_double(void* vstate) {
83 return mt_get(vstate) / 4294967296.0;
84}
85
86static void
87mt_set(void* vstate, unsigned long int s) {
88 mt_state_t* state = (mt_state_t*)vstate;
89 int i;
90
91 if (s == 0) {
92 s = 4357; /* the default seed is 4357 */
93 }
94
95 state->mt[0] = s & 0xffffffffUL;
96
97 for (i = 1; i < N; i++) {
98 /* See Knuth's "Art of Computer Programming" Vol. 2, 3rd
99 Ed. p.106 for multiplier. */
100
101 state->mt[i] = (1812433253UL * (state->mt[i - 1] ^ (state->mt[i - 1] >> 30)) + i);
102
103 state->mt[i] &= 0xffffffffUL;
104 }
105
106 state->mti = i;
107}
108
109static mt_state_t mt_state;
110
111void mt_seed(unsigned long s) {
112 mt_set(&mt_state, s);
113}
114
115unsigned long mt_randi() {
116 return mt_get(&mt_state);
117}
118
119double mt_randd() {
120 return mt_get_double(&mt_state);
121}
122