Commit db76bed2205fc807e910af8cb783d98eb8bbd169

czurnieden 2019-05-04T18:57:07

added autotuning functionality for the Toom-Cook cut-offs

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
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
diff --git a/.travis.yml b/.travis.yml
index 56d4a60..bde3d08 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -141,6 +141,16 @@ matrix:
     - env: BUILDOPTIONS='--with-cc=clang-7 --cflags=-DMP_16BIT --with-valgrind'
     - env: BUILDOPTIONS='--with-cc=clang-7 --cflags=-DMP_32BIT --with-valgrind'
 
+    # Test "autotuning", the automatic evaluation and setting of the Toom-Cook cut-offs.
+    - env: BUILDOPTIONS='--with-cc=gcc-5 --cflags=-DMP_8BIT  --with-valgrind --make-option=tune'
+    - env: BUILDOPTIONS='--with-cc=gcc-5 --cflags=-DMP_16BIT --with-valgrind --make-option=tune'
+    - env: BUILDOPTIONS='--with-cc=gcc-5 --cflags=-DMP_32BIT --with-valgrind --make-option=tune'
+    - env: BUILDOPTIONS='--with-cc=gcc-5 --with-valgrind --make-option=tune'
+    - env: BUILDOPTIONS='--with-cc=clang-7 --cflags=-DMP_8BIT  --with-valgrind --make-option=tune'
+    - env: BUILDOPTIONS='--with-cc=clang-7 --cflags=-DMP_16BIT --with-valgrind --make-option=tune'
+    - env: BUILDOPTIONS='--with-cc=clang-7 --cflags=-DMP_32BIT --with-valgrind --make-option=tune'
+    - env: BUILDOPTIONS='--with-cc=clang-7 --with-valgrind --make-option=tune'
+
     # GCC for the x86-64 architecture testing against a different Bigint-implementation
     # with 333333 different inputs.
     - env: BUILDOPTIONS='--with-cc=gcc-5 --test-vs-mtest=333333 --with-valgrind'
@@ -151,6 +161,7 @@ matrix:
     - env: BUILDOPTIONS='--with-cc=gcc-5 --test-vs-mtest=333333 --mtest-real-rand --with-valgrind'
     - env: BUILDOPTIONS='--with-cc=clang-7 --test-vs-mtest=333333 --mtest-real-rand --with-valgrind'
 
+
 # Notifications go to
 # An email address is also possible.
 notifications:
diff --git a/doc/bn.tex b/doc/bn.tex
index 2e00bee..ed4c2f2 100644
--- a/doc/bn.tex
+++ b/doc/bn.tex
@@ -110,7 +110,7 @@ ICC does not know all options available for GCC and LibTomMath uses two diagnost
 icc: command line warning #10148: option '-Wbad-function-cast' not supported
 icc: command line warning #10148: option '-Wcast-align' not supported
 \end{alltt}
-It is possible to mute this ICC warning with the compiler flag \texttt{-diag-disable=10006}\footnote{It is not recommended to suppress warnings without a very good reason but there is no harm in doing so in this very special case.}.
+It is possible to mute this ICC warning with the compiler flag \texttt{-diag-disable=10148}\footnote{It is not recommended to suppress warnings without a very good reason but there is no harm in doing so in this very special case.}.
 
 \subsection{Static Libraries}
 To build as a static library for GCC issue the following
@@ -127,6 +127,12 @@ nmake -f makefile.msvc
 This will build the library and archive the object files in ``tommath.lib''.  This has been tested with MSVC
 version 6.00 with service pack 5.
 
+To run a program to adapt the Toom-Cook cut-off values to your architecture type
+\begin{alltt}
+make tune
+\end{alltt}
+This will take some time.
+
 \subsection{Shared Libraries}
 \subsubsection{GNU based Operating Systems}
 To build as a shared library for GCC issue the following
@@ -137,6 +143,13 @@ This requires the ``libtool'' package (common on most Linux/BSD systems).  It wi
 and static then install (by default) into /usr/lib as well as install the header files in /usr/include.  The shared
 library (resource) will be called ``libtommath.la'' while the static library called ``libtommath.a''.  Generally
 you use libtool to link your application against the shared object.
+
+To run a program to adapt the Toom-Cook cut-off values to your architecture type
+\begin{alltt}
+make -f makefile.shared tune
+\end{alltt}
+This will take some time.
+
 \subsubsection{Microsoft Windows based Operating Systems}
 There is limited support for making a ``DLL'' in windows via the ``makefile.cygwin\_dll'' makefile.  It requires
 Cygwin to work with since it requires the auto-export/import functionality.  The resulting DLL and import library
@@ -1366,7 +1379,7 @@ should only be used with very large inputs.  This is followed by the Karatsuba m
 sized inputs.  Then followed by the Comba and baseline multipliers.
 
 Fortunately for the developer you don't really need to know this unless you really want to fine tune the system.  mp\_mul()
-will determine on its own\footnote{Some tweaking may be required.} what routine to use automatically when it is called.
+will determine on its own\footnote{Some tweaking may be required but \texttt{make tune} will put some reasonable values in \texttt{bncore.c}} what routine to use automatically when it is called.
 
 \begin{alltt}
 int main(void)
