annotate src/SFMT/SFMT.h @ 274:210b465e4720

refactored walking code from buy-initial-objects to its own function.
author Robert McIntyre <rlm@mit.edu>
date Tue, 27 Mar 2012 11:30:58 -0500
parents f9f4f1b99eed
children
rev   line source
rlm@1 1 /**
rlm@1 2 * @file SFMT.h
rlm@1 3 *
rlm@1 4 * @brief SIMD oriented Fast Mersenne Twister(SFMT) pseudorandom
rlm@1 5 * number generator
rlm@1 6 *
rlm@1 7 * @author Mutsuo Saito (Hiroshima University)
rlm@1 8 * @author Makoto Matsumoto (Hiroshima University)
rlm@1 9 *
rlm@1 10 * Copyright (C) 2006, 2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima
rlm@1 11 * University. All rights reserved.
rlm@1 12 *
rlm@1 13 * The new BSD License is applied to this software.
rlm@1 14 * see LICENSE.txt
rlm@1 15 *
rlm@1 16 * @note We assume that your system has inttypes.h. If your system
rlm@1 17 * doesn't have inttypes.h, you have to typedef uint32_t and uint64_t,
rlm@1 18 * and you have to define PRIu64 and PRIx64 in this file as follows:
rlm@1 19 * @verbatim
rlm@1 20 typedef unsigned int uint32_t
rlm@1 21 typedef unsigned long long uint64_t
rlm@1 22 #define PRIu64 "llu"
rlm@1 23 #define PRIx64 "llx"
rlm@1 24 @endverbatim
rlm@1 25 * uint32_t must be exactly 32-bit unsigned integer type (no more, no
rlm@1 26 * less), and uint64_t must be exactly 64-bit unsigned integer type.
rlm@1 27 * PRIu64 and PRIx64 are used for printf function to print 64-bit
rlm@1 28 * unsigned int and 64-bit unsigned int in hexadecimal format.
rlm@1 29 */
rlm@1 30
rlm@1 31 #ifndef SFMT_H
rlm@1 32 #define SFMT_H
rlm@1 33
rlm@1 34 #include <stdio.h>
rlm@1 35
rlm@1 36 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
rlm@1 37 #include <inttypes.h>
rlm@1 38 #elif defined(_MSC_VER) || defined(__BORLANDC__)
rlm@1 39 typedef unsigned int uint32_t;
rlm@1 40 typedef unsigned __int64 uint64_t;
rlm@1 41 #define inline __inline
rlm@1 42 #else
rlm@1 43 #include <inttypes.h>
rlm@1 44 #if defined(__GNUC__)
rlm@1 45 #define inline __inline__
rlm@1 46 #endif
rlm@1 47 #endif
rlm@1 48
rlm@1 49 #ifndef PRIu64
rlm@1 50 #if defined(_MSC_VER) || defined(__BORLANDC__)
rlm@1 51 #define PRIu64 "I64u"
rlm@1 52 #define PRIx64 "I64x"
rlm@1 53 #else
rlm@1 54 #define PRIu64 "llu"
rlm@1 55 #define PRIx64 "llx"
rlm@1 56 #endif
rlm@1 57 #endif
rlm@1 58
rlm@1 59 #if defined(__GNUC__)
rlm@1 60 #define ALWAYSINLINE __attribute__((always_inline))
rlm@1 61 #else
rlm@1 62 #define ALWAYSINLINE
rlm@1 63 #endif
rlm@1 64
rlm@1 65 #if defined(_MSC_VER)
rlm@1 66 #if _MSC_VER >= 1200
rlm@1 67 #define PRE_ALWAYS __forceinline
rlm@1 68 #else
rlm@1 69 #define PRE_ALWAYS inline
rlm@1 70 #endif
rlm@1 71 #else
rlm@1 72 #define PRE_ALWAYS inline
rlm@1 73 #endif
rlm@1 74
rlm@1 75 #if defined(__cplusplus) || defined(c_plusplus)
rlm@1 76 //extern "C" {
rlm@1 77 #endif
rlm@1 78
rlm@1 79 uint32_t gen_rand32(void);
rlm@1 80 uint64_t gen_rand64(void);
rlm@1 81 void fill_array32(uint32_t *array, int size);
rlm@1 82 void fill_array64(uint64_t *array, int size);
rlm@1 83 void init_gen_rand(uint32_t seed);
rlm@1 84 void init_by_array(uint32_t *init_key, int key_length);
rlm@1 85 const char *get_idstring(void);
rlm@1 86 int get_min_array_size32(void);
rlm@1 87 int get_min_array_size64(void);
rlm@1 88
rlm@1 89 #if defined(__cplusplus) || defined(c_plusplus)
rlm@1 90 //} // end extern
rlm@1 91 #endif
rlm@1 92
rlm@1 93 /* These real versions are due to Isaku Wada */
rlm@1 94 /** generates a random number on [0,1]-real-interval */
rlm@1 95 inline static double to_real1(uint32_t v)
rlm@1 96 {
rlm@1 97 return v * (1.0/4294967295.0);
rlm@1 98 /* divided by 2^32-1 */
rlm@1 99 }
rlm@1 100
rlm@1 101 /** generates a random number on [0,1]-real-interval */
rlm@1 102 inline static double genrand_real1(void)
rlm@1 103 {
rlm@1 104 return to_real1(gen_rand32());
rlm@1 105 }
rlm@1 106
rlm@1 107 /** generates a random number on [0,1)-real-interval */
rlm@1 108 inline static double to_real2(uint32_t v)
rlm@1 109 {
rlm@1 110 return v * (1.0/4294967296.0);
rlm@1 111 /* divided by 2^32 */
rlm@1 112 }
rlm@1 113
rlm@1 114 /** generates a random number on [0,1)-real-interval */
rlm@1 115 inline static double genrand_real2(void)
rlm@1 116 {
rlm@1 117 return to_real2(gen_rand32());
rlm@1 118 }
rlm@1 119
rlm@1 120 /** generates a random number on (0,1)-real-interval */
rlm@1 121 inline static double to_real3(uint32_t v)
rlm@1 122 {
rlm@1 123 return (((double)v) + 0.5)*(1.0/4294967296.0);
rlm@1 124 /* divided by 2^32 */
rlm@1 125 }
rlm@1 126
rlm@1 127 /** generates a random number on (0,1)-real-interval */
rlm@1 128 inline static double genrand_real3(void)
rlm@1 129 {
rlm@1 130 return to_real3(gen_rand32());
rlm@1 131 }
rlm@1 132 /** These real versions are due to Isaku Wada */
rlm@1 133
rlm@1 134 /** generates a random number on [0,1) with 53-bit resolution*/
rlm@1 135 inline static double to_res53(uint64_t v)
rlm@1 136 {
rlm@1 137 return v * (1.0/18446744073709551616.0L);
rlm@1 138 }
rlm@1 139
rlm@1 140 /** generates a random number on [0,1) with 53-bit resolution from two
rlm@1 141 * 32 bit integers */
rlm@1 142 inline static double to_res53_mix(uint32_t x, uint32_t y)
rlm@1 143 {
rlm@1 144 return to_res53(x | ((uint64_t)y << 32));
rlm@1 145 }
rlm@1 146
rlm@1 147 /** generates a random number on [0,1) with 53-bit resolution
rlm@1 148 */
rlm@1 149 inline static double genrand_res53(void)
rlm@1 150 {
rlm@1 151 return to_res53(gen_rand64());
rlm@1 152 }
rlm@1 153
rlm@1 154 /** generates a random number on [0,1) with 53-bit resolution
rlm@1 155 using 32bit integer.
rlm@1 156 */
rlm@1 157 inline static double genrand_res53_mix(void)
rlm@1 158 {
rlm@1 159 uint32_t x, y;
rlm@1 160
rlm@1 161 x = gen_rand32();
rlm@1 162 y = gen_rand32();
rlm@1 163 return to_res53_mix(x, y);
rlm@1 164 }
rlm@1 165 #endif