vSMC
vSMC: Scalable Monte Carlo
u01.h
Go to the documentation of this file.
1 //============================================================================
2 // vSMC/include/vsmc/rng/u01.h
3 //----------------------------------------------------------------------------
4 // vSMC: Scalable Monte Carlo
5 //----------------------------------------------------------------------------
6 // Copyright (c) 2013,2014, Yan Zhou
7 // 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 met:
11 //
12 // Redistributions of source code must retain the above copyright notice,
13 // this list of conditions and the following disclaimer.
14 //
15 // Redistributions in binary form must reproduce the above copyright notice,
16 // this list of conditions and the following disclaimer in the documentation
17 // and/or other materials provided with the distribution.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS
20 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 // POSSIBILITY OF SUCH DAMAGE.
30 //============================================================================
31 
32 #ifndef VSMC_RNG_U01_H
33 #define VSMC_RNG_U01_H
34 
69 
71 
72 #if VSMC_HAS_OPENCL_DOUBLE
73 
74 #define U01_OPEN_OPEN_32 u01_open_open_32_53
75 #define U01_OPEN_CLOSED_32 u01_open_closed_32_53
76 #define U01_CLOSED_OPEN_32 u01_closed_closed_32_53
77 #define U01_CLOSED_CLOSED_32 u01_closed_closed_32_53
78 
79 #define U01_OPEN_OPEN_64 u01_open_open_64_53
80 #define U01_OPEN_CLOSED_64 u01_open_closed_64_53
81 #define U01_CLOSED_OPEN_64 u01_closed_closed_64_53
82 #define U01_CLOSED_CLOSED_64 u01_closed_closed_64_53
83 
84 #else // VSMC_HAS_OPENCL_DOUBLE
85 
86 #define U01_OPEN_OPEN_32 u01_open_open_32_24
87 #define U01_OPEN_CLOSED_32 u01_open_closed_32_24
88 #define U01_CLOSED_OPEN_32 u01_closed_closed_32_24
89 #define U01_CLOSED_CLOSED_32 u01_closed_closed_32_24
90 
91 #endif // VSMC_HAS_OPENCL_DOUBLE
92 
93 #define VSMC_RNG_U01_31f (1.0f / (1024.0f * 1024.0f * 1024.0f * 2.0f))
94 #define VSMC_RNG_U01_24f (128.0f * VSMC_RNG_U01_31f)
95 #define VSMC_RNG_U01_23f (256.0f * VSMC_RNG_U01_31f)
96 #define VSMC_RNG_U01_32 (1.0 / (1024.0 *1024.0 * 1024.0 * 4.0))
97 #define VSMC_RNG_U01_63 (2.0 * VSMC_RNG_U01_32 * VSMC_RNG_U01_32)
98 #define VSMC_RNG_U01_53 (1024.0 * VSMC_RNG_U01_63)
99 #define VSMC_RNG_U01_52 (2048.0 * VSMC_RNG_U01_63)
100 
104 {return ((u & 0x7fffffc0) + (u & 0x40)) * VSMC_RNG_U01_31f;}
105 
109 {return (u >> 8) * VSMC_RNG_U01_24f;}
110 
114 {return (1.0f + (u >> 8)) * VSMC_RNG_U01_24f;}
115 
119 {return (0.5f + (u >> 9)) * VSMC_RNG_U01_23f;}
120 
121 #if VSMC_HAS_OPENCL_DOUBLE
122 
126 {
127 #ifdef __cplusplus
128  return (static_cast<double>(u & 1) + u) * VSMC_RNG_U01_32;
129 #else
130  return (((double)(u & 1)) + u) * VSMC_RNG_U01_32;
131 #endif
132 }
133 
137 {return u * VSMC_RNG_U01_32;}
138 
142 {return (1.0 + u) * VSMC_RNG_U01_32;}
143 
147 {return (0.5 + u) * VSMC_RNG_U01_32;}
148 
152 {return ((u & UINT64_C(0x7ffffffffffffe00)) + (u & 0x200)) * VSMC_RNG_U01_63;}
153 
157 {return (u >> 11) * VSMC_RNG_U01_53;}
158 
162 {return (1.0 + (u >> 11)) * VSMC_RNG_U01_53;}
163 
167 {return (0.5 + (u >> 12)) * VSMC_RNG_U01_52;}
168 
172 {
173 #ifdef __cplusplus
174  return static_cast<float>(u01_closed_closed_64_53(u));
175 #else
176  return (float) u01_closed_closed_64_53(u);
177 #endif
178 }
179 
183 {
184 #ifdef __cplusplus
185  return static_cast<float>(u01_closed_open_64_53(u));
186 #else
187  return (float) u01_closed_open_64_53(u);
188 #endif
189 }
190 
194 {
195 #ifdef __cplusplus
196  return static_cast<float>(u01_open_closed_64_53(u));
197 #else
198  return (float) u01_open_closed_64_53(u);
199 #endif
200 }
201 
205 {
206 #ifdef __cplusplus
207  return static_cast<float>(u01_open_open_64_53(u));
208 #else
209  return (float) u01_open_open_64_53(u);
210 #endif
211 }
212 
213 #endif // VSMC_HAS_OPENCL_DOUBLE
214 
215 #endif // VSMC_RNG_U01_H
#define VSMC_RNG_U01_32
Definition: u01.h:96
static float u01_closed_closed_32_24(uint32_t u)
Converting 32-bits unsigned to single precision uniform .
Definition: u01.h:103
#define VSMC_RNG_U01_23f
Definition: u01.h:95
static float u01_open_open_64_24(uint64_t u)
Converting 64-bits unsigned to single precision uniform .
Definition: u01.h:204
#define VSMC_RNG_U01_52
Definition: u01.h:99
static float u01_closed_closed_64_24(uint64_t u)
Converting 64-bits unsigned to single precision uniform .
Definition: u01.h:171
static double u01_closed_open_32_53(uint32_t u)
Converting 32-bits unsigned to double precision uniform .
Definition: u01.h:136
#define VSMC_RNG_U01_53
Definition: u01.h:98
static float u01_closed_open_32_24(uint32_t u)
Converting 32-bits unsigned to single precision uniform .
Definition: u01.h:108
static double u01_open_open_32_53(uint32_t u)
Converting 32-bits unsigned to double precision uniform .
Definition: u01.h:146
static float u01_open_closed_64_24(uint64_t u)
Converting 64-bits unsigned to single precision uniform .
Definition: u01.h:193
#define VSMC_RNG_U01_63
Definition: u01.h:97
#define VSMC_RNG_U01_31f
Definition: u01.h:93
static double u01_open_closed_32_53(uint32_t u)
Converting 32-bits unsigned to double precision uniform .
Definition: u01.h:141
static float u01_open_closed_32_24(uint32_t u)
Converting 32-bits unsigned to single precision uniform .
Definition: u01.h:113
static float u01_closed_open_64_24(uint64_t u)
Converting 64-bits unsigned to single precision uniform .
Definition: u01.h:182
#define VSMC_STATIC_INLINE
Definition: common.h:70
static double u01_closed_closed_32_53(uint32_t u)
Converting 32-bits unsigned to double precision uniform .
Definition: u01.h:125
static double u01_open_closed_64_53(uint64_t u)
Converting 64-bits unsigned to double precision uniform .
Definition: u01.h:161
static double u01_open_open_64_53(uint64_t u)
Converting 64-bits unsigned to double precision uniform .
Definition: u01.h:166
static float u01_open_open_32_24(uint32_t u)
Converting 32-bits unsigned to single precision uniform .
Definition: u01.h:118
static double u01_closed_closed_64_53(uint64_t u)
Converting 64-bits unsigned to double precision uniform .
Definition: u01.h:151
#define VSMC_RNG_U01_24f
Definition: u01.h:94
static double u01_closed_open_64_53(uint64_t u)
Converting 64-bits unsigned to double precision uniform .
Definition: u01.h:156