dSFMT  2.1.1
dSFMT.h
Go to the documentation of this file.
00001 
00031 #ifndef DSFMT_H
00032 #define DSFMT_H
00033 
00034 #include <stdio.h>
00035 #include <assert.h>
00036 
00037 #if !defined(DSFMT_MEXP)
00038 #ifdef __GNUC__
00039   #warning "DSFMT_MEXP is not defined. I assume DSFMT_MEXP is 19937."
00040 #endif
00041   #define DSFMT_MEXP 19937
00042 #endif
00043 /*-----------------
00044   BASIC DEFINITIONS
00045   -----------------*/
00046 /* Mersenne Exponent. The period of the sequence 
00047  *  is a multiple of 2^DSFMT_MEXP-1.
00048  * #define DSFMT_MEXP 19937 */
00051 #define DSFMT_N ((DSFMT_MEXP - 128) / 104 + 1)
00052 
00054 #define DSFMT_N32 (DSFMT_N * 4)
00055 
00057 #define DSFMT_N64 (DSFMT_N * 2)
00058 
00059 #if !defined(DSFMT_BIG_ENDIAN)
00060 #  if defined(__BYTE_ORDER) && defined(__BIG_ENDIAN)
00061 #    if __BYTE_ORDER == __BIG_ENDIAN
00062 #      define DSFMT_BIG_ENDIAN 1
00063 #    endif
00064 #  elif defined(_BYTE_ORDER) && defined(_BIG_ENDIAN)
00065 #    if _BYTE_ORDER == _BIG_ENDIAN
00066 #      define DSFMT_BIG_ENDIAN 1
00067 #    endif
00068 #  elif defined(__BYTE_ORDER__) && defined(__BIG_ENDIAN__)
00069 #    if __BYTE_ORDER__ == __BIG_ENDIAN__
00070 #      define DSFMT_BIG_ENDIAN 1
00071 #    endif
00072 #  elif defined(BYTE_ORDER) && defined(BIG_ENDIAN)
00073 #    if BYTE_ORDER == BIG_ENDIAN
00074 #      define DSFMT_BIG_ENDIAN 1
00075 #    endif
00076 #  elif defined(__BIG_ENDIAN) || defined(_BIG_ENDIAN) \
00077     || defined(__BIG_ENDIAN__) || defined(BIG_ENDIAN)
00078 #      define DSFMT_BIG_ENDIAN 1
00079 #  endif
00080 #endif
00081 
00082 #if defined(DSFMT_BIG_ENDIAN) && defined(__amd64)
00083 #  undef DSFMT_BIG_ENDIAN
00084 #endif
00085 
00086 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
00087 #  include <inttypes.h>
00088 #elif defined(_MSC_VER) || defined(__BORLANDC__)
00089 #  if !defined(DSFMT_UINT32_DEFINED) && !defined(SFMT_UINT32_DEFINED)
00090 typedef unsigned int uint32_t;
00091 typedef unsigned __int64 uint64_t;
00092 #    define UINT64_C(v) (v ## ui64)
00093 #    define DSFMT_UINT32_DEFINED
00094 #    if !defined(inline)
00095 #      define inline __inline
00096 #    endif
00097 #  endif
00098 #else
00099 #  include <inttypes.h>
00100 #  if !defined(inline)
00101 #    if defined(__GNUC__)
00102 #      define inline __inline__
00103 #    else
00104 #      define inline
00105 #    endif
00106 #  endif
00107 #endif
00108 
00109 #ifndef PRIu64
00110 #  if defined(_MSC_VER) || defined(__BORLANDC__)
00111 #    define PRIu64 "I64u"
00112 #    define PRIx64 "I64x"
00113 #  else
00114 #    define PRIu64 "llu"
00115 #    define PRIx64 "llx"
00116 #  endif
00117 #endif
00118 
00119 #ifndef UINT64_C
00120 #  define UINT64_C(v) (v ## ULL) 
00121 #endif
00122 
00123 /*------------------------------------------
00124   128-bit SIMD like data type for standard C
00125   ------------------------------------------*/
00126 #if defined(HAVE_ALTIVEC)
00127 #  if !defined(__APPLE__)
00128 #    include <altivec.h>
00129 #  endif
00130 
00131 union W128_T {
00132     vector unsigned int s;
00133     uint64_t u[2];
00134     uint32_t u32[4];
00135     double d[2];
00136 };
00137 
00138 #elif defined(HAVE_SSE2)
00139 #  include <emmintrin.h>
00140 
00142 union W128_T {
00143     __m128i si;
00144     __m128d sd;
00145     uint64_t u[2];
00146     uint32_t u32[4];
00147     double d[2];
00148 };
00149 #else  /* standard C */
00150 
00151 union W128_T {
00152     uint64_t u[2];
00153     uint32_t u32[4];
00154     double d[2];
00155 };
00156 #endif
00157 
00159 typedef union W128_T w128_t;
00160 
00162 struct DSFMT_T {
00163     w128_t status[DSFMT_N + 1];
00164     int idx;
00165 };
00166 typedef struct DSFMT_T dsfmt_t;
00167 
00169 extern dsfmt_t dsfmt_global_data;
00171 extern const int dsfmt_global_mexp;
00172 
00173 void dsfmt_gen_rand_all(dsfmt_t *dsfmt);
00174 void dsfmt_fill_array_open_close(dsfmt_t *dsfmt, double array[], int size);
00175 void dsfmt_fill_array_close_open(dsfmt_t *dsfmt, double array[], int size);
00176 void dsfmt_fill_array_open_open(dsfmt_t *dsfmt, double array[], int size);
00177 void dsfmt_fill_array_close1_open2(dsfmt_t *dsfmt, double array[], int size);
00178 void dsfmt_chk_init_gen_rand(dsfmt_t *dsfmt, uint32_t seed, int mexp);
00179 void dsfmt_chk_init_by_array(dsfmt_t *dsfmt, uint32_t init_key[],
00180                              int key_length, int mexp);
00181 const char *dsfmt_get_idstring(void);
00182 int dsfmt_get_min_array_size(void);
00183 
00184 #if defined(__GNUC__)
00185 #  define DSFMT_PRE_INLINE inline static
00186 #  define DSFMT_PST_INLINE __attribute__((always_inline))
00187 #elif defined(_MSC_VER) && _MSC_VER >= 1200
00188 #  define DSFMT_PRE_INLINE __forceinline static
00189 #  define DSFMT_PST_INLINE
00190 #else
00191 #  define DSFMT_PRE_INLINE inline static
00192 #  define DSFMT_PST_INLINE
00193 #endif
00194 DSFMT_PRE_INLINE uint32_t dsfmt_genrand_uint32(dsfmt_t *dsfmt) DSFMT_PST_INLINE;
00195 DSFMT_PRE_INLINE double dsfmt_genrand_close1_open2(dsfmt_t *dsfmt)
00196     DSFMT_PST_INLINE;
00197 DSFMT_PRE_INLINE double dsfmt_genrand_close_open(dsfmt_t *dsfmt)
00198     DSFMT_PST_INLINE;
00199 DSFMT_PRE_INLINE double dsfmt_genrand_open_close(dsfmt_t *dsfmt)
00200     DSFMT_PST_INLINE;
00201 DSFMT_PRE_INLINE double dsfmt_genrand_open_open(dsfmt_t *dsfmt)
00202     DSFMT_PST_INLINE;
00203 DSFMT_PRE_INLINE uint32_t dsfmt_gv_genrand_uint32(void) DSFMT_PST_INLINE;
00204 DSFMT_PRE_INLINE double dsfmt_gv_genrand_close1_open2(void) DSFMT_PST_INLINE;
00205 DSFMT_PRE_INLINE double dsfmt_gv_genrand_close_open(void) DSFMT_PST_INLINE;
00206 DSFMT_PRE_INLINE double dsfmt_gv_genrand_open_close(void) DSFMT_PST_INLINE;
00207 DSFMT_PRE_INLINE double dsfmt_gv_genrand_open_open(void) DSFMT_PST_INLINE;
00208 DSFMT_PRE_INLINE void dsfmt_gv_fill_array_open_close(double array[], int size)
00209     DSFMT_PST_INLINE;
00210 DSFMT_PRE_INLINE void dsfmt_gv_fill_array_close_open(double array[], int size)
00211     DSFMT_PST_INLINE;
00212 DSFMT_PRE_INLINE void dsfmt_gv_fill_array_open_open(double array[], int size)
00213     DSFMT_PST_INLINE;
00214 DSFMT_PRE_INLINE void dsfmt_gv_fill_array_close1_open2(double array[], int size)
00215     DSFMT_PST_INLINE;
00216 DSFMT_PRE_INLINE void dsfmt_gv_init_gen_rand(uint32_t seed) DSFMT_PST_INLINE;
00217 DSFMT_PRE_INLINE void dsfmt_gv_init_by_array(uint32_t init_key[],
00218                                              int key_length) DSFMT_PST_INLINE;
00219 DSFMT_PRE_INLINE void dsfmt_init_gen_rand(dsfmt_t *dsfmt, uint32_t seed)
00220     DSFMT_PST_INLINE;
00221 DSFMT_PRE_INLINE void dsfmt_init_by_array(dsfmt_t *dsfmt, uint32_t init_key[],
00222                                           int key_length) DSFMT_PST_INLINE;
00223 
00232 inline static uint32_t dsfmt_genrand_uint32(dsfmt_t *dsfmt) {
00233     uint32_t r;
00234     uint64_t *psfmt64 = &dsfmt->status[0].u[0];
00235 
00236     if (dsfmt->idx >= DSFMT_N64) {
00237         dsfmt_gen_rand_all(dsfmt);
00238         dsfmt->idx = 0;
00239     }
00240     r = psfmt64[dsfmt->idx++] & 0xffffffffU;
00241     return r;
00242 }
00243 
00253 inline static double dsfmt_genrand_close1_open2(dsfmt_t *dsfmt) {
00254     double r;
00255     double *psfmt64 = &dsfmt->status[0].d[0];
00256 
00257     if (dsfmt->idx >= DSFMT_N64) {
00258         dsfmt_gen_rand_all(dsfmt);
00259         dsfmt->idx = 0;
00260     }
00261     r = psfmt64[dsfmt->idx++];
00262     return r;
00263 }
00264 
00272 inline static uint32_t dsfmt_gv_genrand_uint32(void) {
00273     return dsfmt_genrand_uint32(&dsfmt_global_data);
00274 }
00275 
00283 inline static double dsfmt_gv_genrand_close1_open2(void) {
00284     return dsfmt_genrand_close1_open2(&dsfmt_global_data);
00285 }
00286 
00295 inline static double dsfmt_genrand_close_open(dsfmt_t *dsfmt) {
00296     return dsfmt_genrand_close1_open2(dsfmt) - 1.0;
00297 }
00298 
00306 inline static double dsfmt_gv_genrand_close_open(void) {
00307     return dsfmt_gv_genrand_close1_open2() - 1.0;
00308 }
00309 
00318 inline static double dsfmt_genrand_open_close(dsfmt_t *dsfmt) {
00319     return 2.0 - dsfmt_genrand_close1_open2(dsfmt);
00320 }
00321 
00329 inline static double dsfmt_gv_genrand_open_close(void) {
00330     return 2.0 - dsfmt_gv_genrand_close1_open2();
00331 }
00332 
00341 inline static double dsfmt_genrand_open_open(dsfmt_t *dsfmt) {
00342     double *dsfmt64 = &dsfmt->status[0].d[0];
00343     union {
00344         double d;
00345         uint64_t u;
00346     } r;
00347 
00348     if (dsfmt->idx >= DSFMT_N64) {
00349         dsfmt_gen_rand_all(dsfmt);
00350         dsfmt->idx = 0;
00351     }
00352     r.d = dsfmt64[dsfmt->idx++];
00353     r.u |= 1;
00354     return r.d - 1.0;
00355 }
00356 
00364 inline static double dsfmt_gv_genrand_open_open(void) {
00365     return dsfmt_genrand_open_open(&dsfmt_global_data);
00366 }
00367 
00379 inline static void dsfmt_gv_fill_array_close1_open2(double array[], int size) {
00380     dsfmt_fill_array_close1_open2(&dsfmt_global_data, array, size);
00381 }
00382 
00395 inline static void dsfmt_gv_fill_array_open_close(double array[], int size) {
00396     dsfmt_fill_array_open_close(&dsfmt_global_data, array, size);
00397 }
00398 
00411 inline static void dsfmt_gv_fill_array_close_open(double array[], int size) {
00412     dsfmt_fill_array_close_open(&dsfmt_global_data, array, size);
00413 }
00414 
00427 inline static void dsfmt_gv_fill_array_open_open(double array[], int size) {
00428     dsfmt_fill_array_open_open(&dsfmt_global_data, array, size);
00429 }
00430 
00437 inline static void dsfmt_init_gen_rand(dsfmt_t *dsfmt, uint32_t seed) {
00438     dsfmt_chk_init_gen_rand(dsfmt, seed, DSFMT_MEXP);
00439 }
00440 
00447 inline static void dsfmt_gv_init_gen_rand(uint32_t seed) {
00448     dsfmt_init_gen_rand(&dsfmt_global_data, seed);
00449 }
00450 
00458 inline static void dsfmt_init_by_array(dsfmt_t *dsfmt, uint32_t init_key[],
00459                                        int key_length) {
00460     dsfmt_chk_init_by_array(dsfmt, init_key, key_length, DSFMT_MEXP);
00461 }
00462 
00471 inline static void dsfmt_gv_init_by_array(uint32_t init_key[], int key_length) {
00472     dsfmt_init_by_array(&dsfmt_global_data, init_key, key_length);
00473 }
00474 
00475 #if !defined(DSFMT_DO_NOT_USE_OLD_NAMES)
00476 DSFMT_PRE_INLINE const char *get_idstring(void) DSFMT_PST_INLINE;
00477 DSFMT_PRE_INLINE int get_min_array_size(void) DSFMT_PST_INLINE;
00478 DSFMT_PRE_INLINE void init_gen_rand(uint32_t seed) DSFMT_PST_INLINE;
00479 DSFMT_PRE_INLINE void init_by_array(uint32_t init_key[], int key_length)
00480     DSFMT_PST_INLINE;
00481 DSFMT_PRE_INLINE double genrand_close1_open2(void) DSFMT_PST_INLINE;
00482 DSFMT_PRE_INLINE double genrand_close_open(void) DSFMT_PST_INLINE;
00483 DSFMT_PRE_INLINE double genrand_open_close(void) DSFMT_PST_INLINE;
00484 DSFMT_PRE_INLINE double genrand_open_open(void) DSFMT_PST_INLINE;
00485 DSFMT_PRE_INLINE void fill_array_open_close(double array[], int size)
00486     DSFMT_PST_INLINE;
00487 DSFMT_PRE_INLINE void fill_array_close_open(double array[], int size)
00488     DSFMT_PST_INLINE;
00489 DSFMT_PRE_INLINE void fill_array_open_open(double array[], int size)
00490     DSFMT_PST_INLINE;
00491 DSFMT_PRE_INLINE void fill_array_close1_open2(double array[], int size)
00492     DSFMT_PST_INLINE;
00493 
00499 inline static const char *get_idstring(void) {
00500     return dsfmt_get_idstring();
00501 }
00502 
00508 inline static int get_min_array_size(void) {
00509     return dsfmt_get_min_array_size();
00510 }
00511 
00517 inline static void init_gen_rand(uint32_t seed) {
00518     dsfmt_gv_init_gen_rand(seed);
00519 }
00520 
00527 inline static void init_by_array(uint32_t init_key[], int key_length) {
00528     dsfmt_gv_init_by_array(init_key, key_length);
00529 }
00530 
00537 inline static double genrand_close1_open2(void) {
00538     return dsfmt_gv_genrand_close1_open2();
00539 }
00540 
00547 inline static double genrand_close_open(void) {
00548     return dsfmt_gv_genrand_close_open();
00549 }
00550 
00557 inline static double genrand_open_close(void) {
00558     return dsfmt_gv_genrand_open_close();
00559 }
00560 
00567 inline static double genrand_open_open(void) {
00568     return dsfmt_gv_genrand_open_open();
00569 }
00570 
00580 inline static void fill_array_open_close(double array[], int size) {
00581     dsfmt_gv_fill_array_open_close(array, size);
00582 }
00583 
00593 inline static void fill_array_close_open(double array[], int size) {
00594     dsfmt_gv_fill_array_close_open(array, size);
00595 }
00596 
00606 inline static void fill_array_open_open(double array[], int size) {
00607     dsfmt_gv_fill_array_open_open(array, size);
00608 }
00609 
00618 inline static void fill_array_close1_open2(double array[], int size) {
00619     dsfmt_gv_fill_array_close1_open2(array, size);
00620 }
00621 #endif /* DSFMT_DO_NOT_USE_OLD_NAMES */
00622 
00623 #endif /* DSFMT_H */