aboutsummaryrefslogtreecommitdiff
path: root/sys/libn
diff options
context:
space:
mode:
authorNicholas Noll <nbnoll@eml.cc>2021-09-08 15:51:40 -0700
committerNicholas Noll <nbnoll@eml.cc>2021-09-08 15:51:53 -0700
commitc0a7b53baf2a6e7bf9bc1fbec7ac05e43ac59154 (patch)
tree385b83f4d505da960820932f7357eb46b31abac3 /sys/libn
parent8c0475f97675245d1bcbb112dc79c9f490fad361 (diff)
checkin
Diffstat (limited to 'sys/libn')
-rw-r--r--sys/libn/error.c5
-rw-r--r--sys/libn/memory.c22
-rw-r--r--sys/libn/random.c35
3 files changed, 47 insertions, 15 deletions
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 <u.h>
+#include <libn.h>
// ----------------------------------------------------------------------------
// 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<mean; ++n, c+=rng·exponential(1.0))
+ ;
+ return n;
+ }
+
+ return 0;
+}