aboutsummaryrefslogtreecommitdiff
path: root/src/core/random.c
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2017-07-10 15:02:38 -0700
committerGarrett D'Amore <garrett@damore.org>2017-07-10 15:02:38 -0700
commit795aebbee77bb74d8792df96dfe1aa79ec9548fc (patch)
tree58c16424c16b9e71cebdceaee4507ab6608f80da /src/core/random.c
parentde90f97167d2df6739db47b2c6aad85f06250270 (diff)
downloadnng-795aebbee77bb74d8792df96dfe1aa79ec9548fc.tar.gz
nng-795aebbee77bb74d8792df96dfe1aa79ec9548fc.tar.bz2
nng-795aebbee77bb74d8792df96dfe1aa79ec9548fc.zip
Give up on uncrustify; switch to clang-format.
Diffstat (limited to 'src/core/random.c')
-rw-r--r--src/core/random.c153
1 files changed, 82 insertions, 71 deletions
diff --git a/src/core/random.c b/src/core/random.c
index 00c0bcf7..eb0b4fc2 100644
--- a/src/core/random.c
+++ b/src/core/random.c
@@ -21,47 +21,46 @@
typedef struct {
// the rsl is the actual results, and the randcnt is the length
// of the results.
- uint32_t randrsl[256];
- uint32_t randcnt;
+ uint32_t randrsl[256];
+ uint32_t randcnt;
// lock to protect concurrent access
- nni_mtx mx;
+ nni_mtx mx;
// more or less internal state
- uint32_t mm[256];
- uint32_t aa;
- uint32_t bb;
- uint32_t cc;
+ uint32_t mm[256];
+ uint32_t aa;
+ uint32_t bb;
+ uint32_t cc;
} nni_isaac_ctx;
-
static void
nni_isaac(nni_isaac_ctx *ctx)
{
register uint32_t i, x, y;
- ctx->cc++; // cc incremented once per 256 results
- ctx->bb += ctx->cc; // then combined with bb
+ ctx->cc++; // cc incremented once per 256 results
+ ctx->bb += ctx->cc; // then combined with bb
for (i = 0; i < 256; ++i) {
x = ctx->mm[i];
- switch (i%4) {
+ switch (i % 4) {
case 0:
- ctx->aa ^= (ctx->aa<<13);
+ ctx->aa ^= (ctx->aa << 13);
break;
case 1:
- ctx->aa ^= (ctx->aa>>6);
+ ctx->aa ^= (ctx->aa >> 6);
break;
case 2:
- ctx->aa ^= (ctx->aa<<2);
+ ctx->aa ^= (ctx->aa << 2);
break;
case 3:
- ctx->aa ^= (ctx->aa>>16);
+ ctx->aa ^= (ctx->aa >> 16);
break;
}
- ctx->aa += ctx->mm[(i+128)%256];
- ctx->mm[i] = y = ctx->mm[(x>>2)%256] + ctx->aa + ctx->bb;
- ctx->randrsl[i] = ctx->bb = ctx->mm[(y>>10)%256] + x;
+ ctx->aa += ctx->mm[(i + 128) % 256];
+ ctx->mm[i] = y = ctx->mm[(x >> 2) % 256] + ctx->aa + ctx->bb;
+ ctx->randrsl[i] = ctx->bb = ctx->mm[(y >> 10) % 256] + x;
// Note that bits 2..9 are chosen from x but 10..17 are chosen
// from y. The only important thing here is that 2..9 and
@@ -73,83 +72,97 @@ nni_isaac(nni_isaac_ctx *ctx)
}
}
-
// if (flag!=0), then use the contents of randrsl[] to initialize mm[].
#define nni_isaac_mix(a, b, c, d, e, f, g, h) \
- { \
- a ^= b<<11; d += a; b += c; \
- b ^= c>>2; e += b; c += d; \
- c ^= d<<8; f += c; d += e; \
- d ^= e>>16; g += d; e += f; \
- e ^= f<<10; h += e; f += g; \
- f ^= g>>4; a += f; g += h; \
- g ^= h<<8; b += g; h += a; \
- h ^= a>>9; c += h; a += b; \
+ { \
+ a ^= b << 11; \
+ d += a; \
+ b += c; \
+ b ^= c >> 2; \
+ e += b; \
+ c += d; \
+ c ^= d << 8; \
+ f += c; \
+ d += e; \
+ d ^= e >> 16; \
+ g += d; \
+ e += f; \
+ e ^= f << 10; \
+ h += e; \
+ f += g; \
+ f ^= g >> 4; \
+ a += f; \
+ g += h; \
+ g ^= h << 8; \
+ b += g; \
+ h += a; \
+ h ^= a >> 9; \
+ c += h; \
+ a += b; \
}
static void
nni_isaac_randinit(nni_isaac_ctx *ctx, int flag)
{
- int i;
+ int i;
uint32_t a, b, c, d, e, f, g, h;
ctx->aa = ctx->bb = ctx->cc = 0;
- a = b = c = d = e = f = g = h = 0x9e3779b9; // the golden ratio
+ a = b = c = d = e = f = g = h = 0x9e3779b9; // the golden ratio
- for (i = 0; i < 4; ++i) { // scramble it
+ for (i = 0; i < 4; ++i) { // scramble it
nni_isaac_mix(a, b, c, d, e, f, g, h);
}
- for (i = 0; i < 256; i += 8) { // fill in mm[] with messy stuff
- if (flag) { // use all the information in the seed
+ for (i = 0; i < 256; i += 8) { // fill in mm[] with messy stuff
+ if (flag) { // use all the information in the seed
a += ctx->randrsl[i];
- b += ctx->randrsl[i+1];
- c += ctx->randrsl[i+2];
- d += ctx->randrsl[i+3];
- e += ctx->randrsl[i+4];
- f += ctx->randrsl[i+5];
- g += ctx->randrsl[i+6];
- h += ctx->randrsl[i+7];
+ b += ctx->randrsl[i + 1];
+ c += ctx->randrsl[i + 2];
+ d += ctx->randrsl[i + 3];
+ e += ctx->randrsl[i + 4];
+ f += ctx->randrsl[i + 5];
+ g += ctx->randrsl[i + 6];
+ h += ctx->randrsl[i + 7];
}
nni_isaac_mix(a, b, c, d, e, f, g, h);
- ctx->mm[i] = a;
- ctx->mm[i+1] = b;
- ctx->mm[i+2] = c;
- ctx->mm[i+3] = d;
- ctx->mm[i+4] = e;
- ctx->mm[i+5] = f;
- ctx->mm[i+6] = g;
- ctx->mm[i+7] = h;
+ ctx->mm[i] = a;
+ ctx->mm[i + 1] = b;
+ ctx->mm[i + 2] = c;
+ ctx->mm[i + 3] = d;
+ ctx->mm[i + 4] = e;
+ ctx->mm[i + 5] = f;
+ ctx->mm[i + 6] = g;
+ ctx->mm[i + 7] = h;
}
if (flag) {
// do a second pass to make all of the seed affect all of mm
for (i = 0; i < 256; i += 8) {
a += ctx->mm[i];
- b += ctx->mm[i+1];
- c += ctx->mm[i+2];
- d += ctx->mm[i+3];
- e += ctx->mm[i+4];
- f += ctx->mm[i+5];
- g += ctx->mm[i+6];
- h += ctx->mm[i+7];
+ b += ctx->mm[i + 1];
+ c += ctx->mm[i + 2];
+ d += ctx->mm[i + 3];
+ e += ctx->mm[i + 4];
+ f += ctx->mm[i + 5];
+ g += ctx->mm[i + 6];
+ h += ctx->mm[i + 7];
nni_isaac_mix(a, b, c, d, e, f, g, h);
- ctx->mm[i] = a;
- ctx->mm[i+1] = b;
- ctx->mm[i+2] = c;
- ctx->mm[i+3] = d;
- ctx->mm[i+4] = e;
- ctx->mm[i+5] = f;
- ctx->mm[i+6] = g;
- ctx->mm[i+7] = h;
+ ctx->mm[i] = a;
+ ctx->mm[i + 1] = b;
+ ctx->mm[i + 2] = c;
+ ctx->mm[i + 3] = d;
+ ctx->mm[i + 4] = e;
+ ctx->mm[i + 5] = f;
+ ctx->mm[i + 6] = g;
+ ctx->mm[i + 7] = h;
}
}
- nni_isaac(ctx); // fill in the first set of results
- ctx->randcnt = 256; // prepare to use the first set of results
+ nni_isaac(ctx); // fill in the first set of results
+ ctx->randcnt = 256; // prepare to use the first set of results
}
-
static nni_isaac_ctx nni_random_ctx;
int
@@ -157,22 +170,21 @@ nni_random_sys_init(void)
{
// minimally, grab the system clock
nni_isaac_ctx *ctx = &nni_random_ctx;
- int rv;
+ int rv;
if ((rv = nni_mtx_init(&ctx->mx)) != 0) {
return (rv);
}
- nni_plat_seed_prng(ctx->randrsl, sizeof (ctx->randrsl));
+ nni_plat_seed_prng(ctx->randrsl, sizeof(ctx->randrsl));
nni_isaac_randinit(ctx, 1);
return (0);
}
-
uint32_t
nni_random(void)
{
- uint32_t rv;
+ uint32_t rv;
nni_isaac_ctx *ctx = &nni_random_ctx;
nni_mtx_lock(&ctx->mx);
@@ -187,7 +199,6 @@ nni_random(void)
return (rv);
}
-
void
nni_random_sys_fini(void)
{