3 * Generate a prime-iteration wheel
5 * (c) 2007 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
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.
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.
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,
28 /*----- Header files ------------------------------------------------------*/
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>
43 /*----- Data structures ---------------------------------------------------*/
45 DA_DECL(uintv, unsigned int);
47 /*----- Main code ---------------------------------------------------------*/
49 static unsigned long gcd(unsigned long a, unsigned long b)
53 while (b) { t = a%b; a = b; b = t; }
57 int main(int argc, char *argv[])
60 const char *type = "unsigned char";
61 const char *source = "wheel.c";
62 const char *header = "wheel.h";
63 const char *name = "wheel";
73 o = getopt(argc, argv, "n:c:h:s:t:i:");
96 pquis(stderr, "Usage: $ [-n nprimes] [-s source] [-h header]\n");
101 if ((hdrbase = strrchr(header, '/')) == 0) hdrbase = header;
104 for (mod = 1, i = 2, n = 0;
107 if (gcd(i, mod) == 1) {
114 for (i = 2; i < mod; i++) {
115 if (gcd(mod, i) == 1) {
120 DA_PUSH(&v, mod + 1 - n);
123 FILE *fp = fopen(header, "w");
127 die(EXIT_FAILURE, "couldn't write `%s': %s", header, strerror(errno));
129 for (q = header; *q; q++) {
130 int ch = (unsigned char)*q;
143 * Wheel for small prime iteration [generated]\n\
149 #define WHEELN %luu\n\
150 #define WHEELMOD %luu\n\
152 extern const %s %s[];\n\
157 (unsigned long)DA_LEN(&v),
161 if (fclose(fp) == EOF) {
163 die(EXIT_FAILURE, "error writing `%s': %s", header, strerror(errno));
168 FILE *fp = fopen(source, "w");
171 die(EXIT_FAILURE, "couldn't write `%s': %s", source, strerror(errno));
175 * Wheel for small prime iteration [generated]\n\
181 hdrbase, type, name);
182 for (i = 0; i < DA_LEN(&v); i++) {
185 fprintf(fp, "%5u, ", DA(&v)[i]);
190 if (fclose(fp) == EOF) {
192 die(EXIT_FAILURE, "error writing `%s': %s", source, strerror(errno));
199 /*----- That's all, folks -------------------------------------------------*/