aboutsummaryrefslogtreecommitdiff
path: root/sys
diff options
context:
space:
mode:
authorNicholas Noll <nbnoll@eml.cc>2021-09-23 12:35:04 -0700
committerNicholas Noll <nbnoll@eml.cc>2021-09-23 12:35:04 -0700
commit83cd586ea304d6f6aa190c65ee796baaba1941a7 (patch)
tree74bf8efcfb0a3d458767659d34ed3e58618f9d71 /sys
parent67c778d19c862218423a17f889401eaeb2ebfbb9 (diff)
feat: improved interface of map macro
Diffstat (limited to 'sys')
-rw-r--r--sys/cmd/cc/cc.c2
-rw-r--r--sys/cmd/cc/lex.c2
-rw-r--r--sys/libbio/fasta.c9
-rw-r--r--sys/libn/fs.c18
-rw-r--r--sys/libn/gz.c21
-rw-r--r--sys/libn/io.c28
-rw-r--r--sys/libn/memory.c10
-rw-r--r--sys/libn/random.c118
8 files changed, 160 insertions, 48 deletions
diff --git a/sys/cmd/cc/cc.c b/sys/cmd/cc/cc.c
index 3602643..8ad0022 100644
--- a/sys/cmd/cc/cc.c
+++ b/sys/cmd/cc/cc.c
@@ -397,7 +397,7 @@ main(int argc, byte *argv[])
// NOTE: This is just for my comfort during debugging.
pushinclude("/home/nolln/root/include");
pushinclude("/home/nolln/root/include/vendor/libc");
-
+
src = (argc == 0) ? "<stdin>" : argv[0];
intern(&src);
diff --git a/sys/cmd/cc/lex.c b/sys/cmd/cc/lex.c
index 0963fb9..33fc5d0 100644
--- a/sys/cmd/cc/lex.c
+++ b/sys/cmd/cc/lex.c
@@ -813,7 +813,7 @@ static
int
moresymtab(SymTab *tab, int n)
{
- MAP_GROW(tab, string, Sym*, n, PTR_HASH, ·calloc, ·free, nil);
+ MAP_GROW(tab, string, Sym*, n, PTR_HASH, sys·Memory, nil);
}
static
diff --git a/sys/libbio/fasta.c b/sys/libbio/fasta.c
index 078325a..0c47199 100644
--- a/sys/libbio/fasta.c
+++ b/sys/libbio/fasta.c
@@ -190,7 +190,7 @@ readfasta(bio·SeqReader *rdr, bio·Seq *seq, byte hdr, byte stop)
{
error err;
byte *beg;
-
+
if (rdr->eof && rdr->b == rdr->bend-1)
return EOF;
@@ -236,13 +236,6 @@ SEQLOOP:
rdr->b++;
}
-#if 0
- for(byte *cb = rdr->seq->b+rdr->seq->off; cb != rdr->seq->it; ++cb) {
- if(*cb == 0) {
- printf("ERROR @ pos=%ld: Found zero byte\n", cb - rdr->seq->b+rdr->seq->off);
- }
- }
-#endif
push(&rdr->seq, rdr->b - beg, beg);
diff --git a/sys/libn/fs.c b/sys/libn/fs.c
index 10cd93e..e89ef07 100644
--- a/sys/libn/fs.c
+++ b/sys/libn/fs.c
@@ -12,20 +12,6 @@ struct Key
dev_t dev;
};
-static
-void*
-xalloc(void *_, int n, uintptr size)
-{
- return malloc(n*size);
-}
-
-static
-void
-xfree(void *_, void *ptr)
-{
- return free(ptr);
-}
-
#define hash(k) ((int32)k.ino ^ (int32)k.dev)
#define equal(k1, k2) (k1.ino == k2.ino && k1.dev == k2.dev)
@@ -38,7 +24,7 @@ static
int
morehistory(fs·History *h, int n)
{
- SET_GROW(h, struct Key, n, hash, xalloc, xfree, nil);
+ SET_GROW(h, struct Key, n, hash, sys·Memory, nil);
}
static
@@ -62,7 +48,7 @@ static
void
delete(fs·History *h)
{
- SET_FREE(h, xfree, nil);
+ SET_FREE(h, sys·Memory, nil);
}
#undef hash
diff --git a/sys/libn/gz.c b/sys/libn/gz.c
index 79bc13f..040d25a 100644
--- a/sys/libn/gz.c
+++ b/sys/libn/gz.c
@@ -7,15 +7,16 @@
// interface implementations
/* actual interfaces */
-io·Reader gz·FileReader = (io·Reader){ gz·read };
-io·Peeker gz·FilePeeker = (io·Peeker){ gz·getbyte, gz·ungetbyte };
-io·FullReader gz·FullFileReader = (io·FullReader){ gz·read, gz·getbyte, gz·ungetbyte };
+io·Reader gz·Reader = (io·Reader){ gz·read };
+io·Peeker gz·Peeker = (io·Peeker){ gz·getbyte, gz·ungetbyte };
+io·Seeker gz·Seeker = (io·Seeker){ gz·seek, gz·tell };
+io·PeekReader gz·Peekreader = (io·PeekReader){ gz·read, gz·getbyte, gz·ungetbyte };
-io·Writer gz·FileWriter = (io·Writer){ gz·write };
-io·Putter gz·FilePutter = (io·Putter){ gz·putbyte, gz·putstring };
-io·FullWriter gz·FullFileWriter = (io·FullWriter){ gz·write, gz·putbyte, gz·putstring };
+io·Writer gz·Writer = (io·Writer){ gz·write };
+io·Putter gz·Putter = (io·Putter){ gz·putbyte, gz·putstring };
+io·PutWriter gz·PutWriter = (io·PutWriter){ gz·write, gz·putbyte, gz·putstring };
-io·ReadWriter gz·FileReadWriter = (io·ReadWriter){ gz·read, gz·write };
+io·ReadWriter gz·ReadWriter = (io·ReadWriter){ gz·read, gz·write };
// -----------------------------------------------------------------------
// functions implementations
@@ -69,13 +70,13 @@ gz·write(gz·Stream *s, int sz, int n, void* buf)
return gzwrite(s, buf, n*sz);
}
-error
+error
gz·putbyte(gz·Stream *s, byte c)
{
return gzputc(s, c);
}
-error
+error
gz·putstring(gz·Stream *s, byte *str)
{
return gzputs(s, str);
@@ -100,7 +101,7 @@ gz·flush(gz·Stream *s)
return gzflush(s, Z_FINISH);
}
-vlong
+int
gz·seek(gz·Stream *s, long off, enum SeekPos whence)
{
return gzseek(s, off, whence);
diff --git a/sys/libn/io.c b/sys/libn/io.c
index f0d270e..ff4fe67 100644
--- a/sys/libn/io.c
+++ b/sys/libn/io.c
@@ -40,21 +40,37 @@ error
return io·putbyte((io·Stream *)wtr, c);
}
-static
+static
int
·puts(void *wtr, string s)
{
return io·putstring((io·Stream *)wtr, s);
}
+static
+int
+·seek(void *skr, long off, enum SeekPos whence)
+{
+ return io·seek((io·Stream *)skr, off, whence);
+}
+
+static
+long
+·tell(void *skr)
+{
+ return io·tell((io·Stream *)skr);
+}
+
/* actual interfaces */
io·Reader sys·Reader = (io·Reader){ ·read };
+io·Seeker sys·Seeker = (io·Seeker){ ·seek, ·tell };
io·Peeker sys·Peeker = (io·Peeker){ ·get, ·unget };
-io·FullReader sys·FullReader = (io·FullReader){ ·read, ·get, ·unget };
+io·SeekReader sys·SeekReader = (io·SeekReader){ ·seek, ·tell, ·read };
+io·PeekReader sys·PeekReader = (io·PeekReader){ ·read, ·get, ·unget };
io·Writer sys·Writer = (io·Writer){ ·write };
io·Putter sys·Putter = (io·Putter){ ·put, ·puts };
-io·FullWriter sys·FullWriter = (io·FullWriter){ ·write, ·put, ·puts };
+io·PutWriter sys·PutWriter = (io·PutWriter){ ·write, ·put, ·puts };
io·ReadWriter sys·ReadWriter = (io·ReadWriter){ ·read, ·write };
@@ -152,3 +168,9 @@ io·seek(io·Stream *s, long off, enum SeekPos origin)
{
return fseek(s, off, origin);
}
+
+long
+io·tell(io·Stream *s)
+{
+ return ftell(s);
+}
diff --git a/sys/libn/memory.c b/sys/libn/memory.c
index 1c7ab07..795fe1f 100644
--- a/sys/libn/memory.c
+++ b/sys/libn/memory.c
@@ -1,19 +1,19 @@
#include <u.h>
#include <libn.h>
-static
-void
+static
+void
·free(void* _, void* ptr) {
return free(ptr);
}
-static
+static
void *
·alloc(void* _, uint n, ulong size) {
return malloc(n*size);
}
-static
+static
void *
·calloc(void* _, uint n, ulong size) {
return calloc(n, size);
@@ -26,7 +26,7 @@ void *
}
mem·Allocator sys·Memory = {
- .alloc = ·alloc,
+ .alloc = ·calloc,
.free = ·free
};
diff --git a/sys/libn/random.c b/sys/libn/random.c
index 551d1e9..237127e 100644
--- a/sys/libn/random.c
+++ b/sys/libn/random.c
@@ -107,17 +107,127 @@ rng·randi(int max)
return r % max;
}
+/*
+ * Ahrens, J. H., & Dieter, U. (1982).
+ * Computer Generation of Poisson Deviates from Modified Normal Distributions.
+ */
+static double factorial[10] = {1., 1., 2., 6., 24., 120., 720., 5040., 40320., 362880.};
+static double coeffs[9] = {
+ -.500000000, +.333333333, -.249999856,
+ +.200011780, -.166684875, +.142187833,
+ -.124196313, +.125005956, -.114265030,
+};
+
+static inline
+double
+log1pmx(double x, double off)
+{
+ int i;
+ double r, t;
+
+ if(-0.25 < x && x < 0.25) {
+ r = 0;
+ t = 1;
+ for(i=0;i<arrlen(coeffs);i++) {
+ r += coeffs[i]*t;
+ t *= x;
+ }
+
+ return x*x*r;
+ }
+ return log(1+x) - off;
+}
+
+static inline
+double
+procf(double mu, double s, int64 K, double *px, double *py, double *fx, double *fy)
+{
+ double d, V, X;
+ double w, b1, b2, c1, c2, c3, c0, c;
+
+ w = 0.3989422804014327/s;
+ b1 = 0.041666666666666664/mu;
+ b2 = 0.3*b1*b1;
+ c3 = 0.14285714285714285*b1*b2;
+ c2 = b2 - 15.*c3;
+ c1 = b1 - 6.*b2 + 45.*c3;
+ c0 = 1 - b1 + 3.*b2 - 15.*c3;
+ c = .1069/mu;
+
+ if(K < 10) {
+ *px = -mu;
+ *py = pow(mu,K) / factorial[K];
+ }else{
+ d = 0.08333333333333333/K;
+ d = d - 4.8*d*d*d;
+ V = (mu - K) / K;
+
+ *px = K*log1pmx(V,mu-K) - d;
+ *py = 0.3989422804014327/sqrt(K);
+ }
+
+ X = (K - mu + 0.5)/s;
+ *fx = -0.5*X*X;
+ *fy = w*(((c3*X*X + c2)*X*X + c1)*X*X + c0);
+
+ return c;
+}
+
+static inline
+uint64
+bigpoisson(double mu)
+{
+ int64 L,K;
+ double G,s,d,U,E,T;
+ double px,py,fx,fy,c;
+
+ s = sqrt(mu);
+ d = 6*mu*mu;
+ L = floor(mu - 1.1484);
+
+stepN:
+ G = mu + s*rng·normal();
+ K = floor(G);
+ if(K<0)
+ goto stepP;
+stepI:
+ if(K>=L)
+ return K;
+stepS:
+ U = rng·random();
+ if(d*U >= (mu-K)*(mu-K)*(mu-K))
+ return K;
+stepP:
+ if(G < 0)
+ goto stepE;
+stepQ:
+ c = procf(mu, s, K, &px, &py, &fx, &fy);
+stepE:
+ E = rng·exponential(1.0);
+ U = rng·random();
+ U = U + U - 1;
+ T = 1.8 + copysign(E,U);
+ if(T < 0.6744)
+ goto stepE;
+ K = floor(mu + s*T);
+ c = procf(mu, s, K, &px, &py, &fx, &fy);
+stepH:
+ if(c*fabs(U) > (py*exp(px + E) - fy*exp(fx + E)))
+ goto stepE;
+ return K;
+}
+
uint64
rng·poisson(double mean)
{
- uint64 n;
- double c;
+ int64 n;
+ double z;
if(mean<10.0) {
- for(n=0, c=rng·exponential(1.0); c<mean; ++n, c+=rng·exponential(1.0))
+ for(n=0, z=rng·exponential(1.0); z<mean; ++n, z+=rng·exponential(1.0))
;
return n;
}
- return 0;
+ return bigpoisson(mean);
}