8b47ac942e5ba79078773dff7267d32a69554c1b
[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 *hdrbase;
56 char *sym = 0;
57 intv p = DA_INIT;
58 int i;
59
60 ego(argv[0]);
61
62 for (;;) {
63 int i = getopt(argc, argv, "h:c:i:n:m:t:s:");
64 if (i < 0)
65 break;
66 switch (i) {
67 case 'h':
68 header = optarg;
69 break;
70 case 'c':
71 source = optarg;
72 break;
73 case 'i':
74 name = optarg;
75 break;
76 case 'n':
77 p_max = 0;
78 p_n = atoi(optarg);
79 break;
80 case 'm':
81 p_n = 0;
82 p_max = atoi(optarg);
83 break;
84 case 't':
85 type = optarg;
86 break;
87 case 's':
88 sym = optarg;
89 break;
90 default:
91 pquis(stderr, "Usage: $ [-n nprimes] [-m maxprime] [-t type]\n");
92 exit(EXIT_FAILURE);
93 }
94 }
95
96 if (!p_max && !p_n)
97 die(EXIT_FAILURE, "bad arguments to `-n' or `-m'");
98
99 if ((hdrbase = strrchr(header, '/')) == 0) hdrbase = header;
100 else hdrbase++;
101
102 if (p_n || p_max >= 2)
103 DA_PUSH(&p, 2);
104 for (i = 3; (!p_max && !p_n) ||
105 (p_n && DA_LEN(&p) < p_n) ||
106 (p_max && i <= p_max);
107 i += 2) {
108 int j;
109 for (j = 0; j < DA_LEN(&p); j++) {
110 if (i % DA(&p)[j] == 0)
111 goto composite;
112 }
113 DA_PUSH(&p, i);
114 composite:;
115 }
116
117 {
118 FILE *fp = fopen(header, "w");
119 dstr d = DSTR_INIT;
120 char *q;
121 if (!fp)
122 die(EXIT_FAILURE, "couldn't write `%s': %s", header, strerror(errno));
123 if (!sym) {
124 for (q = header; *q; q++) {
125 int ch = (unsigned char)*q;
126 if (isalnum(ch))
127 ch = toupper(ch);
128 else
129 ch = '_';
130 DPUTC(&d, ch);
131 }
132 DPUTZ(&d);
133 sym = d.buf;
134 }
135 fprintf(fp, "\
136 /* -*-c-*-\n\
137 *\n\
138 * Table of small prime numbers [generated]\n\
139 */\n\
140 \n\
141 #ifndef %s\n\
142 #define %s\n\
143 \n\
144 #define NPRIME %luu\n\
145 #define MAXPRIME %uu\n\
146 \n\
147 typedef %s smallprime;\n\
148 extern const smallprime %s[];\n\
149 \n\
150 #endif\n\
151 ",
152 sym, sym,
153 (unsigned long)DA_LEN(&p),
154 DA_LAST(&p),
155 type, name);
156 dstr_destroy(&d);
157 if (fclose(fp) == EOF) {
158 remove(header);
159 die(EXIT_FAILURE, "error writing `%s': %s", header, strerror(errno));
160 }
161 }
162
163 {
164 FILE *fp = fopen(source, "w");
165 int i;
166 if (!fp)
167 die(EXIT_FAILURE, "couldn't write `%s': %s", source, strerror(errno));
168 fprintf(fp, "\
169 /* -*-c-*-\n\
170 *\n\
171 * Table of small prime numbers [generated]\n\
172 */\n\
173 \n\
174 #include \"%s\"\n\
175 \n\
176 const %s %s[] = {",
177 hdrbase, type, name);
178 for (i = 0; i < DA_LEN(&p); i++) {
179 if (i % 8 == 0)
180 fputs("\n ", fp);
181 fprintf(fp, "%5i, ", DA(&p)[i]);
182 }
183 fputs("\n\
184 };\n\
185 ", fp);
186 if (fclose(fp) == EOF) {
187 remove(source);
188 die(EXIT_FAILURE, "error writing `%s': %s", source, strerror(errno));
189 }
190 }
191
192 return (0);
193 }
194
195 /*----- That's all, folks -------------------------------------------------*/