Commit | Line | Data |
---|---|---|
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 | ||
44 | DA_DECL(uintv, unsigned int); | |
45 | ||
46 | /*----- Main code ---------------------------------------------------------*/ | |
47 | ||
48 | static 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 | ||
56 | int 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; | |
6c71d5e4 | 64 | const char *hdrbase; |
1d6d3b01 MW |
65 | unsigned long i, n; |
66 | unsigned long mod; | |
67 | int o; | |
68 | uintv v = DA_INIT; | |
69 | ||
70 | ego(argv[0]); | |
71 | for (;;) { | |
72 | o = getopt(argc, argv, "n:c:h:s:t:i:"); | |
73 | if (o < 0) | |
74 | break; | |
75 | switch (o) { | |
76 | case 'n': | |
77 | np = atoi(optarg); | |
78 | break; | |
79 | case 's': | |
80 | sym = optarg; | |
81 | break; | |
82 | case 'c': | |
83 | source = optarg; | |
84 | break; | |
85 | case 'h': | |
86 | header = optarg; | |
87 | break; | |
88 | case 't': | |
89 | type = optarg; | |
90 | break; | |
91 | case 'i': | |
92 | name = optarg; | |
93 | break; | |
94 | default: | |
95 | pquis(stderr, "Usage: $ [-n nprimes] [-s source] [-h header]\n"); | |
96 | exit(EXIT_FAILURE); | |
97 | } | |
98 | } | |
99 | ||
6c71d5e4 MW |
100 | if ((hdrbase = strrchr(header, '/')) == 0) hdrbase = header; |
101 | else hdrbase++; | |
102 | ||
1d6d3b01 MW |
103 | for (mod = 1, i = 2, n = 0; |
104 | n < np; | |
105 | i++) { | |
106 | if (gcd(i, mod) == 1) { | |
107 | mod *= i; | |
108 | n++; | |
109 | } | |
110 | } | |
111 | ||
112 | n = 1; | |
113 | for (i = 2; i < mod; i++) { | |
114 | if (gcd(mod, i) == 1) { | |
115 | DA_PUSH(&v, i - n); | |
116 | n = i; | |
117 | } | |
118 | } | |
119 | DA_PUSH(&v, mod + 1 - n); | |
120 | ||
121 | { | |
122 | FILE *fp = fopen(header, "w"); | |
123 | dstr d = DSTR_INIT; | |
124 | const char *q; | |
125 | if (!fp) | |
126 | die(EXIT_FAILURE, "couldn't write `%s': %s", header, strerror(errno)); | |
127 | if (!sym) { | |
128 | for (q = header; *q; q++) { | |
129 | int ch = (unsigned char)*q; | |
130 | if (isalnum(ch)) | |
131 | ch = toupper(ch); | |
132 | else | |
133 | ch = '_'; | |
134 | DPUTC(&d, ch); | |
135 | } | |
136 | DPUTZ(&d); | |
137 | sym = d.buf; | |
138 | } | |
139 | fprintf(fp, "\ | |
140 | /* -*-c-*-\n\ | |
141 | *\n\ | |
142 | * Wheel for small prime iteration [generated]\n\ | |
143 | */\n\ | |
144 | \n\ | |
145 | #ifndef %s\n\ | |
146 | #define %s\n\ | |
147 | \n\ | |
148 | #define WHEELN %luu\n\ | |
149 | #define WHEELMOD %luu\n\ | |
150 | \n\ | |
151 | extern const %s %s[];\n\ | |
152 | \n\ | |
153 | #endif\n\ | |
154 | ", | |
155 | sym, sym, | |
156 | (unsigned long)DA_LEN(&v), | |
157 | mod, | |
158 | type, name); | |
159 | dstr_destroy(&d); | |
160 | if (fclose(fp) == EOF) { | |
161 | remove(header); | |
162 | die(EXIT_FAILURE, "error writing `%s': %s", header, strerror(errno)); | |
163 | } | |
164 | } | |
165 | ||
166 | { | |
167 | FILE *fp = fopen(source, "w"); | |
168 | int i; | |
169 | if (!fp) | |
170 | die(EXIT_FAILURE, "couldn't write `%s': %s", source, strerror(errno)); | |
171 | fprintf(fp, "\ | |
172 | /* -*-c-*-\n\ | |
173 | *\n\ | |
174 | * Wheel for small prime iteration [generated]\n\ | |
175 | */\n\ | |
176 | \n\ | |
177 | #include \"%s\"\n\ | |
178 | \n\ | |
179 | const %s %s[] = {", | |
6c71d5e4 | 180 | hdrbase, type, name); |
1d6d3b01 MW |
181 | for (i = 0; i < DA_LEN(&v); i++) { |
182 | if (i % 8 == 0) | |
183 | fputs("\n ", fp); | |
184 | fprintf(fp, "%5u, ", DA(&v)[i]); | |
185 | } | |
186 | fputs("\n\ | |
187 | };\n\ | |
188 | ", fp); | |
189 | if (fclose(fp) == EOF) { | |
190 | remove(source); | |
191 | die(EXIT_FAILURE, "error writing `%s': %s", source, strerror(errno)); | |
192 | } | |
193 | } | |
194 | ||
195 | return (0); | |
196 | } | |
197 | ||
198 | /*----- That's all, folks -------------------------------------------------*/ |