2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 ------------------------------------------------------------------------------
17 By Bob Jenkins, 1996, Public Domain
19 960327: Creation (addition of randinit, really)
20 970719: use context, not global variables, for internal state
21 980324: renamed seed to flag
22 980605: recommend ISAAC_LOG2_SIZE=4 for noncryptography.
23 010626: note this is public domain
24 ------------------------------------------------------------------------------
26 Modified for CLIB by Eliot Dresselhaus.
27 Dear Bob, Thanks for all the great work. - Eliot
29 modifications copyright (c) 2003 Eliot Dresselhaus
31 Permission is hereby granted, free of charge, to any person obtaining
32 a copy of this software and associated documentation files (the
33 "Software"), to deal in the Software without restriction, including
34 without limitation the rights to use, copy, modify, merge, publish,
35 distribute, sublicense, and/or sell copies of the Software, and to
36 permit persons to whom the Software is furnished to do so, subject to
37 the following conditions:
39 The above copyright notice and this permission notice shall be
40 included in all copies or substantial portions of the Software.
42 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
43 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
44 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
45 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
46 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
47 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
48 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
51 /* ISAAC is Bob Jenkins' random number generator.
52 http://burtleburtle.net/bob/rand/isaacafa.html */
54 #include <vppinfra/random_isaac.h>
56 #if uword_bits != 32 && uword_bits != 64
57 #error "isaac only works for 32 or 64 bit words"
62 #define ind32(mm,x) (*(u32 *)((u8 *)(mm) + ((x) & ((ISAAC_SIZE-1)<<2))))
63 #define rngstep32(mix,a,b,mm,m,m2,r,x,y) \
66 a = (a^(mix)) + *(m2++); \
67 *(m++) = y = ind32(mm,x) + a + b; \
68 *(r++) = b = ind32(mm,y>>ISAAC_LOG2_SIZE) + x; \
72 isaac (isaac_t * ctx, uword * results)
74 u32 a, b, c, x, y, *m, *mm, *m2, *r, *mend;
83 mend = m2 = mm + ARRAY_LEN (ctx->memory) / 2;
87 rngstep32 (a << 13, a, b, mm, m, m2, r, x, y);
88 rngstep32 (a >> 6, a, b, mm, m, m2, r, x, y);
89 rngstep32 (a << 2, a, b, mm, m, m2, r, x, y);
90 rngstep32 (a >> 16, a, b, mm, m, m2, r, x, y);
96 rngstep32 (a << 13, a, b, mm, m, m2, r, x, y);
97 rngstep32 (a >> 6, a, b, mm, m, m2, r, x, y);
98 rngstep32 (a << 2, a, b, mm, m, m2, r, x, y);
99 rngstep32 (a >> 16, a, b, mm, m, m2, r, x, y);
107 /* Perform 2 isaac runs with different contexts simultaneously. */
109 isaac2 (isaac_t * ctx, uword * results)
112 u32 a##n, b##n, c##n, x##n, y##n, * m##n, * mm##n, * m2##n, * r##n, * mend##n
116 (void) mend1; /* "set but unused variable" error on mend1 with gcc 4.9 */
121 mm##n = ctx[(n)].memory; \
122 r##n = results + (n) * ISAAC_SIZE; \
127 mend##n = m2##n = mm##n + ARRAY_LEN (ctx[(n)].memory) / 2; \
138 rngstep32 (a0 << 13, a0, b0, mm0, m0, m20, r0, x0, y0);
139 rngstep32 (a1 << 13, a1, b1, mm1, m1, m21, r1, x1, y1);
140 rngstep32 (a0 >> 6, a0, b0, mm0, m0, m20, r0, x0, y0);
141 rngstep32 (a1 >> 6, a1, b1, mm1, m1, m21, r1, x1, y1);
142 rngstep32 (a0 << 2, a0, b0, mm0, m0, m20, r0, x0, y0);
143 rngstep32 (a1 << 2, a1, b1, mm1, m1, m21, r1, x1, y1);
144 rngstep32 (a0 >> 16, a0, b0, mm0, m0, m20, r0, x0, y0);
145 rngstep32 (a1 >> 16, a1, b1, mm1, m1, m21, r1, x1, y1);
152 rngstep32 (a0 << 13, a0, b0, mm0, m0, m20, r0, x0, y0);
153 rngstep32 (a1 << 13, a1, b1, mm1, m1, m21, r1, x1, y1);
154 rngstep32 (a0 >> 6, a0, b0, mm0, m0, m20, r0, x0, y0);
155 rngstep32 (a1 >> 6, a1, b1, mm1, m1, m21, r1, x1, y1);
156 rngstep32 (a0 << 2, a0, b0, mm0, m0, m20, r0, x0, y0);
157 rngstep32 (a1 << 2, a1, b1, mm1, m1, m21, r1, x1, y1);
158 rngstep32 (a0 >> 16, a0, b0, mm0, m0, m20, r0, x0, y0);
159 rngstep32 (a1 >> 16, a1, b1, mm1, m1, m21, r1, x1, y1);
170 #define mix32(a,b,c,d,e,f,g,h) \
172 a^=b<<11; d+=a; b+=c; \
173 b^=c>>2; e+=b; c+=d; \
174 c^=d<<8; f+=c; d+=e; \
175 d^=e>>16; g+=d; e+=f; \
176 e^=f<<10; h+=e; f+=g; \
177 f^=g>>4; a+=f; g+=h; \
178 g^=h<<8; b+=g; h+=a; \
179 h^=a>>9; c+=h; a+=b; \
183 isaac_init (isaac_t * ctx, uword * seeds)
186 u32 a, b, c, d, e, f, g, h, *m, *r;
188 ctx->a = ctx->b = ctx->c = 0;
192 a = b = c = d = e = f = g = h = 0x9e3779b9; /* the golden ratio */
194 for (i = 0; i < 4; ++i) /* scramble it */
195 mix32 (a, b, c, d, e, f, g, h);
197 /* initialize using the contents of r[] as the seed */
198 for (i = 0; i < ISAAC_SIZE; i += 8)
208 mix32 (a, b, c, d, e, f, g, h);
219 /* do a second pass to make all of the seed affect all of m */
220 for (i = 0; i < ISAAC_SIZE; i += 8)
230 mix32 (a, b, c, d, e, f, g, h);
241 #endif /* uword_bits == 32 */
245 #define ind64(mm,x) (*(u64 *)((u8 *)(mm) + ((x) & ((ISAAC_SIZE-1)<<3))))
246 #define rngstep64(mix,a,b,mm,m,m2,r,x,y) \
249 a = (mix) + *(m2++); \
250 *(m++) = y = ind64(mm,x) + a + b; \
251 *(r++) = b = ind64(mm,y>>ISAAC_LOG2_SIZE) + x; \
255 isaac (isaac_t * ctx, uword * results)
257 u64 a, b, c, x, y, *m, *mm, *m2, *r, *mend;
266 mend = m2 = mm + ARRAY_LEN (ctx->memory) / 2;
270 rngstep64 (~(a ^ (a << 21)), a, b, mm, m, m2, r, x, y);
271 rngstep64 (a ^ (a >> 5), a, b, mm, m, m2, r, x, y);
272 rngstep64 (a ^ (a << 12), a, b, mm, m, m2, r, x, y);
273 rngstep64 (a ^ (a >> 33), a, b, mm, m, m2, r, x, y);
279 rngstep64 (~(a ^ (a << 21)), a, b, mm, m, m2, r, x, y);
280 rngstep64 (a ^ (a >> 5), a, b, mm, m, m2, r, x, y);
281 rngstep64 (a ^ (a << 12), a, b, mm, m, m2, r, x, y);
282 rngstep64 (a ^ (a >> 33), a, b, mm, m, m2, r, x, y);
290 /* Perform 2 isaac runs with different contexts simultaneously. */
292 isaac2 (isaac_t * ctx, uword * results)
295 u64 a##n, b##n, c##n, x##n, y##n, * m##n, * mm##n, * m2##n, * r##n, * mend##n
304 mm##n = ctx[(n)].memory; \
305 r##n = results + (n) * ISAAC_SIZE; \
310 mend##n = m2##n = mm##n + ARRAY_LEN (ctx[(n)].memory) / 2; \
319 (void) mend1; /* compiler warning */
323 rngstep64 (~(a0 ^ (a0 << 21)), a0, b0, mm0, m0, m20, r0, x0, y0);
324 rngstep64 (~(a1 ^ (a1 << 21)), a1, b1, mm1, m1, m21, r1, x1, y1);
325 rngstep64 (a0 ^ (a0 >> 5), a0, b0, mm0, m0, m20, r0, x0, y0);
326 rngstep64 (a1 ^ (a1 >> 5), a1, b1, mm1, m1, m21, r1, x1, y1);
327 rngstep64 (a0 ^ (a0 << 12), a0, b0, mm0, m0, m20, r0, x0, y0);
328 rngstep64 (a1 ^ (a1 << 12), a1, b1, mm1, m1, m21, r1, x1, y1);
329 rngstep64 (a0 ^ (a0 >> 33), a0, b0, mm0, m0, m20, r0, x0, y0);
330 rngstep64 (a1 ^ (a1 >> 33), a1, b1, mm1, m1, m21, r1, x1, y1);
337 rngstep64 (~(a0 ^ (a0 << 21)), a0, b0, mm0, m0, m20, r0, x0, y0);
338 rngstep64 (~(a1 ^ (a1 << 21)), a1, b1, mm1, m1, m21, r1, x1, y1);
339 rngstep64 (a0 ^ (a0 >> 5), a0, b0, mm0, m0, m20, r0, x0, y0);
340 rngstep64 (a1 ^ (a1 >> 5), a1, b1, mm1, m1, m21, r1, x1, y1);
341 rngstep64 (a0 ^ (a0 << 12), a0, b0, mm0, m0, m20, r0, x0, y0);
342 rngstep64 (a1 ^ (a1 << 12), a1, b1, mm1, m1, m21, r1, x1, y1);
343 rngstep64 (a0 ^ (a0 >> 33), a0, b0, mm0, m0, m20, r0, x0, y0);
344 rngstep64 (a1 ^ (a1 >> 33), a1, b1, mm1, m1, m21, r1, x1, y1);
355 #define mix64(a,b,c,d,e,f,g,h) \
357 a-=e; f^=h>>9; h+=a; \
358 b-=f; g^=a<<9; a+=b; \
359 c-=g; h^=b>>23; b+=c; \
360 d-=h; a^=c<<15; c+=d; \
361 e-=a; b^=d>>14; d+=e; \
362 f-=b; c^=e<<20; e+=f; \
363 g-=c; d^=f>>17; f+=g; \
364 h-=d; e^=g<<14; g+=h; \
368 isaac_init (isaac_t * ctx, uword * seeds)
371 u64 a, b, c, d, e, f, g, h, *m, *r;
373 ctx->a = ctx->b = ctx->c = 0;
377 a = b = c = d = e = f = g = h = 0x9e3779b97f4a7c13LL; /* the golden ratio */
379 for (i = 0; i < 4; ++i) /* scramble it */
380 mix64 (a, b, c, d, e, f, g, h);
382 for (i = 0; i < ISAAC_SIZE; i += 8) /* fill in mm[] with messy stuff */
392 mix64 (a, b, c, d, e, f, g, h);
403 /* do a second pass to make all of the seed affect all of mm */
404 for (i = 0; i < ISAAC_SIZE; i += 8)
414 mix64 (a, b, c, d, e, f, g, h);
425 #endif /* uword_bits == 64 */
429 * fd.io coding-style-patch-verification: ON
432 * eval: (c-set-style "gnu")