Commit 12873fb6f7ffa34e27cde22369339b6861dc1ead

Steffen Jaeckel 2019-10-04T14:28:52

Merge pull request #342 from libtom/refactor-tune Refactor tune

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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
diff --git a/etc/makefile b/etc/makefile
index bad1741..c7280e7 100644
--- a/etc/makefile
+++ b/etc/makefile
@@ -40,3 +40,4 @@ clean:
 	rm -f *.log *.o *.obj *.exe pprime tune mersenne drprime mont 2kprime pprime.dat \
         tuning_list multiplying squaring test *.da *.dyn *.dpi *~
 
+.PHONY: tune
diff --git a/etc/tune.c b/etc/tune.c
index 780ee54..057e372 100644
--- a/etc/tune.c
+++ b/etc/tune.c
@@ -144,6 +144,75 @@ LTM_ERR:
    return t1;
 }
 
+struct tune_args {
+   int testmode;
+   int verbose;
+   int print;
+   int bncore;
+   int terse;
+   int upper_limit_print;
+   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 long s_strtol(const char *str, char **endptr, const char *err)
+{
+   const int base = 10;
+   char *_endptr;
+   long val;
+   errno = 0;
+   val = strtol(str, &_endptr, base);
+   if ((val > INT_MAX || val < 0) || (errno != 0)) {
+      fprintf(stderr, "Value %s not usable\n", str);
+      exit(EXIT_FAILURE);
+   }
+   if (_endptr == str) {
+      fprintf(stderr, "%s\n", err);
+      exit(EXIT_FAILURE);
+   }
+   if (endptr) *endptr = _endptr;
+   return val;
+}
+
+static int s_exit_code = EXIT_FAILURE;
 static void s_usage(char *s)
 {
    fprintf(stderr,"Usage: %s [TvcpGbtrSLFfMmosh]\n",s);
@@ -172,35 +241,48 @@ static void s_usage(char *s)
    fprintf(stderr,"             tc3s = Toom-Cook 3-way squaring\n");
    fprintf(stderr,"             Implies '-p'\n");
    fprintf(stderr,"          -h this message\n");
+   exit(s_exit_code);
 }
 
+struct cutoffs {
+   int KARATSUBA_MUL, KARATSUBA_SQR;
+   int TOOM_MUL, TOOM_SQR;
+};
+
+const struct cutoffs max_cutoffs =
+{ INT_MAX, INT_MAX, INT_MAX, INT_MAX };
 
+static void set_cutoffs(const struct cutoffs *c)
+{
+   KARATSUBA_MUL_CUTOFF = c->KARATSUBA_MUL;
+   KARATSUBA_SQR_CUTOFF = c->KARATSUBA_SQR;
+   TOOM_MUL_CUTOFF = c->TOOM_MUL;
+   TOOM_SQR_CUTOFF = c->TOOM_SQR;
+}
+
+static void get_cutoffs(struct cutoffs *c)
+{
+   c->KARATSUBA_MUL  = KARATSUBA_MUL_CUTOFF;
+   c->KARATSUBA_SQR  = KARATSUBA_SQR_CUTOFF;
+   c->TOOM_MUL = TOOM_MUL_CUTOFF;
+   c->TOOM_SQR = TOOM_SQR_CUTOFF;
+
+}
 
 int main(int argc, char **argv)
 {
    uint64_t t1, t2;
    int x, i, j;
-   int count = 0;
-
-   int testmode = 0;
-   int verbose = 0;
-   int print = 0;
-   int bncore = 0;
-   int terse = 0;
-
-   int upper_limit_print = 3000;
-   int increment_print = 1;
+   size_t n;
 
    int printpreset = 0;
    /*int preset[8];*/
-   int base = 10;
    char *endptr, *str;
-   long val;
 
    uint64_t seed = 0xdeadbeef;
 
    int opt;
-   int ksm, kss, tc3m, tc3s;
+   struct cutoffs orig, updated;
 
    FILE *squaring, *multiplying;
    char mullog[256] = "multiplying";
@@ -208,30 +290,41 @@ int main(int argc, char **argv)
    s_number_of_test_loops = 64;
    s_stabilization_extra = 3;
 
+   MP_ZERO_BUFFER(&args, sizeof(args));
+
+   args.testmode = 0;
+   args.verbose = 0;
+   args.print = 0;
+   args.bncore = 0;
+   args.terse = 0;
+
+   args.upper_limit_print = 3000;
+   args.increment_print = 1;
+
    /* Very simple option parser, please treat it nicely. */
    if (argc != 1) {
       for (opt = 1; (opt < argc) && (argv[opt][0] == '-'); opt++) {
          switch (argv[opt][1]) {
          case 'T':
-            testmode = 1;
+            args.testmode = 1;
             s_check_result = 1;
-            upper_limit_print = 1000;
-            increment_print = 11;
+            args.upper_limit_print = 1000;
+            args.increment_print = 11;
             s_number_of_test_loops = 1;
             s_stabilization_extra = 1;
             s_offset = 1;
             break;
          case 'v':
-            verbose = 1;
+            args.verbose = 1;
             break;
          case 'c':
             s_check_result = 1;
             break;
          case 'p':
-            print = 1;
+            args.print = 1;
             break;
          case 'G':
-            print = 1;
+            args.print = 1;
             opt++;
             if (opt >= argc) {
                s_usage(argv[0]);
@@ -261,10 +354,10 @@ int main(int argc, char **argv)
             }
             break;
          case 'b':
-            bncore = 1;
+            args.bncore = 1;
             break;
          case 't':
-            terse = 1;
+            args.terse = 1;
             break;
          case 'S':
             opt++;
@@ -273,71 +366,28 @@ int main(int argc, char **argv)
             }
             str = argv[opt];
             errno = 0;
-            val = strtol(str, &endptr, base);
-            if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
-                || (errno != 0 && val == 0)) {
-               fprintf(stderr,"Seed %s not usable\n", argv[opt]);
-               exit(EXIT_FAILURE);
-            }
-            if (endptr == str) {
-               fprintf(stderr, "No seed given?\n");
-               exit(EXIT_FAILURE);
-            }
-            seed = (uint64_t)val;
+            seed = (uint64_t)s_strtol(argv[opt], NULL, "No seed given?\n");
             break;
          case 'L':
             opt++;
             if (opt >= argc) {
                s_usage(argv[0]);
             }
-            str = argv[opt];
-            errno = 0;
-            val = strtol(str, &endptr, base);
-            if ((val > INT_MAX || val < 0) || (errno != 0)) {
-               fprintf(stderr,"Value %s not usable\n", argv[opt]);
-               exit(EXIT_FAILURE);
-            }
-            if (endptr == str) {
-               fprintf(stderr, "No value for option \"-L\"given\n");
-               exit(EXIT_FAILURE);
-            }
-            s_stabilization_extra = (int)val;
+            s_stabilization_extra = (int)s_strtol(argv[opt], NULL, "No value for option \"-L\"given");
             break;
          case 'o':
             opt++;
             if (opt >= argc) {
                s_usage(argv[0]);
             }
-            str = argv[opt];
-            errno = 0;
-            val = strtol(str, &endptr, base);
-            if ((val > INT_MAX || val < 0) || (errno != 0)) {
-               fprintf(stderr,"Value %s not usable as an offset\n", argv[opt]);
-               exit(EXIT_FAILURE);
-            }
-            if (endptr == str) {
-               fprintf(stderr, "No value for the offset given\n");
-               exit(EXIT_FAILURE);
-            }
-            s_offset = (int)val;
+            s_offset = (int)s_strtol(argv[opt], NULL, "No value for the offset given");
             break;
          case 'r':
             opt++;
             if (opt >= argc) {
                s_usage(argv[0]);
             }
-            str = argv[opt];
-            errno = 0;
-            val = strtol(str, &endptr, base);
-            if ((val > INT_MAX || val < 0) || (errno != 0)) {
-               fprintf(stderr,"Value %s not usable as the number of rounds for \"-r\"\n", argv[opt]);
-               exit(EXIT_FAILURE);
-            }
-            if (endptr == str) {
-               fprintf(stderr, "No value for the number of rounds given\n");
-               exit(EXIT_FAILURE);
-            }
-            s_number_of_test_loops = (int)val;
+            s_number_of_test_loops = (int)s_strtol(argv[opt], NULL, "No value for the number of rounds given");
             break;
 
          case 'M':
@@ -345,104 +395,36 @@ int main(int argc, char **argv)
             if (opt >= argc) {
                s_usage(argv[0]);
             }
-            str = argv[opt];
-            errno = 0;
-            val = strtol(str, &endptr, base);
-            if ((val > INT_MAX || val < 0) || (errno != 0)) {
-               fprintf(stderr,"Value %s not usable as the upper limit of T-C tests (\"-M\")\n", argv[opt]);
-               exit(EXIT_FAILURE);
-            }
-            if (endptr == str) {
-               fprintf(stderr, "No value for the upper limit of T-C tests given\n");
-               exit(EXIT_FAILURE);
-            }
-            upper_limit_print = (int)val;
+            args.upper_limit_print = (int)s_strtol(argv[opt], NULL, "No value for the upper limit of T-C tests given");
             break;
          case 'm':
             opt++;
             if (opt >= argc) {
                s_usage(argv[0]);
             }
-            str = argv[opt];
-            errno = 0;
-            val = strtol(str, &endptr, base);
-            if ((val > INT_MAX || val < 0) || (errno != 0)) {
-               fprintf(stderr,"Value %s not usable as the increment for the T-C tests (\"-m\")\n", argv[opt]);
-               exit(EXIT_FAILURE);
-            }
-            if (endptr == str) {
-               fprintf(stderr, "No value for the increment for the T-C tests given\n");
-               exit(EXIT_FAILURE);
-            }
-            increment_print = (int)val;
+            args.increment_print = (int)s_strtol(argv[opt], NULL, "No value for the increment for the T-C tests given");
             break;
          case 's':
             printpreset = 1;
-            print = 1;
+            args.print = 1;
             opt++;
             if (opt >= argc) {
                s_usage(argv[0]);
             }
             str = argv[opt];
-            i = 0;
-            /* Only the most basic checks */
-            errno = 0;
-            val = strtol(str, &endptr, base);
-            if ((val > INT_MAX || val < 0) || (errno != 0)) {
-               fprintf(stderr,"input #%d wrong\n", i+1);
-               exit(EXIT_FAILURE);
-            }
-            if (endptr == str) {
-               fprintf(stderr, "No input for #%d?\n", i+1);
-               exit(EXIT_FAILURE);
-            }
-            i++;
-            str = endptr + 1;
-            KARATSUBA_MUL_CUTOFF = (int)val;
-            errno = 0;
-            val = strtol(str, &endptr, base);
-            if ((val > INT_MAX || val < 0) || (errno != 0)) {
-               fprintf(stderr,"input #%d wrong\n", i+1);
-               exit(EXIT_FAILURE);
-            }
-            if (endptr == str) {
-               fprintf(stderr, "No input for #%d?\n", i+1);
-               exit(EXIT_FAILURE);
-            }
-            i++;
+            KARATSUBA_MUL_CUTOFF = (int)s_strtol(str, &endptr, "[1/4] No value for KARATSUBA_MUL_CUTOFF given");
             str = endptr + 1;
-            KARATSUBA_SQR_CUTOFF = (int)val;
-            errno = 0;
-            val = strtol(str, &endptr, base);
-            if ((val > INT_MAX || val < 0) || (errno != 0)) {
-               fprintf(stderr,"input #%d wrong\n", i+1);
-               exit(EXIT_FAILURE);
-            }
-            if (endptr == str) {
-               fprintf(stderr, "No input for #%d?\n", i+1);
-               exit(EXIT_FAILURE);
-            }
-            i++;
+            KARATSUBA_SQR_CUTOFF = (int)s_strtol(str, &endptr, "[2/4] No value for KARATSUBA_SQR_CUTOFF given");
             str = endptr + 1;
-            TOOM_MUL_CUTOFF = (int)val;
-            errno = 0;
-            val = strtol(str, &endptr, base);
-            if ((val > INT_MAX || val < 0) || (errno != 0)) {
-               fprintf(stderr,"input #%d wrong\n", i+1);
-               exit(EXIT_FAILURE);
-            }
-            if (endptr == str) {
-               fprintf(stderr, "No input for #%d?\n", i+1);
-               exit(EXIT_FAILURE);
-            }
-            i++;
+            TOOM_MUL_CUTOFF = (int)s_strtol(str, &endptr, "[3/4] No value for TOOM_MUL_CUTOFF given");
             str = endptr + 1;
-            TOOM_SQR_CUTOFF = (int)val;
+            TOOM_SQR_CUTOFF = (int)s_strtol(str, &endptr, "[4/4] No value for TOOM_SQR_CUTOFF given");
             break;
          case 'h':
+            s_exit_code = EXIT_SUCCESS;
+         /* FALLTHROUGH */
          default:
             s_usage(argv[0]);
-            exit(EXIT_FAILURE);
          }
       }
    }
