Force subkeys to be sorted in structured keys.
[u/mdw/catacomb] / genprimes.c
CommitLineData
8ecfe0dc 1/* -*-c-*-
2 *
4e66da02 3 * $Id: genprimes.c,v 1.6 2004/04/02 01:03:49 mdw Exp $
8ecfe0dc 4 *
5 * Generate prime number table
6 *
7 * (c) 1999 Straylight/Edgeware
8 */
9
10/*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Catacomb.
13 *
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.
18 *
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.
23 *
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,
27 * MA 02111-1307, USA.
28 */
29
30/*----- Revision history --------------------------------------------------*
31 *
32 * $Log: genprimes.c,v $
4e66da02 33 * Revision 1.6 2004/04/02 01:03:49 mdw
34 * Miscellaneous constification.
35 *
34e4f738 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.)
42 *
62f34e46 43 * Revision 1.4 2001/03/04 13:08:10 mdw
44 * Use @DA_LAST@ to determine @MAXPRIME@, now that it exists.
45 *
88b913b1 46 * Revision 1.3 2000/08/15 21:41:58 mdw
47 * Create a new type for the small primes table elements.
48 *
2791153f 49 * Revision 1.2 1999/12/22 15:48:39 mdw
50 * Rename output file. Make output constants unsigned.
51 *
8ecfe0dc 52 * Revision 1.1 1999/11/19 13:19:37 mdw
53 * Generate small primes table.
54 *
55 */
56
57/*----- Header files ------------------------------------------------------*/
58
59#include <ctype.h>
60#include <errno.h>
61#include <stdio.h>
62#include <stdlib.h>
63#include <string.h>
64
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>
70
71/*----- Data structures ---------------------------------------------------*/
72
73DA_DECL(intv, int);
74
75/*----- Main code ---------------------------------------------------------*/
76
77int main(int argc, char *argv[])
78{
79 int p_max = 0, p_n = 0;
80 char *type = "unsigned int";
2791153f 81 char *header = "primetab.h";
82 char *source = "primetab.c";
83 char *name = "primetab";
34e4f738 84 char *sym = 0;
8ecfe0dc 85 intv p = DA_INIT;
86 int i;
87
88 ego(argv[0]);
89
90 for (;;) {
34e4f738 91 int i = getopt(argc, argv, "h:c:i:n:m:t:s:");
8ecfe0dc 92 if (i < 0)
93 break;
94 switch (i) {
95 case 'h':
96 header = optarg;
97 break;
98 case 'c':
99 source = optarg;
100 break;
101 case 'i':
102 name = optarg;
103 break;
104 case 'n':
105 p_max = 0;
106 p_n = atoi(optarg);
107 break;
108 case 'm':
109 p_n = 0;
110 p_max = atoi(optarg);
111 break;
112 case 't':
113 type = optarg;
114 break;
34e4f738 115 case 's':
116 sym = optarg;
117 break;
8ecfe0dc 118 default:
119 pquis(stderr, "Usage: $ [-n nprimes] [-m maxprime] [-t type]\n");
120 exit(EXIT_FAILURE);
121 }
122 }
123
124 if (!p_max && !p_n)
125 die(EXIT_FAILURE, "bad arguments to `-n' or `-m'");
126
88b913b1 127 if (p_n || p_max >= 2)
8ecfe0dc 128 DA_PUSH(&p, 2);
129 for (i = 3; (!p_max && !p_n) ||
130 (p_n && DA_LEN(&p) < p_n) ||
131 (p_max && i <= p_max);
132 i += 2) {
133 int j;
134 for (j = 0; j < DA_LEN(&p); j++) {
135 if (i % DA(&p)[j] == 0)
136 goto composite;
137 }
138 DA_PUSH(&p, i);
139 composite:;
140 }
141
142 {
143 FILE *fp = fopen(header, "w");
144 dstr d = DSTR_INIT;
145 char *q;
146 if (!fp)
147 die(EXIT_FAILURE, "couldn't write `%s': %s", header, strerror(errno));
34e4f738 148 if (!sym) {
149 for (q = header; *q; q++) {
150 int ch = (unsigned char)*q;
151 if (isalnum(ch))
152 ch = toupper(ch);
153 else
154 ch = '_';
155 DPUTC(&d, ch);
156 }
157 DPUTZ(&d);
158 sym = d.buf;
8ecfe0dc 159 }
8ecfe0dc 160 fprintf(fp, "\
161/* -*-c-*-\n\
162 *\n\
163 * Table of small prime numbers [generated]\n\
164 */\n\
165\n\
166#ifndef %s\n\
167#define %s\n\
168\n\
2791153f 169#define NPRIME %luu\n\
170#define MAXPRIME %uu\n\
8ecfe0dc 171\n\
88b913b1 172typedef %s smallprime;\n\
4e66da02 173extern const smallprime %s[];\n\
8ecfe0dc 174\n\
175#endif\n\
176",
34e4f738 177 sym, sym,
8ecfe0dc 178 (unsigned long)DA_LEN(&p),
62f34e46 179 DA_LAST(&p),
8ecfe0dc 180 type, name);
181 dstr_destroy(&d);
182 if (fclose(fp) == EOF) {
183 remove(header);
184 die(EXIT_FAILURE, "error writing `%s': %s", header, strerror(errno));
185 }
186 }
187
188 {
189 FILE *fp = fopen(source, "w");
190 int i;
191 if (!fp)
192 die(EXIT_FAILURE, "couldn't write `%s': %s", source, strerror(errno));
193 fprintf(fp, "\
194/* -*-c-*-\n\
195 *\n\
196 * Table of small prime numbers [generated]\n\
197 */\n\
198\n\
199#include \"%s\"\n\
200\n\
4e66da02 201const %s %s[] = {",
8ecfe0dc 202 header, type, name);
203 for (i = 0; i < DA_LEN(&p); i++) {
204 if (i % 8 == 0)
205 fputs("\n ", fp);
206 fprintf(fp, "%5i, ", DA(&p)[i]);
207 }
208 fputs("\n\
209};\n\
210", fp);
211 if (fclose(fp) == EOF) {
212 remove(source);
213 die(EXIT_FAILURE, "error writing `%s': %s", source, strerror(errno));
214 }
215 }
216
217 return (0);
218}
219
220/*----- That's all, folks -------------------------------------------------*/