math/mpreduce.h: Missing include files.
[u/mdw/catacomb] / math / genprimes.c
1 /* -*-c-*-
2 *
3 * Generate prime number table
4 *
5 * (c) 1999 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of Catacomb.
11 *
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.
16 *
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.
21 *
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,
25 * MA 02111-1307, USA.
26 */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 #include <ctype.h>
31 #include <errno.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35
36 #include <mLib/darray.h>
37 #include <mLib/dstr.h>
38 #include <mLib/mdwopt.h>
39 #include <mLib/quis.h>
40 #include <mLib/report.h>
41
42 /*----- Data structures ---------------------------------------------------*/
43
44 DA_DECL(intv, int);
45
46 /*----- Main code ---------------------------------------------------------*/
47
48 int main(int argc, char *argv[])
49 {
50 int p_max = 0, p_n = 0;
51 char *type = "unsigned int";
52 char *header = "primetab.h";
53 char *source = "primetab.c";
54 char *name = "primetab";
55 char *sym = 0;
56 intv p = DA_INIT;
57 int i;
58
59 ego(argv[0]);
60
61 for (;;) {
62 int i = getopt(argc, argv, "h:c:i:n:m:t:s:");
63 if (i < 0)
64 break;
65 switch (i) {
66 case 'h':
67 header = optarg;
68 break;
69 case 'c':
70 source = optarg;
71 break;
72 case 'i':
73 name = optarg;
74 break;
75 case 'n':
76 p_max = 0;
77 p_n = atoi(optarg);
78 break;
79 case 'm':
80 p_n = 0;
81 p_max = atoi(optarg);
82 break;
83 case 't':
84 type = optarg;
85 break;
86 case 's':
87 sym = optarg;
88 break;
89 default:
90 pquis(stderr, "Usage: $ [-n nprimes] [-m maxprime] [-t type]\n");
91 exit(EXIT_FAILURE);
92 }
93 }
94
95 if (!p_max && !p_n)
96 die(EXIT_FAILURE, "bad arguments to `-n' or `-m'");
97
98 if (p_n || p_max >= 2)
99 DA_PUSH(&p, 2);
100 for (i = 3; (!p_max && !p_n) ||
101 (p_n && DA_LEN(&p) < p_n) ||
102 (p_max && i <= p_max);
103 i += 2) {
104 int j;
105 for (j = 0; j < DA_LEN(&p); j++) {
106 if (i % DA(&p)[j] == 0)
107 goto composite;
108 }
109 DA_PUSH(&p, i);
110 composite:;
111 }
112
113 {
114 FILE *fp = fopen(header, "w");
115 dstr d = DSTR_INIT;
116 char *q;
117 if (!fp)
118 die(EXIT_FAILURE, "couldn't write `%s': %s", header, strerror(errno));
119 if (!sym) {
120 for (q = header; *q; q++) {
121 int ch = (unsigned char)*q;
122 if (isalnum(ch))
123 ch = toupper(ch);
124 else
125 ch = '_';
126 DPUTC(&d, ch);
127 }
128 DPUTZ(&d);
129 sym = d.buf;
130 }
131 fprintf(fp, "\
132 /* -*-c-*-\n\
133 *\n\
134 * Table of small prime numbers [generated]\n\
135 */\n\
136 \n\
137 #ifndef %s\n\
138 #define %s\n\
139 \n\
140 #define NPRIME %luu\n\
141 #define MAXPRIME %uu\n\
142 \n\
143 typedef %s smallprime;\n\
144 extern const smallprime %s[];\n\
145 \n\
146 #endif\n\
147 ",
148 sym, sym,
149 (unsigned long)DA_LEN(&p),
150 DA_LAST(&p),
151 type, name);
152 dstr_destroy(&d);
153 if (fclose(fp) == EOF) {
154 remove(header);
155 die(EXIT_FAILURE, "error writing `%s': %s", header, strerror(errno));
156 }
157 }
158
159 {
160 FILE *fp = fopen(source, "w");
161 int i;
162 if (!fp)
163 die(EXIT_FAILURE, "couldn't write `%s': %s", source, strerror(errno));
164 fprintf(fp, "\
165 /* -*-c-*-\n\
166 *\n\
167 * Table of small prime numbers [generated]\n\
168 */\n\
169 \n\
170 #include \"%s\"\n\
171 \n\
172 const %s %s[] = {",
173 header, type, name);
174 for (i = 0; i < DA_LEN(&p); i++) {
175 if (i % 8 == 0)
176 fputs("\n ", fp);
177 fprintf(fp, "%5i, ", DA(&p)[i]);
178 }
179 fputs("\n\
180 };\n\
181 ", fp);
182 if (fclose(fp) == EOF) {
183 remove(source);
184 die(EXIT_FAILURE, "error writing `%s': %s", source, strerror(errno));
185 }
186 }
187
188 return (0);
189 }
190
191 /*----- That's all, folks -------------------------------------------------*/