3 * $Id: genprimes.c,v 1.5 2004/04/01 12:50:09 mdw Exp $
5 * Generate prime number table
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 --------------------------------------------------*
32 * $Log: genprimes.c,v $
33 * Revision 1.5 2004/04/01 12:50:09 mdw
34 * Add cyclic group abstraction, with test code. Separate off exponentation
35 * functions for better static linking. Fix a buttload of bugs on the way.
36 * Generally ensure that negative exponents do inversion correctly. Add
37 * table of standard prime-field subgroups. (Binary field subgroups are
38 * currently unimplemented but easy to add if anyone ever finds a good one.)
40 * Revision 1.4 2001/03/04 13:08:10 mdw
41 * Use @DA_LAST@ to determine @MAXPRIME@, now that it exists.
43 * Revision 1.3 2000/08/15 21:41:58 mdw
44 * Create a new type for the small primes table elements.
46 * Revision 1.2 1999/12/22 15:48:39 mdw
47 * Rename output file. Make output constants unsigned.
49 * Revision 1.1 1999/11/19 13:19:37 mdw
50 * Generate small primes table.
54 /*----- Header files ------------------------------------------------------*/
62 #include <mLib/darray.h>
63 #include <mLib/dstr.h>
64 #include <mLib/mdwopt.h>
65 #include <mLib/quis.h>
66 #include <mLib/report.h>
68 /*----- Data structures ---------------------------------------------------*/
72 /*----- Main code ---------------------------------------------------------*/
74 int main(int argc
, char *argv
[])
76 int p_max
= 0, p_n
= 0;
77 char *type
= "unsigned int";
78 char *header
= "primetab.h";
79 char *source
= "primetab.c";
80 char *name
= "primetab";
88 int i
= getopt(argc
, argv
, "h:c:i:n:m:t:s:");
107 p_max
= atoi(optarg
);
116 pquis(stderr
, "Usage: $ [-n nprimes] [-m maxprime] [-t type]\n");
122 die(EXIT_FAILURE
, "bad arguments to `-n' or `-m'");
124 if (p_n
|| p_max
>= 2)
126 for (i
= 3; (!p_max
&& !p_n
) ||
127 (p_n
&& DA_LEN(&p
) < p_n
) ||
128 (p_max
&& i
<= p_max
);
131 for (j
= 0; j
< DA_LEN(&p
); j
++) {
132 if (i
% DA(&p
)[j
] == 0)
140 FILE *fp
= fopen(header
, "w");
144 die(EXIT_FAILURE
, "couldn't write `%s': %s", header
, strerror(errno
));
146 for (q
= header
; *q
; q
++) {
147 int ch
= (unsigned char)*q
;
160 * Table of small prime numbers [generated]\n\
166 #define NPRIME %luu\n\
167 #define MAXPRIME %uu\n\
169 typedef %s smallprime;\n\
170 extern smallprime %s[];\n\
175 (unsigned long)DA_LEN(&p
),
179 if (fclose(fp
) == EOF
) {
181 die(EXIT_FAILURE
, "error writing `%s': %s", header
, strerror(errno
));
186 FILE *fp
= fopen(source
, "w");
189 die(EXIT_FAILURE
, "couldn't write `%s': %s", source
, strerror(errno
));
193 * Table of small prime numbers [generated]\n\
200 for (i
= 0; i
< DA_LEN(&p
); i
++) {
203 fprintf(fp
, "%5i, ", DA(&p
)[i
]);
208 if (fclose(fp
) == EOF
) {
210 die(EXIT_FAILURE
, "error writing `%s': %s", source
, strerror(errno
));
217 /*----- That's all, folks -------------------------------------------------*/