3 * Simultaneous prime search
5 * (c) 2006 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
32 #include <mLib/macros.h>
37 /*----- Main code ---------------------------------------------------------*/
39 /* --- @rcmerge@ --- *
41 * Arguments: @int a, b@ = a pair of @PGEN@ return codes
43 * Returns: The overall return code capturing both.
46 static int rcmerge(int a, int b)
49 if (a == PGEN_##state || b == PGEN_##state) return (PGEN_##state)
57 /* --- @pgen_simulstep@ --- *
59 * Step a collection of numbers simultaneously.
62 int pgen_simulstep(int rq, pgen_event *ev, void *p)
64 pgen_simulctx *ss = p;
66 int rc = PGEN_ABORT, nrc;
75 for (i = 0; i < ss->n; i++) {
77 q = mp_mul(q, ss->step, sp->mul);
81 sp->u.jump = xmalloc(sizeof(pfilt));
82 pfilt_create(sp->u.jump, q);
85 q = mp_mul(q, ev->m, sp->mul);
86 q = mp_add(q, q, sp->add);
87 nrc = pfilt_create(&sp->p, q);
88 rc = rcmerge(rc, nrc);
96 for (i = 0; i < ss->n; i++) {
98 if (sp->f & PGENF_JUMP)
99 nrc = pfilt_jump(&sp->p, sp->u.jump);
101 nrc = pfilt_step(&sp->p, sp->u.step);
102 rc = rcmerge(rc, nrc);
109 ev->m = MP_COPY(ss->v[0].p.m);
112 for (i = 0; i < ss->n; i++) {
114 if (sp->f & PGENF_JUMP) {
115 pfilt_destroy(sp->u.jump);
118 if (sp->f & PGENF_KEEP)
119 sp->u.x = MP_COPY(sp->p.m);
120 pfilt_destroy(&sp->p);
128 /* --- @pgen_simultest@ --- *
130 * Test a collection of numbers simultaneously.
133 int pgen_simultest(int rq, pgen_event *ev, void *p)
135 pgen_simulctx *ss = p;
144 for (i = 0; i < ss->n; i++)
145 rabin_create(&ss->v[i].r, ss->v[i].p.m);
150 for (i = 0; i < ss->n; i++) {
152 m = mprand_range(m, sp->p.m, ev->r, 0);
153 rc = rabin_test(&sp->r, m);
160 for (i = 0; i < ss->n; i++)
161 rabin_destroy(&ss->v[i].r);
168 /*----- That's all, folks -------------------------------------------------*/