refactor the 4 equal loops into a run function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
diff --git a/etc/tune.c b/etc/tune.c
index e4a0126..3f02502 100644
--- a/etc/tune.c
+++ b/etc/tune.c
@@ -154,6 +154,45 @@ struct tune_args {
int increment_print;
} args;
+static void s_run(const char *name, uint64_t (*op)(int), int *cutoff)
+{
+ int x, count = 0;
+ uint64_t t1, t2;
+ if ((args.verbose == 1) || (args.testmode == 1)) {
+ printf("# %s.\n", name);
+ }
+ for (x = 8; x < args.upper_limit_print; x += args.increment_print) {
+ *cutoff = INT_MAX;
+ t1 = op(x);
+ if ((t1 == 0uLL) || (t1 == UINT64_MAX)) {
+ fprintf(stderr,"%s failed at x = INT_MAX (%s)\n", name,
+ (t1 == 0uLL)?"wrong result":"internal error");
+ exit(EXIT_FAILURE);
+ }
+ *cutoff = x;
+ t2 = op(x);
+ if ((t2 == 0uLL) || (t2 == UINT64_MAX)) {
+ fprintf(stderr,"%s failed (%s)\n", name,
+ (t2 == 0uLL)?"wrong result":"internal error");
+ exit(EXIT_FAILURE);
+ }
+ if (args.verbose == 1) {
+ printf("%d: %9"PRIu64" %9"PRIu64", %9"PRIi64"\n", x, t1, t2, (int64_t)t2 - (int64_t)t1);
+ }
+ if (t2 < t1) {
+ if (count == s_stabilization_extra) {
+ count = 0;
+ break;
+ } else if (count < s_stabilization_extra) {
+ count++;
+ }
+ } else if (count > 0) {
+ count--;
+ }
+ }
+ *cutoff = x - s_stabilization_extra * args.increment_print;
+}
+
static void s_usage(char *s)
{
fprintf(stderr,"Usage: %s [TvcpGbtrSLFfMmosh]\n",s);
@@ -184,13 +223,15 @@ static void s_usage(char *s)
fprintf(stderr," -h this message\n");
}
-
+struct cutoffs {
+ int kmul, ksqr;
+ int tcmul, tcsqr;
+};
int main(int argc, char **argv)
{
uint64_t t1, t2;
int x, i, j;
- int count = 0;
int printpreset = 0;
/*int preset[8];*/
@@ -201,7 +242,7 @@ int main(int argc, char **argv)
uint64_t seed = 0xdeadbeef;
int opt;
- int ksm, kss, tc3m, tc3s;
+ struct cutoffs orig, updated;
FILE *squaring, *multiplying;
char mullog[256] = "multiplying";
@@ -209,7 +250,7 @@ int main(int argc, char **argv)
s_number_of_test_loops = 64;
s_stabilization_extra = 3;
- MP_ZERO_BUFFER(args, sizeof(args));
+ MP_ZERO_BUFFER(&args, sizeof(args));
args.testmode = 0;
args.verbose = 0;
@@ -467,10 +508,10 @@ int main(int argc, char **argv)
s_mp_rand_jenkins_init(seed);
mp_rand_source(s_mp_rand_jenkins);
- ksm = KARATSUBA_MUL_CUTOFF;
- kss = KARATSUBA_SQR_CUTOFF;
- tc3m = TOOM_MUL_CUTOFF;
- tc3s = TOOM_SQR_CUTOFF;
+ orig.kmul = KARATSUBA_MUL_CUTOFF;
+ orig.ksqr = KARATSUBA_SQR_CUTOFF;
+ orig.tcmul = TOOM_MUL_CUTOFF;
+ orig.tcsqr = TOOM_SQR_CUTOFF;
if ((args.bncore == 0) && (printpreset == 0)) {
/* Turn all limits from bncore.c to the max */
@@ -485,157 +526,37 @@ int main(int argc, char **argv)
of the macro MP_WPARRAY in tommath.h which needs to
be changed manually (to 0 (zero)).
*/
- if ((args.verbose == 1) || (args.testmode == 1)) {
- puts("# Karatsuba multiplication.");
- }
- for (x = 8; x < args.upper_limit_print; x += args.increment_print) {
- KARATSUBA_MUL_CUTOFF = INT_MAX;
- t1 = s_time_mul(x);
- if ((t1 == 0uLL) || (t1 == UINT64_MAX)) {
- fprintf(stderr,"Karatsuba multiplication failed at x = INT_MAX (%s)\n",
- (t1 == 0uLL)?"wrong result":"internal error");
- exit(EXIT_FAILURE);
- }
- KARATSUBA_MUL_CUTOFF = x;
- t2 = s_time_mul(x);
- if ((t2 == 0uLL) || (t2 == UINT64_MAX)) {
- fprintf(stderr,"Karatsuba multiplication failed (%s)\n",
- (t2 == 0uLL)?"wrong result":"internal error");
- exit(EXIT_FAILURE);
- }
- if (args.verbose == 1) {
- printf("%d: %9"PRIu64" %9"PRIu64", %9"PRIi64"\n", x, t1, t2, (int64_t)t2 - (int64_t)t1);
- }
- if (t2 < t1) {
- if (count == s_stabilization_extra) {
- count = 0;
- break;
- } else if (count < s_stabilization_extra) {
- count++;
- }
- } else if (count > 0) {
- count--;
- }
- }
- KARATSUBA_MUL_CUTOFF = x - s_stabilization_extra * args.increment_print;
+ s_run("Karatsuba multiplication", s_time_mul, &KARATSUBA_MUL_CUTOFF);
+ updated.kmul = KARATSUBA_MUL_CUTOFF;
+ KARATSUBA_MUL_CUTOFF = INT_MAX;
#endif
#ifdef BN_S_MP_KARATSUBA_SQR_C
- if ((args.verbose == 1) || (args.testmode == 1)) {
- puts("# Karatsuba squaring.");
- }
- for (x = 8; x < args.upper_limit_print; x += args.increment_print) {
- KARATSUBA_SQR_CUTOFF = INT_MAX;
- t1 = s_time_sqr(x);
- if ((t1 == 0uLL) || (t1 == UINT64_MAX)) {
- fprintf(stderr,"Karatsuba squaring failed at x = INT_MAX (%s)\n",
- (t1 == 0uLL)?"wrong result":"internal error");
- exit(EXIT_FAILURE);
- }
- KARATSUBA_SQR_CUTOFF = x;
- t2 = s_time_sqr(x);
- if ((t2 == 0uLL) || (t2 == UINT64_MAX)) {
- fprintf(stderr,"Karatsuba squaring failed (%s)\n",
- (t2 == 0uLL)?"wrong result":"internal error");
- exit(EXIT_FAILURE);
- }
- if (args.verbose == 1) {
- printf("%d: %9"PRIu64" %9"PRIu64", %9"PRIi64"\n", x, t1, t2, (int64_t)t2 - (int64_t)t1);
- }
- if (t2 < t1) {
- if (count == s_stabilization_extra) {
- count = 0;
- break;
- } else if (count < s_stabilization_extra) {
- count++;
- }
- } else if (count > 0) {
- count--;
- }
- }
- KARATSUBA_SQR_CUTOFF = x - s_stabilization_extra * args.increment_print;
+ s_run("Karatsuba squaring", s_time_sqr, &KARATSUBA_SQR_CUTOFF);
+ updated.ksqr = KARATSUBA_SQR_CUTOFF;
+ KARATSUBA_SQR_CUTOFF = INT_MAX;
#endif
#ifdef BN_S_MP_TOOM_MUL_C
- if ((args.verbose == 1) || (args.testmode == 1)) {
- puts("# Toom-Cook 3-way multiplying.");
- }
- for (x = 8; x < args.upper_limit_print; x += args.increment_print) {
- TOOM_MUL_CUTOFF = INT_MAX;
- t1 = s_time_mul(x);
- if ((t1 == 0uLL) || (t1 == UINT64_MAX)) {
- fprintf(stderr,"Toom-Cook 3-way multiplying failed at x = INT_MAX (%s)\n",
- (t1 == 0uLL)?"wrong result":"internal error");
- exit(EXIT_FAILURE);
- }
- TOOM_MUL_CUTOFF = x;
- t2 = s_time_mul(x);
- if ((t2 == 0uLL) || (t2 == UINT64_MAX)) {
- fprintf(stderr,"Toom-Cook 3-way multiplication failed (%s)\n",
- (t2 == 0uLL)?"wrong result":"internal error");
- exit(EXIT_FAILURE);
- }
- if (args.verbose == 1) {
- printf("%d: %9"PRIu64" %9"PRIu64", %9"PRIi64"\n", x, t1, t2, (int64_t)t2 - (int64_t)t1);
- }
- if (t2 < t1) {
- if (count == s_stabilization_extra) {
- count = 0;
- break;
- } else if (count < s_stabilization_extra) {
- count++;
- }
- } else if (count > 0) {
- count--;
- }
- }
- TOOM_MUL_CUTOFF = x - s_stabilization_extra * args.increment_print;
+ s_run("Toom-Cook 3-way multiplying", s_time_mul, &TOOM_MUL_CUTOFF);
+ updated.tcmul = TOOM_MUL_CUTOFF;
+ TOOM_MUL_CUTOFF = INT_MAX;
#endif
#ifdef BN_S_MP_TOOM_SQR_C
- if ((args.verbose == 1) || (args.testmode == 1)) {
- puts("# Toom-Cook 3-way squaring.");
- }
- for (x = 8; x < args.upper_limit_print; x += args.increment_print) {
- TOOM_SQR_CUTOFF = INT_MAX;
- t1 = s_time_sqr(x);
- if ((t1 == 0uLL) || (t1 == UINT64_MAX)) {
- fprintf(stderr,"Toom-Cook 3-way squaring failed at x = INT_MAX (%s)\n",
- (t1 == 0uLL)?"wrong result":"internal error");
- exit(EXIT_FAILURE);
- }
- TOOM_SQR_CUTOFF = x;
- t2 = s_time_sqr(x);
- if ((t2 == 0uLL) || (t2 == UINT64_MAX)) {
- fprintf(stderr,"Toom-Cook 3-way squaring failed (%s)\n",
- (t2 == 0uLL)?"wrong result":"internal error");
- exit(EXIT_FAILURE);
- }
- if (args.verbose == 1) {
- printf("%d: %9"PRIu64" %9"PRIu64", %9"PRIi64"\n", x, t1, t2, (int64_t)t2 - (int64_t)t1);
- }
- if (t2 < t1) {
- if (count == s_stabilization_extra) {
- count = 0;
- break;
- } else if (count < s_stabilization_extra) {
- count++;
- }
- } else if (count > 0) {
- count--;
- }
- }
- TOOM_SQR_CUTOFF = x - s_stabilization_extra * args.increment_print;
+ s_run("Toom-Cook 3-way squaring", s_time_sqr, &TOOM_SQR_CUTOFF);
+ updated.tcsqr = TOOM_SQR_CUTOFF;
+ TOOM_SQR_CUTOFF = INT_MAX;
#endif
}
if (args.terse == 1) {
printf("%d %d %d %d\n",
- KARATSUBA_MUL_CUTOFF,
- KARATSUBA_SQR_CUTOFF,
- TOOM_MUL_CUTOFF,
- TOOM_SQR_CUTOFF);
+ updated.kmul,
+ updated.ksqr,
+ updated.tcmul,
+ updated.tcsqr);
} else {
- printf("KARATSUBA_MUL_CUTOFF = %d\n", KARATSUBA_MUL_CUTOFF);
- printf("KARATSUBA_SQR_CUTOFF = %d\n", KARATSUBA_SQR_CUTOFF);
- printf("TOOM_MUL_CUTOFF = %d\n", TOOM_MUL_CUTOFF);
- printf("TOOM_SQR_CUTOFF = %d\n", TOOM_SQR_CUTOFF);
+ printf("KARATSUBA_MUL_CUTOFF = %d\n", updated.kmul);
+ printf("KARATSUBA_SQR_CUTOFF = %d\n", updated.ksqr);
+ printf("TOOM_MUL_CUTOFF = %d\n", updated.tcmul);
+ printf("TOOM_SQR_CUTOFF = %d\n", updated.tcsqr);
}
if (args.print == 1) {
@@ -659,10 +580,10 @@ int main(int argc, char **argv)
TOOM_MUL_CUTOFF = INT_MAX;
TOOM_SQR_CUTOFF = INT_MAX;
t1 = s_time_mul(x);
- KARATSUBA_MUL_CUTOFF = kss;
- KARATSUBA_SQR_CUTOFF = ksm;
- TOOM_MUL_CUTOFF = tc3m;
- TOOM_SQR_CUTOFF = tc3s;
+ KARATSUBA_MUL_CUTOFF = orig.kmul;
+ KARATSUBA_SQR_CUTOFF = orig.ksqr;
+ TOOM_MUL_CUTOFF = orig.tcmul;
+ TOOM_SQR_CUTOFF = orig.tcsqr;
t2 = s_time_mul(x);
fprintf(multiplying, "%d: %9"PRIu64" %9"PRIu64", %9"PRIi64"\n", x, t1, t2, (int64_t)t2 - (int64_t)t1);
fflush(multiplying);
@@ -675,10 +596,10 @@ int main(int argc, char **argv)
TOOM_MUL_CUTOFF = INT_MAX;
TOOM_SQR_CUTOFF = INT_MAX;
t1 = s_time_sqr(x);
- KARATSUBA_MUL_CUTOFF = kss;
- KARATSUBA_SQR_CUTOFF = ksm;
- TOOM_MUL_CUTOFF = tc3m;
- TOOM_SQR_CUTOFF = tc3s;
+ KARATSUBA_MUL_CUTOFF = orig.kmul;
+ KARATSUBA_SQR_CUTOFF = orig.ksqr;
+ TOOM_MUL_CUTOFF = orig.tcmul;
+ TOOM_SQR_CUTOFF = orig.tcsqr;
t2 = s_time_sqr(x);
fprintf(squaring,"%d: %9"PRIu64" %9"PRIu64", %9"PRIi64"\n", x, t1, t2, (int64_t)t2 - (int64_t)t1);
fflush(squaring);
@@ -689,10 +610,10 @@ int main(int argc, char **argv)
}
printf("Finished. Data for graphing in \"%s\" and \"%s\"\n",mullog, sqrlog);
if (args.verbose == 1) {
- KARATSUBA_MUL_CUTOFF = kss;
- KARATSUBA_SQR_CUTOFF = ksm;
- TOOM_MUL_CUTOFF = tc3m;
- TOOM_SQR_CUTOFF = tc3s;
+ KARATSUBA_MUL_CUTOFF = orig.kmul;
+ KARATSUBA_SQR_CUTOFF = orig.ksqr;
+ TOOM_MUL_CUTOFF = orig.tcmul;
+ TOOM_SQR_CUTOFF = orig.tcsqr;
if (args.terse == 1) {
printf("%d %d %d %d\n",
KARATSUBA_MUL_CUTOFF,