TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
SFMTRand Class Reference

#include <SFMT.h>

Public Member Functions

 SFMTRand ()
 
void RandomInit (int seed)
 
int32_t IRandom (int32_t min, int32_t max)
 
uint32_t URandom (uint32_t min, uint32_t max)
 
double Random ()
 
uint32_t BRandom ()
 
void * operator new (size_t size, std::nothrow_t const &)
 
void operator delete (void *ptr, std::nothrow_t const &)
 
void * operator new (size_t size)
 
void operator delete (void *ptr)
 
void * operator new[] (size_t size, std::nothrow_t const &)
 
void operator delete[] (void *ptr, std::nothrow_t const &)
 
void * operator new[] (size_t size)
 
void operator delete[] (void *ptr)
 

Private Member Functions

void Init2 ()
 
void Generate ()
 

Private Attributes

__m128i mask
 
__m128i state [SFMT_N]
 
uint32_t ix
 
uint32_t LastInterval
 
uint32_t RLimit
 

Friends

class boost::thread_specific_ptr< SFMTRand >
 

Constructor & Destructor Documentation

SFMTRand::SFMTRand ( )
inline
163  {
164  LastInterval = 0;
165  RandomInit((int)(time(0)));
166  }
uint32_t LastInterval
Definition: SFMT.h:359
void RandomInit(int seed)
Definition: SFMT.h:168

+ Here is the call graph for this function:

Member Function Documentation

uint32_t SFMTRand::BRandom ( )
inline
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  }
#define SFMT_N
Definition: SFMT.h:76
unsigned int uint32_t
Definition: stdint.h:80
void Generate()
Definition: SFMT.h:333
__m128i state[SFMT_N]
Definition: SFMT.h:357
uint32_t ix
Definition: SFMT.h:358
G3D::int16 y
Definition: Vector2int16.h:38

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void SFMTRand::Generate ( )
inlineprivate
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  }
#define SFMT_N
Definition: SFMT.h:76
__m128i mask
Definition: SFMT.h:356
#define SFMT_M
Definition: SFMT.h:77
__m128i state[SFMT_N]
Definition: SFMT.h:357
uint32_t ix
Definition: SFMT.h:358
static __m128i sfmt_recursion(__m128i const &a, __m128i const &b, __m128i const &c, __m128i const &d, __m128i const &mask)
Definition: SFMT.h:138

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void SFMTRand::Init2 ( )
inlineprivate
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  }
__m128i mask
Definition: SFMT.h:356
unsigned int uint32_t
Definition: stdint.h:80
void Generate()
Definition: SFMT.h:333
#define SFMT_PARITY
Definition: SFMT.h:83
__m128i state[SFMT_N]
Definition: SFMT.h:357
#define SFMT_MASK
Definition: SFMT.h:82

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int32_t SFMTRand::IRandom ( int32_t  min,
int32_t  max 
)
inline
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  }
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
unsigned __int64 uint64_t
Definition: stdint.h:90
uint32_t BRandom()
Definition: SFMT.h:240
signed int int32_t
Definition: stdint.h:77

+ Here is the call graph for this function:

void SFMTRand::operator delete ( void *  ptr,
std::nothrow_t const  
)
inline
258  {
259  _mm_free(ptr);
260  }
void SFMTRand::operator delete ( void *  ptr)
inline
268  {
269  _mm_free(ptr);
270  }
void SFMTRand::operator delete[] ( void *  ptr,
std::nothrow_t const  
)
inline
278  {
279  _mm_free(ptr);
280  }
void SFMTRand::operator delete[] ( void *  ptr)
inline
288  {
289  _mm_free(ptr);
290  }
void* SFMTRand::operator new ( size_t  size,
std::nothrow_t const  
)
inline
253  {
254  return _mm_malloc(size, 16);
255  }
void* SFMTRand::operator new ( size_t  size)
inline
263  {
264  return _mm_malloc(size, 16);
265  }
void* SFMTRand::operator new[] ( size_t  size,
std::nothrow_t const  
)
inline
273  {
274  return _mm_malloc(size, 16);
275  }
void* SFMTRand::operator new[] ( size_t  size)
inline
283  {
284  return _mm_malloc(size, 16);
285  }
double SFMTRand::Random ( )
inline
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  }
#define SFMT_N
Definition: SFMT.h:76
signed __int64 int64_t
Definition: stdint.h:89
unsigned int uint32_t
Definition: stdint.h:80
void Generate()
Definition: SFMT.h:333
unsigned __int64 uint64_t
Definition: stdint.h:90
__m128i state[SFMT_N]
Definition: SFMT.h:357
uint32_t ix
Definition: SFMT.h:358

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void SFMTRand::RandomInit ( int  seed)
inline
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  }
#define SFMT_N
Definition: SFMT.h:76
unsigned int uint32_t
Definition: stdint.h:80
void Init2()
Definition: SFMT.h:293
__m128i state[SFMT_N]
Definition: SFMT.h:357
G3D::int16 y
Definition: Vector2int16.h:38

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

uint32_t SFMTRand::URandom ( uint32_t  min,
uint32_t  max 
)
inline
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  }
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
unsigned __int64 uint64_t
Definition: stdint.h:90
uint32_t BRandom()
Definition: SFMT.h:240

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Friends And Related Function Documentation

friend class boost::thread_specific_ptr< SFMTRand >
friend

Member Data Documentation

uint32_t SFMTRand::ix
private
uint32_t SFMTRand::LastInterval
private
__m128i SFMTRand::mask
private
uint32_t SFMTRand::RLimit
private
__m128i SFMTRand::state[SFMT_N]
private

The documentation for this class was generated from the following file: