Mercurial > dropbear
annotate etc/tune.c @ 200:c5c969ed76f3 libtommath
propagate from branch 'au.asn.ucc.matt.ltm-orig' (head 7fa10cba9535de3461cedb14b877c24858826204)
to branch 'au.asn.ucc.matt.dropbear.ltm' (head fc26f60de0370ab0a281fa41a2d13fb17c9d90a8)
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Wed, 11 May 2005 16:15:27 +0000 |
parents | d8254fc979e9 |
children |
rev | line source |
---|---|
2 | 1 /* Tune the Karatsuba parameters |
2 * | |
3 * Tom St Denis, [email protected] | |
4 */ | |
5 #include <tommath.h> | |
6 #include <time.h> | |
7 | |
8 /* how many times todo each size mult. Depends on your computer. For slow computers | |
9 * this can be low like 5 or 10. For fast [re: Athlon] should be 25 - 50 or so | |
10 */ | |
142 | 11 #define TIMES (1UL<<14UL) |
2 | 12 |
190
d8254fc979e9
Initial import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
142
diff
changeset
|
13 /* RDTSC from Scott Duplichan */ |
d8254fc979e9
Initial import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
142
diff
changeset
|
14 static ulong64 TIMFUNC (void) |
d8254fc979e9
Initial import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
142
diff
changeset
|
15 { |
d8254fc979e9
Initial import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
142
diff
changeset
|
16 #if defined __GNUC__ |
d8254fc979e9
Initial import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
142
diff
changeset
|
17 #if defined(__i386__) || defined(__x86_64__) |
d8254fc979e9
Initial import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
142
diff
changeset
|
18 unsigned long long a; |
d8254fc979e9
Initial import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
142
diff
changeset
|
19 __asm__ __volatile__ ("rdtsc\nmovl %%eax,%0\nmovl %%edx,4+%0\n"::"m"(a):"%eax","%edx"); |
d8254fc979e9
Initial import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
142
diff
changeset
|
20 return a; |
d8254fc979e9
Initial import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
142
diff
changeset
|
21 #else /* gcc-IA64 version */ |
d8254fc979e9
Initial import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
142
diff
changeset
|
22 unsigned long result; |
d8254fc979e9
Initial import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
142
diff
changeset
|
23 __asm__ __volatile__("mov %0=ar.itc" : "=r"(result) :: "memory"); |
d8254fc979e9
Initial import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
142
diff
changeset
|
24 while (__builtin_expect ((int) result == -1, 0)) |
d8254fc979e9
Initial import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
142
diff
changeset
|
25 __asm__ __volatile__("mov %0=ar.itc" : "=r"(result) :: "memory"); |
d8254fc979e9
Initial import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
142
diff
changeset
|
26 return result; |
d8254fc979e9
Initial import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
142
diff
changeset
|
27 #endif |
d8254fc979e9
Initial import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
142
diff
changeset
|
28 |
d8254fc979e9
Initial import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
142
diff
changeset
|
29 // Microsoft and Intel Windows compilers |
d8254fc979e9
Initial import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
142
diff
changeset
|
30 #elif defined _M_IX86 |
d8254fc979e9
Initial import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
142
diff
changeset
|
31 __asm rdtsc |
d8254fc979e9
Initial import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
142
diff
changeset
|
32 #elif defined _M_AMD64 |
d8254fc979e9
Initial import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
142
diff
changeset
|
33 return __rdtsc (); |
d8254fc979e9
Initial import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
142
diff
changeset
|
34 #elif defined _M_IA64 |
d8254fc979e9
Initial import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
142
diff
changeset
|
35 #if defined __INTEL_COMPILER |
d8254fc979e9
Initial import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
142
diff
changeset
|
36 #include <ia64intrin.h> |
d8254fc979e9
Initial import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
142
diff
changeset
|
37 #endif |
d8254fc979e9
Initial import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
142
diff
changeset
|
38 return __getReg (3116); |
d8254fc979e9
Initial import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
142
diff
changeset
|
39 #else |
d8254fc979e9
Initial import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
142
diff
changeset
|
40 #error need rdtsc function for this build |
d8254fc979e9
Initial import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
142
diff
changeset
|
41 #endif |
d8254fc979e9
Initial import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
142
diff
changeset
|
42 } |
d8254fc979e9
Initial import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
142
diff
changeset
|
43 |
2 | 44 |
45 #ifndef X86_TIMER | |
46 | |
47 /* generic ISO C timer */ | |
190
d8254fc979e9
Initial import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
142
diff
changeset
|
48 ulong64 LBL_T; |
d8254fc979e9
Initial import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
142
diff
changeset
|
49 void t_start(void) { LBL_T = TIMFUNC(); } |
d8254fc979e9
Initial import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
142
diff
changeset
|
50 ulong64 t_read(void) { return TIMFUNC() - LBL_T; } |
2 | 51 |
52 #else | |
53 extern void t_start(void); | |
54 extern ulong64 t_read(void); | |
55 #endif | |
56 | |
142 | 57 ulong64 time_mult(int size, int s) |
2 | 58 { |
142 | 59 unsigned long x; |
2 | 60 mp_int a, b, c; |
142 | 61 ulong64 t1; |
2 | 62 |
63 mp_init (&a); | |
64 mp_init (&b); | |
65 mp_init (&c); | |
66 | |
142 | 67 mp_rand (&a, size); |
68 mp_rand (&b, size); | |
69 | |
70 if (s == 1) { | |
71 KARATSUBA_MUL_CUTOFF = size; | |
72 } else { | |
73 KARATSUBA_MUL_CUTOFF = 100000; | |
74 } | |
75 | |
2 | 76 t_start(); |
142 | 77 for (x = 0; x < TIMES; x++) { |
78 mp_mul(&a,&b,&c); | |
2 | 79 } |
142 | 80 t1 = t_read(); |
2 | 81 mp_clear (&a); |
82 mp_clear (&b); | |
83 mp_clear (&c); | |
142 | 84 return t1; |
2 | 85 } |
86 | |
142 | 87 ulong64 time_sqr(int size, int s) |
2 | 88 { |
142 | 89 unsigned long x; |
2 | 90 mp_int a, b; |
142 | 91 ulong64 t1; |
2 | 92 |
93 mp_init (&a); | |
94 mp_init (&b); | |
95 | |
142 | 96 mp_rand (&a, size); |
97 | |
98 if (s == 1) { | |
99 KARATSUBA_SQR_CUTOFF = size; | |
100 } else { | |
101 KARATSUBA_SQR_CUTOFF = 100000; | |
102 } | |
103 | |
2 | 104 t_start(); |
142 | 105 for (x = 0; x < TIMES; x++) { |
106 mp_sqr(&a,&b); | |
2 | 107 } |
142 | 108 t1 = t_read(); |
2 | 109 mp_clear (&a); |
110 mp_clear (&b); | |
142 | 111 return t1; |
2 | 112 } |
113 | |
114 int | |
115 main (void) | |
116 { | |
142 | 117 ulong64 t1, t2; |
118 int x, y; | |
2 | 119 |
142 | 120 for (x = 8; ; x += 2) { |
121 t1 = time_mult(x, 0); | |
122 t2 = time_mult(x, 1); | |
123 printf("%d: %9llu %9llu, %9llu\n", x, t1, t2, t2 - t1); | |
124 if (t2 < t1) break; | |
2 | 125 } |
142 | 126 y = x; |
127 | |
128 for (x = 8; ; x += 2) { | |
129 t1 = time_sqr(x, 0); | |
130 t2 = time_sqr(x, 1); | |
131 printf("%d: %9llu %9llu, %9llu\n", x, t1, t2, t2 - t1); | |
132 if (t2 < t1) break; | |
2 | 133 } |
142 | 134 printf("KARATSUBA_MUL_CUTOFF = %d\n", y); |
135 printf("KARATSUBA_SQR_CUTOFF = %d\n", x); | |
2 | 136 |
137 return 0; | |
138 } |