libtommath/etc/tune.c

143 lines
3.0 KiB
C
Raw Normal View History

2003-02-28 16:08:34 +00:00
/* Tune the Karatsuba parameters
*
* Tom St Denis, tstdenis82@gmail.com
2003-02-28 16:08:34 +00:00
*/
#include <tommath.h>
#include <stdint.h>
2003-02-28 16:08:34 +00:00
2003-06-06 19:35:48 +00:00
/* how many times todo each size mult. Depends on your computer. For slow computers
2014-09-02 00:14:38 +00:00
* this can be low like 5 or 10. For fast [re: Athlon] should be 25 - 50 or so
2003-06-06 19:35:48 +00:00
*/
2004-10-29 22:07:18 +00:00
#define TIMES (1UL<<14UL)
2003-06-06 19:35:48 +00:00
#ifndef X86_TIMER
2005-02-12 08:40:15 +00:00
/* RDTSC from Scott Duplichan */
2017-10-18 08:44:06 +00:00
static uint64_t 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 ((uint64_t)lo)|(((uint64_t)hi)<<32);
# 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
2005-02-12 08:40:15 +00:00
2018-12-02 12:12:30 +00:00
/* Microsoft and Intel Windows compilers */
2017-10-18 08:44:06 +00:00
# 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
}
2005-02-12 08:40:15 +00:00
2003-06-06 19:35:48 +00:00
2017-10-18 08:44:06 +00:00
/* *INDENT-OFF* */
2003-05-17 12:33:54 +00:00
/* generic ISO C timer */
2018-02-01 21:28:43 +00:00
static uint64_t LBL_T;
static void t_start(void) { LBL_T = TIMFUNC(); }
static uint64_t t_read(void) { return TIMFUNC() - LBL_T; }
2017-10-18 08:44:06 +00:00
/* *INDENT-ON* */
2003-05-17 12:33:54 +00:00
#else
extern void t_start(void);
extern uint64_t t_read(void);
2003-05-17 12:33:54 +00:00
#endif
2018-02-01 21:28:43 +00:00
static uint64_t time_mult(int size, int s)
2003-02-28 16:08:34 +00:00
{
2017-10-18 08:44:06 +00:00
unsigned long x;
mp_int a, b, c;
uint64_t t1;
2003-02-28 16:08:34 +00:00
2017-10-18 08:44:06 +00:00
mp_init(&a);
mp_init(&b);
mp_init(&c);
2003-02-28 16:08:34 +00:00
2017-10-18 08:44:06 +00:00
mp_rand(&a, size);
mp_rand(&b, size);
2004-10-29 22:07:18 +00:00
2017-10-18 08:44:06 +00:00
if (s == 1) {
2004-10-29 22:07:18 +00:00
KARATSUBA_MUL_CUTOFF = size;
2017-10-18 08:44:06 +00:00
} else {
2004-10-29 22:07:18 +00:00
KARATSUBA_MUL_CUTOFF = 100000;
2017-10-18 08:44:06 +00:00
}
2004-10-29 22:07:18 +00:00
2017-10-18 08:44:06 +00:00
t_start();
for (x = 0; x < TIMES; x++) {
2004-10-29 22:07:18 +00:00
mp_mul(&a,&b,&c);
2017-10-18 08:44:06 +00:00
}
t1 = t_read();
mp_clear(&a);
mp_clear(&b);
mp_clear(&c);
return t1;
2003-02-28 16:08:34 +00:00
}
2018-02-01 21:28:43 +00:00
static uint64_t time_sqr(int size, int s)
2003-02-28 16:08:34 +00:00
{
2017-10-18 08:44:06 +00:00
unsigned long x;
mp_int a, b;
uint64_t t1;
2003-02-28 16:08:34 +00:00
2017-10-18 08:44:06 +00:00
mp_init(&a);
mp_init(&b);
2003-02-28 16:08:34 +00:00
2017-10-18 08:44:06 +00:00
mp_rand(&a, size);
2004-10-29 22:07:18 +00:00
2017-10-18 08:44:06 +00:00
if (s == 1) {
2004-10-29 22:07:18 +00:00
KARATSUBA_SQR_CUTOFF = size;
2017-10-18 08:44:06 +00:00
} else {
2004-10-29 22:07:18 +00:00
KARATSUBA_SQR_CUTOFF = 100000;
2017-10-18 08:44:06 +00:00
}
2004-10-29 22:07:18 +00:00
2017-10-18 08:44:06 +00:00
t_start();
for (x = 0; x < TIMES; x++) {
2004-10-29 22:07:18 +00:00
mp_sqr(&a,&b);
2017-10-18 08:44:06 +00:00
}
t1 = t_read();
mp_clear(&a);
mp_clear(&b);
return t1;
2003-03-13 02:11:11 +00:00
}
2017-10-18 08:44:06 +00:00
int main(void)
2003-02-28 16:08:34 +00:00
{
2017-10-18 08:44:06 +00:00
uint64_t 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;
}
y = x;
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);
return 0;
2003-02-28 16:08:34 +00:00
}