From bc53100f1ef063e09d77e8670e1796bc67017411 Mon Sep 17 00:00:00 2001 From: Nicholas Noll Date: Tue, 28 Sep 2021 13:39:24 -0700 Subject: Checkin: various small changes --- sys/cmd/dwm/util.c | 22 ++++++++--------- sys/cmd/rules.mk | 6 +++++ sys/libbio/fasta.c | 4 +-- sys/libbio/newick.c | 37 ++++++++++++---------------- sys/libbio/phylo.c | 6 ++--- sys/libn/genutf8.py | 1 - sys/libn/memory.c | 45 ++++++++++++++++++++++++---------- sys/libn/random.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++ sys/libn/rules.mk | 2 +- 9 files changed, 141 insertions(+), 52 deletions(-) (limited to 'sys') diff --git a/sys/cmd/dwm/util.c b/sys/cmd/dwm/util.c index c64e652..46db60a 100644 --- a/sys/cmd/dwm/util.c +++ b/sys/cmd/dwm/util.c @@ -3,20 +3,20 @@ void fatal(char *fmt, ...) { - va_list args; + va_list args; - va_start(args, fmt); - vfprintf(stderr, fmt, args); - va_end(args); + va_start(args, fmt); + vfprintf(stderr, fmt, args); + va_end(args); - if (fmt[0] && fmt[strlen(fmt)-1] == ':') { - fputc(' ', stderr); - perror(NULL); - } else { - fputc('\n', stderr); - } + if (fmt[0] && fmt[strlen(fmt)-1] == ':') { + fputc(' ', stderr); + perror(NULL); + } else { + fputc('\n', stderr); + } - exit(1); + exit(1); } void * diff --git a/sys/cmd/rules.mk b/sys/cmd/rules.mk index bd9d232..01edd92 100644 --- a/sys/cmd/rules.mk +++ b/sys/cmd/rules.mk @@ -8,6 +8,9 @@ include share/push.mk DIR := $(d)/dwm include $(DIR)/rules.mk +DIR := $(d)/wm +include $(DIR)/rules.mk + DIR := $(d)/filter include $(DIR)/rules.mk @@ -17,6 +20,9 @@ include $(DIR)/rules.mk DIR := $(d)/term include $(DIR)/rules.mk +# DIR := $(d)/term2 +# include $(DIR)/rules.mk + DIR := $(d)/walk include $(DIR)/rules.mk diff --git a/sys/libbio/fasta.c b/sys/libbio/fasta.c index 0c47199..2feb0a4 100644 --- a/sys/libbio/fasta.c +++ b/sys/libbio/fasta.c @@ -176,7 +176,7 @@ bio·closeseq(bio·SeqReader *rdr) mem = rdr->seq->mem; heap = rdr->seq->heap; - + mem.free(heap, rdr->seq); mem.free(heap, rdr); @@ -191,7 +191,7 @@ readfasta(bio·SeqReader *rdr, bio·Seq *seq, byte hdr, byte stop) error err; byte *beg; - if (rdr->eof && rdr->b == rdr->bend-1) + if(rdr->eof && rdr->b == rdr->bend-1) return EOF; reset(rdr->seq); diff --git a/sys/libbio/newick.c b/sys/libbio/newick.c index 98d30f2..631596f 100644 --- a/sys/libbio/newick.c +++ b/sys/libbio/newick.c @@ -167,10 +167,10 @@ struct Parser bio·Node *root; struct Token tok; - void *fimpl; + void *io; io·Peeker file; - void *himpl; - mem·Allocator heap; + void *heap; + mem·Allocator mem; }; static @@ -184,7 +184,7 @@ parse(struct Parser *p) node = p->root; for (;;) { - tok = lex_nospace(p->file, p->fimpl); + tok = lex_nospace(p->file, p->io); switch (tok.kind) { case tok·lparen: @@ -193,7 +193,7 @@ parse(struct Parser *p) goto ERROR; } - node = p->heap.alloc(p->himpl, 1, sizeof(*node)); + node = p->mem.alloc(p->heap, 1, sizeof(*node)); memset(node, 0, sizeof(*node)); if (p->root) { @@ -231,7 +231,7 @@ parse(struct Parser *p) } node->comment = str·make(""); while (tok.kind != tok·rbrak) { - tok = lex_nospace(p->file, p->fimpl); + tok = lex_nospace(p->file, p->io); if (tok.kind == tok·eof || tok.kind == tok·nil) { errorf("incorrect format: unmatched comment bracket '['"); goto ERROR; @@ -246,7 +246,7 @@ parse(struct Parser *p) break; case tok·colon: - tok = lex_nospace(p->file, p->fimpl); + tok = lex_nospace(p->file, p->io); if (tok.kind != tok·number) { errorf("incorrect format: expected number after colon"); goto ERROR; @@ -280,7 +280,7 @@ parse(struct Parser *p) goto ERROR; } - node = p->heap.alloc(p->himpl, 1, sizeof(*node)); + node = p->mem.alloc(p->heap, 1, sizeof(*node)); memset(node, 0, sizeof(*node)); node->name = str·make(tok.lit.s); @@ -303,7 +303,7 @@ parse(struct Parser *p) break; case tok·semi: - p->file.unget(p->fimpl, ';'); + p->file.unget(p->io, ';'); if (p->lev) { errorf("format error: uneven number of parentheses found at ';'"); goto ERROR; @@ -329,35 +329,30 @@ ERROR: return 1; } -error +int bio·readnewick(io·Peeker stream, void *s, bio·Tree *tree) { error err; struct Parser p; - enum - { - error·nil, - error·notree, - error·parse, - }; if (!tree) { - return error·notree; + errorf("tree pointer nil"); + return 0; } p = (struct Parser){ .lev = 0, .root = nil, .tok = (struct Token){ 0 }, - .fimpl = s, + .io = s, .file = stream, - .himpl = tree->h, + .mem = tree->mem, .heap = tree->heap, }; err = parse(&p); if (err) { errorf("parsing failed\n"); - return error·parse; + return 0; } tree->root = p.root; @@ -367,7 +362,7 @@ bio·readnewick(io·Peeker stream, void *s, bio·Tree *tree) phylo·countleafs(tree->root, &tree->nleaf); phylo·countnodes(tree->root, &tree->root->nnode); - return error·nil; + return 1; } // ----------------------------------------------------------------------- diff --git a/sys/libbio/phylo.c b/sys/libbio/phylo.c index 920102b..cb5c75c 100644 --- a/sys/libbio/phylo.c +++ b/sys/libbio/phylo.c @@ -84,7 +84,7 @@ phylo·countleafs(bio·Node *node, int *n) { error err; bio·Node *child; - + if (!node->nchild) { *n += 1; } @@ -392,7 +392,7 @@ phylo·reroot(bio·Tree *tree, bio·Node *node, double d) // TODO: should check that node is part of this tree? // TODO: should we check if node->parent != nil? - + if (fabs(d) < PREC) { new = node; rotateparent(node->parent, node); @@ -402,7 +402,7 @@ phylo·reroot(bio·Tree *tree, bio·Node *node, double d) rotateparent(new->parent->parent, new->parent); } } else { - new = tree->heap.alloc(tree->h, 1, sizeof(*new)); + new = tree->mem.alloc(tree->heap, 1, sizeof(*new)); memset(new, 0, sizeof(*new)); phylo·addchild(new, node); diff --git a/sys/libn/genutf8.py b/sys/libn/genutf8.py index f4b9f8c..9ce9975 100755 --- a/sys/libn/genutf8.py +++ b/sys/libn/genutf8.py @@ -77,7 +77,6 @@ def puttab(s, fd, name): RUNEMAX = 0x10FFFF if __name__ == "__main__": - alphas = set() lowers = set() uppers = set() diff --git a/sys/libn/memory.c b/sys/libn/memory.c index 795fe1f..a749e4c 100644 --- a/sys/libn/memory.c +++ b/sys/libn/memory.c @@ -3,25 +3,25 @@ static void -·free(void* _, void* ptr) { +·free(void *_, void *ptr) { return free(ptr); } static void * -·alloc(void* _, uint n, ulong size) { +·alloc(void *_, uint n, ulong size) { return malloc(n*size); } static void * -·calloc(void* _, uint n, ulong size) { +·calloc(void *_, uint n, ulong size) { return calloc(n, size); } static void * -·realloc(void* _, void *ptr, uint n, ulong size) { +·realloc(void *_, void *ptr, uint n, ulong size) { return realloc(ptr, n*size); } @@ -89,7 +89,7 @@ void #define ALIGN_DOWN_PTR(p, a) ((void*)ALIGN_DOWN((uintptr)(p), (a))) #define ALIGN_UP_PTR(p, a) ((void*)ALIGN_UP((uintptr)(p), (a))) -struct Block +struct Block { struct Block *next; byte buf[]; @@ -97,8 +97,8 @@ struct Block struct mem·Arena { - mem·Allocator heap; - void *impl; + void *heap; + mem·Allocator mem; byte *off; byte *end; @@ -110,8 +110,8 @@ mem·Arena* mem·makearena(mem·Allocator from, void *impl) { mem·Arena *a = from.alloc(impl, 1, sizeof(*a) + ARENA_BLOCK_SIZE); - a->impl = impl; - a->heap = from; + a->mem = from; + a->heap = impl; a->off = a->first.buf; a->end = a->first.buf + ARENA_BLOCK_SIZE; a->curr = &a->first; @@ -128,7 +128,7 @@ grow(mem·Arena *a, vlong min) struct Block *blk; size = ALIGN_UP(MAX(min, ARENA_BLOCK_SIZE), ARENA_ALIGN); - blk = a->heap.alloc(a->impl, 1, sizeof(*blk) + size); + blk = a->mem.alloc(a->heap, 1, sizeof(*blk) + size); a->off = blk->buf; a->end = a->off + size; @@ -142,7 +142,7 @@ grow(mem·Arena *a, vlong min) void* mem·arenaalloc(mem·Arena *a, uint n, ulong size) { - if (!n) { + if(!n) { return nil; } @@ -172,13 +172,32 @@ mem·freearena(mem·Arena *a) it = a->first.next; while (it != nil) { next = it->next; - a->heap.free(a->impl, it); + a->mem.free(a->heap, it); it = next; } - a->heap.free(a->impl, a); + a->mem.free(a->heap, a); } +static +void* +·arenaalloc(void *heap, uint n, ulong size) +{ + return mem·arenaalloc(heap, n, size); +} + +static +void +·arenafree(void *heap, void *ptr) +{ + /* no-op */ +} + +mem·Allocator mem·ArenaAllocator = { + .alloc = ·arenaalloc, + .free = ·arenafree, +}; + // ------------------------------------------------------------------------- // Generalized memory helpers diff --git a/sys/libn/random.c b/sys/libn/random.c index 237127e..9e80b34 100644 --- a/sys/libn/random.c +++ b/sys/libn/random.c @@ -82,12 +82,82 @@ rng·exponential(double lambda) return -log(1 - f)/lambda; } +static inline +double +erfinv(double x) +{ + /* useful constants */ + static double + a0 = 1.1975323115670912564578e0, a1 = 4.7072688112383978012285e1, + a2 = 6.9706266534389598238465e2, a3 = 4.8548868893843886794648e3, + a4 = 1.6235862515167575384252e4, a5 = 2.3782041382114385731252e4, + a6 = 1.1819493347062294404278e4, a7 = 8.8709406962545514830200e2, + + b0 = 1.0000000000000000000e0, b1 = 4.2313330701600911252e1, + b2 = 6.8718700749205790830e2, b3 = 5.3941960214247511077e3, + b4 = 2.1213794301586595867e4, b5 = 3.9307895800092710610e4, + b6 = 2.8729085735721942674e4, b7 = 5.2264952788528545610e3, + + c0 = 1.42343711074968357734e0, c1 = 4.63033784615654529590e0, + c2 = 5.76949722146069140550e0, c3 = 3.64784832476320460504e0, + c4 = 1.27045825245236838258e0, c5 = 2.41780725177450611770e-1, + c6 = 2.27238449892691845833e-2, c7 = 7.74545014278341407640e-4, + + d0 = 1.4142135623730950488016887e0, d1 = 2.9036514445419946173133295e0, + d2 = 2.3707661626024532365971225e0, d3 = 9.7547832001787427186894837e-1, + d4 = 2.0945065210512749128288442e-1, d5 = 2.1494160384252876777097297e-2, + d6 = 7.7441459065157709165577218e-4, d7 = 1.4859850019840355905497876e-9, + + e0 = 6.65790464350110377720e0, e1 = 5.46378491116411436990e0, + e2 = 1.78482653991729133580e0, e3 = 2.96560571828504891230e-1, + e4 = 2.65321895265761230930e-2, e5 = 1.24266094738807843860e-3, + e6 = 2.71155556874348757815e-5, e7 = 2.01033439929228813265e-7, + + f0 = 1.414213562373095048801689e0, f1 = 8.482908416595164588112026e-1, + f2 = 1.936480946950659106176712e-1, f3 = 2.103693768272068968719679e-2, + f4 = 1.112800997078859844711555e-3, f5 = 2.611088405080593625138020e-5, + f6 = 2.010321207683943062279931e-7, f7 = 2.891024605872965461538222e-15, + + Ln2 = 0.693147180559945309417232121458176568075500134360255254120680009; + + int s; + double r, z1, z2; + + if(x < 0) { + s = -1; + x = -x; + } else { + s = +1; + } + + if(x <= 0.85) { + r = 0.180625 - 0.25*x*x; + z1 = ((((((a7*r+a6)*r+a5)*r+a4)*r+a3)*r+a2)*r+a1)*r + a0; + z2 = ((((((b7*r+b6)*r+b5)*r+b4)*r+b3)*r+b2)*r+b1)*r + b0; + return s*(x*z1) / z2; + } + r = sqrt(Ln2 - log(1.0-x)); + if(r <= 5.0) { + r -= 1.6; + z1 = ((((((c7*r+c6)*r+c5)*r+c4)*r+c3)*r+c2)*r+c1)*r + c0; + z2 = ((((((d7*r+d6)*r+d5)*r+d4)*r+d3)*r+d2)*r+d1)*r + d0; + } else { + r -= 5.0; + z1 = ((((((e7*r+e6)*r+e5)*r+e4)*r+e3)*r+e2)*r+e1)*r + e0; + z2 = ((((((f7*r+f6)*r+f5)*r+f4)*r+f3)*r+f2)*r+f1)*r + f0; + } + + return s*z1/z2; +} + + double rng·normal(void) { double f; f = rng·random(); + return sqrt(2)*erfinv(2*f-1); } /* Returns true or false on success of trial */ diff --git a/sys/libn/rules.mk b/sys/libn/rules.mk index d519b67..452587e 100644 --- a/sys/libn/rules.mk +++ b/sys/libn/rules.mk @@ -21,7 +21,7 @@ SRCS_$(d) := \ $(d)/string.c TSTS_$(d) := \ - $(d)/test.c + $(d)/test.c LIBS_$(d) := $(d)/libn.a BINS_$(d) := -- cgit v1.2.1