3 * $Id: genprimes.c,v 1.6 2004/04/02 01:03:49 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.6 2004/04/02 01:03:49 mdw
34 * Miscellaneous constification.
36 * Revision 1.5 2004/04/01 12:50:09 mdw
37 * Add cyclic group abstraction, with test code. Separate off exponentation
38 * functions for better static linking. Fix a buttload of bugs on the way.
39 * Generally ensure that negative exponents do inversion correctly. Add
40 * table of standard prime-field subgroups. (Binary field subgroups are
41 * currently unimplemented but easy to add if anyone ever finds a good one.)
43 * Revision 1.4 2001/03/04 13:08:10 mdw
44 * Use @DA_LAST@ to determine @MAXPRIME@, now that it exists.
46 * Revision 1.3 2000/08/15 21:41:58 mdw
47 * Create a new type for the small primes table elements.
49 * Revision 1.2 1999/12/22 15:48:39 mdw
50 * Rename output file. Make output constants unsigned.
52 * Revision 1.1 1999/11/19 13:19:37 mdw
53 * Generate small primes table.
57 /*----- Header files ------------------------------------------------------*/
65 #include <mLib/darray.h>
66 #include <mLib/dstr.h>
67 #include <mLib/mdwopt.h>
68 #include <mLib/quis.h>
69 #include <mLib/report.h>
71 /*----- Data structures ---------------------------------------------------*/
75 /*----- Main code ---------------------------------------------------------*/
77 int main(int argc
, char *argv
[])
79 int p_max
= 0, p_n
= 0;
80 char *type
= "unsigned int";
81 char *header
= "primetab.h";
82 char *source
= "primetab.c";
83 char *name
= "primetab";
91 int i
= getopt(argc
, argv
, "h:c:i:n:m:t:s:");
110 p_max
= atoi(optarg
);
119 pquis(stderr
, "Usage: $ [-n nprimes] [-m maxprime] [-t type]\n");
125 die(EXIT_FAILURE
, "bad arguments to `-n' or `-m'");
127 if (p_n
|| p_max
>= 2)
129 for (i
= 3; (!p_max
&& !p_n
) ||
130 (p_n
&& DA_LEN(&p
) < p_n
) ||
131 (p_max
&& i
<= p_max
);
134 for (j
= 0; j
< DA_LEN(&p
); j
++) {
135 if (i
% DA(&p
)[j
] == 0)
143 FILE *fp
= fopen(header
, "w");
147 die(EXIT_FAILURE
, "couldn't write `%s': %s", header
, strerror(errno
));
149 for (q
= header
; *q
; q
++) {
150 int ch
= (unsigned char)*q
;
163 * Table of small prime numbers [generated]\n\
169 #define NPRIME %luu\n\
170 #define MAXPRIME %uu\n\
172 typedef %s smallprime;\n\
173 extern const smallprime %s[];\n\
178 (unsigned long)DA_LEN(&p
),
182 if (fclose(fp
) == EOF
) {
184 die(EXIT_FAILURE
, "error writing `%s': %s", header
, strerror(errno
));
189 FILE *fp
= fopen(source
, "w");
192 die(EXIT_FAILURE
, "couldn't write `%s': %s", source
, strerror(errno
));
196 * Table of small prime numbers [generated]\n\
203 for (i
= 0; i
< DA_LEN(&p
); i
++) {
206 fprintf(fp
, "%5i, ", DA(&p
)[i
]);
211 if (fclose(fp
) == EOF
) {
213 die(EXIT_FAILURE
, "error writing `%s': %s", source
, strerror(errno
));
220 /*----- That's all, folks -------------------------------------------------*/