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