Debian Codegraph @

terps/tads/tads3/vmisaac.cpp

raw
1 /* Copyright (c) 2010 by Michael J. Roberts. All Rights Reserved. */
2 /*
3 Name
4 vmisaac.cpp - T3 VM ISAAC random number generator implementation
5 Function
6
7 Notes
8
9 Modified
10 04/11/10 MJRoberts - Creation
11 */
12
13 #include "vmisaac.h"
14
15 /* service macros for ISAAC random number generator */
16 #define isaac_ind(mm,x) ((mm)[(x>>2)&(ISAAC_RANDSIZ-1)])
17 #define isaac_step(mix,a,b,mm,m,m2,r,x) \
18 { \
19 x = *m; \
20 a = ((a^(mix)) + *(m2++)) & 0xffffffff; \
21 *(m++) = y = (isaac_ind(mm,x) + a + b) & 0xffffffff; \
22 *(r++) = b = (isaac_ind(mm,y>>ISAAC_RANDSIZL) + x) & 0xffffffff; \
23 }
24
25 #define isaac_mix(a,b,c,d,e,f,g,h) \
26 { \
27 a^=b<<11; d+=a; b+=c; \
28 b^=c>>2; e+=b; c+=d; \
29 c^=d<<8; f+=c; d+=e; \
30 d^=e>>16; g+=d; e+=f; \
31 e^=f<<10; h+=e; f+=g; \
32 f^=g>>4; a+=f; g+=h; \
33 g^=h<<8; b+=g; h+=a; \
34 h^=a>>9; c+=h; a+=b; \
35 }
36
37 /* generate the group of numbers */
38 void isaac_gen_group(isaacctx *ctx)
39 {
40 uint32_t a;
41 uint32_t b;
42 uint32_t x;
43 uint32_t y;
44 uint32_t *m;
45 uint32_t *mm;
46 uint32_t *m2;
47 uint32_t *r;
48 uint32_t *mend;
49
50 mm = ctx->mem;
51 r = ctx->rsl;
52 a = ctx->a;
53 b = (ctx->b + (++ctx->c)) & 0xffffffff;
54 for (m = mm, mend = m2 = m + (ISAAC_RANDSIZ/2) ; m<mend ; )
55 {
56 isaac_step(a<<13, a, b, mm, m, m2, r, x);
57 isaac_step(a>>6, a, b, mm, m, m2, r, x);
58 isaac_step(a<<2, a, b, mm, m, m2, r, x);
59 isaac_step(a>>16, a, b, mm, m, m2, r, x);
60 }
61 for (m2 = mm; m2<mend; )
62 {
63 isaac_step(a<<13, a, b, mm, m, m2, r, x);
64 isaac_step(a>>6, a, b, mm, m, m2, r, x);
65 isaac_step(a<<2, a, b, mm, m, m2, r, x);
66 isaac_step(a>>16, a, b, mm, m, m2, r, x);
67 }
68 ctx->b = b;
69 ctx->a = a;
70 }
71
72 /*
73 * Initialize. If flag is true, then use the contents of ctx->rsl[] to
74 * initialize ctx->mm[]; otherwise, we'll use a fixed starting
75 * configuration.
76 */
77 void isaac_init(isaacctx *ctx, int flag)
78 {
79 int i;
80 uint32_t a;
81 uint32_t b;
82 uint32_t c;
83 uint32_t d;
84 uint32_t e;
85 uint32_t f;
86 uint32_t g;
87 uint32_t h;
88 uint32_t *m;
89 uint32_t *r;
90
91 ctx->a = ctx->b = ctx->c = 0;
92 m = ctx->mem;
93 r = ctx->rsl;
94 a = b = c = d = e = f = g = h = 0x9e3779b9; /* the golden ratio */
95
96 /* scramble the initial settings */
97 for (i = 0 ; i < 4 ; ++i)
98 {
99 isaac_mix(a, b, c, d, e, f, g, h);
100 }
101
102 if (flag)
103 {
104 /* initialize using the contents of ctx->rsl[] as the seed */
105 for (i = 0 ; i < ISAAC_RANDSIZ ; i += 8)
106 {
107 a += r[i]; b += r[i+1]; c += r[i+2]; d += r[i+3];
108 e += r[i+4]; f += r[i+5]; g += r[i+6]; h += r[i+7];
109 isaac_mix(a, b, c, d, e, f, g, h);
110 m[i] = a; m[i+1] = b; m[i+2] = c; m[i+3] = d;
111 m[i+4] = e; m[i+5] = f; m[i+6] = g; m[i+7] = h;
112 }
113
114 /* do a second pass to make all of the seed affect all of m */
115 for (i = 0 ; i < ISAAC_RANDSIZ ; i += 8)
116 {
117 a += m[i]; b += m[i+1]; c += m[i+2]; d += m[i+3];
118 e += m[i+4]; f += m[i+5]; g += m[i+6]; h += m[i+7];
119 isaac_mix(a, b, c, d, e, f, g, h);
120 m[i] = a; m[i+1] = b; m[i+2] = c; m[i+3] = d;
121 m[i+4] = e; m[i+5] = f; m[i+6] = g; m[i+7] = h;
122 }
123 }
124 else
125 {
126 /* initialize using fixed initial settings */
127 for (i = 0 ; i < ISAAC_RANDSIZ ; i += 8)
128 {
129 isaac_mix(a, b, c, d, e, f, g, h);
130 m[i] = a; m[i+1] = b; m[i+2] = c; m[i+3] = d;
131 m[i+4] = e; m[i+5] = f; m[i+6] = g; m[i+7] = h;
132 }
133 }
134
135 /* fill in the first set of results */
136 isaac_gen_group(ctx);
137
138 /* prepare to use the first set of results */
139 ctx->cnt = ISAAC_RANDSIZ;
140 }
141
142 /*
143 * Get the internal state
144 */
145 size_t isaac_get_state(isaacctx *ctx, char *buf)
146 {
147 /* if the didn't provide a buffer, it's a size-needed query */
148 const size_t sz = 4 * (4 + ISAAC_RANDSIZ + ISAAC_RANDSIZ);
149 if (buf == 0)
150 return sz;
151
152 /* copy the scalar members */
153 oswp4(buf, ctx->cnt); buf += 4;
154 oswp4(buf, ctx->a); buf += 4;
155 oswp4(buf, ctx->b); buf += 4;
156 oswp4(buf, ctx->c); buf += 4;
157
158 /* copy the array members */
159 for (int i = 0 ; i < ISAAC_RANDSIZ ; ++i, buf += 8)
160 {
161 oswp4(buf, ctx->rsl[i]);
162 oswp4(buf+4, ctx->mem[i]);
163 }
164
165 return sz;
166 }
167
168 /*
169 * Set the internal state
170 */
171 void isaac_set_state(isaacctx *ctx, const char *buf)
172 {
173 /* decode the buffer into our context */
174 ctx->cnt = osrp4(buf); buf += 4;
175 ctx->a = osrp4(buf); buf += 4;
176 ctx->b = osrp4(buf); buf += 4;
177 ctx->c = osrp4(buf); buf += 4;
178 for (int i = 0 ; i < ISAAC_RANDSIZ ; ++i, buf += 8)
179 {
180 ctx->rsl[i] = osrp4(buf);
181 ctx->mem[i] = osrp4(buf+4);
182 }
183
184 /* sanity check the result: make sure 'cnt' is in range */
185 if (ctx->cnt > ISAAC_RANDSIZ)
186 ctx->cnt = 0;
187 }