NEURON
isaac64.h
Go to the documentation of this file.
1 /*
2 ------------------------------------------------------------------------------
3 isaac64.h: Definitions for a fast cryptographic random number generator
4 Bob Jenkins, 1996, Public Domain
5 
6 Modified for modularity by Tom Bartol and Rex Kerr
7 
8 Reference:
9 http://burtleburtle.net/bob/rand/isaacafa.html
10 Jenkins, R.J. (1996) ISAAC, in Fast Software Encryption, vol. 1039,
11  ed. Gollmann, D. Spinger-Verlag, Cambridge
12 
13 ------------------------------------------------------------------------------
14 */
15 #ifndef ISAAC64_H
16 #define ISAAC64_H
17 
18 #include <nrnconf.h>
19 #if defined(HAVE_STDINT_H)
20 #include <stdint.h>
21 #endif
22 
23 #define RANDSIZL (4) /* I recommend 8 for crypto, 4 for simulations */
24 #define RANDSIZ (1 << RANDSIZL)
25 #define RANDMAX (2 * RANDSIZ)
26 
27 typedef unsigned long long ub8;
28 #if defined(uint32_t)
29 typedef uint32_t ub4;
30 #else
31 typedef unsigned int ub4;
32 #endif
33 typedef unsigned short int ub2;
34 typedef unsigned char ub1;
35 
36 #define DBL32 (2.3283064365386962890625e-10)
37 #define DBL53 (1.1102230246251565404236316680908203125e-16)
38 #define DBL64 (5.42101086242752217003726400434970855712890625e-20)
39 #define MSK53 0x001FFFFFFFFFFFFFLL
40 
41 struct isaac64_state {
42  int randcnt;
48 };
49 
50 
51 void isaac64_init(struct isaac64_state* rng, ub4 seed);
52 
53 void isaac64_generate(struct isaac64_state* rng);
54 
55 /*
56 ------------------------------------------------------------------------------
57 Macros to get individual random numbers
58 ------------------------------------------------------------------------------
59 */
60 
61 #define isaac64_uint32(rng) \
62  (rng->randcnt > 0 ? (*(((ub4*) (rng->randrsl)) + (rng->randcnt -= 1))) \
63  : (isaac64_generate(rng), \
64  rng->randcnt = RANDMAX - 1, \
65  *(((ub4*) (rng->randrsl)) + rng->randcnt)))
66 
67 #define isaac64_uint64(rng) \
68  (rng->randcnt > 1 ? (*((ub8*) (((ub4*) (rng->randrsl)) + (rng->randcnt -= 2)))) \
69  : (isaac64_generate(rng), \
70  rng->randcnt = RANDMAX - 2, \
71  *((ub8*) (((ub4*) (rng->randrsl)) + rng->randcnt))))
72 
73 #define isaac64_dbl32(rng) \
74  (rng->randcnt > 0 ? (DBL32 * (*(((ub4*) (rng->randrsl)) + (rng->randcnt -= 1)))) \
75  : (isaac64_generate(rng), \
76  rng->randcnt = RANDMAX - 1, \
77  DBL32 * (*(((ub4*) (rng->randrsl)) + rng->randcnt))))
78 
79 #define isaac64_dbl53(rng) \
80  (rng->randcnt > 1 \
81  ? (DBL53 * ((*((ub8*) (((ub4*) (rng->randrsl)) + (rng->randcnt -= 2)))) >> 11)) \
82  : (isaac64_generate(rng), \
83  rng->randcnt = RANDMAX - 2, \
84  DBL64 * ((*((ub8*) (((ub4*) (rng->randrsl)) + rng->randcnt))) >> 11)))
85 
86 #define isaac64_dbl64(rng) \
87  (rng->randcnt > 1 ? (DBL64 * (*((ub8*) (((ub4*) (rng->randrsl)) + (rng->randcnt -= 2))))) \
88  : (isaac64_generate(rng), \
89  rng->randcnt = RANDMAX - 2, \
90  DBL64 * (*((ub8*) (((ub4*) (rng->randrsl)) + rng->randcnt)))))
91 
92 
93 #endif /* ISAAC64_H */
void isaac64_generate(struct isaac64_state *rng)
Definition: isaac64.cpp:57
unsigned short int ub2
Definition: isaac64.h:33
unsigned char ub1
Definition: isaac64.h:34
void isaac64_init(struct isaac64_state *rng, ub4 seed)
Definition: isaac64.cpp:81
#define RANDSIZ
Definition: isaac64.h:24
unsigned int ub4
Definition: isaac64.h:31
unsigned long long ub8
Definition: isaac64.h:27
ub8 mm[RANDSIZ]
Definition: isaac64.h:47
int randcnt
Definition: isaac64.h:42
ub8 randrsl[RANDSIZ]
Definition: isaac64.h:46