TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
SFMT.h
Go to the documentation of this file.
1 /*
2  * Copyright notice
3  * ================
4  * GNU General Public License http://www.gnu.org/licenses/gpl.html
5  * This C++ implementation of SFMT contains parts of the original C code
6  * which was published under the following BSD license, which is therefore
7  * in effect in addition to the GNU General Public License.
8  * Copyright (c) 2006, 2007 by Mutsuo Saito, Makoto Matsumoto and Hiroshima University.
9  * Copyright (c) 2008 by Agner Fog.
10  * Copyright (c) 2008-2013 Trinity Core
11  *
12  * BSD License:
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions are met:
15  * > Redistributions of source code must retain the above copyright notice,
16  * this list of conditions and the following disclaimer.
17  * > Redistributions in binary form must reproduce the above copyright notice,
18  * this list of conditions and the following disclaimer in the documentation
19  * and/or other materials provided with the distribution.
20  * > Neither the name of the Hiroshima University nor the names of its
21  * contributors may be used to endorse or promote products derived from
22  * this software without specific prior written permission.
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #ifndef SFMT_H
37 #define SFMT_H
38 
39 #include <emmintrin.h> // Define SSE2 intrinsics
40 #include "randomc.h" // Define integer types etc
41 #include <time.h>
42 #include <new>
43 
44 // Choose one of the possible Mersenne exponents.
45 // Higher values give longer cycle length and use more memory:
46 //#define MEXP 607
47 //#define MEXP 1279
48 //#define MEXP 2281
49 //#define MEXP 4253
50  #define MEXP 11213
51 //#define MEXP 19937
52 //#define MEXP 44497
53 
54 // Define constants for the selected Mersenne exponent:
55 #if MEXP == 44497
56 #define SFMT_N 348 // Size of state vector
57 #define SFMT_M 330 // Position of intermediate feedback
58 #define SFMT_SL1 5 // Left shift of W[N-1], 32-bit words
59 #define SFMT_SL2 3 // Left shift of W[0], *8, 128-bit words
60 #define SFMT_SR1 9 // Right shift of W[M], 32-bit words
61 #define SFMT_SR2 3 // Right shift of W[N-2], *8, 128-bit words
62 #define SFMT_MASK 0xeffffffb,0xdfbebfff,0xbfbf7bef,0x9ffd7bff // AND mask
63 #define SFMT_PARITY 1,0,0xa3ac4000,0xecc1327a // Period certification vector
64 
65 #elif MEXP == 19937
66 #define SFMT_N 156 // Size of state vector
67 #define SFMT_M 122 // Position of intermediate feedback
68 #define SFMT_SL1 18 // Left shift of W[N-1], 32-bit words
69 #define SFMT_SL2 1 // Left shift of W[0], *8, 128-bit words
70 #define SFMT_SR1 11 // Right shift of W[M], 32-bit words
71 #define SFMT_SR2 1 // Right shift of W[N-2], *8, 128-bit words
72 #define SFMT_MASK 0xdfffffef,0xddfecb7f,0xbffaffff,0xbffffff6 // AND mask
73 #define SFMT_PARITY 1,0,0,0x13c9e684 // Period certification vector
74 
75 #elif MEXP == 11213
76 #define SFMT_N 88 // Size of state vector
77 #define SFMT_M 68 // Position of intermediate feedback
78 #define SFMT_SL1 14 // Left shift of W[N-1], 32-bit words
79 #define SFMT_SL2 3 // Left shift of W[0], *8, 128-bit words
80 #define SFMT_SR1 7 // Right shift of W[M], 32-bit words
81 #define SFMT_SR2 3 // Right shift of W[N-2], *8, 128-bit words
82 #define SFMT_MASK 0xeffff7fb,0xffffffef,0xdfdfbfff,0x7fffdbfd // AND mask
83 #define SFMT_PARITY 1,0,0xe8148000,0xd0c7afa3 // Period certification vector
84 
85 #elif MEXP == 4253
86 #define SFMT_N 34 // Size of state vector
87 #define SFMT_M 17 // Position of intermediate feedback
88 #define SFMT_SL1 20 // Left shift of W[N-1], 32-bit words
89 #define SFMT_SL2 1 // Left shift of W[0], *8, 128-bit words
90 #define SFMT_SR1 7 // Right shift of W[M], 32-bit words
91 #define SFMT_SR2 1 // Right shift of W[N-2], *8, 128-bit words
92 #define SFMT_MASK 0x9f7bffff, 0x9fffff5f, 0x3efffffb, 0xfffff7bb // AND mask
93 #define SFMT_PARITY 0xa8000001, 0xaf5390a3, 0xb740b3f8, 0x6c11486d // Period certification vector
94 
95 #elif MEXP == 2281
96 #define SFMT_N 18 // Size of state vector
97 #define SFMT_M 12 // Position of intermediate feedback
98 #define SFMT_SL1 19 // Left shift of W[N-1], 32-bit words
99 #define SFMT_SL2 1 // Left shift of W[0], *8, 128-bit words
100 #define SFMT_SR1 5 // Right shift of W[M], 32-bit words
101 #define SFMT_SR2 1 // Right shift of W[N-2], *8, 128-bit words
102 #define SFMT_MASK 0xbff7ffbf, 0xfdfffffe, 0xf7ffef7f, 0xf2f7cbbf // AND mask
103 #define SFMT_PARITY 0x00000001, 0x00000000, 0x00000000, 0x41dfa600 // Period certification vector
104 
105 #elif MEXP == 1279
106 #define SFMT_N 10 // Size of state vector
107 #define SFMT_M 7 // Position of intermediate feedback
108 #define SFMT_SL1 14 // Left shift of W[N-1], 32-bit words
109 #define SFMT_SL2 3 // Left shift of W[0], *8, 128-bit words
110 #define SFMT_SR1 5 // Right shift of W[M], 32-bit words
111 #define SFMT_SR2 1 // Right shift of W[N-2], *8, 128-bit words
112 #define SFMT_MASK 0xf7fefffd, 0x7fefcfff, 0xaff3ef3f, 0xb5ffff7f // AND mask
113 #define SFMT_PARITY 0x00000001, 0x00000000, 0x00000000, 0x20000000 // Period certification vector
114 
115 #elif MEXP == 607
116 #define SFMT_N 5 // Size of state vector
117 #define SFMT_M 2 // Position of intermediate feedback
118 #define SFMT_SL1 15 // Left shift of W[N-1], 32-bit words
119 #define SFMT_SL2 3 // Left shift of W[0], *8, 128-bit words
120 #define SFMT_SR1 13 // Right shift of W[M], 32-bit words
121 #define SFMT_SR2 3 // Right shift of W[N-2], *8, 128-bit words
122 #define SFMT_MASK 0xfdff37ff, 0xef7f3f7d, 0xff777b7d, 0x7ff7fb2f // AND mask
123 #define SFMT_PARITY 0x00000001, 0x00000000, 0x00000000, 0x5986f054 // Period certification vector
124 #endif
125 
126 // Functions used by SFMTRand::RandomInitByArray (UNUSED AND COMMENTED OUT)
127 /*
128 static uint32_t func1(uint32_t x) {
129  return (x ^ (x >> 27)) * 1664525U;
130 }
131 
132 static uint32_t func2(uint32_t x) {
133  return (x ^ (x >> 27)) * 1566083941U;
134 }
135 */
136 
137 // Subfunction for the sfmt algorithm
138 static inline __m128i sfmt_recursion(__m128i const &a, __m128i const &b,
139 __m128i const &c, __m128i const &d, __m128i const &mask) {
140  __m128i a1, b1, c1, d1, z1, z2;
141  b1 = _mm_srli_epi32(b, SFMT_SR1);
142  a1 = _mm_slli_si128(a, SFMT_SL2);
143  c1 = _mm_srli_si128(c, SFMT_SR2);
144  d1 = _mm_slli_epi32(d, SFMT_SL1);
145  b1 = _mm_and_si128(b1, mask);
146  z1 = _mm_xor_si128(a, a1);
147  z2 = _mm_xor_si128(b1, d1);
148  z1 = _mm_xor_si128(z1, c1);
149  z2 = _mm_xor_si128(z1, z2);
150  return z2;
151 }
152 
153 namespace boost {
154  template <typename T> class thread_specific_ptr;
155 }
156 
157 // Class for SFMT generator
158 class SFMTRand { // Encapsulate random number generator
160 
161 public:
163  {
164  LastInterval = 0;
165  RandomInit((int)(time(0)));
166  }
167 
168  void RandomInit(int seed) // Re-seed
169  {
170  // Re-seed
171  uint32_t i; // Loop counter
172  uint32_t y = seed; // Temporary
173  uint32_t statesize = SFMT_N*4; // Size of state vector
174 
175  // Fill state vector with random numbers from seed
176  uint32_t* s = (uint32_t*)&state;
177  s[0] = y;
178  const uint32_t factor = 1812433253U;// Multiplication factor
179 
180  for (i = 1; i < statesize; i++) {
181  y = factor * (y ^ (y >> 30)) + i;
182  ((uint32_t*)state)[i] = y;
183  }
184 
185  // Further initialization and period certification
186  Init2();
187  }
188 
189  int32_t IRandom(int32_t min, int32_t max) // Output random integer
190  {
191  // Output random integer in the interval min <= x <= max
192  // Slightly inaccurate if (max-min+1) is not a power of 2
193  if (max <= min) {
194  if (max == min) return min; else return 0x80000000;
195  }
196  // Assume 64 bit integers supported. Use multiply and shift method
197  uint32_t interval; // Length of interval
198  uint64_t longran; // Random bits * interval
199  uint32_t iran; // Longran / 2^32
200 
201  interval = (uint32_t)(max - min + 1);
202  longran = (uint64_t)BRandom() * interval;
203  iran = (uint32_t)(longran >> 32);
204  // Convert back to signed and return result
205  return (int32_t)iran + min;
206  }
207 
209  {
210  // Output random integer in the interval min <= x <= max
211  // Slightly inaccurate if (max-min+1) is not a power of 2
212  if (max <= min) {
213  if (max == min) return min; else return 0;
214  }
215  // Assume 64 bit integers supported. Use multiply and shift method
216  uint32_t interval; // Length of interval
217  uint64_t longran; // Random bits * interval
218  uint32_t iran; // Longran / 2^32
219 
220  interval = (uint32_t)(max - min + 1);
221  longran = (uint64_t)BRandom() * interval;
222  iran = (uint32_t)(longran >> 32);
223  // Convert back to signed and return result
224  return iran + min;
225  }
226 
227  double Random() // Output random floating point number
228  {
229  // Output random floating point number
230  if (ix >= SFMT_N*4-1) {
231  // Make sure we have at least two 32-bit numbers
232  Generate();
233  }
234  uint64_t r = *(uint64_t*)((uint32_t*)state+ix);
235  ix += 2;
236  // 52 bits resolution for compatibility with assembly version:
237  return (int64_t)(r >> 12) * (1./(67108864.0*67108864.0));
238  }
239 
240  uint32_t BRandom() // Output random bits
241  {
242  // Output 32 random bits
243  uint32_t y;
244 
245  if (ix >= SFMT_N*4) {
246  Generate();
247  }
248  y = ((uint32_t*)state)[ix++];
249  return y;
250  }
251 
252  void* operator new(size_t size, std::nothrow_t const&)
253  {
254  return _mm_malloc(size, 16);
255  }
256 
257  void operator delete(void* ptr, std::nothrow_t const&)
258  {
259  _mm_free(ptr);
260  }
261 
262  void* operator new(size_t size)
263  {
264  return _mm_malloc(size, 16);
265  }
266 
267  void operator delete(void* ptr)
268  {
269  _mm_free(ptr);
270  }
271 
272  void* operator new[](size_t size, std::nothrow_t const&)
273  {
274  return _mm_malloc(size, 16);
275  }
276 
277  void operator delete[](void* ptr, std::nothrow_t const&)
278  {
279  _mm_free(ptr);
280  }
281 
282  void* operator new[](size_t size)
283  {
284  return _mm_malloc(size, 16);
285  }
286 
287  void operator delete[](void* ptr)
288  {
289  _mm_free(ptr);
290  }
291 
292 private:
293  void Init2() // Various initializations and period certification
294  {
295  // Various initializations and period certification
296  uint32_t i, j, temp;
297 
298  // Initialize mask
299  static const uint32_t maskinit[4] = {SFMT_MASK};
300  mask = _mm_loadu_si128((__m128i*)maskinit);
301 
302  // Period certification
303  // Define period certification vector
304  static const uint32_t parityvec[4] = {SFMT_PARITY};
305 
306  // Check if parityvec & state[0] has odd parity
307  temp = 0;
308  for (i = 0; i < 4; i++)
309  temp ^= parityvec[i] & ((uint32_t*)state)[i];
310 
311  for (i = 16; i > 0; i >>= 1) temp ^= temp >> i;
312  if (!(temp & 1)) {
313  // parity is even. Certification failed
314  // Find a nonzero bit in period certification vector
315  for (i = 0; i < 4; i++) {
316  if (parityvec[i]) {
317  for (j = 1; j; j <<= 1) {
318  if (parityvec[i] & j) {
319  // Flip the corresponding bit in state[0] to change parity
320  ((uint32_t*)state)[i] ^= j;
321  // Done. Exit i and j loops
322  i = 5; break;
323  }
324  }
325  }
326  }
327  }
328 
329  // Generate first random numbers and set ix = 0
330  Generate();
331  }
332 
333  void Generate() // Fill state array with new random numbers
334  {
335  // Fill state array with new random numbers
336  int i;
337  __m128i r, r1, r2;
338 
339  r1 = state[SFMT_N - 2];
340  r2 = state[SFMT_N - 1];
341  for (i = 0; i < SFMT_N - SFMT_M; i++) {
342  r = sfmt_recursion(state[i], state[i + SFMT_M], r1, r2, mask);
343  state[i] = r;
344  r1 = r2;
345  r2 = r;
346  }
347  for (; i < SFMT_N; i++) {
348  r = sfmt_recursion(state[i], state[i + SFMT_M - SFMT_N], r1, r2, mask);
349  state[i] = r;
350  r1 = r2;
351  r2 = r;
352  }
353  ix = 0;
354  }
355 
356  __m128i mask; // AND mask
357  __m128i state[SFMT_N]; // State vector for SFMT generator
358  uint32_t ix; // Index into state array
359  uint32_t LastInterval; // Last interval length for IRandom
360  uint32_t RLimit; // Rejection limit used by IRandom
361 };
362 
363 #endif // SFMT_H
Definition: SFMT.h:154
#define SFMT_SR1
Definition: SFMT.h:80
#define SFMT_N
Definition: SFMT.h:76
Definition: SFMT.h:153
uint32_t LastInterval
Definition: SFMT.h:359
void RandomInit(int seed)
Definition: SFMT.h:168
double Random()
Definition: SFMT.h:227
uint32_t RLimit
Definition: SFMT.h:360
#define SFMT_SR2
Definition: SFMT.h:81
__m128i mask
Definition: SFMT.h:356
signed __int64 int64_t
Definition: stdint.h:89
#define SFMT_SL1
Definition: SFMT.h:78
T max(const T &x, const T &y)
Definition: g3dmath.h:320
unsigned int uint32_t
Definition: stdint.h:80
T min(const T &x, const T &y)
Definition: g3dmath.h:305
void Generate()
Definition: SFMT.h:333
#define SFMT_M
Definition: SFMT.h:77
unsigned __int64 uint64_t
Definition: stdint.h:90
#define SFMT_PARITY
Definition: SFMT.h:83
int32_t IRandom(int32_t min, int32_t max)
Definition: SFMT.h:189
void Init2()
Definition: SFMT.h:293
Definition: SFMT.h:158
__m128i state[SFMT_N]
Definition: SFMT.h:357
uint32_t ix
Definition: SFMT.h:358
G3D::int16 y
Definition: Vector2int16.h:38
SFMTRand()
Definition: SFMT.h:162
#define SFMT_SL2
Definition: SFMT.h:79
uint32_t BRandom()
Definition: SFMT.h:240
#define SFMT_MASK
Definition: SFMT.h:82
static __m128i sfmt_recursion(__m128i const &a, __m128i const &b, __m128i const &c, __m128i const &d, __m128i const &mask)
Definition: SFMT.h:138
signed int int32_t
Definition: stdint.h:77
uint32_t URandom(uint32_t min, uint32_t max)
Definition: SFMT.h:208