2003-02-28 11:08:34 -05:00
|
|
|
/* Tune the Karatsuba parameters
|
|
|
|
*
|
2016-02-03 13:07:27 -05:00
|
|
|
* Tom St Denis, tstdenis82@gmail.com
|
2003-02-28 11:08:34 -05:00
|
|
|
*/
|
|
|
|
#include <tommath.h>
|
|
|
|
#include <time.h>
|
2016-02-03 13:07:27 -05:00
|
|
|
#include <stdint.h>
|
2003-02-28 11:08:34 -05:00
|
|
|
|
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
|
|
|
|
2014-09-28 07:12:10 -04:00
|
|
|
#ifndef X86_TIMER
|
|
|
|
|
2005-02-12 03:40:15 -05:00
|
|
|
/* RDTSC from Scott Duplichan */
|
2017-10-18 04:44:06 -04: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 03:40:15 -05:00
|
|
|
|
|
|
|
// Microsoft and Intel Windows compilers
|
2017-10-18 04:44:06 -04: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 03:40:15 -05:00
|
|
|
|
2003-06-06 15:35:48 -04:00
|
|
|
|
2017-10-18 04:44:06 -04:00
|
|
|
/* *INDENT-OFF* */
|
2003-05-17 08:33:54 -04:00
|
|
|
/* generic ISO C timer */
|
2016-02-03 13:07:27 -05:00
|
|
|
uint64_t LBL_T;
|
2005-02-12 03:40:15 -05:00
|
|
|
void t_start(void) { LBL_T = TIMFUNC(); }
|
2016-02-03 13:07:27 -05:00
|
|
|
uint64_t t_read(void) { return TIMFUNC() - LBL_T; }
|
2017-10-18 04:44:06 -04:00
|
|
|
/* *INDENT-ON* */
|
2003-05-17 08:33:54 -04:00
|
|
|
|
|
|
|
#else
|
|
|
|
extern void t_start(void);
|
2016-02-03 13:07:27 -05:00
|
|
|
extern uint64_t t_read(void);
|
2003-05-17 08:33:54 -04:00
|
|
|
#endif
|
|
|
|
|
2016-02-03 13:07:27 -05:00
|
|
|
uint64_t time_mult(int size, int s)
|
2003-02-28 11:08:34 -05:00
|
|
|
{
|
2017-10-18 04:44:06 -04:00
|
|
|
unsigned long x;
|
|
|
|
mp_int a, b, c;
|
|
|
|
uint64_t t1;
|
2003-02-28 11:08:34 -05:00
|
|
|
|
2017-10-18 04:44:06 -04:00
|
|
|
mp_init(&a);
|
|
|
|
mp_init(&b);
|
|
|
|
mp_init(&c);
|
2003-02-28 11:08:34 -05:00
|
|
|
|
2017-10-18 04:44:06 -04:00
|
|
|
mp_rand(&a, size);
|
|
|
|
mp_rand(&b, size);
|
2004-10-29 18:07:18 -04:00
|
|
|
|
2017-10-18 04:44:06 -04:00
|
|
|
if (s == 1) {
|
2004-10-29 18:07:18 -04:00
|
|
|
KARATSUBA_MUL_CUTOFF = size;
|
2017-10-18 04:44:06 -04:00
|
|
|
} else {
|
2004-10-29 18:07:18 -04:00
|
|
|
KARATSUBA_MUL_CUTOFF = 100000;
|
2017-10-18 04:44:06 -04:00
|
|
|
}
|
2004-10-29 18:07:18 -04:00
|
|
|
|
2017-10-18 04:44:06 -04:00
|
|
|
t_start();
|
|
|
|
for (x = 0; x < TIMES; x++) {
|
2004-10-29 18:07:18 -04:00
|
|
|
mp_mul(&a,&b,&c);
|
2017-10-18 04:44:06 -04:00
|
|
|
}
|
|
|
|
t1 = t_read();
|
|
|
|
mp_clear(&a);
|
|
|
|
mp_clear(&b);
|
|
|
|
mp_clear(&c);
|
|
|
|
return t1;
|
2003-02-28 11:08:34 -05:00
|
|
|
}
|
|
|
|
|
2016-02-03 13:07:27 -05:00
|
|
|
uint64_t time_sqr(int size, int s)
|
2003-02-28 11:08:34 -05:00
|
|
|
{
|
2017-10-18 04:44:06 -04:00
|
|
|
unsigned long x;
|
|
|
|
mp_int a, b;
|
|
|
|
uint64_t t1;
|
2003-02-28 11:08:34 -05:00
|
|
|
|
2017-10-18 04:44:06 -04:00
|
|
|
mp_init(&a);
|
|
|
|
mp_init(&b);
|
2003-02-28 11:08:34 -05:00
|
|
|
|
2017-10-18 04:44:06 -04:00
|
|
|
mp_rand(&a, size);
|
2004-10-29 18:07:18 -04:00
|
|
|
|
2017-10-18 04:44:06 -04:00
|
|
|
if (s == 1) {
|
2004-10-29 18:07:18 -04:00
|
|
|
KARATSUBA_SQR_CUTOFF = size;
|
2017-10-18 04:44:06 -04:00
|
|
|
} else {
|
2004-10-29 18:07:18 -04:00
|
|
|
KARATSUBA_SQR_CUTOFF = 100000;
|
2017-10-18 04:44:06 -04:00
|
|
|
}
|
2004-10-29 18:07:18 -04:00
|
|
|
|
2017-10-18 04:44:06 -04:00
|
|
|
t_start();
|
|
|
|
for (x = 0; x < TIMES; x++) {
|
2004-10-29 18:07:18 -04:00
|
|
|
mp_sqr(&a,&b);
|
2017-10-18 04:44:06 -04:00
|
|
|
}
|
|
|
|
t1 = t_read();
|
|
|
|
mp_clear(&a);
|
|
|
|
mp_clear(&b);
|
|
|
|
return t1;
|
2003-03-12 21:11:11 -05:00
|
|
|
}
|
|
|
|
|
2017-10-18 04:44:06 -04:00
|
|
|
int main(void)
|
2003-02-28 11:08:34 -05:00
|
|
|
{
|
2017-10-18 04:44:06 -04: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 11:08:34 -05:00
|
|
|
}
|
2005-08-01 12:37:28 -04:00
|
|
|
|
2017-08-28 10:27:26 -04:00
|
|
|
/* ref: $Format:%D$ */
|
|
|
|
/* git commit: $Format:%H$ */
|
|
|
|
/* commit time: $Format:%ai$ */
|