• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * This file derives from SFMT 1.3.3
3  * (http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/index.html), which was
4  * released under the terms of the following license:
5  *
6  *   Copyright (c) 2006,2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima
7  *   University. All rights reserved.
8  *
9  *   Redistribution and use in source and binary forms, with or without
10  *   modification, are permitted provided that the following conditions are
11  *   met:
12  *
13  *       * Redistributions of source code must retain the above copyright
14  *         notice, this list of conditions and the following disclaimer.
15  *       * Redistributions in binary form must reproduce the above
16  *         copyright notice, this list of conditions and the following
17  *         disclaimer in the documentation and/or other materials provided
18  *         with the distribution.
19  *       * Neither the name of the Hiroshima University nor the names of
20  *         its contributors may be used to endorse or promote products
21  *         derived from this software without specific prior written
22  *         permission.
23  *
24  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35  */
36 /**
37  * @file SFMT.h
38  *
39  * @brief SIMD oriented Fast Mersenne Twister(SFMT) pseudorandom
40  * number generator
41  *
42  * @author Mutsuo Saito (Hiroshima University)
43  * @author Makoto Matsumoto (Hiroshima University)
44  *
45  * Copyright (C) 2006, 2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima
46  * University. All rights reserved.
47  *
48  * The new BSD License is applied to this software.
49  * see LICENSE.txt
50  *
51  * @note We assume that your system has inttypes.h.  If your system
52  * doesn't have inttypes.h, you have to typedef uint32_t and uint64_t,
53  * and you have to define PRIu64 and PRIx64 in this file as follows:
54  * @verbatim
55  typedef unsigned int uint32_t
56  typedef unsigned long long uint64_t
57  #define PRIu64 "llu"
58  #define PRIx64 "llx"
59 @endverbatim
60  * uint32_t must be exactly 32-bit unsigned integer type (no more, no
61  * less), and uint64_t must be exactly 64-bit unsigned integer type.
62  * PRIu64 and PRIx64 are used for printf function to print 64-bit
63  * unsigned int and 64-bit unsigned int in hexadecimal format.
64  */
65 
66 #ifndef SFMT_H
67 #define SFMT_H
68 
69 typedef struct sfmt_s sfmt_t;
70 
71 uint32_t gen_rand32(sfmt_t *ctx);
72 uint32_t gen_rand32_range(sfmt_t *ctx, uint32_t limit);
73 uint64_t gen_rand64(sfmt_t *ctx);
74 uint64_t gen_rand64_range(sfmt_t *ctx, uint64_t limit);
75 void fill_array32(sfmt_t *ctx, uint32_t *array, int size);
76 void fill_array64(sfmt_t *ctx, uint64_t *array, int size);
77 sfmt_t *init_gen_rand(uint32_t seed);
78 sfmt_t *init_by_array(uint32_t *init_key, int key_length);
79 void fini_gen_rand(sfmt_t *ctx);
80 const char *get_idstring(void);
81 int get_min_array_size32(void);
82 int get_min_array_size64(void);
83 
84 #ifndef JEMALLOC_ENABLE_INLINE
85 double to_real1(uint32_t v);
86 double genrand_real1(sfmt_t *ctx);
87 double to_real2(uint32_t v);
88 double genrand_real2(sfmt_t *ctx);
89 double to_real3(uint32_t v);
90 double genrand_real3(sfmt_t *ctx);
91 double to_res53(uint64_t v);
92 double to_res53_mix(uint32_t x, uint32_t y);
93 double genrand_res53(sfmt_t *ctx);
94 double genrand_res53_mix(sfmt_t *ctx);
95 #endif
96 
97 #if (defined(JEMALLOC_ENABLE_INLINE) || defined(SFMT_C_))
98 /* These real versions are due to Isaku Wada */
99 /** generates a random number on [0,1]-real-interval */
to_real1(uint32_t v)100 JEMALLOC_INLINE double to_real1(uint32_t v)
101 {
102     return v * (1.0/4294967295.0);
103     /* divided by 2^32-1 */
104 }
105 
106 /** generates a random number on [0,1]-real-interval */
genrand_real1(sfmt_t * ctx)107 JEMALLOC_INLINE double genrand_real1(sfmt_t *ctx)
108 {
109     return to_real1(gen_rand32(ctx));
110 }
111 
112 /** generates a random number on [0,1)-real-interval */
to_real2(uint32_t v)113 JEMALLOC_INLINE double to_real2(uint32_t v)
114 {
115     return v * (1.0/4294967296.0);
116     /* divided by 2^32 */
117 }
118 
119 /** generates a random number on [0,1)-real-interval */
genrand_real2(sfmt_t * ctx)120 JEMALLOC_INLINE double genrand_real2(sfmt_t *ctx)
121 {
122     return to_real2(gen_rand32(ctx));
123 }
124 
125 /** generates a random number on (0,1)-real-interval */
to_real3(uint32_t v)126 JEMALLOC_INLINE double to_real3(uint32_t v)
127 {
128     return (((double)v) + 0.5)*(1.0/4294967296.0);
129     /* divided by 2^32 */
130 }
131 
132 /** generates a random number on (0,1)-real-interval */
genrand_real3(sfmt_t * ctx)133 JEMALLOC_INLINE double genrand_real3(sfmt_t *ctx)
134 {
135     return to_real3(gen_rand32(ctx));
136 }
137 /** These real versions are due to Isaku Wada */
138 
139 /** generates a random number on [0,1) with 53-bit resolution*/
to_res53(uint64_t v)140 JEMALLOC_INLINE double to_res53(uint64_t v)
141 {
142     return v * (1.0/18446744073709551616.0L);
143 }
144 
145 /** generates a random number on [0,1) with 53-bit resolution from two
146  * 32 bit integers */
to_res53_mix(uint32_t x,uint32_t y)147 JEMALLOC_INLINE double to_res53_mix(uint32_t x, uint32_t y)
148 {
149     return to_res53(x | ((uint64_t)y << 32));
150 }
151 
152 /** generates a random number on [0,1) with 53-bit resolution
153  */
genrand_res53(sfmt_t * ctx)154 JEMALLOC_INLINE double genrand_res53(sfmt_t *ctx)
155 {
156     return to_res53(gen_rand64(ctx));
157 }
158 
159 /** generates a random number on [0,1) with 53-bit resolution
160     using 32bit integer.
161  */
genrand_res53_mix(sfmt_t * ctx)162 JEMALLOC_INLINE double genrand_res53_mix(sfmt_t *ctx)
163 {
164     uint32_t x, y;
165 
166     x = gen_rand32(ctx);
167     y = gen_rand32(ctx);
168     return to_res53_mix(x, y);
169 }
170 #endif
171 #endif
172