@@ -1448,34 +1461,17 @@ GCC 3.3.1 and an Athlon XP processor the cutoff point is roughly 110 digits (abo
 Toom-Cook has incredible overhead and is probably only useful for very large inputs.  So far no known cutoff points
 exist and for the most part I just set the cutoff points very high to make sure they're not called.
 
-A demo program in the ``etc/'' directory of the project called ``tune.c'' can be used to find the cutoff points.  This
-can be built with GCC as follows
+To get reasonable values for the cut-off points for your architecture, type
 
 \begin{alltt}
-make XXX
+make tune
 \end{alltt}
-Where ``XXX'' is one of the following entries from the table \ref{fig:tuning}.
 
-\begin{figure}[h]
-\begin{center}
-\begin{small}
-\begin{tabular}{|l|l|}
-\hline \textbf{Value of XXX} & \textbf{Meaning} \\
-\hline tune & Builds portable tuning application \\
-\hline tune86 & Builds x86 (pentium and up) program for COFF \\
-\hline tune86c & Builds x86 program for Cygwin \\
-\hline tune86l & Builds x86 program for Linux (ELF format) \\
-\hline
-\end{tabular}
-\end{small}
-\end{center}
-\caption{Build Names for Tuning Programs}
-\label{fig:tuning}
-\end{figure}
+This will run a benchmark, computes the medians, rewrites \texttt{bncore.c}, and recompiles \texttt{bncore.c} and relinks the library.
+
+The benchmark itself can be fine-tuned in the file \texttt{etc/tune\_it.sh}.
 
-When the program is running it will output a series of measurements for different cutoff points.  It will first find
-good Karatsuba squaring and multiplication points.  Then it proceeds to find Toom-Cook points.  Note that the Toom-Cook
-tuning takes a very long time as the cutoff points are likely to be very high.
+The program \texttt{etc/tune} is also able to print a list of values for printing curves with e.g.: \texttt{gnuplot}. type \texttt{./etc/tune -h} to get a list of all available options.
 
 \chapter{Modular Reduction}
 
@@ -1846,7 +1842,7 @@ int mp_sqrt (mp_int * a, mp_digit b, mp_int * c)
 
 \chapter{Logarithm}
 \section{Integer Logarithm}
-A logarithm function for positive integer input \texttt{a, base} computing  $\floor{\log_bx}$ such that $(\ilog_bx)^b \le x$.
+A logarithm function for positive integer input \texttt{a, base} computing  $\floor{\log_bx}$ such that $(\log_b x)^b \le x$.
 \index{mp\_ilogb}
 \begin{alltt}
 int mp_ilogb(mp_int *a, mp_digit base, mp_int *c)
diff --git a/etc/makefile b/etc/makefile
index 99154d8..04a97fa 100644
--- a/etc/makefile
+++ b/etc/makefile
@@ -1,4 +1,4 @@
-CFLAGS += -Wall -W -Wshadow -O3 -fomit-frame-pointer -funroll-loops -I../
+CFLAGS += -Wall -W -Wextra -Wshadow -O3 -I../
 
 # default lib name (requires install with root)
 # LIBNAME=-ltommath
@@ -8,43 +8,36 @@ LIBNAME=../libtommath.a
 
 #provable primes
 pprime: pprime.o
-	$(CC) pprime.o $(LIBNAME) -o pprime
+	$(CC) $(CFLAGS) pprime.o $(LIBNAME) -o pprime
 
 # portable [well requires clock()] tuning app
 tune: tune.o
-	$(CC) tune.o $(LIBNAME) -o tune
-	
-# same app but using RDTSC for higher precision [requires 80586+], coff based gcc installs [e.g. ming, cygwin, djgpp]
-tune86: tune.c
-	nasm -f coff timer.asm
-	$(CC) -DX86_TIMER $(CFLAGS) tune.c timer.o  $(LIBNAME) -o tune86
-	
-# for cygwin
-tune86c: tune.c
-	nasm -f gnuwin32 timer.asm
-	$(CC) -DX86_TIMER $(CFLAGS) tune.c timer.o  $(LIBNAME) -o tune86
-
-#make tune86 for linux or any ELF format
-tune86l: tune.c
-	nasm -f elf -DUSE_ELF timer.asm
-	$(CC) -DX86_TIMER $(CFLAGS) tune.c timer.o $(LIBNAME) -o tune86l
+	# The actual benchmark program
+	$(CC) $(CFLAGS) tune.o $(LIBNAME) -o tune
+	# a small script to run it
+	/bin/sh tune_it.sh
+
+test_standalone: tune.o
+	# The benchmark program works as a testtool, too
+	$(CC) $(CFLAGS) tune.o $(LIBNAME) -o test
         
 # spits out mersenne primes
 mersenne: mersenne.o
-	$(CC) mersenne.o $(LIBNAME) -o mersenne
+	$(CC) $(CFLAGS) mersenne.o $(LIBNAME) -o mersenne
 
-# fines DR safe primes for the given config
+# finds DR safe primes for the given config
 drprime: drprime.o
-	$(CC) drprime.o $(LIBNAME) -o drprime
+	$(CC) $(CFLAGS) drprime.o $(LIBNAME) -o drprime
 	
-# fines 2k safe primes for the given config
+# finds 2k safe primes for the given config
 2kprime: 2kprime.o
-	$(CC) 2kprime.o $(LIBNAME) -o 2kprime
+	$(CC) $(CFLAGS) 2kprime.o $(LIBNAME) -o 2kprime
 
 mont: mont.o
-	$(CC) mont.o $(LIBNAME) -o mont
+	$(CC) $(CFLAGS) mont.o $(LIBNAME) -o mont
 
         
 clean:
-	rm -f *.log *.o *.obj *.exe pprime tune mersenne drprime tune86 tune86l mont 2kprime pprime.dat \
-         *.da *.dyn *.dpi *~
+	rm -f *.log *.o *.obj *.exe pprime tune mersenne drprime mont 2kprime pprime.dat \
+        tuning_list multiplying squaring test *.da *.dyn *.dpi *~
+
diff --git a/etc/makefile.icc b/etc/makefile.icc
index 8a1ffff..fc1512f 100644
--- a/etc/makefile.icc
+++ b/etc/makefile.icc
@@ -28,9 +28,11 @@ LIBNAME=../libtommath.a
 pprime: pprime.o
 	$(CC) pprime.o $(LIBNAME) -o pprime
 
-# portable [well requires clock()] tuning app
 tune: tune.o
-	$(CC) tune.o $(LIBNAME) -o tune
+	# The actual benchmark program
+	$(CC) $(CFLAGS) tune.o $(LIBNAME) -o tune
+	# a small script to run it
+	/bin/sh tune_it.sh
 	
 # same app but using RDTSC for higher precision [requires 80586+], coff based gcc installs [e.g. ming, cygwin, djgpp]
 tune86: tune.c
@@ -64,4 +66,4 @@ mont: mont.o
 
         
 clean:
-	rm -f *.log *.o *.obj *.exe pprime tune mersenne drprime tune86 tune86l mont 2kprime pprime.dat *.il
+	rm -f *.log *.o *.obj *.exe pprime tune mersenne drprime tune86 tune86l mont 2kprime pprime.dat *.il tuning_list
diff --git a/etc/makefile.msvc b/etc/makefile.msvc
index 2833372..592a437 100644
--- a/etc/makefile.msvc
+++ b/etc/makefile.msvc
@@ -9,10 +9,11 @@ pprime: pprime.obj
 
 mersenne: mersenne.obj
 	cl mersenne.obj ../tommath.lib
-	
+
 tune: tune.obj
 	cl tune.obj ../tommath.lib
 
+
 mont: mont.obj
 	cl mont.obj ../tommath.lib
 	
diff --git a/etc/tune.c b/etc/tune.c
index 5232ed7..9038c78 100644
--- a/etc/tune.c
+++ b/etc/tune.c
@@ -2,141 +2,748 @@
  *
  * Tom St Denis, tstdenis82@gmail.com
  */
-#include <tommath.h>
+#include "../tommath.h"
+#include "../tommath_private.h"
 #include <stdint.h>
+#include <time.h>
+#include <inttypes.h>
+#include <limits.h>
+#include <errno.h>
 
-/* how many times todo each size mult.  Depends on your computer.  For slow computers
- * this can be low like 5 or 10.  For fast [re: Athlon] should be 25 - 50 or so
- */
-#define TIMES (1UL<<14UL)
+static uint64_t s_ranval(void);
+static void s_raninit(uint64_t seed);
+static int s_mp_random(mp_int *a, int limbs);
+static uint64_t s_timer_function(void);
+static void s_timer_start(void);
+static uint64_t s_timer_stop(void);
+static uint64_t s_time_mul(int size);
+static uint64_t s_time_sqr(int size);
+static void s_usage(char *s);
+
+/*
+   Please take in mind that both multiplicands are of the same size. The balancing
+   mechanism in mp_balance works well but has some overhead itself. You can test
+   the behaviour of it with the option "-o" followed by a (small) positive number 'x'
+   to generate ratios of the form 1:x.
+*/
 
-#ifndef X86_TIMER
+/* Bob Jenkins' http://burtleburtle.net/bob/rand/smallprng.html */
+/* Chosen for speed and a good "mix" */
+typedef struct ranctx {
+   uint64_t a;
+   uint64_t b;
+   uint64_t c;
+   uint64_t d;
+} ranctx;
 
-/* RDTSC from Scott Duplichan */
-static uint64_t TIMFUNC(void)
+static ranctx burtle_x;
+
+#   define rot(x,k) (((x)<<(k))|((x)>>(64-(k))))
+static uint64_t s_ranval(void)
 {
-#   if defined __GNUC__
-#      if defined(__i386__) || defined(__x86_64__)
-   /* version from http://www.mcs.anl.gov/~kazutomo/rdtsc.html
-    * the old code always got a warning issued by gcc, clang did not complain...
-    */
-   unsigned hi, lo;
-   __asm__ __volatile__("rdtsc" : "=a"(lo), "=d"(hi));
-   return ((uint64_t)lo)|(((uint64_t)hi)<<32);
-#      else /* gcc-IA64 version */
-   unsigned long result;
-   __asm__ __volatile__("mov %0=ar.itc" : "=r"(result) :: "memory");
-   while (__builtin_expect((int) result == -1, 0))
-      __asm__ __volatile__("mov %0=ar.itc" : "=r"(result) :: "memory");
-   return result;
-#      endif
-
-   /* Microsoft and Intel Windows compilers */
-#   elif defined _M_IX86
-   __asm rdtsc
-#   elif defined _M_AMD64
-   return __rdtsc();
-#   elif defined _M_IA64
-#      if defined __INTEL_COMPILER
-#         include <ia64intrin.h>
-#      endif
-   return __getReg(3116);
-#   else
-#      error need rdtsc function for this build
-#   endif
+   uint64_t e = burtle_x.a - rot(burtle_x.b, 7);
+   burtle_x.a = burtle_x.b ^ rot(burtle_x.c, 13);
+   burtle_x.b = burtle_x.c + rot(burtle_x.d, 37);
+   burtle_x.c = burtle_x.d + e;
+   burtle_x.d = e + burtle_x.a;
+   return burtle_x.d;
 }
 
+static void s_raninit(uint64_t seed)
+{
+   uint64_t i;
+   burtle_x.a = 0xf1ea5eed;
+   burtle_x.b = burtle_x.c = burtle_x.d = seed;
+   for (i = 0; i < 20; ++i) {
+      (void) s_ranval();
+   }
+}
 
-/* *INDENT-OFF* */
-/* generic ISO C timer */
-static uint64_t LBL_T;
-static void t_start(void) { LBL_T = TIMFUNC(); }
-static uint64_t t_read(void) { return TIMFUNC() - LBL_T; }
-/* *INDENT-ON* */
+/*
+   The original used LTM's mp_rand which uses the cryptographically secure
+   source of the OS for its purpose. That is too expensive, too slow and
+   most important for a benchmark: it is not repeatable.
+*/
+static int s_mp_random(mp_int *a, int limbs)
+{
+   int e = MP_OKAY;
+   if ((e = mp_grow(a, limbs + 1)) != MP_OKAY) {
+      goto LTM_ERR;
+   }
+   a->used = limbs--;
+   do {
+      a->dp[limbs] = (mp_digit)(s_ranval() & MP_MASK);
+   } while (limbs--);
+   mp_clamp(a);
+LTM_ERR:
+   return e;
+}
+
+static uint64_t s_timer_function(void)
+{
+#if _POSIX_C_SOURCE >= 199309L
+#define LTM_BILLION 1000000000
+   struct timespec ts;
 
+   /* TODO: Sets errno in case of error. Use? */
+   clock_gettime(CLOCK_MONOTONIC, &ts);
+   return (((uint64_t)ts.tv_sec) * LTM_BILLION + (uint64_t)ts.tv_nsec);
 #else
-extern void t_start(void);
-extern uint64_t t_read(void);
+   clock_t t;
+   t = clock();
+   if (t < (clock_t)(0)) {
+      return (uint64_t)(0);
+   }
+   return (uint64_t)(t);
 #endif
+}
 
-static uint64_t time_mult(int size, int s)
+/* generic ISO C timer */
+static uint64_t s_timer_tmp;
+static void s_timer_start(void)
 {
-   unsigned long     x;
-   mp_int  a, b, c;
-   uint64_t t1;
+   s_timer_tmp = s_timer_function();
+}
+static uint64_t s_timer_stop(void)
+{
+   return s_timer_function() - s_timer_tmp;
+}
 
-   mp_init(&a);
-   mp_init(&b);
-   mp_init(&c);
 
-   mp_rand(&a, size);
-   mp_rand(&b, size);
+static int s_check_result;
+static int s_number_of_test_loops;
+static int s_stabilization_extra;
+static int s_offset = 1;
 
-   if (s == 1) {
-      KARATSUBA_MUL_CUTOFF = size;
-   } else {
-      KARATSUBA_MUL_CUTOFF = 100000;
+#define s_mp_mul(a, b, c) s_mp_mul_digs(a, b, c, (a)->used + (b)->used + 1)
+static uint64_t s_time_mul(int size)
+{
+   int x, e;
+   mp_int  a, b, c, d;
+   uint64_t t1;
+
+   if ((e = mp_init_multi(&a, &b, &c, &d, NULL)) != MP_OKAY) {
+      t1 = UINT64_MAX;
+      goto LTM_ERR;
    }
 
-   t_start();
-   for (x = 0; x < TIMES; x++) {
-      mp_mul(&a,&b,&c);
+   if ((e = s_mp_random(&a, size * s_offset)) != MP_OKAY) {
+      t1 = UINT64_MAX;
+      goto LTM_ERR;
    }
-   t1 = t_read();
-   mp_clear(&a);
-   mp_clear(&b);
-   mp_clear(&c);
+   if ((e = s_mp_random(&b, size)) != MP_OKAY) {
+      t1 = UINT64_MAX;
+      goto LTM_ERR;
+   }
+
+   s_timer_start();
+   for (x = 0; x < s_number_of_test_loops; x++) {
+      if ((e = mp_mul(&a,&b,&c)) != MP_OKAY) {
+         t1 = UINT64_MAX;
+         goto LTM_ERR;
+      }
+      if (s_check_result == 1) {
+         if ((e = s_mp_mul(&a,&b,&d)) != MP_OKAY) {
+            t1 = UINT64_MAX;
+            goto LTM_ERR;
+         }
+         if (mp_cmp(&c, &d) != MP_EQ) {
+            /* Time of 0 cannot happen (famous last words?) */
+            t1 = 0uLL;
+            goto LTM_ERR;
+         }
+      }
+   }
+
+   t1 = s_timer_stop();
+LTM_ERR:
+   mp_clear_multi(&a, &b, &c, &d, NULL);
    return t1;
 }
 
-static uint64_t time_sqr(int size, int s)
+static uint64_t s_time_sqr(int size)
 {
-   unsigned long     x;
-   mp_int  a, b;
+   int x, e;
+   mp_int  a, b, c;
    uint64_t t1;
 
-   mp_init(&a);
-   mp_init(&b);
-
-   mp_rand(&a, size);
+   if ((e = mp_init_multi(&a, &b, &c, NULL)) != MP_OKAY) {
+      t1 = UINT64_MAX;
+      goto LTM_ERR;
+   }
 
-   if (s == 1) {
-      KARATSUBA_SQR_CUTOFF = size;
-   } else {
-      KARATSUBA_SQR_CUTOFF = 100000;
+   if ((e = s_mp_random(&a, size)) != MP_OKAY) {
+      t1 = UINT64_MAX;
+      goto LTM_ERR;
    }
 
-   t_start();
-   for (x = 0; x < TIMES; x++) {
-      mp_sqr(&a,&b);
+   s_timer_start();
+   for (x = 0; x < s_number_of_test_loops; x++) {
+      if ((e = mp_sqr(&a,&b)) != MP_OKAY) {
+         t1 = UINT64_MAX;
+         goto LTM_ERR;
+      }
+      if (s_check_result == 1) {
+         if ((e = s_mp_sqr(&a,&c)) != MP_OKAY) {
+            t1 = UINT64_MAX;
+            goto LTM_ERR;
+         }
+         if (mp_cmp(&c, &b) != MP_EQ) {
+            t1 = 0uLL;
+            goto LTM_ERR;
+         }
+      }
    }
-   t1 = t_read();
-   mp_clear(&a);
-   mp_clear(&b);
+
+   t1 = s_timer_stop();
+LTM_ERR:
+   mp_clear_multi(&a, &b, &c, NULL);
    return t1;
 }
 
-int main(void)
+static void s_usage(char *s)
+{
+   fprintf(stderr,"Usage: %s [TvcpGbtrSLFfMmosh]\n",s);
+   fprintf(stderr,"          -T testmode, for use with testme.sh\n");
+   fprintf(stderr,"          -v verbose, print all timings\n");
+   fprintf(stderr,"          -c check results\n");
+   fprintf(stderr,"          -p print benchmark of final cutoffs in files \"multiplying\"\n");
+   fprintf(stderr,"             and \"squaring\"\n");
+   fprintf(stderr,"          -G [string] suffix for the filenames listed above\n");
+   fprintf(stderr,"             Implies '-p'\n");
+   fprintf(stderr,"          -b print benchmark of bncore.c\n");
+   fprintf(stderr,"          -t prints comma separated results\n");
+   fprintf(stderr,"          -r [64] number of rounds\n");
+   fprintf(stderr,"          -S [0xdeadbeef] seed for PRNG\n");
+   fprintf(stderr,"          -L [3] number of negative values accumulated until the result is accepted\n");
+   fprintf(stderr,"          -M [3000] upper limit of T-C tests/prints\n");
+   fprintf(stderr,"          -m [1] increment of T-C tests/prints\n");
+   fprintf(stderr,"          -o [1] multiplier for the second multiplicand\n");
+   fprintf(stderr,"             (Not for computing the cut-offs!)\n");
+   fprintf(stderr,"          -s 'preset' use values in 'preset' for printing.\n");
+   fprintf(stderr,"             'preset' is a comma separated string with cut-offs for\n");
+   fprintf(stderr,"             ksm, kss, tc3m, tc3s in that order\n");
+   fprintf(stderr,"             ksm  = karatsuba multiplication\n");
+   fprintf(stderr,"             kss  = karatsuba squaring\n");
+   fprintf(stderr,"             tc3m = Toom-Cook 3-way multiplication\n");
+   fprintf(stderr,"             tc3s = Toom-Cook 3-way squaring\n");
+   fprintf(stderr,"             Implies '-p'\n");
+   fprintf(stderr,"          -h this message\n");
+}
+
+
+
+int main(int argc, char **argv)
 {
    uint64_t t1, t2;
-   int x, y;
+   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;
+
+   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;
 
-   for (x = 8; ; x += 2) {
-      t1 = time_mult(x, 0);
-      t2 = time_mult(x, 1);
-      printf("%d: %9llu %9llu, %9llu\n", x, t1, t2, t2 - t1);
-      if (t2 < t1) break;
+   FILE *squaring, *multiplying;
+   char mullog[256] = "multiplying";
+   char sqrlog[256] = "squaring";
+   s_number_of_test_loops = 64;
+   s_stabilization_extra = 3;
+
+   /* 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;
+            s_check_result = 1;
+            upper_limit_print = 1000;
+            increment_print = 11;
+            s_number_of_test_loops = 1;
+            s_stabilization_extra = 1;
+            s_offset = 1;
+            break;
+         case 'v':
+            verbose = 1;
+            break;
+         case 'c':
+            s_check_result = 1;
+            break;
+         case 'p':
+            print = 1;
+            break;
+         case 'G':
+            print = 1;
+            opt++;
+            if (opt >= argc) {
+               s_usage(argv[0]);
+            }
+            /* manual strcat() */
+            for (i = 0; i < 255; i++) {
+               if (mullog[i] == '\0') {
+                  break;
+               }
+            }
+            for (j = 0; i < 255; j++, i++) {
+               mullog[i] = argv[opt][j];
+               if (argv[opt][j] == '\0') {
+                  break;
+               }
+            }
+            for (i = 0; i < 255; i++) {
+               if (sqrlog[i] == '\0') {
+                  break;
+               }
+            }
+            for (j = 0; i < 255; j++, i++) {
+               sqrlog[i] = argv[opt][j];
+               if (argv[opt][j] == '\0') {
+                  break;
+               }
+            }
+            break;
+         case 'b':
+            bncore = 1;
+            break;
+         case 't':
+            terse = 1;
+            break;
+         case 'S':
+            opt++;
+            if (opt >= argc) {
+               s_usage(argv[0]);
+            }
+            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;
+            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;
+            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;
+            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;
+            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 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;
+            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;
+            break;
+         case 's':
+            printpreset = 1;
+            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++;
+            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++;
+            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++;
+            str = endptr + 1;
+            TOOM_SQR_CUTOFF = (int)val;
+         case 'h':
+         default:
+            s_usage(argv[0]);
+            exit(EXIT_FAILURE);
+         }
+      }
    }
-   y = x;
 
-   for (x = 8; ; x += 2) {
-      t1 = time_sqr(x, 0);
-      t2 = time_sqr(x, 1);
-      printf("%d: %9llu %9llu, %9llu\n", x, t1, t2, t2 - t1);
-      if (t2 < t1) break;
+   s_raninit(seed);
+
+   ksm  = KARATSUBA_MUL_CUTOFF;
+   kss  = KARATSUBA_SQR_CUTOFF;
+   tc3m = TOOM_MUL_CUTOFF;
+   tc3s = TOOM_SQR_CUTOFF;
+
+   if ((bncore == 0) && (printpreset == 0)) {
+      /* 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);
+         }
+         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) {
+      printf("%d %d %d %d\n",
+             KARATSUBA_MUL_CUTOFF,
+             KARATSUBA_SQR_CUTOFF,
+             TOOM_MUL_CUTOFF,
+             TOOM_SQR_CUTOFF);
+   } 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", y);
-   printf("KARATSUBA_SQR_CUTOFF = %d\n", x);
 
-   return 0;
+   if (print == 1) {
+      printf("Printing data for graphing to \"%s\" and \"%s\"\n",mullog, sqrlog);
+
+      multiplying = fopen(mullog, "w+");
+      if (multiplying == NULL) {
+         fprintf(stderr, "Opening file \"%s\" failed\n", mullog);
+         exit(EXIT_FAILURE);
+      }
+
+      squaring = fopen(sqrlog, "w+");
+      if (squaring == NULL) {
+         fprintf(stderr, "Opening file \"%s\" failed\n",sqrlog);
+         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;
+         t1 = s_time_mul(x);
+         KARATSUBA_MUL_CUTOFF = kss;
+         KARATSUBA_SQR_CUTOFF = ksm;
+         TOOM_MUL_CUTOFF = tc3m;
+         TOOM_SQR_CUTOFF = tc3s;
+         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) {
+            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;
+         t1 = s_time_sqr(x);
+         KARATSUBA_MUL_CUTOFF = kss;
+         KARATSUBA_SQR_CUTOFF = ksm;
+         TOOM_MUL_CUTOFF = tc3m;
+         TOOM_SQR_CUTOFF = tc3s;
+         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) {
+            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) {
+            printf("%d %d %d %d\n",
+                   KARATSUBA_MUL_CUTOFF,
+                   KARATSUBA_SQR_CUTOFF,
+                   TOOM_MUL_CUTOFF,
+                   TOOM_SQR_CUTOFF);
+         } 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);
+         }
+      }
+   }
+   exit(EXIT_SUCCESS);
 }
diff --git a/etc/tune_it.sh b/etc/tune_it.sh
new file mode 100755
index 0000000..4a15646
--- /dev/null
+++ b/etc/tune_it.sh
@@ -0,0 +1,129 @@
+#!/bin/sh
+
+die() {
+  echo "$1 failed"
+  echo "Exiting"
+  exit $2
+}
+# A linear congruential generator is sufficient for the purpose.
+SEED=3735928559
+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
+}
+
+MPWD=$(pwd)
+FILE_NAME="tuning_list"
+BNCORE_C="../bncore.c"
+BACKUP_SUFFIX=".orig"
+RNUM=0;
+#############################################################################
+# It would be a good idea to isolate these processes (with e.g.: cpuset)    #
+#                                                                           #
+# It is not a good idea to e.g: watch high resolution videos while this     #
+# test are running if you do not have enough memory to avoid page faults.   #
+#############################################################################
+
+# Number of rounds overall.
+LIMIT=100
+# Number of loops for each input.
+RLOOPS=10
+# Offset ( > 0 ) . Runs tests with asymmetric input of the form 1:OFFSET
+# Please use another destination for BNCORE_C if you change OFFSET, because the numbers
+# with an offset different from 1 (one) are not usable as the general cut-off values
+# in "bncore.c".
+OFFSET=1
+# Number ( >= 3 ) of positive results (TC-is-faster) accumulated until it is accepted.
+# Due to the algorithm used to compute the median in this Posix compliant shell script
+# the value needs to be 3 (three), not less, to keep the variation small.
+LAG=3
+# Keep the temporary file $FILE_NAME. Set to 0 (zero) to remove it at the end.
+# The file is in a format fit to feed into R directly. If you do it and find the median
+# of this program to be off by more than a couple: please contact the authors and report
+# the numbers from this program and R and the standard deviation. This program is known
+# to get larger errors if the standard deviation is larger than ~50.
+KEEP_TEMP=1
+
+echo "You might like to watch the numbers go up to $LIMIT but it will take a long time!"
+
+# Might not have sufficient rights or disc full.
+echo "km ks tc3m tc3s" > $FILE_NAME || die "Writing header to $FILE_NAME" $?
+i=1
+while [ $i -le $LIMIT ]; do
+   RNUM=$(LCG)
+   echo $i
+   "$MPWD"/tune -t -r $RLOOPS -L $LAG -S "$RNUM" -o $OFFSET >> $FILE_NAME || die "tune" $?
+   i=$((i + 1))
+done
+
+if [ $KEEP_TEMP -eq 0 ]; then
+   rm -v $FILE_NAME || die "Removing $KEEP_TEMP" $?
+fi
+
+echo "Writing cut-off values to \"bncore.c\"."
+echo "In case of failure: a copy of \"bncore.c\" is in \"bncore.c.orig\""
+
+cp -v $BNCORE_C $BNCORE_C$BACKUP_SUFFIX || die "Making backup copy of bncore.c" $?
+
+cat << END_OF_INPUT > $BNCORE_C || die "Writing header to bncore.c" $?
+#include "tommath_private.h"
+#ifdef BNCORE_C
+/* LibTomMath, multiple-precision integer library -- Tom St Denis */
+/* SPDX-License-Identifier: Unlicense */
+/*
+   Current values evaluated on an AMD A8-6600K (64-bit).
+   Type "make tune" to optimize them for your machine but
+   be aware that it may take a long time. It took 2:30 minutes
+   on the aforementioned machine for example.
+ */
+END_OF_INPUT
+
+# The Posix shell does not offer an array data type
+
+i=1;
+TMP=""
+TMP=$(cat $FILE_NAME | cut -d' ' -f$i )
+TMP=$(median $TMP )
+echo "int KARATSUBA_MUL_CUTOFF = $TMP;"
+echo "int KARATSUBA_MUL_CUTOFF = $TMP;" >> $BNCORE_C || die "(km) Appending to bncore.c" $?
+i=$((i + 1))
+TMP=$(cat $FILE_NAME | cut -d' ' -f$i )
+TMP=$(median $TMP );
+echo "int KARATSUBA_SQR_CUTOFF = $TMP;"
+echo "int KARATSUBA_SQR_CUTOFF = $TMP;" >> $BNCORE_C || die "(ks) Appending to bncore.c" $?
+i=$((i + 1))
+TMP=$(cat $FILE_NAME | cut -d' ' -f$i)
+TMP=$(median $TMP );
+echo "int TOOM_MUL_CUTOFF = $TMP;"
+echo "int TOOM_MUL_CUTOFF = $TMP;" >> $BNCORE_C || die "(tc3m) Appending to bncore.c" $?
+i=$((i + 1))
+TMP=$(cat $FILE_NAME | cut -d' ' -f$i)
+TMP=$(median $TMP );
+echo "int TOOM_SQR_CUTOFF = $TMP;"
+echo "int TOOM_SQR_CUTOFF = $TMP;" >> $BNCORE_C || die "(tc3s) Appending to bncore.c" $?
+
+echo "#endif" >> $BNCORE_C || die "(end) Appending to bncore.c" $?
+
diff --git a/makefile b/makefile
index d1d2e23..223b4df 100644
--- a/makefile
+++ b/makefile
@@ -108,6 +108,10 @@ mtest:
 timing: $(LIBNAME) demo/timing.c
 	$(CC) $(CFLAGS) -DTIMER demo/timing.c $(LIBNAME) $(LFLAGS) -o timing
 
+tune: $(LIBNAME)
+	$(MAKE) -C etc tune
+	$(MAKE)
+
 # You have to create a file .coveralls.yml with the content "repo_token: <the token>"
 # in the base folder to be able to submit to coveralls
 coveralls: lcov
diff --git a/makefile.mingw b/makefile.mingw
index 374733a..48d11c9 100644
--- a/makefile.mingw
+++ b/makefile.mingw
@@ -89,6 +89,10 @@ test_standalone: test.exe
 
 all: $(LIBMAIN_S) test_standalone
 
+tune: $(LIBNAME_S)
+	$(MAKE) -C etc tune
+	$(MAKE)
+
 clean:
 	@-cmd /c del /Q /S *.o *.a *.exe *.dll 2>nul
 
diff --git a/makefile.msvc b/makefile.msvc
index 0b2b212..6d96ad1 100644
--- a/makefile.msvc
+++ b/makefile.msvc
@@ -75,6 +75,10 @@ test_standalone: test.exe
 
 all: $(LIBMAIN_S) test_standalone
 
+tune: $(LIBMAIN_S)
+	$(MAKE) -C etc tune
+	$(MAKE)
+
 clean:
 	@-cmd /c del /Q /S *.OBJ *.LIB *.EXE *.DLL 2>nul
 
diff --git a/makefile.shared b/makefile.shared
index 8c5d548..36eea7e 100644
--- a/makefile.shared
+++ b/makefile.shared
@@ -95,3 +95,17 @@ mtest:
 
 timing: $(LIBNAME) demo/timing.c
 	$(LTLINK) $(CFLAGS) $(LDFLAGS) -DTIMER demo/timing.c $(LIBNAME) -o timing
+
+tune: $(LIBNAME)
+	$(LTCOMPILE) $(CFLAGS) -c etc/tune.c -o etc/tune.o
+	$(LTLINK) $(LDFLAGS) -o etc/tune etc/tune.o $(LIBNAME)
+	$(LTCOMPILE) $(CFLAGS) -c etc/statistic_summary_single_column.c -o etc/statistic_summary_single_column.o
+	$(LTLINK) $(LDFLAGS) -o etc/statistic_summary_single_column etc/statistic_summary_single_column.o
+	cd etc/
+	/bin/sh tune_it.sh
+	cd ..
+	$(MAKE) -f makefile.shared
+
+
+
+
diff --git a/makefile.unix b/makefile.unix
index beb7abd..42dfcf9 100644
--- a/makefile.unix
+++ b/makefile.unix
@@ -87,6 +87,10 @@ test_standalone: test
 
 all: $(LIBMAIN_S) test_standalone
 
+tune: $(LIBMAIN_S)
+	$(MAKE) -C etc tune
+	$(MAKE)
+
 #NOTE: this makefile works also on cygwin, thus we need to delete *.exe
 clean:
 	-@rm -f $(OBJECTS) $(LIBMAIN_S)
@@ -98,3 +102,4 @@ install: $(LIBMAIN_S)
 	@cp $(LIBMAIN_S) $(DESTDIR)$(LIBPATH)/
 	@cp $(HEADERS_PUB) $(DESTDIR)$(INCPATH)/
 	@sed -e 's,^prefix=.*,prefix=$(PREFIX),' -e 's,^Version:.*,Version: $(VERSION),' libtommath.pc.in > $(DESTDIR)$(LIBPATH)/pkgconfig/libtommath.pc
+
diff --git a/makefile_include.mk b/makefile_include.mk
index ccc785f..c5cfc28 100644
--- a/makefile_include.mk
+++ b/makefile_include.mk
@@ -147,7 +147,7 @@ cleancov-clean:
 cleancov: cleancov-clean clean
 
 clean:
-	rm -f *.gcda *.gcno *.gcov *.bat *.o *.a *.obj *.lib *.exe *.dll etclib/*.o demo/test.o demo/main.o demo/opponent.o test timing mpitest mtest/mtest mtest/mtest.exe \
+	rm -f *.gcda *.gcno *.gcov *.bat *.o *.a *.obj *.lib *.exe *.dll etclib/*.o demo/test.o demo/main.o demo/opponent.o test timing mpitest mtest/mtest mtest/mtest.exe tuning_list\
         *.idx *.toc *.log *.aux *.dvi *.lof *.ind *.ilg *.ps *.log *.s mpi.c *.da *.dyn *.dpi tommath.tex `find . -type f | grep [~] | xargs` *.lo *.la
 	rm -rf .libs/
 	${MAKE} -C etc/ clean MAKE=${MAKE}
diff --git a/testme.sh b/testme.sh
index 40ecb74..c8237df 100755
--- a/testme.sh
+++ b/testme.sh
@@ -118,11 +118,20 @@ _make()
 _runtest()
 {
   make clean > /dev/null
-  _make "$1" "$2" "test_standalone"
   local _timeout=""
   which timeout >/dev/null && _timeout="timeout --foreground 90"
-  echo -e "\rRun test $1 $2"
-  $_timeout ./test > test_${suffix}.log || _die "running tests" $?
+  if [[ "$MAKE_OPTIONS" =~ "tune" ]]
+  then
+    # "make tune" will run "tune_it.sh" automatically, hence "autotune", but it cannot
+    # get switched off without some effort, so we just let it run twice for testing purposes
+    _make "$1" "$2" ""
+    echo -e "\rRun autotune $1 $2"
+    $_timeout ./etc/tune_it.sh > test_${suffix}.log || _die "running autotune" $?
+  else
+    _make "$1" "$2" "test_standalone"
+    echo -e "\rRun test $1 $2"
+    $_timeout ./test > test_${suffix}.log || _die "running tests" $?
+  fi
 }
 
 # This is not much more of a C&P of _runtest with a different timeout
@@ -131,13 +140,24 @@ _runtest()
 _runvalgrind()
 {
   make clean > /dev/null
-  _make "$1" "$2" "test_standalone"
   local _timeout=""
   # 30 minutes? Yes. Had it at 20 minutes and the Valgrind run needed over 25 minutes.
   # A bit too close for comfort.
   which timeout >/dev/null && _timeout="timeout --foreground 1800"
-  echo -e "\rRun test $1 $2 inside valgrind"
-  $_timeout $VALGRIND_BIN $VALGRIND_OPTS ./test > test_${suffix}.log || _die "running tests" $?
+echo "MAKE_OPTIONS = \"$MAKE_OPTIONS\""
+  if [[ "$MAKE_OPTIONS" =~ "tune"  ]]
+  then
+echo "autotune branch"
+    _make "$1" "$2" ""
+    # The shell used for /bin/sh is DASH 0.5.7-4ubuntu1 on the author's machine which fails valgrind, so
+    # we just run on instance of etc/tune with the same options as in etc/tune_it.sh
+    echo -e "\rRun etc/tune $1 $2 once inside valgrind"
+    $_timeout $VALGRIND_BIN $VALGRIND_OPTS ./etc/tune -t -r 10 -L 3 > test_${suffix}.log || _die "running etc/tune" $?
+  else
+    _make "$1" "$2" "test_standalone"
+    echo -e "\rRun test $1 $2 inside valgrind"
+    $_timeout $VALGRIND_BIN $VALGRIND_OPTS ./test > test_${suffix}.log || _die "running tests" $?
+  fi
 }