1 /* udns_jran.c: small non-cryptographic random number generator
2 * taken from http://burtleburtle.net/bob/rand/smallprng.html
3 * by Bob Jenkins, Public domain.
8 #define rot32(x,k) (((x) << (k)) | ((x) >> (32-(k))))
9 #define rot64(x,k) (((x) << (k)) | ((x) >> (64-(k))))
10 #define tr32(x) ((x)&0xffffffffu)
12 unsigned udns_jranval(struct udns_jranctx *x) {
13 /* This routine can be made to work with either 32 or 64bit words -
14 * if JRAN_32_64 is defined when compiling the file.
15 * We use if() instead of #if since there's no good
16 * portable way to check sizeof() in preprocessor without
17 * introducing some ugly configure-time checks.
18 * Most compilers will optimize the wrong branches away anyway.
19 * By default it assumes 32bit integers
22 if (sizeof(unsigned) == 4) {
24 unsigned e = tr32(x->a - rot32(x->b, 27));
25 x->a = tr32(x->b ^ rot32(x->c, 17));
26 x->b = tr32(x->c + x->d);
27 x->c = tr32(x->d + e);
28 x->d = tr32(e + x->a);
31 else if (sizeof(unsigned) == 8) { /* assuming it's 64bits */
32 unsigned e = x->a - rot64(x->b, 7);
33 x->a = x->b ^ rot64(x->c, 13);
34 x->b = x->c + rot64(x->d, 37);
40 x->d = 1/e; /* bail */
46 void udns_jraninit(struct udns_jranctx *x, unsigned seed) {
49 x->b = x->c = x->d = seed;
50 for (i = 0; i < 20; ++i)
51 (void)udns_jranval(x);