]> git.mxchange.org Git - simgear.git/blob - 3rdparty/udns/udns_jran.c
Initial commit for a DNS service resolver
[simgear.git] / 3rdparty / udns / udns_jran.c
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.
4  */
5
6 #include "udns.h"
7
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)
11
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
20    */
21 #ifdef JRAN_32_64
22   if (sizeof(unsigned) == 4) {
23 #endif
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);
29 #ifdef JRAN_32_64
30   }
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);
35     x->c = x->d + e;
36     x->d = e + x->a;
37   }
38   else {
39     unsigned e = 0;
40     x->d = 1/e; /* bail */
41   }
42 #endif
43   return x->d;
44 }
45
46 void udns_jraninit(struct udns_jranctx *x, unsigned seed) {
47   unsigned i;
48   x->a = 0xf1ea5eed;
49   x->b = x->c = x->d = seed;
50   for (i = 0; i < 20; ++i)
51      (void)udns_jranval(x);
52 }