1 /* Tune the Karatsuba parameters
3 * Tom St Denis, tomstdenis@iahu.ca
8 /* how many times todo each size mult. Depends on your computer. For slow computers
9 * this can be low like 5 or 10. For fast [re: Athlon] should be 25 - 50 or so
16 /* generic ISO C timer */
18 void t_start(void) { __T = clock(); }
19 ulong64 t_read(void) { return clock() - __T; }
22 extern void t_start(void);
23 extern ulong64 t_read(void);
37 for (x = 32; x <= max; x += 4) {
40 for (y = 0; y < TIMES; y++) {
60 for (x = 32; x <= max; x += 4) {
62 for (y = 0; y < TIMES; y++) {
74 int best_kmult, best_tmult, best_ksquare, best_tsquare, counter;
78 best_kmult = best_ksquare = best_tmult = best_tsquare = 0;
79 /* tune multiplication first */
81 /* effectively turn TOOM off */
82 TOOM_SQR_CUTOFF = TOOM_MUL_CUTOFF = 100000;
84 log = fopen ("mult.log", "w");
87 for (KARATSUBA_MUL_CUTOFF = 8; KARATSUBA_MUL_CUTOFF <= 200; KARATSUBA_MUL_CUTOFF++) {
89 printf ("%4d : %9llu \r", KARATSUBA_MUL_CUTOFF, ti);
90 fprintf (log, "%d, %llu\n", KARATSUBA_MUL_CUTOFF, ti);
93 printf ("New best: %llu, %d \r", ti, KARATSUBA_MUL_CUTOFF);
95 best_kmult = KARATSUBA_MUL_CUTOFF;
97 } else if (--counter == 0) {
98 printf("No better found in 16 trials.\n");
103 printf("Karatsuba Multiplier Cutoff (KARATSUBA_MUL_CUTOFF) == %d\n", best_kmult);
106 log = fopen ("sqr.log", "w");
109 for (KARATSUBA_SQR_CUTOFF = 8; KARATSUBA_SQR_CUTOFF <= 200; KARATSUBA_SQR_CUTOFF++) {
111 printf ("%4d : %9llu \r", KARATSUBA_SQR_CUTOFF, ti);
112 fprintf (log, "%d, %llu\n", KARATSUBA_SQR_CUTOFF, ti);
115 printf ("New best: %llu, %d \r", ti, KARATSUBA_SQR_CUTOFF);
117 best_ksquare = KARATSUBA_SQR_CUTOFF;
119 } else if (--counter == 0) {
120 printf("No better found in 16 trials.\n");
125 printf("Karatsuba Squaring Cutoff (KARATSUBA_SQR_CUTOFF) == %d\n", best_ksquare);
127 KARATSUBA_MUL_CUTOFF = best_kmult;
128 KARATSUBA_SQR_CUTOFF = best_ksquare;
132 log = fopen ("tmult.log", "w");
134 for (TOOM_MUL_CUTOFF = best_kmult*5; TOOM_MUL_CUTOFF <= 800; TOOM_MUL_CUTOFF++) {
135 ti = time_mult (1200);
136 printf ("%4d : %9llu \r", TOOM_MUL_CUTOFF, ti);
137 fprintf (log, "%d, %llu\n", TOOM_MUL_CUTOFF, ti);
140 printf ("New best: %llu, %d \r", ti, TOOM_MUL_CUTOFF);
142 best_tmult = TOOM_MUL_CUTOFF;
144 } else if (--counter == 0) {
145 printf("No better found in 16 trials.\n");
150 printf("Toom-Cook Multiplier Cutoff (TOOM_MUL_CUTOFF) == %d\n", best_tmult);
153 log = fopen ("tsqr.log", "w");
156 for (TOOM_SQR_CUTOFF = best_ksquare*3; TOOM_SQR_CUTOFF <= 800; TOOM_SQR_CUTOFF++) {
157 ti = time_sqr (1200);
158 printf ("%4d : %9llu \r", TOOM_SQR_CUTOFF, ti);
159 fprintf (log, "%d, %llu\n", TOOM_SQR_CUTOFF, ti);
162 printf ("New best: %llu, %d \r", ti, TOOM_SQR_CUTOFF);
164 best_tsquare = TOOM_SQR_CUTOFF;
166 } else if (--counter == 0) {
167 printf("No better found in 16 trials.\n");
172 printf("Toom-Cook Squaring Cutoff (TOOM_SQR_CUTOFF) == %d\n", best_tsquare);