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