vSMC
vSMC: Scalable Monte Carlo
urng.h
Go to the documentation of this file.
1 //============================================================================
2 // vSMC/include/vsmc/rng/urng.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_URNG_H
33 #define VSMC_RNG_URNG_H
34 
174 
176 #include <Random123/threefry.h>
177 #include <Random123/philox.h>
178 
179 #define VSMC_DEFINE_RNG_URNG_RNG_T(RNG, N, W) \
180  typedef struct { \
181  RNG##N##x##W##_key_t key; \
182  RNG##N##x##W##_ctr_t ctr; \
183  RNG##N##x##W##_ctr_t rnd; \
184  unsigned char remain; \
185  } RNG##N##x##W##_rng_t;
186 
187 #define VSMC_DEFINE_RNG_URNG_INIT(RNG, N, W) \
188  VSMC_STATIC_INLINE void RNG##N##x##W##_init( \
189  RNG##N##x##W##_rng_t *rng, uint##W##_t seed) \
190  { \
191  RNG##N##x##W##_ctr_t init_ctr = {{}}; \
192  RNG##N##x##W##_ukey_t ukey = {{}}; \
193  rng->ctr = init_ctr; \
194  rng->rnd = init_ctr; \
195  ukey.v[0] = seed; \
196  rng->key = RNG##N##x##W##keyinit(ukey); \
197  rng->remain = 0; \
198  }
199 
200 #define VSMC_DEFINE_RNG_URNG_RAND(RNG, N, W) \
201  VSMC_STATIC_INLINE uint##W##_t RNG##N##x##W##_rand( \
202  RNG##N##x##W##_rng_t *rng) \
203  { \
204  unsigned char remain = rng->remain; \
205  RNG##N##x##W##_ctr_t rnd = rng->rnd; \
206  \
207  if (remain > 0) { \
208  --remain; \
209  rng->remain = remain; \
210  return rnd.v[remain]; \
211  } \
212  \
213  RNG##N##x##W##_ctr_t ctr = rng->ctr; \
214  RNG##N##x##W##_key_t key = rng->key; \
215  \
216  remain = N - 1; \
217  ctr.v[0]++; \
218  rnd = RNG##N##x##W(ctr, key); \
219  \
220  rng->remain = remain; \
221  rng->rnd = rnd; \
222  rng->ctr = ctr; \
223  rng->key = key; \
224  \
225  return rnd.v[remain]; \
226  }
227 
236 
245 
254 
263 
272 
281 
282 #if VSMC_USE_PHILOX_CBURNG
292 #define cburng2x32_init philox2x32_init
293 #define cburng4x32_init philox4x32_init
295 #define cburng2x64_init philox2x64_init
297 #define cburng4x64_init philox4x64_init
299 #define cburng2x32_rand philox2x32_rand
301 #define cburng4x32_rand philox4x32_rand
303 #define cburng2x64_rand philox2x64_rand
305 #define cburng4x64_rand philox4x64_rand
307 #else // VSMC_USE_PHILOX_CBURNG
317 #define cburng2x32_init threefry2x32_init
318 #define cburng4x32_init threefry4x32_init
320 #define cburng2x64_init threefry2x64_init
322 #define cburng4x64_init threefry4x64_init
324 #define cburng2x32_rand threefry2x32_rand
326 #define cburng4x32_rand threefry4x32_rand
328 #define cburng2x64_rand threefry2x64_rand
330 #define cburng4x64_rand threefry4x64_rand
332 #endif // VSMC_USE_PHILOX_CBURNG
333 
334 #endif // VSMC_RNG_URNG_H
#define VSMC_DEFINE_RNG_URNG_INIT(RNG, N, W)
Definition: urng.h:187
threefry2x32_rng_t cburng2x32_rng_t
Definition: urng.h:309
threefry4x64_rng_t cburng4x64_rng_t
Definition: urng.h:315
threefry2x64_rng_t cburng2x64_rng_t
Definition: urng.h:313
threefry4x32_rng_t cburng4x32_rng_t
Definition: urng.h:311
#define VSMC_DEFINE_RNG_URNG_RNG_T(RNG, N, W)
Definition: urng.h:179
#define VSMC_DEFINE_RNG_URNG_RAND(RNG, N, W)
Definition: urng.h:200