Tag
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
/* Tune the Karatsuba parameters
*
* Tom St Denis, tomstdenis@iahu.ca
*/
#include <tommath.h>
#include <time.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 50
#ifndef X86_TIMER
/* generic ISO C timer */
ulong64 __T;
void t_start(void) { __T = clock(); }
ulong64 t_read(void) { return clock() - __T; }
#else
extern void t_start(void);
extern ulong64 t_read(void);
#endif
ulong64
time_mult (int max)
{
int x, y;
mp_int a, b, c;
mp_init (&a);
mp_init (&b);
mp_init (&c);
t_start();
for (x = 32; x <= max; x += 4) {
mp_rand (&a, x);
mp_rand (&b, x);
for (y = 0; y < TIMES; y++) {
mp_mul (&a, &b, &c);
}
}
mp_clear (&a);
mp_clear (&b);
mp_clear (&c);
return t_read();
}
ulong64
time_sqr (int max)
{
int x, y;
mp_int a, b;
mp_init (&a);
mp_init (&b);
t_start();
for (x = 32; x <= max; x += 4) {
mp_rand (&a, x);
for (y = 0; y < TIMES; y++) {
mp_sqr (&a, &b);
}
}
mp_clear (&a);
mp_clear (&b);
return t_read();
}
int
main (void)
{
int best_kmult, best_tmult, best_ksquare, best_tsquare, counter;
ulong64 best, ti;
FILE *log;
best_kmult = best_ksquare = best_tmult = best_tsquare = 0;
/* tune multiplication first */
/* effectively turn TOOM off */
TOOM_SQR_CUTOFF = TOOM_MUL_CUTOFF = 100000;
log = fopen ("mult.log", "w");
best = -1;
counter = 16;
for (KARATSUBA_MUL_CUTOFF = 8; KARATSUBA_MUL_CUTOFF <= 200; KARATSUBA_MUL_CUTOFF++) {
ti = time_mult (300);
printf ("%4d : %9llu \r", KARATSUBA_MUL_CUTOFF, ti);
fprintf (log, "%d, %llu\n", KARATSUBA_MUL_CUTOFF, ti);
fflush (stdout);
if (ti < best) {
printf ("New best: %llu, %d \r", ti, KARATSUBA_MUL_CUTOFF);
best = ti;
best_kmult = KARATSUBA_MUL_CUTOFF;
counter = 16;
} else if (--counter == 0) {
printf("No better found in 16 trials.\n");
break;
}
}
fclose (log);
printf("Karatsuba Multiplier Cutoff (KARATSUBA_MUL_CUTOFF) == %d\n", best_kmult);
/* tune squaring */
log = fopen ("sqr.log", "w");
best = -1;
counter = 16;
for (KARATSUBA_SQR_CUTOFF = 8; KARATSUBA_SQR_CUTOFF <= 200; KARATSUBA_SQR_CUTOFF++) {
ti = time_sqr (300);
printf ("%4d : %9llu \r", KARATSUBA_SQR_CUTOFF, ti);
fprintf (log, "%d, %llu\n", KARATSUBA_SQR_CUTOFF, ti);
fflush (stdout);
if (ti < best) {
printf ("New best: %llu, %d \r", ti, KARATSUBA_SQR_CUTOFF);
best = ti;
best_ksquare = KARATSUBA_SQR_CUTOFF;
counter = 16;
} else if (--counter == 0) {
printf("No better found in 16 trials.\n");
break;
}
}
fclose (log);
printf("Karatsuba Squaring Cutoff (KARATSUBA_SQR_CUTOFF) == %d\n", best_ksquare);
KARATSUBA_MUL_CUTOFF = best_kmult;
KARATSUBA_SQR_CUTOFF = best_ksquare;
/* tune TOOM mult */
counter = 16;
log = fopen ("tmult.log", "w");
best = -1;
for (TOOM_MUL_CUTOFF = best_kmult*5; TOOM_MUL_CUTOFF <= 800; TOOM_MUL_CUTOFF++) {
ti = time_mult (1200);
printf ("%4d : %9llu \r", TOOM_MUL_CUTOFF, ti);
fprintf (log, "%d, %llu\n", TOOM_MUL_CUTOFF, ti);
fflush (stdout);
if (ti < best) {
printf ("New best: %llu, %d \r", ti, TOOM_MUL_CUTOFF);
best = ti;
best_tmult = TOOM_MUL_CUTOFF;
counter = 16;
} else if (--counter == 0) {
printf("No better found in 16 trials.\n");
break;
}
}
fclose (log);
printf("Toom-Cook Multiplier Cutoff (TOOM_MUL_CUTOFF) == %d\n", best_tmult);
/* tune TOOM sqr */
log = fopen ("tsqr.log", "w");
best = -1;
counter = 16;
for (TOOM_SQR_CUTOFF = best_ksquare*3; TOOM_SQR_CUTOFF <= 800; TOOM_SQR_CUTOFF++) {
ti = time_sqr (1200);
printf ("%4d : %9llu \r", TOOM_SQR_CUTOFF, ti);
fprintf (log, "%d, %llu\n", TOOM_SQR_CUTOFF, ti);
fflush (stdout);
if (ti < best) {
printf ("New best: %llu, %d \r", ti, TOOM_SQR_CUTOFF);
best = ti;
best_tsquare = TOOM_SQR_CUTOFF;
counter = 16;
} else if (--counter == 0) {
printf("No better found in 16 trials.\n");
break;
}
}
fclose (log);
printf("Toom-Cook Squaring Cutoff (TOOM_SQR_CUTOFF) == %d\n", best_tsquare);
return 0;
}