progs/perftest.c: Use from Glibc syscall numbers.
[catacomb] / math / limlee.c
index 13bbc21..c99ba7b 100644 (file)
@@ -114,13 +114,10 @@ static void llgen(limlee_factor *f, unsigned pl, limlee_stepctx *l)
   rabin r;
   mp *p;
 
-again:
   p = mprand(l->newp, pl, l->r, 1);
   pf.step = 2;
   p = pgen(l->u.s.name, p, p, l->iev, l->iec, 0, pgen_filter, &pf,
           rabin_iters(pl), pgen_test, &r);
-  if (!p)
-    goto again;
   f->p = p;
 }
 
@@ -200,7 +197,7 @@ static int init(pgen_event *ev, limlee_stepctx *l)
 static int next(int rq, pgen_event *ev, limlee_stepctx *l)
 {
   dstr d = DSTR_INIT;
-  mp *p;
+  mp *p = 0;
   int rc;
   int dist;
   unsigned nb;
@@ -225,7 +222,7 @@ static int next(int rq, pgen_event *ev, limlee_stepctx *l)
 
     if (l->qq.p) {
       dist = l->u.s.disp < 0 ? -l->u.s.disp : l->u.s.disp;
-      if (dist && dist > l->u.s.steps/dist) {
+      if (dist && dist > l->u.s.steps/3) {
        l->pops->pfree(&l->qq, l);
        l->qq.p = 0;
       }
@@ -242,6 +239,8 @@ static int next(int rq, pgen_event *ev, limlee_stepctx *l)
        dstr_putf(&d, "%s_%lu", ev->name, l->seq++);
        l->u.s.name = d.buf;
        l->pops->pgen(&l->v[i], l->ql, l);
+       if (!l->v[i].p)
+         { mp_drop(mpmul_done(&mm)); rc = PGEN_ABORT; goto end; }
       }
       mpmul_add(&mm, l->v[i].p);
     }
@@ -254,6 +253,7 @@ static int next(int rq, pgen_event *ev, limlee_stepctx *l)
       dstr_putf(&d, "%s*_%lu", ev->name, l->seq++);
       l->u.s.name = d.buf;
       l->pops->pgen(&l->qq, l->pl - mp_bits(p), l);
+      if (!l->qq.p) { MP_DROP(p); p = 0; rc = PGEN_ABORT; break; }
       l->u.s.steps = l->u.s.disp = 0;
       p = mp_mul(p, p, l->qq.p);
     }
@@ -274,9 +274,10 @@ static int next(int rq, pgen_event *ev, limlee_stepctx *l)
 
     if ((rc = pfilt_smallfactor(p)) != PGEN_FAIL)
       break;
-    mp_drop(p);
+    MP_DROP(p); p = 0;
   }
 
+end:
   ev->m = p;
   DDESTROY(&d);
   return (rc);
@@ -389,6 +390,8 @@ mp *limlee(const char *name, mp *d, mp *newp,
 {
   limlee_stepctx l;
   rabin rr;
+  mp **v;
+  size_t i;
 
   l.f = 0; if (f) l.f |= LIMLEE_KEEPFACTORS;
   l.newp = newp;
@@ -401,15 +404,18 @@ mp *limlee(const char *name, mp *d, mp *newp,
   d = pgen(name, d, 0, oev, oec, on, limlee_step, &l,
           rabin_iters(pl), pgen_test, &rr);
 
-  if (d && f) {
-    mp **v;
-    size_t i;
-    v = xmalloc(l.nf * sizeof(mp *));
-    for (i = 0; i < l.nf; i++)
-      v[i] = l.v[i].p;
-    xfree(l.v);
-    *f = v;
-    *nf = l.nf;
+  if (f) {
+    if (!d) {
+      for (i = 0; i < l.nf; i++)
+       if (l.v[i].p) llfree(&l.v[i], &l);
+    } else {
+      v = xmalloc(l.nf * sizeof(mp *));
+      for (i = 0; i < l.nf; i++)
+       v[i] = l.v[i].p;
+      xfree(l.v);
+      *f = v;
+      *nf = l.nf;
+    }
   }
 
   return (d);