1
0
mirror of http://galexander.org/git/simplesshd.git synced 2024-12-11 16:48:09 +00:00
simplesshd/dropbear/libtommath/etc/tune.c

147 lines
3.0 KiB
C
Raw Normal View History

2014-12-10 21:56:49 +00:00
/* Tune the Karatsuba parameters
*
2019-06-09 20:44:26 +00:00
* Tom St Denis, tstdenis82@gmail.com
2014-12-10 21:56:49 +00:00
*/
#include <tommath.h>
#include <time.h>
2019-06-09 20:44:26 +00:00
#include <stdint.h>
2014-12-10 21:56:49 +00:00
/* how many times todo each size mult. Depends on your computer. For slow computers
2019-06-09 20:44:26 +00:00
* this can be low like 5 or 10. For fast [re: Athlon] should be 25 - 50 or so
2014-12-10 21:56:49 +00:00
*/
#define TIMES (1UL<<14UL)
2019-06-09 20:44:26 +00:00
#ifndef X86_TIMER
2014-12-10 21:56:49 +00:00
/* RDTSC from Scott Duplichan */
2019-06-09 20:44:26 +00:00
static uint64_t TIMFUNC (void)
2014-12-10 21:56:49 +00:00
{
#if defined __GNUC__
#if defined(__i386__) || defined(__x86_64__)
2019-06-09 20:44:26 +00:00
/* 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);
2014-12-10 21:56:49 +00: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
}
/* generic ISO C timer */
2019-06-09 20:44:26 +00:00
uint64_t LBL_T;
2014-12-10 21:56:49 +00:00
void t_start(void) { LBL_T = TIMFUNC(); }
2019-06-09 20:44:26 +00:00
uint64_t t_read(void) { return TIMFUNC() - LBL_T; }
2014-12-10 21:56:49 +00:00
#else
extern void t_start(void);
2019-06-09 20:44:26 +00:00
extern uint64_t t_read(void);
2014-12-10 21:56:49 +00:00
#endif
2019-06-09 20:44:26 +00:00
uint64_t time_mult(int size, int s)
2014-12-10 21:56:49 +00:00
{
unsigned long x;
mp_int a, b, c;
2019-06-09 20:44:26 +00:00
uint64_t t1;
2014-12-10 21:56:49 +00:00
mp_init (&a);
mp_init (&b);
mp_init (&c);
mp_rand (&a, size);
mp_rand (&b, size);
2019-06-09 20:44:26 +00:00
if (s == 1) {
2014-12-10 21:56:49 +00:00
KARATSUBA_MUL_CUTOFF = size;
} else {
KARATSUBA_MUL_CUTOFF = 100000;
}
t_start();
for (x = 0; x < TIMES; x++) {
mp_mul(&a,&b,&c);
}
t1 = t_read();
mp_clear (&a);
mp_clear (&b);
mp_clear (&c);
return t1;
}
2019-06-09 20:44:26 +00:00
uint64_t time_sqr(int size, int s)
2014-12-10 21:56:49 +00:00
{
unsigned long x;
mp_int a, b;
2019-06-09 20:44:26 +00:00
uint64_t t1;
2014-12-10 21:56:49 +00:00
mp_init (&a);
mp_init (&b);
mp_rand (&a, size);
2019-06-09 20:44:26 +00:00
if (s == 1) {
2014-12-10 21:56:49 +00:00
KARATSUBA_SQR_CUTOFF = size;
} else {
KARATSUBA_SQR_CUTOFF = 100000;
}
t_start();
for (x = 0; x < TIMES; x++) {
mp_sqr(&a,&b);
}
t1 = t_read();
mp_clear (&a);
mp_clear (&b);
return t1;
}
int
main (void)
{
2019-06-09 20:44:26 +00:00
uint64_t t1, t2;
2014-12-10 21:56:49 +00:00
int x, y;
2019-06-09 20:44:26 +00:00
for (x = 8; ; x += 2) {
2014-12-10 21:56:49 +00: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;
}
y = x;
2019-06-09 20:44:26 +00:00
for (x = 8; ; x += 2) {
2014-12-10 21:56:49 +00: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);
return 0;
}
2019-06-09 20:44:26 +00:00
/* ref: $Format:%D$ */
/* git commit: $Format:%H$ */
/* commit time: $Format:%ai$ */