5 * Simultaneous prime search
7 * (c) 2006 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Header files ------------------------------------------------------*/
34 #include <mLib/macros.h>
39 /*----- Main code ---------------------------------------------------------*/
41 /* --- @rcmerge@ --- *
43 * Arguments: @int a, b@ = a pair of @PGEN@ return codes
45 * Returns: The overall return code capturing both.
48 static int rcmerge(int a, int b)
51 if (a == PGEN_##state || b == PGEN_##state) return (PGEN_##state)
59 /* --- @pgen_simulstep@ --- *
61 * Step a collection of numbers simultaneously.
64 int pgen_simulstep(int rq, pgen_event *ev, void *p)
66 pgen_simulctx *ss = p;
68 int rc = PGEN_ABORT, nrc;
77 for (i = 0; i < ss->n; i++) {
79 q = mp_mul(q, ss->step, sp->mul);
83 sp->u.jump = xmalloc(sizeof(pfilt));
84 pfilt_create(sp->u.jump, q);
87 q = mp_mul(q, ev->m, sp->mul);
88 q = mp_add(q, q, sp->add);
89 nrc = pfilt_create(&sp->p, q);
90 rc = rcmerge(rc, nrc);
98 for (i = 0; i < ss->n; i++) {
100 if (sp->f & PGENF_JUMP)
101 nrc = pfilt_jump(&sp->p, sp->u.jump);
103 nrc = pfilt_step(&sp->p, sp->u.step);
104 rc = rcmerge(rc, nrc);
111 ev->m = MP_COPY(ss->v[0].p.m);
114 for (i = 0; i < ss->n; i++) {
116 if (sp->f & PGENF_JUMP) {
117 pfilt_destroy(sp->u.jump);
120 if (sp->f & PGENF_KEEP)
121 sp->u.x = MP_COPY(sp->p.m);
122 pfilt_destroy(&sp->p);
130 /* --- @pgen_simultest@ --- *
132 * Test a collection of numbers simultaneously.
135 int pgen_simultest(int rq, pgen_event *ev, void *p)
137 pgen_simulctx *ss = p;
146 for (i = 0; i < ss->n; i++)
147 rabin_create(&ss->v[i].r, ss->v[i].p.m);
152 for (i = 0; i < ss->n; i++) {
154 m = mprand_range(m, sp->p.m, ev->r, 0);
155 rc = rabin_test(&sp->r, m);
162 for (i = 0; i < ss->n; i++)
163 rabin_destroy(&ss->v[i].r);
170 /*----- That's all, folks -------------------------------------------------*/