mirror of
https://github.com/TrinityCore/TrinityCore.git
synced 2026-01-16 07:30:42 +01:00
Switch to using SIMD-oriented Fast Mersenne Twister for random number generation.
In testing, reduced random number generation time by a factor of 8-10. Drops support for processors older than Pentium 4. Drop Mersenne Twister library; use a C++ SFMT library. --HG-- branch : trunk
This commit is contained in:
@@ -20,13 +20,9 @@ libMPQ (a library for reading MPQ files)
|
||||
https://libmpq.org/
|
||||
Version: 1.0.4
|
||||
|
||||
MersenneTwister (a very fast random number generator)
|
||||
http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
|
||||
Version: 0.4.2
|
||||
|
||||
SFMT (SIMD-oriented Fast Mersenne Twister)
|
||||
http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/index.html
|
||||
Version: 1.3.3
|
||||
Based on http://agner.org/random/
|
||||
Version: 2010-Aug-03
|
||||
|
||||
sockets (a GPL licensed C++ class library wrapping the berkeley sockets C API)
|
||||
http://www.alhem.net/Sockets/
|
||||
|
||||
@@ -1,156 +0,0 @@
|
||||
/**
|
||||
* @file SFMT-alti.h
|
||||
*
|
||||
* @brief SIMD oriented Fast Mersenne Twister(SFMT)
|
||||
* pseudorandom number generator
|
||||
*
|
||||
* @author Mutsuo Saito (Hiroshima University)
|
||||
* @author Makoto Matsumoto (Hiroshima University)
|
||||
*
|
||||
* Copyright (C) 2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima
|
||||
* University. All rights reserved.
|
||||
*
|
||||
* The new BSD License is applied to this software.
|
||||
* see LICENSE.txt
|
||||
*/
|
||||
|
||||
#ifndef SFMT_ALTI_H
|
||||
#define SFMT_ALTI_H
|
||||
|
||||
inline static vector unsigned int vec_recursion(vector unsigned int a,
|
||||
vector unsigned int b,
|
||||
vector unsigned int c,
|
||||
vector unsigned int d)
|
||||
ALWAYSINLINE;
|
||||
|
||||
/**
|
||||
* This function represents the recursion formula in AltiVec and BIG ENDIAN.
|
||||
* @param a a 128-bit part of the interal state array
|
||||
* @param b a 128-bit part of the interal state array
|
||||
* @param c a 128-bit part of the interal state array
|
||||
* @param d a 128-bit part of the interal state array
|
||||
* @return output
|
||||
*/
|
||||
inline static vector unsigned int vec_recursion(vector unsigned int a,
|
||||
vector unsigned int b,
|
||||
vector unsigned int c,
|
||||
vector unsigned int d) {
|
||||
|
||||
const vector unsigned int sl1 = ALTI_SL1;
|
||||
const vector unsigned int sr1 = ALTI_SR1;
|
||||
#ifdef ONLY64
|
||||
const vector unsigned int mask = ALTI_MSK64;
|
||||
const vector unsigned char perm_sl = ALTI_SL2_PERM64;
|
||||
const vector unsigned char perm_sr = ALTI_SR2_PERM64;
|
||||
#else
|
||||
const vector unsigned int mask = ALTI_MSK;
|
||||
const vector unsigned char perm_sl = ALTI_SL2_PERM;
|
||||
const vector unsigned char perm_sr = ALTI_SR2_PERM;
|
||||
#endif
|
||||
vector unsigned int v, w, x, y, z;
|
||||
x = vec_perm(a, (vector unsigned int)perm_sl, perm_sl);
|
||||
v = a;
|
||||
y = vec_sr(b, sr1);
|
||||
z = vec_perm(c, (vector unsigned int)perm_sr, perm_sr);
|
||||
w = vec_sl(d, sl1);
|
||||
z = vec_xor(z, w);
|
||||
y = vec_and(y, mask);
|
||||
v = vec_xor(v, x);
|
||||
z = vec_xor(z, y);
|
||||
z = vec_xor(z, v);
|
||||
return z;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function fills the internal state array with pseudorandom
|
||||
* integers.
|
||||
*/
|
||||
inline static void gen_rand_all(void) {
|
||||
int i;
|
||||
vector unsigned int r, r1, r2;
|
||||
|
||||
r1 = sfmt[N - 2].s;
|
||||
r2 = sfmt[N - 1].s;
|
||||
for (i = 0; i < N - POS1; i++) {
|
||||
r = vec_recursion(sfmt[i].s, sfmt[i + POS1].s, r1, r2);
|
||||
sfmt[i].s = r;
|
||||
r1 = r2;
|
||||
r2 = r;
|
||||
}
|
||||
for (; i < N; i++) {
|
||||
r = vec_recursion(sfmt[i].s, sfmt[i + POS1 - N].s, r1, r2);
|
||||
sfmt[i].s = r;
|
||||
r1 = r2;
|
||||
r2 = r;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This function fills the user-specified array with pseudorandom
|
||||
* integers.
|
||||
*
|
||||
* @param array an 128-bit array to be filled by pseudorandom numbers.
|
||||
* @param size number of 128-bit pesudorandom numbers to be generated.
|
||||
*/
|
||||
inline static void gen_rand_array(w128_t *array, int size) {
|
||||
int i, j;
|
||||
vector unsigned int r, r1, r2;
|
||||
|
||||
r1 = sfmt[N - 2].s;
|
||||
r2 = sfmt[N - 1].s;
|
||||
for (i = 0; i < N - POS1; i++) {
|
||||
r = vec_recursion(sfmt[i].s, sfmt[i + POS1].s, r1, r2);
|
||||
array[i].s = r;
|
||||
r1 = r2;
|
||||
r2 = r;
|
||||
}
|
||||
for (; i < N; i++) {
|
||||
r = vec_recursion(sfmt[i].s, array[i + POS1 - N].s, r1, r2);
|
||||
array[i].s = r;
|
||||
r1 = r2;
|
||||
r2 = r;
|
||||
}
|
||||
/* main loop */
|
||||
for (; i < size - N; i++) {
|
||||
r = vec_recursion(array[i - N].s, array[i + POS1 - N].s, r1, r2);
|
||||
array[i].s = r;
|
||||
r1 = r2;
|
||||
r2 = r;
|
||||
}
|
||||
for (j = 0; j < 2 * N - size; j++) {
|
||||
sfmt[j].s = array[j + size - N].s;
|
||||
}
|
||||
for (; i < size; i++) {
|
||||
r = vec_recursion(array[i - N].s, array[i + POS1 - N].s, r1, r2);
|
||||
array[i].s = r;
|
||||
sfmt[j++].s = r;
|
||||
r1 = r2;
|
||||
r2 = r;
|
||||
}
|
||||
}
|
||||
|
||||
#ifndef ONLY64
|
||||
#if defined(__APPLE__)
|
||||
#define ALTI_SWAP (vector unsigned char) \
|
||||
(4, 5, 6, 7, 0, 1, 2, 3, 12, 13, 14, 15, 8, 9, 10, 11)
|
||||
#else
|
||||
#define ALTI_SWAP {4, 5, 6, 7, 0, 1, 2, 3, 12, 13, 14, 15, 8, 9, 10, 11}
|
||||
#endif
|
||||
/**
|
||||
* This function swaps high and low 32-bit of 64-bit integers in user
|
||||
* specified array.
|
||||
*
|
||||
* @param array an 128-bit array to be swaped.
|
||||
* @param size size of 128-bit array.
|
||||
*/
|
||||
inline static void swap(w128_t *array, int size) {
|
||||
int i;
|
||||
const vector unsigned char perm = ALTI_SWAP;
|
||||
|
||||
for (i = 0; i < size; i++) {
|
||||
array[i].s = vec_perm(array[i].s, (vector unsigned int)perm, perm);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -1,97 +0,0 @@
|
||||
#ifndef SFMT_PARAMS_H
|
||||
#define SFMT_PARAMS_H
|
||||
|
||||
#if !defined(MEXP)
|
||||
#ifdef __GNUC__
|
||||
#warning "MEXP is not defined. I assume MEXP is 19937."
|
||||
#endif
|
||||
#define MEXP 19937
|
||||
#endif
|
||||
/*-----------------
|
||||
BASIC DEFINITIONS
|
||||
-----------------*/
|
||||
/** Mersenne Exponent. The period of the sequence
|
||||
* is a multiple of 2^MEXP-1.
|
||||
* #define MEXP 19937 */
|
||||
/** SFMT generator has an internal state array of 128-bit integers,
|
||||
* and N is its size. */
|
||||
#define N (MEXP / 128 + 1)
|
||||
/** N32 is the size of internal state array when regarded as an array
|
||||
* of 32-bit integers.*/
|
||||
#define N32 (N * 4)
|
||||
/** N64 is the size of internal state array when regarded as an array
|
||||
* of 64-bit integers.*/
|
||||
#define N64 (N * 2)
|
||||
|
||||
/*----------------------
|
||||
the parameters of SFMT
|
||||
following definitions are in paramsXXXX.h file.
|
||||
----------------------*/
|
||||
/** the pick up position of the array.
|
||||
#define POS1 122
|
||||
*/
|
||||
|
||||
/** the parameter of shift left as four 32-bit registers.
|
||||
#define SL1 18
|
||||
*/
|
||||
|
||||
/** the parameter of shift left as one 128-bit register.
|
||||
* The 128-bit integer is shifted by (SL2 * 8) bits.
|
||||
#define SL2 1
|
||||
*/
|
||||
|
||||
/** the parameter of shift right as four 32-bit registers.
|
||||
#define SR1 11
|
||||
*/
|
||||
|
||||
/** the parameter of shift right as one 128-bit register.
|
||||
* The 128-bit integer is shifted by (SL2 * 8) bits.
|
||||
#define SR2 1
|
||||
*/
|
||||
|
||||
/** A bitmask, used in the recursion. These parameters are introduced
|
||||
* to break symmetry of SIMD.
|
||||
#define MSK1 0xdfffffefU
|
||||
#define MSK2 0xddfecb7fU
|
||||
#define MSK3 0xbffaffffU
|
||||
#define MSK4 0xbffffff6U
|
||||
*/
|
||||
|
||||
/** These definitions are part of a 128-bit period certification vector.
|
||||
#define PARITY1 0x00000001U
|
||||
#define PARITY2 0x00000000U
|
||||
#define PARITY3 0x00000000U
|
||||
#define PARITY4 0xc98e126aU
|
||||
*/
|
||||
|
||||
#if MEXP == 607
|
||||
#include "SFMT-params607.h"
|
||||
#elif MEXP == 1279
|
||||
#include "SFMT-params1279.h"
|
||||
#elif MEXP == 2281
|
||||
#include "SFMT-params2281.h"
|
||||
#elif MEXP == 4253
|
||||
#include "SFMT-params4253.h"
|
||||
#elif MEXP == 11213
|
||||
#include "SFMT-params11213.h"
|
||||
#elif MEXP == 19937
|
||||
#include "SFMT-params19937.h"
|
||||
#elif MEXP == 44497
|
||||
#include "SFMT-params44497.h"
|
||||
#elif MEXP == 86243
|
||||
#include "SFMT-params86243.h"
|
||||
#elif MEXP == 132049
|
||||
#include "SFMT-params132049.h"
|
||||
#elif MEXP == 216091
|
||||
#include "SFMT-params216091.h"
|
||||
#else
|
||||
#ifdef __GNUC__
|
||||
#error "MEXP is not valid."
|
||||
#undef MEXP
|
||||
#else
|
||||
#undef MEXP
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
#endif /* SFMT_PARAMS_H */
|
||||
@@ -1,121 +0,0 @@
|
||||
/**
|
||||
* @file SFMT-sse2.h
|
||||
* @brief SIMD oriented Fast Mersenne Twister(SFMT) for Intel SSE2
|
||||
*
|
||||
* @author Mutsuo Saito (Hiroshima University)
|
||||
* @author Makoto Matsumoto (Hiroshima University)
|
||||
*
|
||||
* @note We assume LITTLE ENDIAN in this file
|
||||
*
|
||||
* Copyright (C) 2006, 2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima
|
||||
* University. All rights reserved.
|
||||
*
|
||||
* The new BSD License is applied to this software, see LICENSE.txt
|
||||
*/
|
||||
|
||||
#ifndef SFMT_SSE2_H
|
||||
#define SFMT_SSE2_H
|
||||
|
||||
PRE_ALWAYS static __m128i mm_recursion(__m128i *a, __m128i *b, __m128i c,
|
||||
__m128i d, __m128i mask) ALWAYSINLINE;
|
||||
|
||||
/**
|
||||
* This function represents the recursion formula.
|
||||
* @param a a 128-bit part of the interal state array
|
||||
* @param b a 128-bit part of the interal state array
|
||||
* @param c a 128-bit part of the interal state array
|
||||
* @param d a 128-bit part of the interal state array
|
||||
* @param mask 128-bit mask
|
||||
* @return output
|
||||
*/
|
||||
PRE_ALWAYS static __m128i mm_recursion(__m128i *a, __m128i *b,
|
||||
__m128i c, __m128i d, __m128i mask) {
|
||||
__m128i v, x, y, z;
|
||||
|
||||
x = _mm_load_si128(a);
|
||||
y = _mm_srli_epi32(*b, SR1);
|
||||
z = _mm_srli_si128(c, SR2);
|
||||
v = _mm_slli_epi32(d, SL1);
|
||||
z = _mm_xor_si128(z, x);
|
||||
z = _mm_xor_si128(z, v);
|
||||
x = _mm_slli_si128(x, SL2);
|
||||
y = _mm_and_si128(y, mask);
|
||||
z = _mm_xor_si128(z, x);
|
||||
z = _mm_xor_si128(z, y);
|
||||
return z;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function fills the internal state array with pseudorandom
|
||||
* integers.
|
||||
*/
|
||||
inline static void gen_rand_all(void) {
|
||||
int i;
|
||||
__m128i r, r1, r2, mask;
|
||||
mask = _mm_set_epi32(MSK4, MSK3, MSK2, MSK1);
|
||||
|
||||
r1 = _mm_load_si128(&sfmt[N - 2].si);
|
||||
r2 = _mm_load_si128(&sfmt[N - 1].si);
|
||||
for (i = 0; i < N - POS1; i++) {
|
||||
r = mm_recursion(&sfmt[i].si, &sfmt[i + POS1].si, r1, r2, mask);
|
||||
_mm_store_si128(&sfmt[i].si, r);
|
||||
r1 = r2;
|
||||
r2 = r;
|
||||
}
|
||||
for (; i < N; i++) {
|
||||
r = mm_recursion(&sfmt[i].si, &sfmt[i + POS1 - N].si, r1, r2, mask);
|
||||
_mm_store_si128(&sfmt[i].si, r);
|
||||
r1 = r2;
|
||||
r2 = r;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This function fills the user-specified array with pseudorandom
|
||||
* integers.
|
||||
*
|
||||
* @param array an 128-bit array to be filled by pseudorandom numbers.
|
||||
* @param size number of 128-bit pesudorandom numbers to be generated.
|
||||
*/
|
||||
inline static void gen_rand_array(w128_t *array, int size) {
|
||||
int i, j;
|
||||
__m128i r, r1, r2, mask;
|
||||
mask = _mm_set_epi32(MSK4, MSK3, MSK2, MSK1);
|
||||
|
||||
r1 = _mm_load_si128(&sfmt[N - 2].si);
|
||||
r2 = _mm_load_si128(&sfmt[N - 1].si);
|
||||
for (i = 0; i < N - POS1; i++) {
|
||||
r = mm_recursion(&sfmt[i].si, &sfmt[i + POS1].si, r1, r2, mask);
|
||||
_mm_store_si128(&array[i].si, r);
|
||||
r1 = r2;
|
||||
r2 = r;
|
||||
}
|
||||
for (; i < N; i++) {
|
||||
r = mm_recursion(&sfmt[i].si, &array[i + POS1 - N].si, r1, r2, mask);
|
||||
_mm_store_si128(&array[i].si, r);
|
||||
r1 = r2;
|
||||
r2 = r;
|
||||
}
|
||||
/* main loop */
|
||||
for (; i < size - N; i++) {
|
||||
r = mm_recursion(&array[i - N].si, &array[i + POS1 - N].si, r1, r2,
|
||||
mask);
|
||||
_mm_store_si128(&array[i].si, r);
|
||||
r1 = r2;
|
||||
r2 = r;
|
||||
}
|
||||
for (j = 0; j < 2 * N - size; j++) {
|
||||
r = _mm_load_si128(&array[j + size - N].si);
|
||||
_mm_store_si128(&sfmt[j].si, r);
|
||||
}
|
||||
for (; i < size; i++) {
|
||||
r = mm_recursion(&array[i - N].si, &array[i + POS1 - N].si, r1, r2,
|
||||
mask);
|
||||
_mm_store_si128(&array[i].si, r);
|
||||
_mm_store_si128(&sfmt[j++].si, r);
|
||||
r1 = r2;
|
||||
r2 = r;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
620
dep/SFMT/SFMT.c
620
dep/SFMT/SFMT.c
@@ -1,620 +0,0 @@
|
||||
/**
|
||||
* @file SFMT.c
|
||||
* @brief SIMD oriented Fast Mersenne Twister(SFMT)
|
||||
*
|
||||
* @author Mutsuo Saito (Hiroshima University)
|
||||
* @author Makoto Matsumoto (Hiroshima University)
|
||||
*
|
||||
* Copyright (C) 2006,2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima
|
||||
* University. All rights reserved.
|
||||
*
|
||||
* The new BSD License is applied to this software, see LICENSE.txt
|
||||
*/
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
#include "SFMT.h"
|
||||
#include "SFMT-params.h"
|
||||
|
||||
#if defined(__BIG_ENDIAN__) && !defined(__amd64) && !defined(BIG_ENDIAN64)
|
||||
#define BIG_ENDIAN64 1
|
||||
#endif
|
||||
#if defined(HAVE_ALTIVEC) && !defined(BIG_ENDIAN64)
|
||||
#define BIG_ENDIAN64 1
|
||||
#endif
|
||||
#if defined(ONLY64) && !defined(BIG_ENDIAN64)
|
||||
#if defined(__GNUC__)
|
||||
#error "-DONLY64 must be specified with -DBIG_ENDIAN64"
|
||||
#endif
|
||||
#undef ONLY64
|
||||
#endif
|
||||
/*------------------------------------------------------
|
||||
128-bit SIMD data type for Altivec, SSE2 or standard C
|
||||
------------------------------------------------------*/
|
||||
#if defined(HAVE_ALTIVEC)
|
||||
#if !defined(__APPLE__)
|
||||
#include <altivec.h>
|
||||
#endif
|
||||
/** 128-bit data structure */
|
||||
union W128_T {
|
||||
vector unsigned int s;
|
||||
uint32_t u[4];
|
||||
};
|
||||
/** 128-bit data type */
|
||||
typedef union W128_T w128_t;
|
||||
|
||||
#elif defined(HAVE_SSE2)
|
||||
#include <emmintrin.h>
|
||||
|
||||
/** 128-bit data structure */
|
||||
union W128_T {
|
||||
__m128i si;
|
||||
uint32_t u[4];
|
||||
};
|
||||
/** 128-bit data type */
|
||||
typedef union W128_T w128_t;
|
||||
|
||||
#else
|
||||
|
||||
/** 128-bit data structure */
|
||||
struct W128_T {
|
||||
uint32_t u[4];
|
||||
};
|
||||
/** 128-bit data type */
|
||||
typedef struct W128_T w128_t;
|
||||
|
||||
#endif
|
||||
|
||||
/*--------------------------------------
|
||||
FILE GLOBAL VARIABLES
|
||||
internal state, index counter and flag
|
||||
--------------------------------------*/
|
||||
/** the 128-bit internal state array */
|
||||
static w128_t sfmt[N];
|
||||
/** the 32bit integer pointer to the 128-bit internal state array */
|
||||
static uint32_t *psfmt32 = &sfmt[0].u[0];
|
||||
#if !defined(BIG_ENDIAN64) || defined(ONLY64)
|
||||
/** the 64bit integer pointer to the 128-bit internal state array */
|
||||
static uint64_t *psfmt64 = (uint64_t *)&sfmt[0].u[0];
|
||||
#endif
|
||||
/** index counter to the 32-bit internal state array */
|
||||
static int idx;
|
||||
/** a flag: it is 0 if and only if the internal state is not yet
|
||||
* initialized. */
|
||||
static int initialized = 0;
|
||||
/** a parity check vector which certificate the period of 2^{MEXP} */
|
||||
static uint32_t parity[4] = {PARITY1, PARITY2, PARITY3, PARITY4};
|
||||
|
||||
/*----------------
|
||||
STATIC FUNCTIONS
|
||||
----------------*/
|
||||
inline static int idxof(int i);
|
||||
inline static void rshift128(w128_t *out, w128_t const *in, int shift);
|
||||
inline static void lshift128(w128_t *out, w128_t const *in, int shift);
|
||||
inline static void gen_rand_all(void);
|
||||
inline static void gen_rand_array(w128_t *array, int size);
|
||||
inline static uint32_t func1(uint32_t x);
|
||||
inline static uint32_t func2(uint32_t x);
|
||||
static void period_certification(void);
|
||||
#if defined(BIG_ENDIAN64) && !defined(ONLY64)
|
||||
inline static void swap(w128_t *array, int size);
|
||||
#endif
|
||||
|
||||
#if defined(HAVE_ALTIVEC)
|
||||
#include "SFMT-alti.h"
|
||||
#elif defined(HAVE_SSE2)
|
||||
#include "SFMT-sse2.h"
|
||||
#endif
|
||||
|
||||
/**
|
||||
* This function simulate a 64-bit index of LITTLE ENDIAN
|
||||
* in BIG ENDIAN machine.
|
||||
*/
|
||||
#ifdef ONLY64
|
||||
inline static int idxof(int i) {
|
||||
return i ^ 1;
|
||||
}
|
||||
#else
|
||||
inline static int idxof(int i) {
|
||||
return i;
|
||||
}
|
||||
#endif
|
||||
/**
|
||||
* This function simulates SIMD 128-bit right shift by the standard C.
|
||||
* The 128-bit integer given in in is shifted by (shift * 8) bits.
|
||||
* This function simulates the LITTLE ENDIAN SIMD.
|
||||
* @param out the output of this function
|
||||
* @param in the 128-bit data to be shifted
|
||||
* @param shift the shift value
|
||||
*/
|
||||
#ifdef ONLY64
|
||||
inline static void rshift128(w128_t *out, w128_t const *in, int shift) {
|
||||
uint64_t th, tl, oh, ol;
|
||||
|
||||
th = ((uint64_t)in->u[2] << 32) | ((uint64_t)in->u[3]);
|
||||
tl = ((uint64_t)in->u[0] << 32) | ((uint64_t)in->u[1]);
|
||||
|
||||
oh = th >> (shift * 8);
|
||||
ol = tl >> (shift * 8);
|
||||
ol |= th << (64 - shift * 8);
|
||||
out->u[0] = (uint32_t)(ol >> 32);
|
||||
out->u[1] = (uint32_t)ol;
|
||||
out->u[2] = (uint32_t)(oh >> 32);
|
||||
out->u[3] = (uint32_t)oh;
|
||||
}
|
||||
#else
|
||||
inline static void rshift128(w128_t *out, w128_t const *in, int shift) {
|
||||
uint64_t th, tl, oh, ol;
|
||||
|
||||
th = ((uint64_t)in->u[3] << 32) | ((uint64_t)in->u[2]);
|
||||
tl = ((uint64_t)in->u[1] << 32) | ((uint64_t)in->u[0]);
|
||||
|
||||
oh = th >> (shift * 8);
|
||||
ol = tl >> (shift * 8);
|
||||
ol |= th << (64 - shift * 8);
|
||||
out->u[1] = (uint32_t)(ol >> 32);
|
||||
out->u[0] = (uint32_t)ol;
|
||||
out->u[3] = (uint32_t)(oh >> 32);
|
||||
out->u[2] = (uint32_t)oh;
|
||||
}
|
||||
#endif
|
||||
/**
|
||||
* This function simulates SIMD 128-bit left shift by the standard C.
|
||||
* The 128-bit integer given in in is shifted by (shift * 8) bits.
|
||||
* This function simulates the LITTLE ENDIAN SIMD.
|
||||
* @param out the output of this function
|
||||
* @param in the 128-bit data to be shifted
|
||||
* @param shift the shift value
|
||||
*/
|
||||
#ifdef ONLY64
|
||||
inline static void lshift128(w128_t *out, w128_t const *in, int shift) {
|
||||
uint64_t th, tl, oh, ol;
|
||||
|
||||
th = ((uint64_t)in->u[2] << 32) | ((uint64_t)in->u[3]);
|
||||
tl = ((uint64_t)in->u[0] << 32) | ((uint64_t)in->u[1]);
|
||||
|
||||
oh = th << (shift * 8);
|
||||
ol = tl << (shift * 8);
|
||||
oh |= tl >> (64 - shift * 8);
|
||||
out->u[0] = (uint32_t)(ol >> 32);
|
||||
out->u[1] = (uint32_t)ol;
|
||||
out->u[2] = (uint32_t)(oh >> 32);
|
||||
out->u[3] = (uint32_t)oh;
|
||||
}
|
||||
#else
|
||||
inline static void lshift128(w128_t *out, w128_t const *in, int shift) {
|
||||
uint64_t th, tl, oh, ol;
|
||||
|
||||
th = ((uint64_t)in->u[3] << 32) | ((uint64_t)in->u[2]);
|
||||
tl = ((uint64_t)in->u[1] << 32) | ((uint64_t)in->u[0]);
|
||||
|
||||
oh = th << (shift * 8);
|
||||
ol = tl << (shift * 8);
|
||||
oh |= tl >> (64 - shift * 8);
|
||||
out->u[1] = (uint32_t)(ol >> 32);
|
||||
out->u[0] = (uint32_t)ol;
|
||||
out->u[3] = (uint32_t)(oh >> 32);
|
||||
out->u[2] = (uint32_t)oh;
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
* This function represents the recursion formula.
|
||||
* @param r output
|
||||
* @param a a 128-bit part of the internal state array
|
||||
* @param b a 128-bit part of the internal state array
|
||||
* @param c a 128-bit part of the internal state array
|
||||
* @param d a 128-bit part of the internal state array
|
||||
*/
|
||||
#if (!defined(HAVE_ALTIVEC)) && (!defined(HAVE_SSE2))
|
||||
#ifdef ONLY64
|
||||
inline static void do_recursion(w128_t *r, w128_t *a, w128_t *b, w128_t *c,
|
||||
w128_t *d) {
|
||||
w128_t x;
|
||||
w128_t y;
|
||||
|
||||
lshift128(&x, a, SL2);
|
||||
rshift128(&y, c, SR2);
|
||||
r->u[0] = a->u[0] ^ x.u[0] ^ ((b->u[0] >> SR1) & MSK2) ^ y.u[0]
|
||||
^ (d->u[0] << SL1);
|
||||
r->u[1] = a->u[1] ^ x.u[1] ^ ((b->u[1] >> SR1) & MSK1) ^ y.u[1]
|
||||
^ (d->u[1] << SL1);
|
||||
r->u[2] = a->u[2] ^ x.u[2] ^ ((b->u[2] >> SR1) & MSK4) ^ y.u[2]
|
||||
^ (d->u[2] << SL1);
|
||||
r->u[3] = a->u[3] ^ x.u[3] ^ ((b->u[3] >> SR1) & MSK3) ^ y.u[3]
|
||||
^ (d->u[3] << SL1);
|
||||
}
|
||||
#else
|
||||
inline static void do_recursion(w128_t *r, w128_t *a, w128_t *b, w128_t *c,
|
||||
w128_t *d) {
|
||||
w128_t x;
|
||||
w128_t y;
|
||||
|
||||
lshift128(&x, a, SL2);
|
||||
rshift128(&y, c, SR2);
|
||||
r->u[0] = a->u[0] ^ x.u[0] ^ ((b->u[0] >> SR1) & MSK1) ^ y.u[0]
|
||||
^ (d->u[0] << SL1);
|
||||
r->u[1] = a->u[1] ^ x.u[1] ^ ((b->u[1] >> SR1) & MSK2) ^ y.u[1]
|
||||
^ (d->u[1] << SL1);
|
||||
r->u[2] = a->u[2] ^ x.u[2] ^ ((b->u[2] >> SR1) & MSK3) ^ y.u[2]
|
||||
^ (d->u[2] << SL1);
|
||||
r->u[3] = a->u[3] ^ x.u[3] ^ ((b->u[3] >> SR1) & MSK4) ^ y.u[3]
|
||||
^ (d->u[3] << SL1);
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if (!defined(HAVE_ALTIVEC)) && (!defined(HAVE_SSE2))
|
||||
/**
|
||||
* This function fills the internal state array with pseudorandom
|
||||
* integers.
|
||||
*/
|
||||
inline static void gen_rand_all(void) {
|
||||
int i;
|
||||
w128_t *r1, *r2;
|
||||
|
||||
r1 = &sfmt[N - 2];
|
||||
r2 = &sfmt[N - 1];
|
||||
for (i = 0; i < N - POS1; i++) {
|
||||
do_recursion(&sfmt[i], &sfmt[i], &sfmt[i + POS1], r1, r2);
|
||||
r1 = r2;
|
||||
r2 = &sfmt[i];
|
||||
}
|
||||
for (; i < N; i++) {
|
||||
do_recursion(&sfmt[i], &sfmt[i], &sfmt[i + POS1 - N], r1, r2);
|
||||
r1 = r2;
|
||||
r2 = &sfmt[i];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This function fills the user-specified array with pseudorandom
|
||||
* integers.
|
||||
*
|
||||
* @param array an 128-bit array to be filled by pseudorandom numbers.
|
||||
* @param size number of 128-bit pseudorandom numbers to be generated.
|
||||
*/
|
||||
inline static void gen_rand_array(w128_t *array, int size) {
|
||||
int i, j;
|
||||
w128_t *r1, *r2;
|
||||
|
||||
r1 = &sfmt[N - 2];
|
||||
r2 = &sfmt[N - 1];
|
||||
for (i = 0; i < N - POS1; i++) {
|
||||
do_recursion(&array[i], &sfmt[i], &sfmt[i + POS1], r1, r2);
|
||||
r1 = r2;
|
||||
r2 = &array[i];
|
||||
}
|
||||
for (; i < N; i++) {
|
||||
do_recursion(&array[i], &sfmt[i], &array[i + POS1 - N], r1, r2);
|
||||
r1 = r2;
|
||||
r2 = &array[i];
|
||||
}
|
||||
for (; i < size - N; i++) {
|
||||
do_recursion(&array[i], &array[i - N], &array[i + POS1 - N], r1, r2);
|
||||
r1 = r2;
|
||||
r2 = &array[i];
|
||||
}
|
||||
for (j = 0; j < 2 * N - size; j++) {
|
||||
sfmt[j] = array[j + size - N];
|
||||
}
|
||||
for (; i < size; i++, j++) {
|
||||
do_recursion(&array[i], &array[i - N], &array[i + POS1 - N], r1, r2);
|
||||
r1 = r2;
|
||||
r2 = &array[i];
|
||||
sfmt[j] = array[i];
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(BIG_ENDIAN64) && !defined(ONLY64) && !defined(HAVE_ALTIVEC)
|
||||
inline static void swap(w128_t *array, int size) {
|
||||
int i;
|
||||
uint32_t x, y;
|
||||
|
||||
for (i = 0; i < size; i++) {
|
||||
x = array[i].u[0];
|
||||
y = array[i].u[2];
|
||||
array[i].u[0] = array[i].u[1];
|
||||
array[i].u[2] = array[i].u[3];
|
||||
array[i].u[1] = x;
|
||||
array[i].u[3] = y;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
/**
|
||||
* This function represents a function used in the initialization
|
||||
* by init_by_array
|
||||
* @param x 32-bit integer
|
||||
* @return 32-bit integer
|
||||
*/
|
||||
static uint32_t func1(uint32_t x) {
|
||||
return (x ^ (x >> 27)) * (uint32_t)1664525UL;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function represents a function used in the initialization
|
||||
* by init_by_array
|
||||
* @param x 32-bit integer
|
||||
* @return 32-bit integer
|
||||
*/
|
||||
static uint32_t func2(uint32_t x) {
|
||||
return (x ^ (x >> 27)) * (uint32_t)1566083941UL;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function certificate the period of 2^{MEXP}
|
||||
*/
|
||||
static void period_certification(void) {
|
||||
int inner = 0;
|
||||
int i, j;
|
||||
uint32_t work;
|
||||
|
||||
for (i = 0; i < 4; i++)
|
||||
inner ^= psfmt32[idxof(i)] & parity[i];
|
||||
for (i = 16; i > 0; i >>= 1)
|
||||
inner ^= inner >> i;
|
||||
inner &= 1;
|
||||
/* check OK */
|
||||
if (inner == 1) {
|
||||
return;
|
||||
}
|
||||
/* check NG, and modification */
|
||||
for (i = 0; i < 4; i++) {
|
||||
work = 1;
|
||||
for (j = 0; j < 32; j++) {
|
||||
if ((work & parity[i]) != 0) {
|
||||
psfmt32[idxof(i)] ^= work;
|
||||
return;
|
||||
}
|
||||
work = work << 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*----------------
|
||||
PUBLIC FUNCTIONS
|
||||
----------------*/
|
||||
/**
|
||||
* This function returns the identification string.
|
||||
* The string shows the word size, the Mersenne exponent,
|
||||
* and all parameters of this generator.
|
||||
*/
|
||||
const char *get_idstring(void) {
|
||||
return IDSTR;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function returns the minimum size of array used for \b
|
||||
* fill_array32() function.
|
||||
* @return minimum size of array used for fill_array32() function.
|
||||
*/
|
||||
int get_min_array_size32(void) {
|
||||
return N32;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function returns the minimum size of array used for \b
|
||||
* fill_array64() function.
|
||||
* @return minimum size of array used for fill_array64() function.
|
||||
*/
|
||||
int get_min_array_size64(void) {
|
||||
return N64;
|
||||
}
|
||||
|
||||
#ifndef ONLY64
|
||||
/**
|
||||
* This function generates and returns 32-bit pseudorandom number.
|
||||
* init_gen_rand or init_by_array must be called before this function.
|
||||
* @return 32-bit pseudorandom number
|
||||
*/
|
||||
uint32_t gen_rand32(void) {
|
||||
uint32_t r;
|
||||
|
||||
assert(initialized);
|
||||
if (idx >= N32) {
|
||||
gen_rand_all();
|
||||
idx = 0;
|
||||
}
|
||||
r = psfmt32[idx++];
|
||||
return r;
|
||||
}
|
||||
#endif
|
||||
/**
|
||||
* This function generates and returns 64-bit pseudorandom number.
|
||||
* init_gen_rand or init_by_array must be called before this function.
|
||||
* The function gen_rand64 should not be called after gen_rand32,
|
||||
* unless an initialization is again executed.
|
||||
* @return 64-bit pseudorandom number
|
||||
*/
|
||||
uint64_t gen_rand64(void) {
|
||||
#if defined(BIG_ENDIAN64) && !defined(ONLY64)
|
||||
uint32_t r1, r2;
|
||||
#else
|
||||
uint64_t r;
|
||||
#endif
|
||||
|
||||
assert(initialized);
|
||||
assert(idx % 2 == 0);
|
||||
|
||||
if (idx >= N32) {
|
||||
gen_rand_all();
|
||||
idx = 0;
|
||||
}
|
||||
#if defined(BIG_ENDIAN64) && !defined(ONLY64)
|
||||
r1 = psfmt32[idx];
|
||||
r2 = psfmt32[idx + 1];
|
||||
idx += 2;
|
||||
return ((uint64_t)r2 << 32) | r1;
|
||||
#else
|
||||
r = psfmt64[idx / 2];
|
||||
idx += 2;
|
||||
return r;
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifndef ONLY64
|
||||
/**
|
||||
* This function generates pseudorandom 32-bit integers in the
|
||||
* specified array[] by one call. The number of pseudorandom integers
|
||||
* is specified by the argument size, which must be at least 624 and a
|
||||
* multiple of four. The generation by this function is much faster
|
||||
* than the following gen_rand function.
|
||||
*
|
||||
* For initialization, init_gen_rand or init_by_array must be called
|
||||
* before the first call of this function. This function can not be
|
||||
* used after calling gen_rand function, without initialization.
|
||||
*
|
||||
* @param array an array where pseudorandom 32-bit integers are filled
|
||||
* by this function. The pointer to the array must be \b "aligned"
|
||||
* (namely, must be a multiple of 16) in the SIMD version, since it
|
||||
* refers to the address of a 128-bit integer. In the standard C
|
||||
* version, the pointer is arbitrary.
|
||||
*
|
||||
* @param size the number of 32-bit pseudorandom integers to be
|
||||
* generated. size must be a multiple of 4, and greater than or equal
|
||||
* to (MEXP / 128 + 1) * 4.
|
||||
*
|
||||
* @note \b memalign or \b posix_memalign is available to get aligned
|
||||
* memory. Mac OSX doesn't have these functions, but \b malloc of OSX
|
||||
* returns the pointer to the aligned memory block.
|
||||
*/
|
||||
void fill_array32(uint32_t *array, int size) {
|
||||
assert(initialized);
|
||||
assert(idx == N32);
|
||||
assert(size % 4 == 0);
|
||||
assert(size >= N32);
|
||||
|
||||
gen_rand_array((w128_t *)array, size / 4);
|
||||
idx = N32;
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
* This function generates pseudorandom 64-bit integers in the
|
||||
* specified array[] by one call. The number of pseudorandom integers
|
||||
* is specified by the argument size, which must be at least 312 and a
|
||||
* multiple of two. The generation by this function is much faster
|
||||
* than the following gen_rand function.
|
||||
*
|
||||
* For initialization, init_gen_rand or init_by_array must be called
|
||||
* before the first call of this function. This function can not be
|
||||
* used after calling gen_rand function, without initialization.
|
||||
*
|
||||
* @param array an array where pseudorandom 64-bit integers are filled
|
||||
* by this function. The pointer to the array must be "aligned"
|
||||
* (namely, must be a multiple of 16) in the SIMD version, since it
|
||||
* refers to the address of a 128-bit integer. In the standard C
|
||||
* version, the pointer is arbitrary.
|
||||
*
|
||||
* @param size the number of 64-bit pseudorandom integers to be
|
||||
* generated. size must be a multiple of 2, and greater than or equal
|
||||
* to (MEXP / 128 + 1) * 2
|
||||
*
|
||||
* @note \b memalign or \b posix_memalign is available to get aligned
|
||||
* memory. Mac OSX doesn't have these functions, but \b malloc of OSX
|
||||
* returns the pointer to the aligned memory block.
|
||||
*/
|
||||
void fill_array64(uint64_t *array, int size) {
|
||||
assert(initialized);
|
||||
assert(idx == N32);
|
||||
assert(size % 2 == 0);
|
||||
assert(size >= N64);
|
||||
|
||||
gen_rand_array((w128_t *)array, size / 2);
|
||||
idx = N32;
|
||||
|
||||
#if defined(BIG_ENDIAN64) && !defined(ONLY64)
|
||||
swap((w128_t *)array, size /2);
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* This function initializes the internal state array with a 32-bit
|
||||
* integer seed.
|
||||
*
|
||||
* @param seed a 32-bit integer used as the seed.
|
||||
*/
|
||||
void init_gen_rand(uint32_t seed) {
|
||||
int i;
|
||||
|
||||
psfmt32[idxof(0)] = seed;
|
||||
for (i = 1; i < N32; i++) {
|
||||
psfmt32[idxof(i)] = 1812433253UL * (psfmt32[idxof(i - 1)]
|
||||
^ (psfmt32[idxof(i - 1)] >> 30))
|
||||
+ i;
|
||||
}
|
||||
idx = N32;
|
||||
period_certification();
|
||||
initialized = 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function initializes the internal state array,
|
||||
* with an array of 32-bit integers used as the seeds
|
||||
* @param init_key the array of 32-bit integers, used as a seed.
|
||||
* @param key_length the length of init_key.
|
||||
*/
|
||||
void init_by_array(uint32_t *init_key, int key_length) {
|
||||
int i, j, count;
|
||||
uint32_t r;
|
||||
int lag;
|
||||
int mid;
|
||||
int size = N * 4;
|
||||
|
||||
if (size >= 623) {
|
||||
lag = 11;
|
||||
} else if (size >= 68) {
|
||||
lag = 7;
|
||||
} else if (size >= 39) {
|
||||
lag = 5;
|
||||
} else {
|
||||
lag = 3;
|
||||
}
|
||||
mid = (size - lag) / 2;
|
||||
|
||||
memset(sfmt, 0x8b, sizeof(sfmt));
|
||||
if (key_length + 1 > N32) {
|
||||
count = key_length + 1;
|
||||
} else {
|
||||
count = N32;
|
||||
}
|
||||
r = func1(psfmt32[idxof(0)] ^ psfmt32[idxof(mid)]
|
||||
^ psfmt32[idxof(N32 - 1)]);
|
||||
psfmt32[idxof(mid)] += r;
|
||||
r += key_length;
|
||||
psfmt32[idxof(mid + lag)] += r;
|
||||
psfmt32[idxof(0)] = r;
|
||||
|
||||
count--;
|
||||
for (i = 1, j = 0; (j < count) && (j < key_length); j++) {
|
||||
r = func1(psfmt32[idxof(i)] ^ psfmt32[idxof((i + mid) % N32)]
|
||||
^ psfmt32[idxof((i + N32 - 1) % N32)]);
|
||||
psfmt32[idxof((i + mid) % N32)] += r;
|
||||
r += init_key[j] + i;
|
||||
psfmt32[idxof((i + mid + lag) % N32)] += r;
|
||||
psfmt32[idxof(i)] = r;
|
||||
i = (i + 1) % N32;
|
||||
}
|
||||
for (; j < count; j++) {
|
||||
r = func1(psfmt32[idxof(i)] ^ psfmt32[idxof((i + mid) % N32)]
|
||||
^ psfmt32[idxof((i + N32 - 1) % N32)]);
|
||||
psfmt32[idxof((i + mid) % N32)] += r;
|
||||
r += i;
|
||||
psfmt32[idxof((i + mid + lag) % N32)] += r;
|
||||
psfmt32[idxof(i)] = r;
|
||||
i = (i + 1) % N32;
|
||||
}
|
||||
for (j = 0; j < N32; j++) {
|
||||
r = func2(psfmt32[idxof(i)] + psfmt32[idxof((i + mid) % N32)]
|
||||
+ psfmt32[idxof((i + N32 - 1) % N32)]);
|
||||
psfmt32[idxof((i + mid) % N32)] ^= r;
|
||||
r -= i;
|
||||
psfmt32[idxof((i + mid + lag) % N32)] ^= r;
|
||||
psfmt32[idxof(i)] = r;
|
||||
i = (i + 1) % N32;
|
||||
}
|
||||
|
||||
idx = N32;
|
||||
period_certification();
|
||||
initialized = 1;
|
||||
}
|
||||
423
dep/SFMT/SFMT.h
423
dep/SFMT/SFMT.h
@@ -1,157 +1,308 @@
|
||||
/**
|
||||
* @file SFMT.h
|
||||
*
|
||||
* @brief SIMD oriented Fast Mersenne Twister(SFMT) pseudorandom
|
||||
* number generator
|
||||
*
|
||||
* @author Mutsuo Saito (Hiroshima University)
|
||||
* @author Makoto Matsumoto (Hiroshima University)
|
||||
*
|
||||
* Copyright (C) 2006, 2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima
|
||||
* University. All rights reserved.
|
||||
*
|
||||
* The new BSD License is applied to this software.
|
||||
* see LICENSE.txt
|
||||
*
|
||||
* @note We assume that your system has inttypes.h. If your system
|
||||
* doesn't have inttypes.h, you have to typedef uint32_t and uint64_t,
|
||||
* and you have to define PRIu64 and PRIx64 in this file as follows:
|
||||
* @verbatim
|
||||
typedef unsigned int uint32_t
|
||||
typedef unsigned long long uint64_t
|
||||
#define PRIu64 "llu"
|
||||
#define PRIx64 "llx"
|
||||
@endverbatim
|
||||
* uint32_t must be exactly 32-bit unsigned integer type (no more, no
|
||||
* less), and uint64_t must be exactly 64-bit unsigned integer type.
|
||||
* PRIu64 and PRIx64 are used for printf function to print 64-bit
|
||||
* unsigned int and 64-bit unsigned int in hexadecimal format.
|
||||
/*
|
||||
* Copyright notice
|
||||
* ================
|
||||
* GNU General Public License http://www.gnu.org/licenses/gpl.html
|
||||
* This C++ implementation of SFMT contains parts of the original C code
|
||||
* which was published under the following BSD license, which is therefore
|
||||
* in effect in addition to the GNU General Public License.
|
||||
* Copyright (c) 2006, 2007 by Mutsuo Saito, Makoto Matsumoto and Hiroshima University.
|
||||
* Copyright (c) 2008 by Agner Fog.
|
||||
* Copyright (c) 2010 Trinity Core
|
||||
*
|
||||
* BSD License:
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* > Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* > Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* > Neither the name of the Hiroshima University nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef SFMT_H
|
||||
#define SFMT_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <emmintrin.h> // Define SSE2 intrinsics
|
||||
#include "randomc.h" // Define integer types etc
|
||||
#include <time.h>
|
||||
|
||||
#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
|
||||
#include <inttypes.h>
|
||||
#elif defined(_MSC_VER) || defined(__BORLANDC__)
|
||||
typedef unsigned int uint32_t;
|
||||
typedef unsigned __int64 uint64_t;
|
||||
#define inline __inline
|
||||
#else
|
||||
#include <inttypes.h>
|
||||
#if defined(__GNUC__)
|
||||
#define inline __inline__
|
||||
#endif
|
||||
// Choose one of the possible Mersenne exponents.
|
||||
// Higher values give longer cycle length and use more memory:
|
||||
//#define MEXP 607
|
||||
//#define MEXP 1279
|
||||
//#define MEXP 2281
|
||||
//#define MEXP 4253
|
||||
#define MEXP 11213
|
||||
//#define MEXP 19937
|
||||
//#define MEXP 44497
|
||||
|
||||
// Define constants for the selected Mersenne exponent:
|
||||
#if MEXP == 44497
|
||||
#define SFMT_N 348 // Size of state vector
|
||||
#define SFMT_M 330 // Position of intermediate feedback
|
||||
#define SFMT_SL1 5 // Left shift of W[N-1], 32-bit words
|
||||
#define SFMT_SL2 3 // Left shift of W[0], *8, 128-bit words
|
||||
#define SFMT_SR1 9 // Right shift of W[M], 32-bit words
|
||||
#define SFMT_SR2 3 // Right shift of W[N-2], *8, 128-bit words
|
||||
#define SFMT_MASK 0xeffffffb,0xdfbebfff,0xbfbf7bef,0x9ffd7bff // AND mask
|
||||
#define SFMT_PARITY 1,0,0xa3ac4000,0xecc1327a // Period certification vector
|
||||
|
||||
#elif MEXP == 19937
|
||||
#define SFMT_N 156 // Size of state vector
|
||||
#define SFMT_M 122 // Position of intermediate feedback
|
||||
#define SFMT_SL1 18 // Left shift of W[N-1], 32-bit words
|
||||
#define SFMT_SL2 1 // Left shift of W[0], *8, 128-bit words
|
||||
#define SFMT_SR1 11 // Right shift of W[M], 32-bit words
|
||||
#define SFMT_SR2 1 // Right shift of W[N-2], *8, 128-bit words
|
||||
#define SFMT_MASK 0xdfffffef,0xddfecb7f,0xbffaffff,0xbffffff6 // AND mask
|
||||
#define SFMT_PARITY 1,0,0,0x13c9e684 // Period certification vector
|
||||
|
||||
#elif MEXP == 11213
|
||||
#define SFMT_N 88 // Size of state vector
|
||||
#define SFMT_M 68 // Position of intermediate feedback
|
||||
#define SFMT_SL1 14 // Left shift of W[N-1], 32-bit words
|
||||
#define SFMT_SL2 3 // Left shift of W[0], *8, 128-bit words
|
||||
#define SFMT_SR1 7 // Right shift of W[M], 32-bit words
|
||||
#define SFMT_SR2 3 // Right shift of W[N-2], *8, 128-bit words
|
||||
#define SFMT_MASK 0xeffff7fb,0xffffffef,0xdfdfbfff,0x7fffdbfd // AND mask
|
||||
#define SFMT_PARITY 1,0,0xe8148000,0xd0c7afa3 // Period certification vector
|
||||
|
||||
#elif MEXP == 4253
|
||||
#define SFMT_N 34 // Size of state vector
|
||||
#define SFMT_M 17 // Position of intermediate feedback
|
||||
#define SFMT_SL1 20 // Left shift of W[N-1], 32-bit words
|
||||
#define SFMT_SL2 1 // Left shift of W[0], *8, 128-bit words
|
||||
#define SFMT_SR1 7 // Right shift of W[M], 32-bit words
|
||||
#define SFMT_SR2 1 // Right shift of W[N-2], *8, 128-bit words
|
||||
#define SFMT_MASK 0x9f7bffff, 0x9fffff5f, 0x3efffffb, 0xfffff7bb // AND mask
|
||||
#define SFMT_PARITY 0xa8000001, 0xaf5390a3, 0xb740b3f8, 0x6c11486d // Period certification vector
|
||||
|
||||
#elif MEXP == 2281
|
||||
#define SFMT_N 18 // Size of state vector
|
||||
#define SFMT_M 12 // Position of intermediate feedback
|
||||
#define SFMT_SL1 19 // Left shift of W[N-1], 32-bit words
|
||||
#define SFMT_SL2 1 // Left shift of W[0], *8, 128-bit words
|
||||
#define SFMT_SR1 5 // Right shift of W[M], 32-bit words
|
||||
#define SFMT_SR2 1 // Right shift of W[N-2], *8, 128-bit words
|
||||
#define SFMT_MASK 0xbff7ffbf, 0xfdfffffe, 0xf7ffef7f, 0xf2f7cbbf // AND mask
|
||||
#define SFMT_PARITY 0x00000001, 0x00000000, 0x00000000, 0x41dfa600 // Period certification vector
|
||||
|
||||
#elif MEXP == 1279
|
||||
#define SFMT_N 10 // Size of state vector
|
||||
#define SFMT_M 7 // Position of intermediate feedback
|
||||
#define SFMT_SL1 14 // Left shift of W[N-1], 32-bit words
|
||||
#define SFMT_SL2 3 // Left shift of W[0], *8, 128-bit words
|
||||
#define SFMT_SR1 5 // Right shift of W[M], 32-bit words
|
||||
#define SFMT_SR2 1 // Right shift of W[N-2], *8, 128-bit words
|
||||
#define SFMT_MASK 0xf7fefffd, 0x7fefcfff, 0xaff3ef3f, 0xb5ffff7f // AND mask
|
||||
#define SFMT_PARITY 0x00000001, 0x00000000, 0x00000000, 0x20000000 // Period certification vector
|
||||
|
||||
#elif MEXP == 607
|
||||
#define SFMT_N 5 // Size of state vector
|
||||
#define SFMT_M 2 // Position of intermediate feedback
|
||||
#define SFMT_SL1 15 // Left shift of W[N-1], 32-bit words
|
||||
#define SFMT_SL2 3 // Left shift of W[0], *8, 128-bit words
|
||||
#define SFMT_SR1 13 // Right shift of W[M], 32-bit words
|
||||
#define SFMT_SR2 3 // Right shift of W[N-2], *8, 128-bit words
|
||||
#define SFMT_MASK 0xfdff37ff, 0xef7f3f7d, 0xff777b7d, 0x7ff7fb2f // AND mask
|
||||
#define SFMT_PARITY 0x00000001, 0x00000000, 0x00000000, 0x5986f054 // Period certification vector
|
||||
#endif
|
||||
|
||||
#ifndef PRIu64
|
||||
#if defined(_MSC_VER) || defined(__BORLANDC__)
|
||||
#define PRIu64 "I64u"
|
||||
#define PRIx64 "I64x"
|
||||
#else
|
||||
#define PRIu64 "llu"
|
||||
#define PRIx64 "llx"
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined(__GNUC__)
|
||||
#define ALWAYSINLINE __attribute__((always_inline))
|
||||
#else
|
||||
#define ALWAYSINLINE
|
||||
#endif
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#if _MSC_VER >= 1200
|
||||
#define PRE_ALWAYS __forceinline
|
||||
#else
|
||||
#define PRE_ALWAYS inline
|
||||
#endif
|
||||
#else
|
||||
#define PRE_ALWAYS inline
|
||||
#endif
|
||||
|
||||
uint32_t gen_rand32(void);
|
||||
uint64_t gen_rand64(void);
|
||||
void fill_array32(uint32_t *array, int size);
|
||||
void fill_array64(uint64_t *array, int size);
|
||||
void init_gen_rand(uint32_t seed);
|
||||
void init_by_array(uint32_t *init_key, int key_length);
|
||||
const char *get_idstring(void);
|
||||
int get_min_array_size32(void);
|
||||
int get_min_array_size64(void);
|
||||
|
||||
/* These real versions are due to Isaku Wada */
|
||||
/** generates a random number on [0,1]-real-interval */
|
||||
inline static double to_real1(uint32_t v)
|
||||
{
|
||||
return v * (1.0/4294967295.0);
|
||||
/* divided by 2^32-1 */
|
||||
// Functions used by SFMTRand::RandomInitByArray
|
||||
static uint32_t func1(uint32_t x) {
|
||||
return (x ^ (x >> 27)) * 1664525U;
|
||||
}
|
||||
|
||||
/** generates a random number on [0,1]-real-interval */
|
||||
inline static double genrand_real1(void)
|
||||
{
|
||||
return to_real1(gen_rand32());
|
||||
static uint32_t func2(uint32_t x) {
|
||||
return (x ^ (x >> 27)) * 1566083941U;
|
||||
}
|
||||
|
||||
/** generates a random number on [0,1)-real-interval */
|
||||
inline static double to_real2(uint32_t v)
|
||||
{
|
||||
return v * (1.0/4294967296.0);
|
||||
/* divided by 2^32 */
|
||||
// Subfunction for the sfmt algorithm
|
||||
static inline __m128i sfmt_recursion(__m128i const &a, __m128i const &b,
|
||||
__m128i const &c, __m128i const &d, __m128i const &mask) {
|
||||
__m128i a1, b1, c1, d1, z1, z2;
|
||||
b1 = _mm_srli_epi32(b, SFMT_SR1);
|
||||
a1 = _mm_slli_si128(a, SFMT_SL2);
|
||||
c1 = _mm_srli_si128(c, SFMT_SR2);
|
||||
d1 = _mm_slli_epi32(d, SFMT_SL1);
|
||||
b1 = _mm_and_si128(b1, mask);
|
||||
z1 = _mm_xor_si128(a, a1);
|
||||
z2 = _mm_xor_si128(b1, d1);
|
||||
z1 = _mm_xor_si128(z1, c1);
|
||||
z2 = _mm_xor_si128(z1, z2);
|
||||
return z2;
|
||||
}
|
||||
|
||||
/** generates a random number on [0,1)-real-interval */
|
||||
inline static double genrand_real2(void)
|
||||
{
|
||||
return to_real2(gen_rand32());
|
||||
}
|
||||
// Class for SFMT generator
|
||||
class SFMTRand { // Encapsulate random number generator
|
||||
public:
|
||||
SFMTRand() { LastInterval = 0; RandomInit((int)(time(0))); }
|
||||
|
||||
/** generates a random number on (0,1)-real-interval */
|
||||
inline static double to_real3(uint32_t v)
|
||||
{
|
||||
return (((double)v) + 0.5)*(1.0/4294967296.0);
|
||||
/* divided by 2^32 */
|
||||
}
|
||||
void RandomInit(int seed) // Re-seed
|
||||
{
|
||||
// Re-seed
|
||||
uint32_t i; // Loop counter
|
||||
uint32_t y = seed; // Temporary
|
||||
uint32_t statesize = SFMT_N*4; // Size of state vector
|
||||
|
||||
/** generates a random number on (0,1)-real-interval */
|
||||
inline static double genrand_real3(void)
|
||||
{
|
||||
return to_real3(gen_rand32());
|
||||
}
|
||||
/** These real versions are due to Isaku Wada */
|
||||
// Fill state vector with random numbers from seed
|
||||
((uint32_t*)state)[0] = y;
|
||||
const uint32_t factor = 1812433253U;// Multiplication factor
|
||||
|
||||
/** generates a random number on [0,1) with 53-bit resolution*/
|
||||
inline static double to_res53(uint64_t v)
|
||||
{
|
||||
return v * (1.0/18446744073709551616.0L);
|
||||
}
|
||||
for (i = 1; i < statesize; i++) {
|
||||
y = factor * (y ^ (y >> 30)) + i;
|
||||
((uint32_t*)state)[i] = y;
|
||||
}
|
||||
|
||||
/** generates a random number on [0,1) with 53-bit resolution from two
|
||||
* 32 bit integers */
|
||||
inline static double to_res53_mix(uint32_t x, uint32_t y)
|
||||
{
|
||||
return to_res53(x | ((uint64_t)y << 32));
|
||||
}
|
||||
// Further initialization and period certification
|
||||
Init2();
|
||||
}
|
||||
|
||||
/** generates a random number on [0,1) with 53-bit resolution
|
||||
*/
|
||||
inline static double genrand_res53(void)
|
||||
{
|
||||
return to_res53(gen_rand64());
|
||||
}
|
||||
int32_t IRandom(int32_t min, int32_t max) // Output random integer
|
||||
{
|
||||
// Output random integer in the interval min <= x <= max
|
||||
// Slightly inaccurate if (max-min+1) is not a power of 2
|
||||
if (max <= min) {
|
||||
if (max == min) return min; else return 0x80000000;
|
||||
}
|
||||
// Assume 64 bit integers supported. Use multiply and shift method
|
||||
uint32_t interval; // Length of interval
|
||||
uint64_t longran; // Random bits * interval
|
||||
uint32_t iran; // Longran / 2^32
|
||||
|
||||
/** generates a random number on [0,1) with 53-bit resolution
|
||||
using 32bit integer.
|
||||
*/
|
||||
inline static double genrand_res53_mix(void)
|
||||
{
|
||||
uint32_t x, y;
|
||||
interval = (uint32_t)(max - min + 1);
|
||||
longran = (uint64_t)BRandom() * interval;
|
||||
iran = (uint32_t)(longran >> 32);
|
||||
// Convert back to signed and return result
|
||||
return (int32_t)iran + min;
|
||||
}
|
||||
|
||||
x = gen_rand32();
|
||||
y = gen_rand32();
|
||||
return to_res53_mix(x, y);
|
||||
}
|
||||
#endif
|
||||
uint32_t URandom(uint32_t min, uint32_t max)
|
||||
{
|
||||
// Output random integer in the interval min <= x <= max
|
||||
// Slightly inaccurate if (max-min+1) is not a power of 2
|
||||
if (max <= min) {
|
||||
if (max == min) return min; else return 0;
|
||||
}
|
||||
// Assume 64 bit integers supported. Use multiply and shift method
|
||||
uint32_t interval; // Length of interval
|
||||
uint64_t longran; // Random bits * interval
|
||||
uint32_t iran; // Longran / 2^32
|
||||
|
||||
interval = (uint32_t)(max - min + 1);
|
||||
longran = (uint64_t)BRandom() * interval;
|
||||
iran = (uint32_t)(longran >> 32);
|
||||
// Convert back to signed and return result
|
||||
return iran + min;
|
||||
}
|
||||
|
||||
double Random() // Output random floating point number
|
||||
{
|
||||
// Output random floating point number
|
||||
if (ix >= SFMT_N*4-1) {
|
||||
// Make sure we have at least two 32-bit numbers
|
||||
Generate();
|
||||
}
|
||||
uint64_t r = *(uint64_t*)((uint32_t*)state+ix);
|
||||
ix += 2;
|
||||
// 52 bits resolution for compatibility with assembly version:
|
||||
return (int64_t)(r >> 12) * (1./(67108864.0*67108864.0));
|
||||
}
|
||||
|
||||
uint32_t BRandom() // Output random bits
|
||||
{
|
||||
// Output 32 random bits
|
||||
uint32_t y;
|
||||
|
||||
if (ix >= SFMT_N*4) {
|
||||
Generate();
|
||||
}
|
||||
y = ((uint32_t*)state)[ix++];
|
||||
return y;
|
||||
}
|
||||
private:
|
||||
void Init2() // Various initializations and period certification
|
||||
{
|
||||
// Various initializations and period certification
|
||||
uint32_t i, j, temp;
|
||||
|
||||
// Initialize mask
|
||||
static const uint32_t maskinit[4] = {SFMT_MASK};
|
||||
mask = _mm_loadu_si128((__m128i*)maskinit);
|
||||
|
||||
// Period certification
|
||||
// Define period certification vector
|
||||
static const uint32_t parityvec[4] = {SFMT_PARITY};
|
||||
|
||||
// Check if parityvec & state[0] has odd parity
|
||||
temp = 0;
|
||||
for (i = 0; i < 4; i++)
|
||||
temp ^= parityvec[i] & ((uint32_t*)state)[i];
|
||||
|
||||
for (i = 16; i > 0; i >>= 1) temp ^= temp >> i;
|
||||
if (!(temp & 1)) {
|
||||
// parity is even. Certification failed
|
||||
// Find a nonzero bit in period certification vector
|
||||
for (i = 0; i < 4; i++) {
|
||||
if (parityvec[i]) {
|
||||
for (j = 1; j; j <<= 1) {
|
||||
if (parityvec[i] & j) {
|
||||
// Flip the corresponding bit in state[0] to change parity
|
||||
((uint32_t*)state)[i] ^= j;
|
||||
// Done. Exit i and j loops
|
||||
i = 5; break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Generate first random numbers and set ix = 0
|
||||
Generate();
|
||||
}
|
||||
|
||||
void Generate() // Fill state array with new random numbers
|
||||
{
|
||||
// Fill state array with new random numbers
|
||||
int i;
|
||||
__m128i r, r1, r2;
|
||||
|
||||
r1 = state[SFMT_N - 2];
|
||||
r2 = state[SFMT_N - 1];
|
||||
for (i = 0; i < SFMT_N - SFMT_M; i++) {
|
||||
r = sfmt_recursion(state[i], state[i + SFMT_M], r1, r2, mask);
|
||||
state[i] = r;
|
||||
r1 = r2;
|
||||
r2 = r;
|
||||
}
|
||||
for (; i < SFMT_N; i++) {
|
||||
r = sfmt_recursion(state[i], state[i + SFMT_M - SFMT_N], r1, r2, mask);
|
||||
state[i] = r;
|
||||
r1 = r2;
|
||||
r2 = r;
|
||||
}
|
||||
ix = 0;
|
||||
}
|
||||
|
||||
uint32_t ix; // Index into state array
|
||||
uint32_t LastInterval; // Last interval length for IRandom
|
||||
uint32_t RLimit; // Rejection limit used by IRandom
|
||||
__m128i mask; // AND mask
|
||||
__m128i state[SFMT_N]; // State vector for SFMT generator
|
||||
};
|
||||
|
||||
#endif // SFMT_H
|
||||
|
||||
65
dep/SFMT/randomc.h
Normal file
65
dep/SFMT/randomc.h
Normal file
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Copyright notice
|
||||
* ================
|
||||
* GNU General Public License http://www.gnu.org/licenses/gpl.html
|
||||
* This C++ implementation of SFMT contains parts of the original C code
|
||||
* which was published under the following BSD license, which is therefore
|
||||
* in effect in addition to the GNU General Public License.
|
||||
* Copyright (c) 2006, 2007 by Mutsuo Saito, Makoto Matsumoto and Hiroshima University.
|
||||
* Copyright (c) 2008 by Agner Fog.
|
||||
* Copyright (c) 2010 Trinity Core
|
||||
*
|
||||
* BSD License:
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* > Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* > Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* > Neither the name of the Hiroshima University nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef RANDOMC_H
|
||||
#define RANDOMC_H
|
||||
|
||||
// Define integer types with known size: int32_t, uint32_t, int64_t, uint64_t.
|
||||
// If this doesn't work then insert compiler-specific definitions here:
|
||||
#if defined(__GNUC__)
|
||||
// Compilers supporting C99 or C++0x have inttypes.h defining these integer types
|
||||
#include <inttypes.h>
|
||||
#define INT64_SUPPORTED // Remove this if the compiler doesn't support 64-bit integers
|
||||
#elif defined(_WIN16) || defined(__MSDOS__) || defined(_MSDOS)
|
||||
// 16 bit systems use long int for 32 bit integer
|
||||
typedef signed long int int32_t;
|
||||
typedef unsigned long int uint32_t;
|
||||
#elif defined(_MSC_VER)
|
||||
// Microsoft have their own definition
|
||||
typedef signed __int32 int32_t;
|
||||
typedef unsigned __int32 uint32_t;
|
||||
typedef signed __int64 int64_t;
|
||||
typedef unsigned __int64 uint64_t;
|
||||
#define INT64_SUPPORTED // Remove this if the compiler doesn't support 64-bit integers
|
||||
#else
|
||||
// This works with most compilers
|
||||
typedef signed int int32_t;
|
||||
typedef unsigned int uint32_t;
|
||||
typedef long long int64_t;
|
||||
typedef unsigned long long uint64_t;
|
||||
#define INT64_SUPPORTED // Remove this if the compiler doesn't support 64-bit integers
|
||||
#endif
|
||||
|
||||
#endif // RANDOMC_H
|
||||
@@ -1,405 +0,0 @@
|
||||
// MersenneTwister.h
|
||||
// Mersenne Twister random number generator -- a C++ class MTRand
|
||||
// Based on code by Makoto Matsumoto, Takuji Nishimura, and Shawn Cokus
|
||||
// Richard J. Wagner v1.0 15 May 2003 rjwagner@writeme.com
|
||||
|
||||
// The Mersenne Twister is an algorithm for generating random numbers. It
|
||||
// was designed with consideration of the flaws in various other generators.
|
||||
// The period, 2^19937-1, and the order of equidistribution, 623 dimensions,
|
||||
// are far greater. The generator is also fast; it avoids multiplication and
|
||||
// division, and it benefits from caches and pipelines. For more information
|
||||
// see the inventors' web page at http://www.math.keio.ac.jp/~matumoto/emt.html
|
||||
|
||||
// Reference
|
||||
// M. Matsumoto and T. Nishimura, "Mersenne Twister: A 623-Dimensionally
|
||||
// Equidistributed Uniform Pseudo-Random Number Generator", ACM Transactions on
|
||||
// Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30.
|
||||
|
||||
// Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
|
||||
// Copyright (C) 2000 - 2003, Richard J. Wagner
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions
|
||||
// are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright
|
||||
// notice, this list of conditions and the following disclaimer in the
|
||||
// documentation and/or other materials provided with the distribution.
|
||||
//
|
||||
// 3. The names of its contributors may not be used to endorse or promote
|
||||
// products derived from this software without specific prior written
|
||||
// permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
// The original code included the following notice:
|
||||
//
|
||||
// When you use this, send an email to: matumoto@math.keio.ac.jp
|
||||
// with an appropriate reference to your work.
|
||||
//
|
||||
// It would be nice to CC: rjwagner@writeme.com and Cokus@math.washington.edu
|
||||
// when you write.
|
||||
|
||||
#ifndef MERSENNETWISTER_H
|
||||
#define MERSENNETWISTER_H
|
||||
|
||||
// Not thread safe (unless auto-initialization is avoided and each thread has
|
||||
// its own MTRand object)
|
||||
|
||||
#include"Define.h"
|
||||
|
||||
#include <limits.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
|
||||
class MTRand {
|
||||
// Data
|
||||
public:
|
||||
typedef ::uint32 uint32;
|
||||
enum { N = 624 }; // length of state vector
|
||||
enum { SAVE = N + 1 }; // length of array for save()
|
||||
|
||||
protected:
|
||||
enum { M = 397 }; // period parameter
|
||||
|
||||
uint32 state[N]; // internal state
|
||||
uint32 *pNext; // next value to get from state
|
||||
int left; // number of values left before reload needed
|
||||
|
||||
//Methods
|
||||
public:
|
||||
MTRand( const uint32& oneSeed ); // initialize with a simple uint32
|
||||
MTRand( uint32 *const bigSeed, uint32 const seedLength = N ); // or an array
|
||||
MTRand(); // auto-initialize with /dev/urandom or time() and clock()
|
||||
MTRand(const MTRand&); // prevent copy constructor
|
||||
MTRand& operator=(const MTRand&); // no-op operator=
|
||||
|
||||
// Do NOT use for CRYPTOGRAPHY without securely hashing several returned
|
||||
// values together, otherwise the generator state can be learned after
|
||||
// reading 624 consecutive values.
|
||||
|
||||
// Access to 32-bit random numbers
|
||||
double rand(); // real number in [0,1]
|
||||
double rand( const double& n ); // real number in [0,n]
|
||||
double randExc(); // real number in [0,1)
|
||||
double randExc( const double& n ); // real number in [0,n)
|
||||
double randDblExc(); // real number in (0,1)
|
||||
double randDblExc( const double& n ); // real number in (0,n)
|
||||
uint32 randInt(); // integer in [0,2^32-1]
|
||||
uint32 randInt( const uint32& n ); // integer in [0,n] for n < 2^32
|
||||
double operator()() { return rand(); } // same as rand()
|
||||
|
||||
// Access to 53-bit random numbers (capacity of IEEE double precision)
|
||||
double rand53(); // real number in [0,1)
|
||||
|
||||
// Access to nonuniform random number distributions
|
||||
double randNorm( const double& mean = 0.0, const double& variance = 0.0 );
|
||||
|
||||
// Re-seeding functions with same behavior as initializers
|
||||
void seed( const uint32 oneSeed );
|
||||
void seed( uint32 *const bigSeed, const uint32 seedLength = N );
|
||||
void seed();
|
||||
|
||||
// Saving and loading generator state
|
||||
void save( uint32* saveArray ) const; // to array of size SAVE
|
||||
void load( uint32 *const loadArray ); // from such array
|
||||
/* Trinity not use streams for random values output
|
||||
friend std::ostream& operator<<( std::ostream& os, const MTRand& mtrand );
|
||||
friend std::istream& operator>>( std::istream& is, MTRand& mtrand );
|
||||
*/
|
||||
protected:
|
||||
void initialize( const uint32 oneSeed );
|
||||
void reload();
|
||||
uint32 hiBit( const uint32& u ) const { return u & 0x80000000UL; }
|
||||
uint32 loBit( const uint32& u ) const { return u & 0x00000001UL; }
|
||||
uint32 loBits( const uint32& u ) const { return u & 0x7fffffffUL; }
|
||||
uint32 mixBits( const uint32& u, const uint32& v ) const
|
||||
{ return hiBit(u) | loBits(v); }
|
||||
uint32 twist( const uint32& m, const uint32& s0, const uint32& s1 ) const
|
||||
{ return m ^ (mixBits(s0,s1)>>1) ^ uint32(-(int32)(loBit(s1) & 0x9908b0dfUL)); }
|
||||
static uint32 hash( time_t t, clock_t c );
|
||||
};
|
||||
|
||||
inline MTRand::MTRand(const MTRand&)
|
||||
{ seed(); }
|
||||
|
||||
inline MTRand& MTRand::operator=(const MTRand&)
|
||||
{ return *this; }
|
||||
|
||||
inline MTRand::MTRand( const uint32& oneSeed )
|
||||
{ seed(oneSeed); }
|
||||
|
||||
inline MTRand::MTRand( uint32 *const bigSeed, const uint32 seedLength )
|
||||
{ seed(bigSeed,seedLength); }
|
||||
|
||||
inline MTRand::MTRand()
|
||||
{ seed(); }
|
||||
|
||||
inline double MTRand::rand()
|
||||
{ return double(randInt()) * (1.0/4294967295.0); }
|
||||
|
||||
inline double MTRand::rand( const double& n )
|
||||
{ return rand() * n; }
|
||||
|
||||
inline double MTRand::randExc()
|
||||
{ return double(randInt()) * (1.0/4294967296.0); }
|
||||
|
||||
inline double MTRand::randExc( const double& n )
|
||||
{ return randExc() * n; }
|
||||
|
||||
inline double MTRand::randDblExc()
|
||||
{ return ( double(randInt()) + 0.5 ) * (1.0/4294967296.0); }
|
||||
|
||||
inline double MTRand::randDblExc( const double& n )
|
||||
{ return randDblExc() * n; }
|
||||
|
||||
inline double MTRand::rand53()
|
||||
{
|
||||
uint32 a = randInt() >> 5, b = randInt() >> 6;
|
||||
return ( a * 67108864.0 + b ) * (1.0/9007199254740992.0); // by Isaku Wada
|
||||
}
|
||||
|
||||
inline double MTRand::randNorm( const double& mean, const double& variance )
|
||||
{
|
||||
// Return a real number from a normal (Gaussian) distribution with given
|
||||
// mean and variance by Box-Muller method
|
||||
double r = sqrt( -2.0 * log( 1.0-randDblExc()) ) * variance;
|
||||
double phi = 2.0 * 3.14159265358979323846264338328 * randExc();
|
||||
return mean + r * cos(phi);
|
||||
}
|
||||
|
||||
inline MTRand::uint32 MTRand::randInt()
|
||||
{
|
||||
// Pull a 32-bit integer from the generator state
|
||||
// Every other access function simply transforms the numbers extracted here
|
||||
|
||||
if( left == 0 ) reload();
|
||||
--left;
|
||||
|
||||
register uint32 s1;
|
||||
s1 = *pNext++;
|
||||
s1 ^= (s1 >> 11);
|
||||
s1 ^= (s1 << 7) & 0x9d2c5680UL;
|
||||
s1 ^= (s1 << 15) & 0xefc60000UL;
|
||||
return ( s1 ^ (s1 >> 18) );
|
||||
}
|
||||
|
||||
inline MTRand::uint32 MTRand::randInt( const uint32& n )
|
||||
{
|
||||
// Find which bits are used in n
|
||||
// Optimized by Magnus Jonsson (magnus@smartelectronix.com)
|
||||
uint32 used = n;
|
||||
used |= used >> 1;
|
||||
used |= used >> 2;
|
||||
used |= used >> 4;
|
||||
used |= used >> 8;
|
||||
used |= used >> 16;
|
||||
|
||||
// Draw numbers until one is found in [0,n]
|
||||
uint32 i;
|
||||
do
|
||||
i = randInt() & used; // toss unused bits to shorten search
|
||||
while( i > n );
|
||||
return i;
|
||||
}
|
||||
|
||||
inline void MTRand::seed( const uint32 oneSeed )
|
||||
{
|
||||
// Seed the generator with a simple uint32
|
||||
initialize(oneSeed);
|
||||
reload();
|
||||
}
|
||||
|
||||
inline void MTRand::seed( uint32 *const bigSeed, const uint32 seedLength )
|
||||
{
|
||||
// Seed the generator with an array of uint32's
|
||||
// There are 2^19937-1 possible initial states. This function allows
|
||||
// all of those to be accessed by providing at least 19937 bits (with a
|
||||
// default seed length of N = 624 uint32's). Any bits above the lower 32
|
||||
// in each element are discarded.
|
||||
// Just call seed() if you want to get array from /dev/urandom
|
||||
initialize(19650218UL);
|
||||
register int i = 1;
|
||||
register uint32 j = 0;
|
||||
register int k = ( N > int(seedLength) ? N : int(seedLength) );
|
||||
for (; k; --k )
|
||||
{
|
||||
state[i] =
|
||||
state[i] ^ ( (state[i-1] ^ (state[i-1] >> 30)) * 1664525UL );
|
||||
state[i] += ( bigSeed[j] & 0xffffffffUL ) + j;
|
||||
state[i] &= 0xffffffffUL;
|
||||
++i; ++j;
|
||||
if( i >= N ) { state[0] = state[N-1]; i = 1; }
|
||||
if( j >= seedLength ) j = 0;
|
||||
}
|
||||
for (k = N - 1; k; --k )
|
||||
{
|
||||
state[i] =
|
||||
state[i] ^ ( (state[i-1] ^ (state[i-1] >> 30)) * 1566083941UL );
|
||||
state[i] -= i;
|
||||
state[i] &= 0xffffffffUL;
|
||||
++i;
|
||||
if( i >= N ) { state[0] = state[N-1]; i = 1; }
|
||||
}
|
||||
state[0] = 0x80000000UL; // MSB is 1, assuring non-zero initial array
|
||||
reload();
|
||||
}
|
||||
|
||||
inline void MTRand::seed()
|
||||
{
|
||||
// Seed the generator with hash of time() and clock() values
|
||||
seed( hash( time(NULL), clock() ) );
|
||||
}
|
||||
|
||||
inline void MTRand::initialize( const uint32 seed )
|
||||
{
|
||||
// Initialize generator state with seed
|
||||
// See Knuth TAOCP Vol 2, 3rd Ed, p.106 for multiplier.
|
||||
// In previous versions, most significant bits (MSBs) of the seed affect
|
||||
// only MSBs of the state array. Modified 9 Jan 2002 by Makoto Matsumoto.
|
||||
register uint32 *s = state;
|
||||
register uint32 *r = state;
|
||||
register int i = 1;
|
||||
*s++ = seed & 0xffffffffUL;
|
||||
for (; i < N; ++i )
|
||||
{
|
||||
*s++ = ( 1812433253UL * ( *r ^ (*r >> 30) ) + i ) & 0xffffffffUL;
|
||||
r++;
|
||||
}
|
||||
}
|
||||
|
||||
inline void MTRand::reload()
|
||||
{
|
||||
// Generate N new values in state
|
||||
// Made clearer and faster by Matthew Bellew (matthew.bellew@home.com)
|
||||
register uint32 *p = state;
|
||||
register int i;
|
||||
for (i = N - M; i--; ++p )
|
||||
*p = twist( p[M], p[0], p[1] );
|
||||
for (i = M; --i; ++p )
|
||||
*p = twist( p[M-N], p[0], p[1] );
|
||||
*p = twist( p[M-N], p[0], state[0] );
|
||||
|
||||
left = N, pNext = state;
|
||||
}
|
||||
|
||||
inline MTRand::uint32 MTRand::hash( time_t t, clock_t c )
|
||||
{
|
||||
// Get a uint32 from t and c
|
||||
// Better than uint32(x) in case x is floating point in [0,1]
|
||||
// Based on code by Lawrence Kirby (fred@genesis.demon.co.uk)
|
||||
|
||||
static uint32 differ = 0; // guarantee time-based seeds will change
|
||||
|
||||
uint32 h1 = 0;
|
||||
unsigned char *p = (unsigned char *) &t;
|
||||
for (size_t i = 0; i < sizeof(t); ++i )
|
||||
{
|
||||
h1 *= UCHAR_MAX + 2U;
|
||||
h1 += p[i];
|
||||
}
|
||||
uint32 h2 = 0;
|
||||
p = (unsigned char *) &c;
|
||||
for (size_t j = 0; j < sizeof(c); ++j )
|
||||
{
|
||||
h2 *= UCHAR_MAX + 2U;
|
||||
h2 += p[j];
|
||||
}
|
||||
return ( h1 + differ++ ) ^ h2;
|
||||
}
|
||||
|
||||
inline void MTRand::save( uint32* saveArray ) const
|
||||
{
|
||||
register uint32 *sa = saveArray;
|
||||
register const uint32 *s = state;
|
||||
register int i = N;
|
||||
for (; i--; *sa++ = *s++ ) {}
|
||||
*sa = left;
|
||||
}
|
||||
|
||||
inline void MTRand::load( uint32 *const loadArray )
|
||||
{
|
||||
register uint32 *s = state;
|
||||
register uint32 *la = loadArray;
|
||||
register int i = N;
|
||||
for (; i--; *s++ = *la++ ) {}
|
||||
left = *la;
|
||||
pNext = &state[N-left];
|
||||
}
|
||||
|
||||
/* Trinity not use streams for random values output
|
||||
inline std::ostream& operator<<( std::ostream& os, const MTRand& mtrand )
|
||||
{
|
||||
register const MTRand::uint32 *s = mtrand.state;
|
||||
register int i = mtrand.N;
|
||||
for (; i--; os << *s++ << "\t" ) {}
|
||||
return os << mtrand.left;
|
||||
}
|
||||
|
||||
inline std::istream& operator>>( std::istream& is, MTRand& mtrand )
|
||||
{
|
||||
register MTRand::uint32 *s = mtrand.state;
|
||||
register int i = mtrand.N;
|
||||
for (; i--; is >> *s++ ) {}
|
||||
is >> mtrand.left;
|
||||
mtrand.pNext = &mtrand.state[mtrand.N-mtrand.left];
|
||||
return is;
|
||||
}
|
||||
*/
|
||||
|
||||
#endif // MERSENNETWISTER_H
|
||||
|
||||
// Change log:
|
||||
//
|
||||
// v0.1 - First release on 15 May 2000
|
||||
// - Based on code by Makoto Matsumoto, Takuji Nishimura, and Shawn Cokus
|
||||
// - Translated from C to C++
|
||||
// - Made completely ANSI compliant
|
||||
// - Designed convenient interface for initialization, seeding, and
|
||||
// obtaining numbers in default or user-defined ranges
|
||||
// - Added automatic seeding from /dev/urandom or time() and clock()
|
||||
// - Provided functions for saving and loading generator state
|
||||
//
|
||||
// v0.2 - Fixed bug which reloaded generator one step too late
|
||||
//
|
||||
// v0.3 - Switched to clearer, faster reload() code from Matthew Bellew
|
||||
//
|
||||
// v0.4 - Removed trailing newline in saved generator format to be consistent
|
||||
// with output format of built-in types
|
||||
//
|
||||
// v0.5 - Improved portability by replacing static const int's with enum's and
|
||||
// clarifying return values in seed(); suggested by Eric Heimburg
|
||||
// - Removed MAXINT constant; use 0xffffffffUL instead
|
||||
//
|
||||
// v0.6 - Eliminated seed overflow when uint32 is larger than 32 bits
|
||||
// - Changed integer [0,n] generator to give better uniformity
|
||||
//
|
||||
// v0.7 - Fixed operator precedence ambiguity in reload()
|
||||
// - Added access for real numbers in (0,1) and (0,n)
|
||||
//
|
||||
// v0.8 - Included time.h header to properly support time_t and clock_t
|
||||
//
|
||||
// v1.0 - Revised seeding to match 26 Jan 2002 update of Nishimura and Matsumoto
|
||||
// - Allowed for seeding with arrays of any length
|
||||
// - Added access for real numbers in [0,1) with 53-bit resolution
|
||||
// - Added access for real numbers from normal (Gaussian) distributions
|
||||
// - Increased overall speed by optimizing twist()
|
||||
// - Doubled speed of integer [0,n] generation
|
||||
// - Fixed out-of-range number generation on 64-bit machines
|
||||
// - Improved portability by substituting literal constants for long enum's
|
||||
// - Changed license from GNU LGPL to BSD
|
||||
|
||||
@@ -98,7 +98,7 @@ set(game_STAT_SRCS
|
||||
|
||||
include_directories(
|
||||
${CMAKE_BINARY_DIR}
|
||||
${CMAKE_SOURCE_DIR}/dep/mersennetwister
|
||||
${CMAKE_SOURCE_DIR}/dep/SFMT
|
||||
${CMAKE_SOURCE_DIR}/dep/zlib
|
||||
${CMAKE_SOURCE_DIR}/src/server/collision
|
||||
${CMAKE_SOURCE_DIR}/src/server/collision/Management
|
||||
|
||||
@@ -32,7 +32,7 @@
|
||||
#include "SharedDefines.h"
|
||||
#include "GridRefManager.h"
|
||||
#include "MapRefManager.h"
|
||||
#include "MersenneTwister.h"
|
||||
#include "sfmt.h"
|
||||
|
||||
#include <bitset>
|
||||
#include <list>
|
||||
@@ -427,13 +427,13 @@ class Map : public GridRefManager<NGridType>
|
||||
void UpdateIteratorBack(Player *player);
|
||||
|
||||
#ifdef MAP_BASED_RAND_GEN
|
||||
MTRand mtRand;
|
||||
int32 irand(int32 min, int32 max) { return int32 (mtRand.randInt(max - min)) + min; }
|
||||
uint32 urand(uint32 min, uint32 max) { return mtRand.randInt(max - min) + min; }
|
||||
int32 rand32() { return mtRand.randInt(); }
|
||||
double rand_norm() { return mtRand.randExc(); }
|
||||
double rand_chance() { return mtRand.randExc(100.0); }
|
||||
#endif
|
||||
SFMTRand sfmtRand;
|
||||
int32 irand(int32 min, int32 max) { return int32(sfmtRand.IRandom(min, max)); }
|
||||
uint32 urand(uint32 min, uint32 max) { return uint32(sfmtRand.URandom(min, max)); }
|
||||
int32 rand32() { return int32(sfmtRand.BRandom()); }
|
||||
double rand_norm() { return sfmtRand.Random(); }
|
||||
double rand_chance() { return sfmtRand.Random() * 100.0; }
|
||||
#endif // MAP_BASED_RAND_GEN
|
||||
|
||||
TempSummon *SummonCreature(uint32 entry, const Position &pos, SummonPropertiesEntry const *properties = NULL, uint32 duration = 0, Unit *summoner = NULL, uint32 vehId = 0);
|
||||
Creature* GetCreature(uint64 guid);
|
||||
|
||||
@@ -52,7 +52,6 @@ set(shared_STAT_SRCS
|
||||
|
||||
include_directories(
|
||||
${CMAKE_BINARY_DIR}
|
||||
${CMAKE_SOURCE_DIR}/dep/mersennetwister
|
||||
${CMAKE_SOURCE_DIR}/dep/SFMT
|
||||
${CMAKE_SOURCE_DIR}/dep/sockets/include
|
||||
${CMAKE_SOURCE_DIR}/dep/utf8cpp
|
||||
|
||||
@@ -22,36 +22,35 @@
|
||||
|
||||
#include "socket_include.h"
|
||||
#include "utf8.h"
|
||||
//#include "SFMT.h"
|
||||
#include "MersenneTwister.h"
|
||||
#include "sfmt.h"
|
||||
#include <ace/TSS_T.h>
|
||||
|
||||
typedef ACE_TSS<MTRand> MTRandTSS;
|
||||
static MTRandTSS mtRand;
|
||||
typedef ACE_TSS<SFMTRand> SFMTRandTSS;
|
||||
static SFMTRandTSS sfmtRand;
|
||||
|
||||
int32 irand (int32 min, int32 max)
|
||||
{
|
||||
return int32 (mtRand->randInt (max - min)) + min;
|
||||
return int32(sfmtRand->IRandom(min, max));
|
||||
}
|
||||
|
||||
uint32 urand (uint32 min, uint32 max)
|
||||
{
|
||||
return mtRand->randInt (max - min) + min;
|
||||
return sfmtRand->URandom(min, max);
|
||||
}
|
||||
|
||||
int32 rand32 ()
|
||||
{
|
||||
return mtRand->randInt ();
|
||||
return int32(sfmtRand->BRandom());
|
||||
}
|
||||
|
||||
double rand_norm(void)
|
||||
{
|
||||
return mtRand->randExc ();
|
||||
return sfmtRand->Random();
|
||||
}
|
||||
|
||||
double rand_chance (void)
|
||||
{
|
||||
return mtRand->randExc (100.0);
|
||||
return sfmtRand->Random() * 100.0;
|
||||
}
|
||||
|
||||
Tokens StrSplit(const std::string &src, const std::string &sep)
|
||||
|
||||
@@ -46,7 +46,7 @@ include_directories(
|
||||
${CMAKE_BINARY_DIR}
|
||||
${CMAKE_SOURCE_DIR}/dep/gsoap
|
||||
${CMAKE_SOURCE_DIR}/dep/sockets/include
|
||||
${CMAKE_SOURCE_DIR}/dep/mersennetwister
|
||||
${CMAKE_SOURCE_DIR}/dep/sfmt
|
||||
${CMAKE_SOURCE_DIR}/src/server/collision
|
||||
${CMAKE_SOURCE_DIR}/src/server/collision/Management
|
||||
${CMAKE_SOURCE_DIR}/src/server/shared
|
||||
|
||||
Reference in New Issue
Block a user