projects
/
u
/
mdw
/
catacomb
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Correctly work with the bizarre negative bases the @mptext@ functions allow.
[u/mdw/catacomb]
/
pgen.c
diff --git
a/pgen.c
b/pgen.c
index
ac8db38
..
86e31c1
100644
(file)
--- a/
pgen.c
+++ b/
pgen.c
@@
-1,6
+1,6
@@
/* -*-c-*-
*
/* -*-c-*-
*
- * $Id
: pgen.c,v 1.10 2004/04/08 01:36:15 mdw Exp
$
+ * $Id$
*
* Prime generation glue
*
*
* Prime generation glue
*
@@
-49,7
+49,7
@@
int pgen_filter(int rq, pgen_event *ev, void *p)
{
pgen_filterctx *f = p;
int pgen_filter(int rq, pgen_event *ev, void *p)
{
pgen_filterctx *f = p;
- int rc = PGEN_
ABORT
;
+ int rc = PGEN_
FAIL
;
switch (rq) {
case PGEN_BEGIN:
switch (rq) {
case PGEN_BEGIN:
@@
-58,16
+58,17
@@
int pgen_filter(int rq, pgen_event *ev, void *p)
break;
case PGEN_TRY:
mp_drop(ev->m);
break;
case PGEN_TRY:
mp_drop(ev->m);
- if (!((f->step | f->f.m->v[0]) & 1))
- rc = pfilt_step(&f->f, 1);
- else
- rc = pfilt_step(&f->f, f->step);
break;
case PGEN_DONE:
pfilt_destroy(&f->f);
return (PGEN_DONE);
break;
case PGEN_DONE:
pfilt_destroy(&f->f);
return (PGEN_DONE);
+ default:
+ rc = PGEN_ABORT;
+ break;
}
}
+ if (rc == PGEN_FAIL && !((f->step | f->f.m->v[0]) & 1))
+ rc = pfilt_step(&f->f, 1);
while (rc == PGEN_FAIL)
rc = pfilt_step(&f->f, f->step);
ev->m = MP_COPY(f->f.m);
while (rc == PGEN_FAIL)
rc = pfilt_step(&f->f, f->step);
ev->m = MP_COPY(f->f.m);
@@
-174,6
+175,9
@@
mp *pgen(const char *name, mp *d, mp *m, pgen_proc *event, void *ectx,
int rq, rc;
pgen_proc *proc;
void *ctx;
int rq, rc;
pgen_proc *proc;
void *ctx;
+ int p;
+
+ enum { P_STEP, P_TEST };
/* --- Set up the initial event block --- */
/* --- Set up the initial event block --- */
@@
-188,67
+192,67
@@
mp *pgen(const char *name, mp *d, mp *m, pgen_proc *event, void *ectx,
/* --- Tell the event handler we're under way --- */
/* --- Tell the event handler we're under way --- */
- if (event && event(PGEN_BEGIN, &ev, ectx) == PGEN_ABORT)
+ if (event && event(PGEN_BEGIN, &ev, ectx) == PGEN_ABORT) {
+ ev.r->ops->destroy(ev.r);
return (0);
return (0);
+ }
/* --- Set up for the initial call --- */
/* --- Set up for the initial call --- */
- proc = step; ctx = sctx; rq = PGEN_BEGIN;
+ proc = step; ctx = sctx;
p = P_STEP;
rq = PGEN_BEGIN;
/* --- Enter the great maelstrom of state transitions --- */
for (;;) {
unsigned act = 0;
/* --- Enter the great maelstrom of state transitions --- */
for (;;) {
unsigned act = 0;
- enum {
- A_STEP = 1u,
- A_TEST = 2u,
- A_EVENT = 4u,
- A_ENDTEST = 8u,
- A_ENDSTEP = 16u,
- A_DONE = 32u
- };
+#define A_STEP 1u
+#define A_TEST 2u
+#define A_EVENT 4u
+#define A_ENDTEST 8u
+#define A_ENDSTEP 16u
+#define A_DONE 32u
/* --- Call the procedure and decide what to do next --- */
rc = proc(rq, &ev, ctx);
switch (rc) {
case PGEN_TRY:
/* --- Call the procedure and decide what to do next --- */
rc = proc(rq, &ev, ctx);
switch (rc) {
case PGEN_TRY:
- if (p
roc == test
)
+ if (p
== P_TEST
)
rq = PGEN_TRY;
else {
act |= A_EVENT;
rq = PGEN_TRY;
else {
act |= A_EVENT;
- proc = test; ctx = tctx;
+ proc = test; ctx = tctx;
p = P_TEST;
rq = PGEN_BEGIN;
}
break;
case PGEN_PASS:
act |= A_TEST | A_EVENT;
rq = PGEN_BEGIN;
}
break;
case PGEN_PASS:
act |= A_TEST | A_EVENT;
- if (p
roc == test
)
+ if (p
== P_TEST
)
rq = PGEN_TRY;
else {
rq = PGEN_TRY;
else {
- proc = test; ctx = tctx;
+ proc = test; ctx = tctx;
p = P_TEST;
rq = PGEN_BEGIN;
}
break;
case PGEN_FAIL:
act |= A_STEP;
rq = PGEN_BEGIN;
}
break;
case PGEN_FAIL:
act |= A_STEP;
- if (p
roc == test
) {
+ if (p
== P_TEST
) {
act |= A_ENDTEST | A_EVENT;
act |= A_ENDTEST | A_EVENT;
- proc = step; ctx = sctx;
+ proc = step; ctx = sctx;
p = P_STEP;
}
rq = PGEN_TRY;
break;
case PGEN_DONE:
act |= A_EVENT | A_DONE | A_ENDSTEP;
}
rq = PGEN_TRY;
break;
case PGEN_DONE:
act |= A_EVENT | A_DONE | A_ENDSTEP;
- if (p
roc == test
)
+ if (p
== P_TEST
)
act |= A_ENDTEST;
break;
case PGEN_ABORT:
act |= A_EVENT | A_DONE;
act |= A_ENDTEST;
break;
case PGEN_ABORT:
act |= A_EVENT | A_DONE;
- if (p
roc == test
|| rq == PGEN_TRY)
+ if (p
== P_TEST
|| rq == PGEN_TRY)
act |= A_ENDSTEP;
act |= A_ENDSTEP;
- if (p
roc == test && rq =
= PGEN_BEGIN)
+ if (p
== P_TEST && rq !
= PGEN_BEGIN)
act |= A_ENDTEST;
break;
default:
act |= A_ENDTEST;
break;
default:
@@
-281,7
+285,7
@@
mp *pgen(const char *name, mp *d, mp *m, pgen_proc *event, void *ectx,
rc = PGEN_ABORT;
if (!(act & A_DONE)) {
act |= A_ENDSTEP | A_DONE;
rc = PGEN_ABORT;
if (!(act & A_DONE)) {
act |= A_ENDSTEP | A_DONE;
- if (p
roc == test
)
+ if (p
== P_TEST
)
act |= A_ENDTEST;
}
}
act |= A_ENDTEST;
}
}
@@
-321,21
+325,20
@@
mp *pgen(const char *name, mp *d, mp *m, pgen_proc *event, void *ectx,
int pgen_primep(mp *p, grand *gr)
{
int pgen_primep(mp *p, grand *gr)
{
- int i
= rabin_iters(mp_bits(p))
;
+ int i;
rabin r;
mp *x = MP_NEW;
rabin r;
mp *x = MP_NEW;
- if (MP_
ISNEG
(p)) return (0);
+ if (MP_
NEGP
(p)) return (0);
switch (pfilt_smallfactor(p)) {
case PGEN_DONE: return (1);
case PGEN_FAIL: return (0);
}
rabin_create(&r, p);
switch (pfilt_smallfactor(p)) {
case PGEN_DONE: return (1);
case PGEN_FAIL: return (0);
}
rabin_create(&r, p);
-
while (i
) {
+
for (i = 32; i; i--
) {
x = mprand_range(x, p, gr, 0);
if (rabin_rtest(&r, x) == PGEN_FAIL)
break;
x = mprand_range(x, p, gr, 0);
if (rabin_rtest(&r, x) == PGEN_FAIL)
break;
- i--;
}
MP_DROP(x);
rabin_destroy(&r);
}
MP_DROP(x);
rabin_destroy(&r);