Project Alice
Loading...
Searching...
No Matches
gsl_microrng.h
Go to the documentation of this file.
1/*
2Copyright 2010-2011, D. E. Shaw Research.
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions are
7met:
8
9* Redistributions of source code must retain the above copyright
10 notice, this list of conditions, and the following disclaimer.
11
12* Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions, and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
15
16* Neither the name of D. E. Shaw Research nor the names of its
17 contributors may be used to endorse or promote products derived from
18 this software without specific prior written permission.
19
20THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31*/
32#ifndef __r123_gslmicrorng_dot_h__
33#define __r123_gslmicrorng_dot_h__
34
35#include <gsl/gsl_rng.h>
36#include <string.h>
37
78#define GSL_MICRORNG(NAME, CBRNGNAME) \
79 gsl_rng_type const* gsl_rng_##NAME; \
80 \
81 typedef struct { \
82 CBRNGNAME##_ctr_t ctr; \
83 CBRNGNAME##_ctr_t r; \
84 CBRNGNAME##_key_t key; \
85 R123_ULONG_LONG n; \
86 int elem; \
87 } NAME##_state; \
88 \
89 static unsigned long int NAME##_get(void* vstate) { \
90 NAME##_state* st = (NAME##_state*)vstate; \
91 int const N = sizeof(st->ctr.v) / sizeof(st->ctr.v[0]); \
92 if(st->elem == 0) { \
93 CBRNGNAME##_ctr_t c = st->ctr; \
94 c.v[N - 1] |= st->n << (R123_W(CBRNGNAME##_ctr_t) - 32); \
95 st->n++; \
96 st->r = CBRNGNAME(c, st->key); \
97 st->elem = N; \
98 } \
99 return 0xffffffff & st->r.v[--st->elem]; \
100 } \
101 \
102 static double NAME##_get_double(void* vstate) { return NAME##_get(vstate) / 4294967296.; } \
103 \
104 static void NAME##_set(void* vstate, unsigned long int s) { \
105 NAME##_state* st = (NAME##_state*)vstate; \
106 (void)s; /* ignored */ \
107 st->elem = 0; \
108 st->n = ~0; /* will abort if _reset is not called */ \
109 } \
110 \
111 static const gsl_rng_type NAME##_type = {#NAME, 0xffffffffUL, 0, sizeof(NAME##_state), &NAME##_set, &NAME##_get, &NAME##_get_double}; \
112 \
113 R123_STATIC_INLINE void NAME##_reset(gsl_rng const* gr, CBRNGNAME##_ctr_t c, CBRNGNAME##_key_t k) { \
114 NAME##_state* state = (NAME##_state*)gr->state; \
115 state->ctr = c; \
116 state->key = k; \
117 state->n = 0; \
118 state->elem = 0; \
119 } \
120 \
121 gsl_rng_type const* gsl_rng_##NAME = &NAME##_type
122
123#endif