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/mdwopt.h>
39 #include <mLib/quis.h>
40 #include <mLib/report.h>
42 /*----- Data structures ---------------------------------------------------*/
44 DA_DECL(uintv
, unsigned int);
46 /*----- Main code ---------------------------------------------------------*/
48 static unsigned long gcd(unsigned long a
, unsigned long b
)
52 while (b
) { t
= a
%b
; a
= b
; b
= t
; }
56 int main(int argc
, char *argv
[])
59 const char *type
= "unsigned char";
60 const char *source
= "wheel.c";
61 const char *header
= "wheel.h";
62 const char *name
= "wheel";
72 o
= getopt(argc
, argv
, "n:c:h:s:t:i:");
95 pquis(stderr
, "Usage: $ [-n nprimes] [-s source] [-h header]\n");
100 if ((hdrbase
= strrchr(header
, '/')) == 0) hdrbase
= header
;
103 for (mod
= 1, i
= 2, n
= 0;
106 if (gcd(i
, mod
) == 1) {
113 for (i
= 2; i
< mod
; i
++) {
114 if (gcd(mod
, i
) == 1) {
119 DA_PUSH(&v
, mod
+ 1 - n
);
122 FILE *fp
= fopen(header
, "w");
126 die(EXIT_FAILURE
, "couldn't write `%s': %s", header
, strerror(errno
));
128 for (q
= header
; *q
; q
++) {
129 int ch
= (unsigned char)*q
;
142 * Wheel for small prime iteration [generated]\n\
148 #define WHEELN %luu\n\
149 #define WHEELMOD %luu\n\
151 extern const %s %s[];\n\
156 (unsigned long)DA_LEN(&v
),
160 if (fclose(fp
) == EOF
) {
162 die(EXIT_FAILURE
, "error writing `%s': %s", header
, strerror(errno
));
167 FILE *fp
= fopen(source
, "w");
170 die(EXIT_FAILURE
, "couldn't write `%s': %s", source
, strerror(errno
));
174 * Wheel for small prime iteration [generated]\n\
180 hdrbase
, type
, name
);
181 for (i
= 0; i
< DA_LEN(&v
); i
++) {
184 fprintf(fp
, "%5u, ", DA(&v
)[i
]);
189 if (fclose(fp
) == EOF
) {
191 die(EXIT_FAILURE
, "error writing `%s': %s", source
, strerror(errno
));
198 /*----- That's all, folks -------------------------------------------------*/