From c0a7b53baf2a6e7bf9bc1fbec7ac05e43ac59154 Mon Sep 17 00:00:00 2001 From: Nicholas Noll Date: Wed, 8 Sep 2021 15:51:40 -0700 Subject: checkin --- sys/libn/error.c | 5 ++--- sys/libn/memory.c | 22 +++++++++++----------- sys/libn/random.c | 35 ++++++++++++++++++++++++++++++++++- 3 files changed, 47 insertions(+), 15 deletions(-) (limited to 'sys/libn') diff --git a/sys/libn/error.c b/sys/libn/error.c index a9d684c..508136c 100644 --- a/sys/libn/error.c +++ b/sys/libn/error.c @@ -17,9 +17,8 @@ errorf(byte* fmt, ...) va_list args; va_start(args, fmt); - printf("error: "); - vprintf(fmt, args); - printf("\n"); + fprintf(stderr, "error: "); + vfprintf(stderr, fmt, args); va_end(args); } diff --git a/sys/libn/memory.c b/sys/libn/memory.c index d7df8e3..1c7ab07 100644 --- a/sys/libn/memory.c +++ b/sys/libn/memory.c @@ -36,23 +36,23 @@ mem·Allocator sys·Memory = { /* Grow to particular size */ void* -bufgrow(void* buf, vlong newLen, vlong eltsize) +·bufgrow(void* buf, vlong newLen, vlong eltsize) { assert(bufcap(buf) <= (SIZE_MAX - 1) / 2); vlong newCap = MAX(16, MAX(1 + 2 * bufcap(buf), newLen)); assert(newLen <= newCap); - assert(newCap <= (SIZE_MAX - offsetof(bufHdr, buf)) / eltsize); + assert(newCap <= (SIZE_MAX - offsetof(BufHdr, buf)) / eltsize); - vlong newSize = offsetof(bufHdr, buf) + newCap * eltsize; + vlong newSize = offsetof(BufHdr, buf) + newCap * eltsize; - bufHdr* newHdr; + BufHdr* newHdr; if (buf) { - newHdr = _bufHdr(buf); - newHdr = (bufHdr*)realloc((void*)newHdr, newSize); + newHdr = bufhdr(buf); + newHdr = (BufHdr*)realloc((void*)newHdr, newSize); } else { - newHdr = (bufHdr*)malloc(newSize); + newHdr = (BufHdr*)malloc(newSize); newHdr->len = 0; } @@ -62,20 +62,20 @@ bufgrow(void* buf, vlong newLen, vlong eltsize) /* Pop out a value */ void -_bufpop(void *buf, int i, vlong eltsize) +·bufdel(void *buf, int i, vlong eltsize) { int n; byte *b; byte stk[1024]; assert(eltsize < sizeof(stk)); - b = (byte*) buf; - if (n = buflen(buf), i < n) { + b = (byte*)buf; + if(n = buflen(buf), i < n) { memcpy(stk, b+eltsize*i, eltsize); memcpy(b+eltsize*i, b+eltsize*(i+1), eltsize*(n-i-1)); memcpy(b+eltsize*(n-1), stk, eltsize); } - _bufHdr(buf)->len--; + bufhdr(buf)->len--; } // ------------------------------------------------------------------------- diff --git a/sys/libn/random.c b/sys/libn/random.c index 558641e..551d1e9 100644 --- a/sys/libn/random.c +++ b/sys/libn/random.c @@ -1,4 +1,5 @@ #include +#include // ---------------------------------------------------------------------------- // Internal structure @@ -66,12 +67,29 @@ rng·init(uint64 seed) /* Returns a random float64 between 0 and 1 */ double -rng·random() +rng·random(void) { uint64 r = xoshiro256ss(&RNG); return (double)r / (double)UINT64_MAX; } +double +rng·exponential(double lambda) +{ + double f; + + f = rng·random(); + return -log(1 - f)/lambda; +} + +double +rng·normal(void) +{ + double f; + f = rng·random(); + +} + /* Returns true or false on success of trial */ bool rng·bernoulli(double f) @@ -88,3 +106,18 @@ rng·randi(int max) uint64 r = xoshiro256ss(&RNG); return r % max; } + +uint64 +rng·poisson(double mean) +{ + uint64 n; + double c; + + if(mean<10.0) { + for(n=0, c=rng·exponential(1.0); c