@@ -455,178 +437,52 @@ 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;
-
-   if ((bncore == 0) && (printpreset == 0)) {
+   get_cutoffs(&orig);
+
+   updated = max_cutoffs;
+   if ((args.bncore == 0) && (printpreset == 0)) {
+      struct {
+         const char *name;
+         int *cutoff, *update;
+         uint64_t (*fn)(int);
+      } test[] = {
+#define T_MUL_SQR(n, o, f)  { #n, &o##_CUTOFF, &(updated.o), MP_HAS(S_MP_##o) ? f : NULL }
+         /*
+            The influence of the Comba multiplication cannot be
+            eradicated programmatically. It depends on the size
+            of the macro MP_WPARRAY in tommath.h which needs to
+            be changed manually (to 0 (zero)).
+          */
+         T_MUL_SQR("Karatsuba multiplication", KARATSUBA_MUL, s_time_mul),
+         T_MUL_SQR("Karatsuba squaring", KARATSUBA_SQR, s_time_sqr),
+         T_MUL_SQR("Toom-Cook 3-way multiplying", TOOM_MUL, s_time_mul),
+         T_MUL_SQR("Toom-Cook 3-way squaring", TOOM_SQR, s_time_sqr),
+#undef T_MUL_SQR
+      };
       /* Turn all limits from bncore.c to the max */
-      KARATSUBA_MUL_CUTOFF = INT_MAX;
-      KARATSUBA_SQR_CUTOFF = INT_MAX;
-      TOOM_MUL_CUTOFF = INT_MAX;
-      TOOM_SQR_CUTOFF = INT_MAX;
-#ifdef BN_S_MP_KARATSUBA_MUL_C
-      /*
-         The influence of the Comba multiplication cannot be
-         eradicated programmatically. It depends on the size
-         of the macro MP_WPARRAY in tommath.h which needs to
-         be changed manually (to 0 (zero)).
-       */
-      if ((verbose == 1) || (testmode == 1)) {
-         puts("# Karatsuba multiplication.");
-      }
-      for (x = 8; x < upper_limit_print; x += 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);
+      set_cutoffs(&max_cutoffs);
+      for (n = 0; n < sizeof(test)/sizeof(test[0]); ++n) {
+         if (test[n].fn) {
+            s_run(test[n].name, test[n].fn, test[n].cutoff);
+            *test[n].update = *test[n].cutoff;
+            *test[n].cutoff = INT_MAX;
          }
-         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 (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 * increment_print;
-#endif
-#ifdef BN_S_MP_KARATSUBA_SQR_C
-      if ((verbose == 1) || (testmode == 1)) {
-         puts("# Karatsuba squaring.");
       }
-      for (x = 8; x < upper_limit_print; x += 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 (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 * increment_print;
-#endif
-#ifdef BN_S_MP_TOOM_MUL_C
-      if ((verbose == 1) || (testmode == 1)) {
-         puts("# Toom-Cook 3-way multiplying.");
-      }
-      for (x = 8; x < upper_limit_print; x += 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 (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 * increment_print;
-#endif
-#ifdef BN_S_MP_TOOM_SQR_C
-      if ((verbose == 1) || (testmode == 1)) {
-         puts("# Toom-Cook 3-way squaring.");
-      }
-      for (x = 8; x < upper_limit_print; x += 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 (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 * increment_print;
-#endif
    }
-   if (terse == 1) {
+   if (args.terse == 1) {
       printf("%d %d %d %d\n",
-             KARATSUBA_MUL_CUTOFF,
-             KARATSUBA_SQR_CUTOFF,
-             TOOM_MUL_CUTOFF,
-             TOOM_SQR_CUTOFF);
+             updated.KARATSUBA_MUL,
+             updated.KARATSUBA_SQR,
+             updated.TOOM_MUL,
+             updated.TOOM_SQR);
    } 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.KARATSUBA_MUL);
+      printf("KARATSUBA_SQR_CUTOFF = %d\n", updated.KARATSUBA_SQR);
+      printf("TOOM_MUL_CUTOFF = %d\n", updated.TOOM_MUL);
+      printf("TOOM_SQR_CUTOFF = %d\n", updated.TOOM_SQR);
    }
 
-   if (print == 1) {
+   if (args.print == 1) {
       printf("Printing data for graphing to \"%s\" and \"%s\"\n",mullog, sqrlog);
 
       multiplying = fopen(mullog, "w+");
@@ -641,47 +497,32 @@ int main(int argc, char **argv)
          exit(EXIT_FAILURE);
       }
 
-      for (x = 8; x < upper_limit_print; x += increment_print) {
-         KARATSUBA_MUL_CUTOFF = INT_MAX;
-         KARATSUBA_SQR_CUTOFF = INT_MAX;
-         TOOM_MUL_CUTOFF = INT_MAX;
-         TOOM_SQR_CUTOFF = INT_MAX;
+      for (x = 8; x < args.upper_limit_print; x += args.increment_print) {
+         set_cutoffs(&max_cutoffs);
          t1 = s_time_mul(x);
-         KARATSUBA_MUL_CUTOFF = kss;
-         KARATSUBA_SQR_CUTOFF = ksm;
-         TOOM_MUL_CUTOFF = tc3m;
-         TOOM_SQR_CUTOFF = tc3s;
+         set_cutoffs(&orig);
          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);
-         if (verbose == 1) {
+         if (args.verbose == 1) {
             printf("MUL %d: %9"PRIu64" %9"PRIu64", %9"PRIi64"\n", x, t1, t2, (int64_t)t2 - (int64_t)t1);
             fflush(stdout);
          }
-         KARATSUBA_MUL_CUTOFF = INT_MAX;
-         KARATSUBA_SQR_CUTOFF = INT_MAX;
-         TOOM_MUL_CUTOFF = INT_MAX;
-         TOOM_SQR_CUTOFF = INT_MAX;
+         set_cutoffs(&max_cutoffs);
          t1 = s_time_sqr(x);
-         KARATSUBA_MUL_CUTOFF = kss;
-         KARATSUBA_SQR_CUTOFF = ksm;
-         TOOM_MUL_CUTOFF = tc3m;
-         TOOM_SQR_CUTOFF = tc3s;
+         set_cutoffs(&orig);
          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);
-         if (verbose == 1) {
+         if (args.verbose == 1) {
             printf("SQR %d: %9"PRIu64" %9"PRIu64", %9"PRIi64"\n", x, t1, t2, (int64_t)t2 - (int64_t)t1);
             fflush(stdout);
          }
       }
       printf("Finished. Data for graphing in \"%s\" and \"%s\"\n",mullog, sqrlog);
-      if (verbose == 1) {
-         KARATSUBA_MUL_CUTOFF = kss;
-         KARATSUBA_SQR_CUTOFF = ksm;
-         TOOM_MUL_CUTOFF = tc3m;
-         TOOM_SQR_CUTOFF = tc3s;
-         if (terse == 1) {
+      if (args.verbose == 1) {
+         set_cutoffs(&orig);
+         if (args.terse == 1) {
             printf("%d %d %d %d\n",
                    KARATSUBA_MUL_CUTOFF,
                    KARATSUBA_SQR_CUTOFF,
diff --git a/etc/tune_it.sh b/etc/tune_it.sh
index b323410..5e0fe7c 100755
--- a/etc/tune_it.sh
+++ b/etc/tune_it.sh
@@ -11,28 +11,13 @@ LCG() {
   SEED=$(((1103515245 * $SEED + 12345) % 2147483648))
   echo $SEED
 }
-
 median() {
-  median=0;
-  flag=1;
-  for val in $* ; do
-     if [ $flag -eq 1 ] ; then
-        flag=$((flag + 1))
-        continue
-     elif [ $flag -eq 2 ] ; then
-         median=$val
-         flag=$((flag + 1))
-         continue
-     else
-       if [ $median -lt $val ] ; then
-          median=$((median + 1))
-       fi
-       if [ $median -gt $val ] ; then
-          median=$((median - 1))
-       fi
-     fi
-  done
-  echo $median
+# read everything besides the header from file $1
+#   | cut-out the required column $2
+#     | sort all the entries numerically
+#       | show only the first $3 entries
+#         | show only the last entry
+  tail -n +2 $1 | cut -d' ' -f$2 | sort -n | head -n $3 | tail -n 1
 }
 
 MPWD=$(dirname $(readlink -f "$0"))
@@ -100,27 +85,23 @@ cat << END_OF_INPUT > $TOMMATH_CUTOFFS_H || die "Writing header to $TOMMATH_CUTO
  */
 END_OF_INPUT
 
-# The Posix shell does not offer an array data type
+# The Posix shell does not offer an array data type so we create
+# the median with 'standard tools'^TM
 
-i=1;
-TMP=""
-TMP=$(cat $FILE_NAME | cut -d' ' -f$i )
-TMP=$(median $TMP )
+# read the file (without the first line) and count the lines
+i=$(tail -n +2 $FILE_NAME | wc -l)
+# our median point will be at $i entries
+i=$(( (i / 2) + 1 ))
+TMP=$(median $FILE_NAME 1 $i)
 echo "#define MP_DEFAULT_KARATSUBA_MUL_CUTOFF $TMP"
 echo "#define MP_DEFAULT_KARATSUBA_MUL_CUTOFF $TMP" >> $TOMMATH_CUTOFFS_H || die "(km) Appending to $TOMMATH_CUTOFFS_H" $?
-i=$((i + 1))
-TMP=$(cat $FILE_NAME | cut -d' ' -f$i )
-TMP=$(median $TMP );
+TMP=$(median $FILE_NAME 2 $i)
 echo "#define MP_DEFAULT_KARATSUBA_SQR_CUTOFF $TMP"
 echo "#define MP_DEFAULT_KARATSUBA_SQR_CUTOFF $TMP" >> $TOMMATH_CUTOFFS_H || die "(ks) Appending to $TOMMATH_CUTOFFS_H" $?
-i=$((i + 1))
-TMP=$(cat $FILE_NAME | cut -d' ' -f$i)
-TMP=$(median $TMP );
+TMP=$(median $FILE_NAME 3 $i)
 echo "#define MP_DEFAULT_TOOM_MUL_CUTOFF      $TMP"
 echo "#define MP_DEFAULT_TOOM_MUL_CUTOFF      $TMP" >> $TOMMATH_CUTOFFS_H || die "(tc3m) Appending to $TOMMATH_CUTOFFS_H" $?
-i=$((i + 1))
-TMP=$(cat $FILE_NAME | cut -d' ' -f$i)
-TMP=$(median $TMP );
+TMP=$(median $FILE_NAME 4 $i)
 echo "#define MP_DEFAULT_TOOM_SQR_CUTOFF      $TMP"
 echo "#define MP_DEFAULT_TOOM_SQR_CUTOFF      $TMP" >> $TOMMATH_CUTOFFS_H || die "(tc3s) Appending to $TOMMATH_CUTOFFS_H" $?
 
diff --git a/makefile.mingw b/makefile.mingw
index fcab293..e335d48 100644
--- a/makefile.mingw
+++ b/makefile.mingw
@@ -60,7 +60,7 @@ bn_s_mp_prime_is_divisible.o bn_s_mp_rand_jenkins.o bn_s_mp_rand_platform.o bn_s
 bn_s_mp_sqr.o bn_s_mp_sqr_fast.o bn_s_mp_sub.o bn_s_mp_toom_mul.o bn_s_mp_toom_sqr.o
 
 HEADERS_PUB=tommath.h
-HEADERS=tommath_private.h tommath_class.h tommath_superclass.h $(HEADERS_PUB)
+HEADERS=tommath_private.h tommath_class.h tommath_superclass.h tommath_cutoffs.h $(HEADERS_PUB)
 
 #The default rule for make builds the libtommath.a library (static)
 default: $(LIBMAIN_S)
diff --git a/makefile.msvc b/makefile.msvc
index 5a4418a..aeac2e7 100644
--- a/makefile.msvc
+++ b/makefile.msvc
@@ -52,7 +52,7 @@ bn_s_mp_prime_is_divisible.obj bn_s_mp_rand_jenkins.obj bn_s_mp_rand_platform.ob
 bn_s_mp_sqr.obj bn_s_mp_sqr_fast.obj bn_s_mp_sub.obj bn_s_mp_toom_mul.obj bn_s_mp_toom_sqr.obj
 
 HEADERS_PUB=tommath.h
-HEADERS=tommath_private.h tommath_class.h tommath_superclass.h $(HEADERS_PUB)
+HEADERS=tommath_private.h tommath_class.h tommath_superclass.h tommath_cutoffs.h $(HEADERS_PUB)
 
 #The default rule for make builds the tommath.lib library (static)
 default: $(LIBMAIN_S)
diff --git a/makefile.unix b/makefile.unix
index ba8c243..bd83d85 100644
--- a/makefile.unix
+++ b/makefile.unix
@@ -61,7 +61,7 @@ bn_s_mp_prime_is_divisible.o bn_s_mp_rand_jenkins.o bn_s_mp_rand_platform.o bn_s
 bn_s_mp_sqr.o bn_s_mp_sqr_fast.o bn_s_mp_sub.o bn_s_mp_toom_mul.o bn_s_mp_toom_sqr.o
 
 HEADERS_PUB=tommath.h
-HEADERS=tommath_private.h tommath_class.h tommath_superclass.h $(HEADERS_PUB)
+HEADERS=tommath_private.h tommath_class.h tommath_superclass.h tommath_cutoffs.h $(HEADERS_PUB)
 
 #The default rule for make builds the libtommath.a library (static)
 default: $(LIBMAIN_S)
diff --git a/makefile_include.mk b/makefile_include.mk
index be81586..df0adf8 100644
--- a/makefile_include.mk
+++ b/makefile_include.mk
@@ -124,7 +124,7 @@ else
 endif
 
 HEADERS_PUB=tommath.h
-HEADERS=tommath_private.h tommath_class.h tommath_superclass.h $(HEADERS_PUB)
+HEADERS=tommath_private.h tommath_class.h tommath_superclass.h tommath_cutoffs.h $(HEADERS_PUB)
 
 #LIBPATH  The directory for libtommath to be installed to.
 #INCPATH  The directory to install the header files for libtommath.