Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifndef __r123_gslmicrorng_dot_h__
00033 #define __r123_gslmicrorng_dot_h__
00034
00035
00036 #include <gsl/gsl_rng.h>
00037 #include <string.h>
00038
00079 #define GSL_MICRORNG(NAME, CBRNGNAME, BITS) \
00080 const gsl_rng_type *gsl_rng_##NAME; \
00081 \
00082 typedef struct{ \
00083 CBRNGNAME##_ctr_t ctr; \
00084 CBRNGNAME##_ctr_t r; \
00085 CBRNGNAME##_key_t key; \
00086 R123_ULONG_LONG n; \
00087 int elem; \
00088 } NAME##_state; \
00089 \
00090 static unsigned long int NAME##_get(void *vstate){ \
00091 NAME##_state *st = (NAME##_state *)vstate; \
00092 const int N=sizeof(st->ctr.v)/sizeof(st->ctr.v[0]); \
00093 if( st->elem == 0 ){ \
00094 CBRNGNAME##_ctr_t c = st->ctr; \
00095 if( st->n >= (1U<<BITS) ) \
00096 abort(); \
00097 c.v[N-1] |= st->n<<(R123_W(CBRNGNAME##_ctr_t)-BITS); \
00098 st->n++; \
00099 st->r = CBRNGNAME(c, st->key); \
00100 st->elem = N; \
00101 } \
00102 return st->r.v[--st->elem]; \
00103 } \
00104 \
00105 static double NAME##_get_double (void * vstate); \
00106 \
00107 static void NAME##_set(void *vstate, unsigned long int s){ \
00108 NAME##_state *st = (NAME##_state *)vstate; \
00109 st->elem = 0; \
00110 st->n = ~0; \
00111 } \
00112 \
00113 static const gsl_rng_type NAME##_type = { \
00114 #NAME, \
00115 ~0UL>>((R123_W(CBRNGNAME##_ctr_t)>=8*sizeof(unsigned long))? 0 : (8*sizeof(unsigned long) - R123_W(CBRNGNAME##_ctr_t))), \
00116 0, \
00117 sizeof(NAME##_state), \
00118 &NAME##_set, \
00119 &NAME##_get, \
00120 &NAME##_get_double \
00121 }; \
00122 \
00123 static double \
00124 NAME##_get_double (void * vstate) \
00125 { \
00126 return NAME##_get (vstate)/(double)NAME##_type.max; \
00127 } \
00128 \
00129 R123_STATIC_INLINE void NAME##_reset(const gsl_rng* gr, CBRNGNAME##_ctr_t c, CBRNGNAME##_key_t k) { \
00130 NAME##_state* state = (NAME##_state *)gr->state; \
00131 state->ctr = c; \
00132 state->key = k; \
00133 state->n = 0; \
00134 state->elem = 0; \
00135 } \
00136 \
00137 const gsl_rng_type *gsl_rng_##NAME = &NAME##_type
00138
00139 #endif