From 06fdf49c5f6eb6181a05bf17363c6b016878999f Mon Sep 17 00:00:00 2001 From: Paul Driver Date: Tue, 29 May 2018 21:29:53 -0700 Subject: [PATCH] faster jam --- jets/e/jam.c | 307 +++++++++++++++++++++++++-------------------------- 1 file changed, 153 insertions(+), 154 deletions(-) diff --git a/jets/e/jam.c b/jets/e/jam.c index 0886dad5a..017b6a4a8 100644 --- a/jets/e/jam.c +++ b/jets/e/jam.c @@ -3,185 +3,184 @@ */ #include "all.h" -/* functions -*/ +#define JAM_NONE 0 +#define JAM_HEAD 1 +#define JAM_TAIL 2 - static u3_noun - _jam_pair(u3_noun x, u3_noun d, u3_noun e) - { - u3_noun r, p_d, q_d, r_d; - u3x_trel(d, &p_d, &q_d, &r_d); - { - u3_noun y = u3qa_add(x, p_d); - u3_noun p_e, q_e, r_e; +typedef struct { + c3_w a_w; + c3_w b_w; + c3_w bit_w; + c3_w* wor_w; +} _jam_buf; - u3x_trel(e, &p_e, &q_e, &r_e); - { - u3_noun z = u3qa_add(p_d, p_e); +typedef struct { + c3_y sat_y; + u3_noun a; +} _jam_frame; - r = u3nt(u3qa_add(2, z), u3k(q_e), 0); +static void +_jam_buf_grow(_jam_buf* buf_u, c3_w mor_w) +{ + c3_w wan_w = buf_u->bit_w + mor_w; - u3z(z); - } - u3z(y); + if ( wan_w < mor_w ) { + // overflowed c3_w bits + u3m_bail(c3__fail); + } + + if ( wan_w > buf_u->a_w ) { + c3_w old_w, new_w, c_w = 0; + + old_w = buf_u->a_w >> 5; + if ( (old_w << 5) != buf_u->a_w ) { + ++old_w; } - u3z(x); - u3z(d); - u3z(e); - return r; + + // fibonacci growth + while ( c_w < wan_w ) { + c_w = buf_u->a_w + buf_u->b_w; + buf_u->b_w = buf_u->a_w; + buf_u->a_w = c_w; + } + + new_w = c_w >> 5; + if ( (new_w << 5) != c_w ) { + ++new_w; + } + + buf_u->wor_w = u3a_realloc(buf_u->wor_w, new_w * sizeof(c3_w)); + memset(buf_u->wor_w + old_w, 0, (new_w - old_w) * sizeof(c3_w)); } +} - static u3_noun - _jam_flat(u3_atom a, u3_noun l) - { - u3_noun d = u3qe_mat(a); - u3_noun x = u3qa_add(1, u3h(d)); - u3_noun y = u3nt - (u3k(x), u3nc(u3nc(x, u3qc_lsh(0, 1, u3t(d))), u3k(l)), 0); +static void +_jam_buf_chop(_jam_buf* buf_u, c3_w met_w, u3_noun a) +{ + c3_w bit_w = buf_u->bit_w; + _jam_buf_grow(buf_u, met_w); + u3r_chop(0, 0, met_w, bit_w, buf_u->wor_w, a); + buf_u->bit_w += met_w; +} - u3z(d); - u3z(l); +static void +_jam_buf_atom(_jam_buf* buf_u, u3_noun a) +{ + u3_noun mat = u3qe_mat(a); + _jam_buf_chop(buf_u, u3h(mat), u3t(mat)); + u3z(mat); +} - return y; +static u3_noun +_jam_buf_top(u3_noun a) +{ + u3p(u3h_root) har_p = u3h_new(); + c3_o nor_o = u3a_is_north(u3R); + c3_y wis_y = c3_wiseof(_jam_frame); + c3_ys mov = ( c3y == nor_o ? -wis_y : wis_y ); + c3_ys off = ( c3y == nor_o ? 0 : -wis_y ); + _jam_frame* fam, *don = u3to(_jam_frame, u3R->cap_p + off); + _jam_buf buf_u; + u3_weak c; + c3_o cel_o; + c3_w a_w, c_w, wor_w, len_w, *sal_w; + + buf_u.a_w = 10946; // fib(21) # of bits starting in wor_w + buf_u.b_w = 6765; // fib(20) roughly 1K, a good start + wor_w = (buf_u.a_w>>5); + if ( (wor_w<<5) != buf_u.a_w ) { + ++wor_w; } + buf_u.wor_w = u3a_calloc(wor_w, sizeof(c3_w)); + buf_u.bit_w = 0; - static u3_noun - _jam_ptr(u3_atom u_c, u3_noun l) - { - u3_noun d = u3qe_mat(u_c); - u3_atom x = u3qc_lsh(0, 2, u3t(d)); - u3_atom y = u3qa_add(2, u3h(d)); - u3_noun z = u3nt - (u3k(y), u3nc(u3nc(y, u3qc_mix(3, x)), u3k(l)), 0); + u3R->cap_p += mov; + fam = u3to(_jam_frame, u3R->cap_p + off); + fam->a = a; + fam->sat_y = JAM_NONE; - u3z(d); - u3z(x); - u3z(l); - - return z; - } - - #define JAM_NONE 0 - #define JAM_HEAD 1 - #define JAM_TAIL 2 - - typedef struct { - c3_y sat_y; - u3_noun nun; - u3_noun len; - u3_noun lis; - u3_noun hed; - } jamframe; - - static inline jamframe* - _jam_push(c3_ys mov, c3_ys off) - { - u3R->cap_p += mov; - return u3to(jamframe, u3R->cap_p + off); - } - - static inline jamframe* - _jam_pop(c3_ys mov, c3_ys off) - { - u3R->cap_p -= mov; - return u3to(jamframe, u3R->cap_p + off); - } - - static u3_noun - _jam_cap(u3_atom a) - { - u3p(jamframe) empty = u3R->cap_p; - u3p(u3h_root) har_p = u3h_new(); - c3_o nor_o = u3a_is_north(u3R); - c3_y wis_y = c3_wiseof(jamframe); - c3_ys mov = ( c3y == nor_o ? -wis_y : wis_y ); - c3_ys off = ( c3y == nor_o ? 0 : -wis_y ); - jamframe* fam = _jam_push(mov, off); - jamframe* don = u3to(jamframe, empty + off); - - fam->sat_y = JAM_NONE; - fam->nun = a; - fam->len = 0; - fam->lis = u3_nul; - - u3_noun q, r = u3_none; - - while ( don != fam ) { - switch ( fam->sat_y ) { - case JAM_NONE: { - u3_noun nun = fam->nun; - u3_noun len = fam->len; - u3_noun lis = fam->lis; - u3_weak got = u3h_get(har_p, nun); - - if ( u3_none == got ) { - u3h_put(har_p, nun, u3k(len)); - if ( c3n == u3du(nun) ) { - r = _jam_flat(nun, lis); - fam = _jam_pop(mov, off); - u3z(len); - } - else { - fam->sat_y = JAM_HEAD; - fam = _jam_push(mov, off); - fam->sat_y = JAM_NONE; - fam->nun = u3h(nun); - fam->len = u3qa_add(2, len); - fam->lis = u3nc(u3nc(2, 1), lis); - } + while ( fam != don ) { + switch ( fam->sat_y ) { + case JAM_NONE: + a = fam->a; + cel_o = u3du(a); + c = u3h_git(har_p, a); + if ( u3_none != c ) { + c_w = u3r_met(0, c); + if ( c3y == cel_o ) { + _jam_buf_chop(&buf_u, 2, 3); + _jam_buf_atom(&buf_u, c); } else { - if ( c3y == u3ud(nun) && (u3r_met(0, nun) <= u3r_met(0, got)) ) { - r = _jam_flat(nun, lis); + a_w = u3r_met(0, a); + if ( a_w <= c_w ) { + _jam_buf_chop(&buf_u, 1, 0); + _jam_buf_atom(&buf_u, a); } else { - r = _jam_ptr(got, lis); + _jam_buf_chop(&buf_u, 2, 3); + _jam_buf_atom(&buf_u, c); } - fam = _jam_pop(mov, off); - u3z(len); } - break; + u3R->cap_p -= mov; + fam = u3to(_jam_frame, u3R->cap_p + off); } - case JAM_HEAD: { - u3_noun p_r, q_r, r_r; - u3x_trel(r, &p_r, &q_r, &r_r); - u3_noun nun = fam->nun; - fam->sat_y = JAM_TAIL; - fam->hed = r; - u3_noun z = u3qa_add(2, fam->len); - fam = _jam_push(mov, off); - fam->sat_y = JAM_NONE; - fam->nun = u3t(nun); - fam->len = u3qa_add(z, p_r); - fam->lis = u3k(q_r); - u3z(z); - break; - } - case JAM_TAIL: { - u3_noun len = fam->len; - r = _jam_pair(u3qa_add(2, len), fam->hed, r); - fam = _jam_pop(mov, off); - u3z(len); - break; - } - default: - c3_assert(0); - return u3_none; - } - } + else { + u3h_put(har_p, a, buf_u.bit_w); + if ( c3n == cel_o ) { + _jam_buf_chop(&buf_u, 1, 0); + _jam_buf_atom(&buf_u, a); + u3R->cap_p -= mov; + fam = u3to(_jam_frame, u3R->cap_p + off); + } + else { + fam->sat_y = JAM_HEAD; - q = u3qb_flop(u3h(u3t(r))); - u3z(r); - r = u3qc_can(0, q); - u3z(q); - u3h_free(har_p); - return r; + u3R->cap_p += mov; + fam = u3to(_jam_frame, u3R->cap_p + off); + fam->sat_y = JAM_NONE; + fam->a = u3h(a); + + _jam_buf_chop(&buf_u, 2, 1); + } + } + break; + + case JAM_HEAD: + a = fam->a; + fam->sat_y = JAM_TAIL; + + u3R->cap_p += mov; + fam = u3to(_jam_frame, u3R->cap_p + off); + fam->sat_y = JAM_NONE; + fam->a = u3t(a); + break; + + case JAM_TAIL: + u3R->cap_p -= mov; + fam = u3to(_jam_frame, u3R->cap_p + off); + break; + } } + + len_w = (buf_u.bit_w>>5); + if ( (len_w<<5) != buf_u.bit_w ) { + ++len_w; + } + sal_w = u3a_slab(len_w); + memcpy(sal_w, buf_u.wor_w, len_w<<2); + u3a_free(buf_u.wor_w); + u3h_free(har_p); + return u3a_mint(sal_w, len_w); +} + +/* functions +*/ u3_noun u3qe_jam(u3_atom a) { - return _jam_cap(a); + return _jam_buf_top(a); } u3_noun u3we_jam(u3_noun cor)