tommath/etc/tune.c

146 lines
2.9 KiB
C
Raw Normal View History

2003-02-28 11:08:34 -05:00
/* Tune the Karatsuba parameters
*
2006-04-06 15:49:59 -04:00
* Tom St Denis, tomstdenis@gmail.com
2003-02-28 11:08:34 -05:00
*/
#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
2014-09-01 20:14:38 -04:00
* this can be low like 5 or 10. For fast [re: Athlon] should be 25 - 50 or so
2003-06-06 15:35:48 -04:00
*/
2004-10-29 18:07:18 -04:00
#define TIMES (1UL<<14UL)
2003-06-06 15:35:48 -04:00
#ifndef X86_TIMER
2005-02-12 03:40:15 -05:00
/* RDTSC from Scott Duplichan */
static ulong64 TIMFUNC (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 ((ulong64)lo)|( ((ulong64)hi)<<32);
2005-02-12 03:40:15 -05:00
#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
}
2003-06-06 15:35:48 -04:00
2003-05-17 08:33:54 -04:00
/* generic ISO C timer */
2004-12-22 21:40:37 -05:00
ulong64 LBL_T;
2005-02-12 03:40:15 -05:00
void t_start(void) { LBL_T = TIMFUNC(); }
ulong64 t_read(void) { return TIMFUNC() - LBL_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);
2014-09-01 20:14:38 -04:00
if (s == 1) {
2004-10-29 18:07:18 -04:00
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);
2014-09-01 20:14:38 -04:00
if (s == 1) {
2004-10-29 18:07:18 -04:00
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;
2014-09-01 20:14:38 -04:00
for (x = 8; ; x += 2) {
2004-10-29 18:07:18 -04:00
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
2014-09-01 20:14:38 -04:00
for (x = 8; ; x += 2) {
2004-10-29 18:07:18 -04:00
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;
}
2005-08-01 12:37:28 -04:00
/* $Source$ */
/* $Revision$ */
/* $Date$ */