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
00045
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 long long uint64_t;
00092 # define DSFMT_UINT32_DEFINED
00093 # if !defined(inline)
00094 # define inline __inline
00095 # endif
00096 # endif
00097 #else
00098 # include <inttypes.h>
00099 # if !defined(inline)
00100 # if defined(__GNUC__)
00101 # define inline __inline__
00102 # else
00103 # define inline
00104 # endif
00105 # endif
00106 #endif
00107
00108 #ifndef PRIu64
00109 # if defined(_MSC_VER) || defined(__BORLANDC__)
00110 # define PRIu64 "I64u"
00111 # define PRIx64 "I64x"
00112 # else
00113 # define PRIu64 "llu"
00114 # define PRIx64 "llx"
00115 # endif
00116 #endif
00117
00118 #ifndef UINT64_C
00119 # define UINT64_C(v) (v ## ULL)
00120 #endif
00121
00122
00123
00124
00125 #if defined(HAVE_ALTIVEC)
00126 # if !defined(__APPLE__)
00127 # include <altivec.h>
00128 # endif
00129
00130 union W128_T {
00131 vector unsigned int s;
00132 uint64_t u[2];
00133 uint32_t u32[4];
00134 double d[2];
00135 };
00136
00137 #elif defined(HAVE_SSE2)
00138 # include <emmintrin.h>
00139
00141 union W128_T {
00142 __m128i si;
00143 __m128d sd;
00144 uint64_t u[2];
00145 uint32_t u32[4];
00146 double d[2];
00147 };
00148 #else
00149
00150 union W128_T {
00151 uint64_t u[2];
00152 uint32_t u32[4];
00153 double d[2];
00154 };
00155 #endif
00156
00158 typedef union W128_T w128_t;
00159
00161 struct DSFMT_T {
00162 w128_t status[DSFMT_N + 1];
00163 int idx;
00164 };
00165 typedef struct DSFMT_T dsfmt_t;
00166
00168 extern dsfmt_t dsfmt_global_data;
00170 extern const int dsfmt_global_mexp;
00171
00172 void dsfmt_gen_rand_all(dsfmt_t *dsfmt);
00173 void dsfmt_fill_array_open_close(dsfmt_t *dsfmt, double array[], int size);
00174 void dsfmt_fill_array_close_open(dsfmt_t *dsfmt, double array[], int size);
00175 void dsfmt_fill_array_open_open(dsfmt_t *dsfmt, double array[], int size);
00176 void dsfmt_fill_array_close1_open2(dsfmt_t *dsfmt, double array[], int size);
00177 void dsfmt_chk_init_gen_rand(dsfmt_t *dsfmt, uint32_t seed, int mexp);
00178 void dsfmt_chk_init_by_array(dsfmt_t *dsfmt, uint32_t init_key[],
00179 int key_length, int mexp);
00180 const char *dsfmt_get_idstring(void);
00181 int dsfmt_get_min_array_size(void);
00182
00183 #if defined(__GNUC__)
00184 # define DSFMT_PRE_INLINE inline static
00185 # define DSFMT_PST_INLINE __attribute__((always_inline))
00186 #elif defined(_MSC_VER) && _MSC_VER >= 1200
00187 # define DSFMT_PRE_INLINE __forceinline
00188 # define DSFMT_PST_INLINE
00189 #else
00190 # define DSFMT_PRE_INLINE inline static
00191 # define DSFMT_PST_INLINE
00192 #endif
00193 DSFMT_PRE_INLINE double dsfmt_genrand_close1_open2(dsfmt_t *dsfmt)
00194 DSFMT_PST_INLINE;
00195 DSFMT_PRE_INLINE double dsfmt_genrand_close_open(dsfmt_t *dsfmt)
00196 DSFMT_PST_INLINE;
00197 DSFMT_PRE_INLINE double dsfmt_genrand_open_close(dsfmt_t *dsfmt)
00198 DSFMT_PST_INLINE;
00199 DSFMT_PRE_INLINE double dsfmt_genrand_open_open(dsfmt_t *dsfmt)
00200 DSFMT_PST_INLINE;
00201 DSFMT_PRE_INLINE double dsfmt_gv_genrand_close1_open2(void) DSFMT_PST_INLINE;
00202 DSFMT_PRE_INLINE double dsfmt_gv_genrand_close_open(void) DSFMT_PST_INLINE;
00203 DSFMT_PRE_INLINE double dsfmt_gv_genrand_open_close(void) DSFMT_PST_INLINE;
00204 DSFMT_PRE_INLINE double dsfmt_gv_genrand_open_open(void) DSFMT_PST_INLINE;
00205 DSFMT_PRE_INLINE void dsfmt_gv_fill_array_open_close(double array[], int size)
00206 DSFMT_PST_INLINE;
00207 DSFMT_PRE_INLINE void dsfmt_gv_fill_array_close_open(double array[], int size)
00208 DSFMT_PST_INLINE;
00209 DSFMT_PRE_INLINE void dsfmt_gv_fill_array_open_open(double array[], int size)
00210 DSFMT_PST_INLINE;
00211 DSFMT_PRE_INLINE void dsfmt_gv_fill_array_close1_open2(double array[], int size)
00212 DSFMT_PST_INLINE;
00213 DSFMT_PRE_INLINE void dsfmt_gv_init_gen_rand(uint32_t seed) DSFMT_PST_INLINE;
00214 DSFMT_PRE_INLINE void dsfmt_gv_init_by_array(uint32_t init_key[],
00215 int key_length) DSFMT_PST_INLINE;
00216 DSFMT_PRE_INLINE void dsfmt_init_gen_rand(dsfmt_t *dsfmt, uint32_t seed)
00217 DSFMT_PST_INLINE;
00218 DSFMT_PRE_INLINE void dsfmt_init_by_array(dsfmt_t *dsfmt, uint32_t init_key[],
00219 int key_length) DSFMT_PST_INLINE;
00220
00230 inline static double dsfmt_genrand_close1_open2(dsfmt_t *dsfmt) {
00231 double r;
00232 double *psfmt64 = &dsfmt->status[0].d[0];
00233
00234 if (dsfmt->idx >= DSFMT_N64) {
00235 dsfmt_gen_rand_all(dsfmt);
00236 dsfmt->idx = 0;
00237 }
00238 r = psfmt64[dsfmt->idx++];
00239 return r;
00240 }
00241
00250 inline static double dsfmt_gv_genrand_close1_open2(void) {
00251 return dsfmt_genrand_close1_open2(&dsfmt_global_data);
00252 }
00253
00262 inline static double dsfmt_genrand_close_open(dsfmt_t *dsfmt) {
00263 return dsfmt_genrand_close1_open2(dsfmt) - 1.0;
00264 }
00265
00273 inline static double dsfmt_gv_genrand_close_open(void) {
00274 return dsfmt_gv_genrand_close1_open2() - 1.0;
00275 }
00276
00285 inline static double dsfmt_genrand_open_close(dsfmt_t *dsfmt) {
00286 return 2.0 - dsfmt_genrand_close1_open2(dsfmt);
00287 }
00288
00296 inline static double dsfmt_gv_genrand_open_close(void) {
00297 return 2.0 - dsfmt_gv_genrand_close1_open2();
00298 }
00299
00308 inline static double dsfmt_genrand_open_open(dsfmt_t *dsfmt) {
00309 double *dsfmt64 = &dsfmt->status[0].d[0];
00310 union {
00311 double d;
00312 uint64_t u;
00313 } r;
00314
00315 if (dsfmt->idx >= DSFMT_N64) {
00316 dsfmt_gen_rand_all(dsfmt);
00317 dsfmt->idx = 0;
00318 }
00319 r.d = dsfmt64[dsfmt->idx++];
00320 r.u |= 1;
00321 return r.d - 1.0;
00322 }
00323
00331 inline static double dsfmt_gv_genrand_open_open(void) {
00332 return dsfmt_genrand_open_open(&dsfmt_global_data);
00333 }
00334
00346 inline static void dsfmt_gv_fill_array_close1_open2(double array[], int size) {
00347 dsfmt_fill_array_close1_open2(&dsfmt_global_data, array, size);
00348 }
00349
00362 inline static void dsfmt_gv_fill_array_open_close(double array[], int size) {
00363 dsfmt_fill_array_open_close(&dsfmt_global_data, array, size);
00364 }
00365
00378 inline static void dsfmt_gv_fill_array_close_open(double array[], int size) {
00379 dsfmt_fill_array_close_open(&dsfmt_global_data, array, size);
00380 }
00381
00394 inline static void dsfmt_gv_fill_array_open_open(double array[], int size) {
00395 dsfmt_fill_array_open_open(&dsfmt_global_data, array, size);
00396 }
00397
00404 inline static void dsfmt_init_gen_rand(dsfmt_t *dsfmt, uint32_t seed) {
00405 dsfmt_chk_init_gen_rand(dsfmt, seed, DSFMT_MEXP);
00406 }
00407
00414 inline static void dsfmt_gv_init_gen_rand(uint32_t seed) {
00415 dsfmt_init_gen_rand(&dsfmt_global_data, seed);
00416 }
00417
00425 inline static void dsfmt_init_by_array(dsfmt_t *dsfmt, uint32_t init_key[],
00426 int key_length) {
00427 dsfmt_chk_init_by_array(dsfmt, init_key, key_length, DSFMT_MEXP);
00428 }
00429
00438 inline static void dsfmt_gv_init_by_array(uint32_t init_key[], int key_length) {
00439 dsfmt_init_by_array(&dsfmt_global_data, init_key, key_length);
00440 }
00441
00442 #if !defined(DSFMT_DO_NOT_USE_OLD_NAMES)
00443 DSFMT_PRE_INLINE const char *get_idstring(void) DSFMT_PST_INLINE;
00444 DSFMT_PRE_INLINE int get_min_array_size(void) DSFMT_PST_INLINE;
00445 DSFMT_PRE_INLINE void init_gen_rand(uint32_t seed) DSFMT_PST_INLINE;
00446 DSFMT_PRE_INLINE void init_by_array(uint32_t init_key[], int key_length)
00447 DSFMT_PST_INLINE;
00448 DSFMT_PRE_INLINE double genrand_close1_open2(void) DSFMT_PST_INLINE;
00449 DSFMT_PRE_INLINE double genrand_close_open(void) DSFMT_PST_INLINE;
00450 DSFMT_PRE_INLINE double genrand_open_close(void) DSFMT_PST_INLINE;
00451 DSFMT_PRE_INLINE double genrand_open_open(void) DSFMT_PST_INLINE;
00452 DSFMT_PRE_INLINE void fill_array_open_close(double array[], int size)
00453 DSFMT_PST_INLINE;
00454 DSFMT_PRE_INLINE void fill_array_close_open(double array[], int size)
00455 DSFMT_PST_INLINE;
00456 DSFMT_PRE_INLINE void fill_array_open_open(double array[], int size)
00457 DSFMT_PST_INLINE;
00458 DSFMT_PRE_INLINE void fill_array_close1_open2(double array[], int size)
00459 DSFMT_PST_INLINE;
00460
00466 inline static const char *get_idstring(void) {
00467 return dsfmt_get_idstring();
00468 }
00469
00475 inline static int get_min_array_size(void) {
00476 return dsfmt_get_min_array_size();
00477 }
00478
00484 inline static void init_gen_rand(uint32_t seed) {
00485 dsfmt_gv_init_gen_rand(seed);
00486 }
00487
00494 inline static void init_by_array(uint32_t init_key[], int key_length) {
00495 dsfmt_gv_init_by_array(init_key, key_length);
00496 }
00497
00504 inline static double genrand_close1_open2(void) {
00505 return dsfmt_gv_genrand_close1_open2();
00506 }
00507
00514 inline static double genrand_close_open(void) {
00515 return dsfmt_gv_genrand_close_open();
00516 }
00517
00524 inline static double genrand_open_close(void) {
00525 return dsfmt_gv_genrand_open_close();
00526 }
00527
00534 inline static double genrand_open_open(void) {
00535 return dsfmt_gv_genrand_open_open();
00536 }
00537
00547 inline static void fill_array_open_close(double array[], int size) {
00548 dsfmt_gv_fill_array_open_close(array, size);
00549 }
00550
00560 inline static void fill_array_close_open(double array[], int size) {
00561 dsfmt_gv_fill_array_close_open(array, size);
00562 }
00563
00573 inline static void fill_array_open_open(double array[], int size) {
00574 dsfmt_gv_fill_array_open_open(array, size);
00575 }
00576
00585 inline static void fill_array_close1_open2(double array[], int size) {
00586 dsfmt_gv_fill_array_close1_open2(array, size);
00587 }
00588 #endif
00589
00590 #endif