X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/blobdiff_plain/106b481c4fb7790f93944c6f40ef39a1f1b3058e..7eaaecf58694345b4e40db9981c0c2ac0f728378:/mpreduce.c diff --git a/mpreduce.c b/mpreduce.c index 0f20ed8..bc41f60 100644 --- a/mpreduce.c +++ b/mpreduce.c @@ -60,7 +60,7 @@ int mpreduce_create(mpreduce *r, mp *p) instr_v iv = DA_INIT; unsigned long d, i; unsigned op; - size_t w, b; + size_t w, b, bb; /* --- Fill in the easy stuff --- */ @@ -101,10 +101,12 @@ int mpreduce_create(mpreduce *r, mp *p) case X0 | 0: st = Z; printf("+ %lu\n", i - 1); break; } } - if (st >= X) printf("+ %lu\n", i); + if (st >= X) printf("+ %lu\n", i - 1); + st = Z; #endif - for (i = 0, mp_scan(&sc, p); i < d - 1 && mp_step(&sc); i++) { + bb = MPW_BITS - (d + 1)%MPW_BITS; + for (i = 0, mp_scan(&sc, p); i < d && mp_step(&sc); i++) { switch (st | mp_bit(&sc)) { case Z | 1: st = Z1; break; case Z1 | 0: st = Z; op = MPRI_SUB; goto instr; @@ -114,11 +116,11 @@ int mpreduce_create(mpreduce *r, mp *p) case X0 | 0: st = Z; op = MPRI_SUB; goto instr; instr: w = (d - i)/MPW_BITS + 1; - b = (MPW_BITS + i - d - 1)%MPW_BITS; + b = (bb + i)%MPW_BITS; INSTR(op | !!b, w, b); } } - if ((DA(&iv)[DA_LEN(&iv) - 1].op & ~1u) == MPRI_SUB) { + if (DA_LEN(&iv) && (DA(&iv)[DA_LEN(&iv) - 1].op & ~1u) == MPRI_SUB) { mp_drop(r->p); DA_DESTROY(&iv); return (-1); @@ -129,7 +131,9 @@ int mpreduce_create(mpreduce *r, mp *p) /* --- Wrap up --- */ r->in = DA_LEN(&iv); - if (!r->s) { + if (!r->in) + r->iv = 0; + else if (!r->s) { r->iv = xmalloc(r->in * sizeof(mpreduce_instr)); memcpy(r->iv, DA(&iv), r->in * sizeof(mpreduce_instr)); } else { @@ -151,6 +155,10 @@ int mpreduce_create(mpreduce *r, mp *p) } } DA_DESTROY(&iv); + +#ifdef DEBUG + mpreduce_dump(r, stdout); +#endif return (0); } @@ -166,7 +174,7 @@ int mpreduce_create(mpreduce *r, mp *p) void mpreduce_destroy(mpreduce *r) { mp_drop(r->p); - xfree(r->iv); + if (r->iv) xfree(r->iv); } /* --- @mpreduce_dump@ --- *