perftest: Optionally disable group checking.
[u/mdw/catacomb] / genwheel.c
CommitLineData
1d6d3b01
MW
1/* -*-c-*-
2 *
3 * Generate a prime-iteration wheel
4 *
5 * (c) 2007 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
44DA_DECL(uintv, unsigned int);
45
46/*----- Main code ---------------------------------------------------------*/
47
48static unsigned long gcd(unsigned long a, unsigned long b)
49{
50 int t;
51 if (!a) return (b);
52 while (b) { t = a%b; a = b; b = t; }
53 return (a);
54}
55
56int main(int argc, char *argv[])
57{
58 int np = 5;
59 const char *type = "unsigned char";
60 const char *source = "wheel.c";
61 const char *header = "wheel.h";
62 const char *name = "wheel";
63 const char *sym = 0;
64 unsigned long i, n;
65 unsigned long mod;
66 int o;
67 uintv v = DA_INIT;
68
69 ego(argv[0]);
70 for (;;) {
71 o = getopt(argc, argv, "n:c:h:s:t:i:");
72 if (o < 0)
73 break;
74 switch (o) {
75 case 'n':
76 np = atoi(optarg);
77 break;
78 case 's':
79 sym = optarg;
80 break;
81 case 'c':
82 source = optarg;
83 break;
84 case 'h':
85 header = optarg;
86 break;
87 case 't':
88 type = optarg;
89 break;
90 case 'i':
91 name = optarg;
92 break;
93 default:
94 pquis(stderr, "Usage: $ [-n nprimes] [-s source] [-h header]\n");
95 exit(EXIT_FAILURE);
96 }
97 }
98
99 for (mod = 1, i = 2, n = 0;
100 n < np;
101 i++) {
102 if (gcd(i, mod) == 1) {
103 mod *= i;
104 n++;
105 }
106 }
107
108 n = 1;
109 for (i = 2; i < mod; i++) {
110 if (gcd(mod, i) == 1) {
111 DA_PUSH(&v, i - n);
112 n = i;
113 }
114 }
115 DA_PUSH(&v, mod + 1 - n);
116
117 {
118 FILE *fp = fopen(header, "w");
119 dstr d = DSTR_INIT;
120 const 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 * Wheel for small prime iteration [generated]\n\
139 */\n\
140\n\
141#ifndef %s\n\
142#define %s\n\
143\n\
144#define WHEELN %luu\n\
145#define WHEELMOD %luu\n\
146\n\
147extern const %s %s[];\n\
148\n\
149#endif\n\
150",
151 sym, sym,
152 (unsigned long)DA_LEN(&v),
153 mod,
154 type, name);
155 dstr_destroy(&d);
156 if (fclose(fp) == EOF) {
157 remove(header);
158 die(EXIT_FAILURE, "error writing `%s': %s", header, strerror(errno));
159 }
160 }
161
162 {
163 FILE *fp = fopen(source, "w");
164 int i;
165 if (!fp)
166 die(EXIT_FAILURE, "couldn't write `%s': %s", source, strerror(errno));
167 fprintf(fp, "\
168/* -*-c-*-\n\
169 *\n\
170 * Wheel for small prime iteration [generated]\n\
171 */\n\
172\n\
173#include \"%s\"\n\
174\n\
175const %s %s[] = {",
176 header, type, name);
177 for (i = 0; i < DA_LEN(&v); i++) {
178 if (i % 8 == 0)
179 fputs("\n ", fp);
180 fprintf(fp, "%5u, ", DA(&v)[i]);
181 }
182 fputs("\n\
183};\n\
184", fp);
185 if (fclose(fp) == EOF) {
186 remove(source);
187 die(EXIT_FAILURE, "error writing `%s': %s", source, strerror(errno));
188 }
189 }
190
191 return (0);
192}
193
194/*----- That's all, folks -------------------------------------------------*/