vSMC
vSMC: Scalable Monte Carlo
normal01.h
Go to the documentation of this file.
1 //============================================================================
2 // vSMC/include/vsmc/rng/normal01.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_NORMAL01_H
33 #define VSMC_RNG_NORMAL01_H
34 
76 
78 #include <vsmc/rng/urng.h>
79 #include <vsmc/rng/u01.h>
80 
81 #if VSMC_HAS_OPENCL_DOUBLE
82 
83 #define NORMAL01_2x32 normal01_2x32_53
84 #define NORMAL01_2x32_INIT normal01_2x32_53_init
85 #define NORMAL01_2x32_RAND normal01_2x32_53_rand
86 
87 #define NORMAL01_2x64 normal01_2x64_53
88 #define NORMAL01_2x64_INIT normal01_2x64_53_init
89 #define NORMAL01_2x64_RAND normal01_2x64_53_rand
90 
91 #define NORMAL01_4x32 normal01_4x32_53
92 #define NORMAL01_4x32_INIT normal01_4x32_53_init
93 #define NORMAL01_4x32_RAND normal01_4x32_53_rand
94 
95 #define NORMAL01_4x64 normal01_4x64_53
96 #define NORMAL01_4x64_INIT normal01_4x64_53_init
97 #define NORMAL01_4x64_RAND normal01_4x64_53_rand
98 
99 #else // VSMC_HAS_OPENCL_DOUBLE
100 
101 #define NORMAL01_2x32 normal01_2x32_24
102 #define NORMAL01_2x32_INIT normal01_2x32_24_init
103 #define NORMAL01_2x32_RAND normal01_2x32_24_rand
104 
105 #define NORMAL01_4x32 normal01_4x32_24
106 #define NORMAL01_4x32_INIT normal01_4x32_24_init
107 #define NORMAL01_4x32_RAND normal01_4x32_24_rand
108 
109 #endif // VSMC_HAS_OPENCL_DOUBLE
110 
111 #define VSMC_DEFINE_RNG_NORMAL01(N, W, F, FT) \
112  typedef struct { \
113  FT u1; \
114  FT u2; \
115  unsigned char saved; \
116  } normal01_##N##x##W##_##F;
117 
118 #define VSMC_DEFINE_RNG_NORMAL01_INIT(N, W, F, FT) \
119  VSMC_STATIC_INLINE void normal01_##N##x##W##_##F##_init ( \
120  normal01_##N##x##W##_##F *rnorm, cburng##N##x##W##_rng_t *rng) \
121  { \
122  rnorm->u1 = u01_open_closed_##W##_##F(cburng##N##x##W##_rand(rng)); \
123  rnorm->u2 = u01_open_closed_##W##_##F(cburng##N##x##W##_rand(rng)); \
124  rnorm->saved = 1; \
125  }
126 
127 #define VSMC_DEFINE_RNG_NORMAL01_RAND(N, W, F, FT) \
128  VSMC_STATIC_INLINE FT normal01_##N##x##W##_##F##_rand ( \
129  normal01_##N##x##W##_##F *rnorm, cburng##N##x##W##_rng_t *rng) \
130  { \
131  const FT c_2pi = 6.2831853071795865; \
132  if (rnorm->saved) { \
133  rnorm->saved = 0; \
134  return sqrt(-2 * log(rnorm->u1)) * cos(c_2pi * rnorm->u2); \
135  } else { \
136  normal01_##N##x##W##_##F##_init(rnorm, rng); \
137  return sqrt(-2 * log(rnorm->u1)) * sin(c_2pi * rnorm->u2); \
138  } \
139  }
140 
142 VSMC_DEFINE_RNG_NORMAL01(2, 32, 24, float)
144 VSMC_DEFINE_RNG_NORMAL01(4, 32, 24, float)
145 
150 
155 
156 #if VSMC_HAS_OPENCL_DOUBLE
157 
159 VSMC_DEFINE_RNG_NORMAL01(2, 32, 53, double)
161 VSMC_DEFINE_RNG_NORMAL01(4, 32, 53, double)
162 
167 
172 
174 VSMC_DEFINE_RNG_NORMAL01(2, 64, 53, double)
176 VSMC_DEFINE_RNG_NORMAL01(4, 64, 53, double)
177 
182 
187 
188 #endif // VSMC_HAS_OPENCL_DOUBLE
189 
190 #endif // VSMC_RNG_NORMAL01_H
#define VSMC_DEFINE_RNG_NORMAL01_RAND(N, W, F, FT)
Definition: normal01.h:127
#define VSMC_DEFINE_RNG_NORMAL01(N, W, F, FT)
Definition: normal01.h:111
#define VSMC_DEFINE_RNG_NORMAL01_INIT(N, W, F, FT)
Definition: normal01.h:118