3 * $Id: pgen.c,v 1.4 1999/12/22 16:01:11 mdw Exp $
5 * Prime generation glue
7 * (c) 1999 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 /*----- Revision history --------------------------------------------------*
33 * Revision 1.4 1999/12/22 16:01:11 mdw
34 * Same file, completely different code. Main interface for new prime-
39 /*----- Header files ------------------------------------------------------*/
54 /*----- Standard prime filter ---------------------------------------------*/
56 /* --- @pgen_filter@ --- */
58 int pgen_filter(int rq
, pgen_event
*ev
, void *p
)
60 pgen_filterctx
*f
= p
;
65 rc
= pfilt_create(&f
->f
, ev
->m
);
70 if (!((f
->step
| f
->f
.m
->v
[0]) & 1))
71 rc
= pfilt_step(&f
->f
, 1);
73 rc
= pfilt_step(&f
->f
, f
->step
);
80 while (rc
== PGEN_FAIL
)
81 rc
= pfilt_step(&f
->f
, f
->step
);
82 ev
->m
= MP_COPY(f
->f
.m
);
86 /* --- @pgen_jump@ --- *
88 * Similar to the standard @pgen_filter@, but jumps in large steps rather
92 int pgen_jump(int rq
, pgen_event
*ev
, void *p
)
99 rc
= pfilt_create(&f
->f
, ev
->m
);
104 rc
= pfilt_jump(&f
->f
, f
->j
);
107 pfilt_destroy(&f
->f
);
111 while (rc
== PGEN_FAIL
)
112 rc
= pfilt_jump(&f
->f
, f
->j
);
113 ev
->m
= MP_COPY(f
->f
.m
);
117 /*----- Standard prime test -----------------------------------------------*/
119 /* --- @pgen_test@ --- */
121 int pgen_test(int rq
, pgen_event
*ev
, void *p
)
128 rabin_create(r
, ev
->m
);
132 mp
*a
= mprand_range(MP_NEW
, ev
->m
, ev
->r
, 0);
133 rc
= rabin_test(r
, a
);
145 /*----- The main driver ---------------------------------------------------*/
149 * Arguments: @const char *name@ = name of the value being searched for
150 * @mp *d@ = destination for the result integer
151 * @mp *m@ = start value to pass to stepper
152 * @pgen_proc *event@ = event handler function
153 * @void *ectx@ = context argument for event andler
154 * @unsigned steps@ = number of steps to take in search
155 * @pgen_proc *step@ = stepper function to use
156 * @void *sctx@ = context argument for stepper
157 * @unsigned tests@ = number of tests to make
158 * @pgen_proc *test@ = tester function to use
159 * @void *tctx@ = context argument for tester
161 * Returns: Pointer to final result, or null.
163 * Use: A generalized prime-number search skeleton. Yes, that's a
164 * scary number of arguments.
167 mp
*pgen(const char *name
, mp
*d
, mp
*m
, pgen_proc
*event
, void *ectx
,
168 unsigned steps
, pgen_proc
*step
, void *sctx
,
169 unsigned tests
, pgen_proc
*test
, void *tctx
)
176 /* --- Set up the initial event block --- */
185 ev
.r
= fibrand_create(0);
187 /* --- Tell the event handler we're under way --- */
189 if (event
&& event(PGEN_BEGIN
, &ev
, ectx
) == PGEN_ABORT
)
192 /* --- Set up for the initial call --- */
194 proc
= step
; ctx
= sctx
; rq
= PGEN_BEGIN
;
196 /* --- Enter the great maelstrom of state transitions --- */
210 /* --- Call the procedure and decide what to do next --- */
212 rc
= proc(rq
, &ev
, ctx
);
219 proc
= test
; ctx
= tctx
;
224 act
|= A_TEST
| A_EVENT
;
228 proc
= test
; ctx
= tctx
;
235 act
|= A_ENDTEST
| A_EVENT
;
236 proc
= step
; ctx
= sctx
;
241 act
|= A_EVENT
| A_DONE
| A_ENDSTEP
;
246 act
|= A_EVENT
| A_DONE
;
247 if (proc
== test
|| rq
== PGEN_TRY
)
249 if (proc
== test
&& rq
== PGEN_BEGIN
)
253 assert(((void)"Invalid response from function", 0));
257 /* --- If decrementing counters is requested, do that --- */
259 if ((act
& A_STEP
) && steps
) {
262 act
|= A_EVENT
| A_ENDSTEP
| A_DONE
;
268 if ((act
& A_TEST
) && tests
) {
271 act
|= A_ENDTEST
| A_ENDSTEP
| A_DONE
;
276 /* --- Report an event if so directed --- */
278 if ((act
& A_EVENT
) && event
&& event(rc
, &ev
, ectx
) == PGEN_ABORT
) {
280 if (!(act
& A_DONE
)) {
281 act
|= A_ENDSTEP
| A_DONE
;
287 /* --- Close down tester and stepper functions --- */
290 test(PGEN_DONE
, &ev
, tctx
);
292 step(PGEN_DONE
, &ev
, sctx
);
294 /* --- Stop the entire test if necessary --- */
300 /* --- Tidy up and return --- */
302 if (rc
== PGEN_ABORT
) {
306 ev
.r
->ops
->destroy(ev
.r
);
313 /*----- Test rig ----------------------------------------------------------*/
317 #include <mLib/testrig.h>
319 static int verify(dstr
*v
)
321 mp
*m
= *(mp
**)v
[0].buf
;
322 mp
*q
= *(mp
**)v
[1].buf
;
330 p
= pgen("p", MP_NEW
, m
, pgen_evspin
, 0, 0, pgen_filter
, &pf
,
331 rabin_iters(mp_bits(m
)), pgen_test
, &r
);
332 if (!p
|| MP_CMP(p
, !=, q
)) {
333 fputs("\n*** pgen failed", stderr
);
334 fputs("\nm = ", stderr
); mp_writefile(m
, stderr
, 10);
335 fputs("\np = ", stderr
); mp_writefile(p
, stderr
, 10);
336 fputs("\nq = ", stderr
); mp_writefile(q
, stderr
, 10);
345 assert(mparena_count(MPARENA_GLOBAL
) == 0);
349 static test_chunk tests
[] = {
350 { "pgen", verify
, { &type_mp
, &type_mp
, 0 } },
354 int main(int argc
, char *argv
[])
357 test_run(argc
, argv
, tests
, SRCDIR
"/tests/pgen");
362 /*----- That's all, folks -------------------------------------------------*/