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