tommath/etc/tune.c

108 lines
1.8 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-06-06 15:35:48 -04:00
/* 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
*/
2004-10-29 18:07:18 -04:00
#define TIMES (1UL<<14UL)
2003-06-06 15:35:48 -04:00
2003-05-17 08:33:54 -04:00
#ifndef X86_TIMER
/* generic ISO C timer */
2003-05-29 09:35:26 -04:00
ulong64 __T;
2003-05-17 08:33:54 -04:00
void t_start(void) { __T = clock(); }
2003-05-29 09:35:26 -04:00
ulong64 t_read(void) { return clock() - __T; }
2003-05-17 08:33:54 -04:00
#else
extern void t_start(void);
2003-05-29 09:35:26 -04:00
extern ulong64 t_read(void);
2003-05-17 08:33:54 -04:00
#endif
2004-10-29 18:07:18 -04:00
ulong64 time_mult(int size, int s)
2003-02-28 11:08:34 -05:00
{
2004-10-29 18:07:18 -04:00
unsigned long x;
2003-03-12 21:11:11 -05:00
mp_int a, b, c;
2004-10-29 18:07:18 -04:00
ulong64 t1;
2003-02-28 11:08:34 -05:00
mp_init (&a);
mp_init (&b);
mp_init (&c);
2004-10-29 18:07:18 -04:00
mp_rand (&a, size);
mp_rand (&b, size);
if (s == 1) {
KARATSUBA_MUL_CUTOFF = size;
} else {
KARATSUBA_MUL_CUTOFF = 100000;
}
2003-05-17 08:33:54 -04:00
t_start();
2004-10-29 18:07:18 -04:00
for (x = 0; x < TIMES; x++) {
mp_mul(&a,&b,&c);
2003-02-28 11:08:34 -05:00
}
2004-10-29 18:07:18 -04:00
t1 = t_read();
2003-02-28 11:08:34 -05:00
mp_clear (&a);
mp_clear (&b);
mp_clear (&c);
2004-10-29 18:07:18 -04:00
return t1;
2003-02-28 11:08:34 -05:00
}
2004-10-29 18:07:18 -04:00
ulong64 time_sqr(int size, int s)
2003-02-28 11:08:34 -05:00
{
2004-10-29 18:07:18 -04:00
unsigned long x;
2003-03-12 21:11:11 -05:00
mp_int a, b;
2004-10-29 18:07:18 -04:00
ulong64 t1;
2003-02-28 11:08:34 -05:00
mp_init (&a);
mp_init (&b);
2004-10-29 18:07:18 -04:00
mp_rand (&a, size);
if (s == 1) {
KARATSUBA_SQR_CUTOFF = size;
} else {
KARATSUBA_SQR_CUTOFF = 100000;
}
2003-05-17 08:33:54 -04:00
t_start();
2004-10-29 18:07:18 -04:00
for (x = 0; x < TIMES; x++) {
mp_sqr(&a,&b);
2003-02-28 11:08:34 -05:00
}
2004-10-29 18:07:18 -04:00
t1 = t_read();
2003-02-28 11:08:34 -05:00
mp_clear (&a);
mp_clear (&b);
2004-10-29 18:07:18 -04:00
return t1;
2003-03-12 21:11:11 -05:00
}
2003-02-28 11:08:34 -05:00
int
main (void)
{
2004-10-29 18:07:18 -04:00
ulong64 t1, t2;
int x, y;
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;
2003-05-29 09:35:26 -04:00
}
2004-10-29 18:07:18 -04:00
y = x;
2003-03-12 21:11:11 -05:00
2004-10-29 18:07:18 -04:00
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;
}
printf("KARATSUBA_MUL_CUTOFF = %d\n", y);
printf("KARATSUBA_SQR_CUTOFF = %d\n", x);
2003-02-28 11:08:34 -05:00
return 0;
}