chiark / gitweb /
math/f25519.[ch]: More field operations.
[catacomb] / math / genprimes.c
CommitLineData
8ecfe0dc 1/* -*-c-*-
8ecfe0dc 2 *
3 * Generate prime number table
4 *
5 * (c) 1999 Straylight/Edgeware
6 */
7
45c0fd36 8/*----- Licensing notice --------------------------------------------------*
8ecfe0dc 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.
45c0fd36 16 *
8ecfe0dc 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.
45c0fd36 21 *
8ecfe0dc 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
8ecfe0dc 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
44DA_DECL(intv, int);
45
46/*----- Main code ---------------------------------------------------------*/
47
48int main(int argc, char *argv[])
49{
50 int p_max = 0, p_n = 0;
51 char *type = "unsigned int";
2791153f 52 char *header = "primetab.h";
53 char *source = "primetab.c";
54 char *name = "primetab";
6c71d5e4 55 char *hdrbase;
34e4f738 56 char *sym = 0;
8ecfe0dc 57 intv p = DA_INIT;
58 int i;
59
60 ego(argv[0]);
61
62 for (;;) {
34e4f738 63 int i = getopt(argc, argv, "h:c:i:n:m:t:s:");
8ecfe0dc 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;
34e4f738 87 case 's':
88 sym = optarg;
89 break;
8ecfe0dc 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
6c71d5e4
MW
99 if ((hdrbase = strrchr(header, '/')) == 0) hdrbase = header;
100 else hdrbase++;
101
88b913b1 102 if (p_n || p_max >= 2)
8ecfe0dc 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));
34e4f738 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;
8ecfe0dc 134 }
8ecfe0dc 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\
2791153f 144#define NPRIME %luu\n\
145#define MAXPRIME %uu\n\
8ecfe0dc 146\n\
88b913b1 147typedef %s smallprime;\n\
4e66da02 148extern const smallprime %s[];\n\
8ecfe0dc 149\n\
150#endif\n\
151",
34e4f738 152 sym, sym,
8ecfe0dc 153 (unsigned long)DA_LEN(&p),
62f34e46 154 DA_LAST(&p),
8ecfe0dc 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\
4e66da02 176const %s %s[] = {",
6c71d5e4 177 hdrbase, type, name);
8ecfe0dc 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 -------------------------------------------------*/