diff src/SFMT/SFMT.h @ 23:bf9169ad4222

add SMID-oriented fast mersenne twister
author Robert McIntyre <rlm@mit.edu>
date Sun, 04 Mar 2012 17:38:32 -0600
parents f9f4f1b99eed
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/SFMT/SFMT.h	Sun Mar 04 17:38:32 2012 -0600
     1.3 @@ -0,0 +1,165 @@
     1.4 +/** 
     1.5 + * @file SFMT.h 
     1.6 + *
     1.7 + * @brief SIMD oriented Fast Mersenne Twister(SFMT) pseudorandom
     1.8 + * number generator
     1.9 + *
    1.10 + * @author Mutsuo Saito (Hiroshima University)
    1.11 + * @author Makoto Matsumoto (Hiroshima University)
    1.12 + *
    1.13 + * Copyright (C) 2006, 2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima
    1.14 + * University. All rights reserved.
    1.15 + *
    1.16 + * The new BSD License is applied to this software.
    1.17 + * see LICENSE.txt
    1.18 + *
    1.19 + * @note We assume that your system has inttypes.h.  If your system
    1.20 + * doesn't have inttypes.h, you have to typedef uint32_t and uint64_t,
    1.21 + * and you have to define PRIu64 and PRIx64 in this file as follows:
    1.22 + * @verbatim
    1.23 + typedef unsigned int uint32_t
    1.24 + typedef unsigned long long uint64_t  
    1.25 + #define PRIu64 "llu"
    1.26 + #define PRIx64 "llx"
    1.27 +@endverbatim
    1.28 + * uint32_t must be exactly 32-bit unsigned integer type (no more, no
    1.29 + * less), and uint64_t must be exactly 64-bit unsigned integer type.
    1.30 + * PRIu64 and PRIx64 are used for printf function to print 64-bit
    1.31 + * unsigned int and 64-bit unsigned int in hexadecimal format.
    1.32 + */
    1.33 +
    1.34 +#ifndef SFMT_H
    1.35 +#define SFMT_H
    1.36 +
    1.37 +#include <stdio.h>
    1.38 +
    1.39 +#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
    1.40 +  #include <inttypes.h>
    1.41 +#elif defined(_MSC_VER) || defined(__BORLANDC__)
    1.42 +  typedef unsigned int uint32_t;
    1.43 +  typedef unsigned __int64 uint64_t;
    1.44 +  #define inline __inline
    1.45 +#else
    1.46 +  #include <inttypes.h>
    1.47 +  #if defined(__GNUC__)
    1.48 +    #define inline __inline__
    1.49 +  #endif
    1.50 +#endif
    1.51 +
    1.52 +#ifndef PRIu64
    1.53 +  #if defined(_MSC_VER) || defined(__BORLANDC__)
    1.54 +    #define PRIu64 "I64u"
    1.55 +    #define PRIx64 "I64x"
    1.56 +  #else
    1.57 +    #define PRIu64 "llu"
    1.58 +    #define PRIx64 "llx"
    1.59 +  #endif
    1.60 +#endif
    1.61 +
    1.62 +#if defined(__GNUC__)
    1.63 +#define ALWAYSINLINE __attribute__((always_inline))
    1.64 +#else
    1.65 +#define ALWAYSINLINE
    1.66 +#endif
    1.67 +
    1.68 +#if defined(_MSC_VER)
    1.69 +  #if _MSC_VER >= 1200
    1.70 +    #define PRE_ALWAYS __forceinline
    1.71 +  #else
    1.72 +    #define PRE_ALWAYS inline
    1.73 +  #endif
    1.74 +#else
    1.75 +  #define PRE_ALWAYS inline
    1.76 +#endif
    1.77 +
    1.78 +#if defined(__cplusplus) || defined(c_plusplus)
    1.79 +//extern "C" {
    1.80 +#endif
    1.81 +
    1.82 +uint32_t gen_rand32(void);
    1.83 +uint64_t gen_rand64(void);
    1.84 +void fill_array32(uint32_t *array, int size);
    1.85 +void fill_array64(uint64_t *array, int size);
    1.86 +void init_gen_rand(uint32_t seed);
    1.87 +void init_by_array(uint32_t *init_key, int key_length);
    1.88 +const char *get_idstring(void);
    1.89 +int get_min_array_size32(void);
    1.90 +int get_min_array_size64(void);
    1.91 +
    1.92 +#if defined(__cplusplus) || defined(c_plusplus)
    1.93 +//} // end extern
    1.94 +#endif
    1.95 +
    1.96 +/* These real versions are due to Isaku Wada */
    1.97 +/** generates a random number on [0,1]-real-interval */
    1.98 +inline static double to_real1(uint32_t v)
    1.99 +{
   1.100 +    return v * (1.0/4294967295.0); 
   1.101 +    /* divided by 2^32-1 */ 
   1.102 +}
   1.103 +
   1.104 +/** generates a random number on [0,1]-real-interval */
   1.105 +inline static double genrand_real1(void)
   1.106 +{
   1.107 +    return to_real1(gen_rand32());
   1.108 +}
   1.109 +
   1.110 +/** generates a random number on [0,1)-real-interval */
   1.111 +inline static double to_real2(uint32_t v)
   1.112 +{
   1.113 +    return v * (1.0/4294967296.0); 
   1.114 +    /* divided by 2^32 */
   1.115 +}
   1.116 +
   1.117 +/** generates a random number on [0,1)-real-interval */
   1.118 +inline static double genrand_real2(void)
   1.119 +{
   1.120 +    return to_real2(gen_rand32());
   1.121 +}
   1.122 +
   1.123 +/** generates a random number on (0,1)-real-interval */
   1.124 +inline static double to_real3(uint32_t v)
   1.125 +{
   1.126 +    return (((double)v) + 0.5)*(1.0/4294967296.0); 
   1.127 +    /* divided by 2^32 */
   1.128 +}
   1.129 +
   1.130 +/** generates a random number on (0,1)-real-interval */
   1.131 +inline static double genrand_real3(void)
   1.132 +{
   1.133 +    return to_real3(gen_rand32());
   1.134 +}
   1.135 +/** These real versions are due to Isaku Wada */
   1.136 +
   1.137 +/** generates a random number on [0,1) with 53-bit resolution*/
   1.138 +inline static double to_res53(uint64_t v) 
   1.139 +{ 
   1.140 +    return v * (1.0/18446744073709551616.0L);
   1.141 +}
   1.142 +
   1.143 +/** generates a random number on [0,1) with 53-bit resolution from two
   1.144 + * 32 bit integers */
   1.145 +inline static double to_res53_mix(uint32_t x, uint32_t y) 
   1.146 +{ 
   1.147 +    return to_res53(x | ((uint64_t)y << 32));
   1.148 +}
   1.149 +
   1.150 +/** generates a random number on [0,1) with 53-bit resolution
   1.151 + */
   1.152 +inline static double genrand_res53(void) 
   1.153 +{ 
   1.154 +    return to_res53(gen_rand64());
   1.155 +} 
   1.156 +
   1.157 +/** generates a random number on [0,1) with 53-bit resolution
   1.158 +    using 32bit integer.
   1.159 + */
   1.160 +inline static double genrand_res53_mix(void) 
   1.161 +{ 
   1.162 +    uint32_t x, y;
   1.163 +
   1.164 +    x = gen_rand32();
   1.165 +    y = gen_rand32();
   1.166 +    return to_res53_mix(x, y);
   1.167 +} 
   1.168 +#endif