tommath/etc/tune.c

110 lines
2.2 KiB
C
Raw Normal View History

2003-02-28 11:08:34 -05:00
/* Tune the Karatsuba parameters
*
* Tom St Denis, tomstdenis@iahu.ca
*/
#include <tommath.h>
#include <time.h>
2003-05-17 08:33:54 -04:00
#ifndef X86_TIMER
/* generic ISO C timer */
unsigned long long __T;
void t_start(void) { __T = clock(); }
unsigned long long t_read(void) { return clock() - __T; }
#else
extern void t_start(void);
extern unsigned long long t_read(void);
#endif
unsigned long long
2003-02-28 11:08:34 -05:00
time_mult (void)
{
2003-03-12 21:11:11 -05:00
int x, y;
mp_int a, b, c;
2003-02-28 11:08:34 -05:00
mp_init (&a);
mp_init (&b);
mp_init (&c);
2003-05-17 08:33:54 -04:00
t_start();
for (x = 32; x <= 288; x += 4) {
2003-03-12 21:11:11 -05:00
mp_rand (&a, x);
mp_rand (&b, x);
2003-05-17 08:33:54 -04:00
for (y = 0; y < 100; y++) {
2003-02-28 11:08:34 -05:00
mp_mul (&a, &b, &c);
}
}
mp_clear (&a);
mp_clear (&b);
mp_clear (&c);
2003-05-17 08:33:54 -04:00
return t_read();
2003-02-28 11:08:34 -05:00
}
2003-05-17 08:33:54 -04:00
unsigned long long
2003-02-28 11:08:34 -05:00
time_sqr (void)
{
2003-03-12 21:11:11 -05:00
int x, y;
mp_int a, b;
2003-02-28 11:08:34 -05:00
mp_init (&a);
mp_init (&b);
2003-05-17 08:33:54 -04:00
t_start();
for (x = 32; x <= 288; x += 4) {
2003-03-12 21:11:11 -05:00
mp_rand (&a, x);
2003-05-17 08:33:54 -04:00
for (y = 0; y < 100; y++) {
2003-02-28 11:08:34 -05:00
mp_sqr (&a, &b);
}
}
mp_clear (&a);
mp_clear (&b);
2003-05-17 08:33:54 -04:00
return t_read();
2003-03-12 21:11:11 -05:00
}
2003-02-28 11:08:34 -05:00
int
main (void)
{
2003-05-17 08:33:54 -04:00
int best_mult, best_square;
unsigned long long best, ti;
2003-03-12 21:11:11 -05:00
FILE *log;
2003-02-28 11:08:34 -05:00
2003-05-17 08:33:54 -04:00
best_mult = best_square = 0;
2003-02-28 11:08:34 -05:00
/* tune multiplication first */
2003-03-12 21:11:11 -05:00
log = fopen ("mult.log", "w");
2003-05-17 08:33:54 -04:00
best = -1;
for (KARATSUBA_MUL_CUTOFF = 8; KARATSUBA_MUL_CUTOFF <= 200; KARATSUBA_MUL_CUTOFF++) {
2003-02-28 11:08:34 -05:00
ti = time_mult ();
2003-05-17 08:33:54 -04:00
printf ("%4d : %9llu\r", KARATSUBA_MUL_CUTOFF, ti);
fprintf (log, "%d, %llu\n", KARATSUBA_MUL_CUTOFF, ti);
2003-02-28 11:08:34 -05:00
fflush (stdout);
if (ti < best) {
2003-05-17 08:33:54 -04:00
printf ("New best: %llu, %d \n", ti, KARATSUBA_MUL_CUTOFF);
2003-02-28 11:08:34 -05:00
best = ti;
best_mult = KARATSUBA_MUL_CUTOFF;
}
}
2003-03-12 21:11:11 -05:00
fclose (log);
2003-02-28 11:08:34 -05:00
/* tune squaring */
2003-03-12 21:11:11 -05:00
log = fopen ("sqr.log", "w");
2003-05-17 08:33:54 -04:00
best = -1;
for (KARATSUBA_SQR_CUTOFF = 8; KARATSUBA_SQR_CUTOFF <= 200; KARATSUBA_SQR_CUTOFF++) {
2003-02-28 11:08:34 -05:00
ti = time_sqr ();
2003-05-17 08:33:54 -04:00
printf ("%4d : %9llu\r", KARATSUBA_SQR_CUTOFF, ti);
fprintf (log, "%d, %llu\n", KARATSUBA_SQR_CUTOFF, ti);
2003-02-28 11:08:34 -05:00
fflush (stdout);
if (ti < best) {
2003-05-17 08:33:54 -04:00
printf ("New best: %llu, %d \n", ti, KARATSUBA_SQR_CUTOFF);
2003-02-28 11:08:34 -05:00
best = ti;
best_square = KARATSUBA_SQR_CUTOFF;
}
}
2003-03-12 21:11:11 -05:00
fclose (log);
2003-02-28 11:08:34 -05:00
printf
2003-05-17 08:33:54 -04:00
("\n\n\nKaratsuba Multiplier Cutoff: %d\nKaratsuba Squaring Cutoff: %d\n",
best_mult, best_square);
2003-02-28 11:08:34 -05:00
return 0;
}