comparison libtommath/etc/tune.c @ 1436:60fc6476e044

Update to libtommath v1.0
author Matt Johnston <matt@ucc.asn.au>
date Sat, 24 Jun 2017 22:37:14 +0800
parents 5ff8218bcee9
children 8bba51a55704
comparison
equal deleted inserted replaced
1435:f849a5ca2efc 1436:60fc6476e044
1 /* Tune the Karatsuba parameters 1 /* Tune the Karatsuba parameters
2 * 2 *
3 * Tom St Denis, [email protected] 3 * Tom St Denis, [email protected]
4 */ 4 */
5 #include <tommath.h> 5 #include <tommath.h>
6 #include <time.h> 6 #include <time.h>
7 #include <stdint.h>
7 8
8 /* how many times todo each size mult. Depends on your computer. For slow computers 9 /* 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 * this can be low like 5 or 10. For fast [re: Athlon] should be 25 - 50 or so
10 */ 11 */
11 #define TIMES (1UL<<14UL) 12 #define TIMES (1UL<<14UL)
12 13
14 #ifndef X86_TIMER
15
13 /* RDTSC from Scott Duplichan */ 16 /* RDTSC from Scott Duplichan */
14 static ulong64 TIMFUNC (void) 17 static uint64_t TIMFUNC (void)
15 { 18 {
16 #if defined __GNUC__ 19 #if defined __GNUC__
17 #if defined(__i386__) || defined(__x86_64__) 20 #if defined(__i386__) || defined(__x86_64__)
18 unsigned long long a; 21 /* version from http://www.mcs.anl.gov/~kazutomo/rdtsc.html
19 __asm__ __volatile__ ("rdtsc\nmovl %%eax,%0\nmovl %%edx,4+%0\n"::"m"(a):"%eax","%edx"); 22 * the old code always got a warning issued by gcc, clang did not complain...
20 return a; 23 */
24 unsigned hi, lo;
25 __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
26 return ((uint64_t)lo)|( ((uint64_t)hi)<<32);
21 #else /* gcc-IA64 version */ 27 #else /* gcc-IA64 version */
22 unsigned long result; 28 unsigned long result;
23 __asm__ __volatile__("mov %0=ar.itc" : "=r"(result) :: "memory"); 29 __asm__ __volatile__("mov %0=ar.itc" : "=r"(result) :: "memory");
24 while (__builtin_expect ((int) result == -1, 0)) 30 while (__builtin_expect ((int) result == -1, 0))
25 __asm__ __volatile__("mov %0=ar.itc" : "=r"(result) :: "memory"); 31 __asm__ __volatile__("mov %0=ar.itc" : "=r"(result) :: "memory");
40 #error need rdtsc function for this build 46 #error need rdtsc function for this build
41 #endif 47 #endif
42 } 48 }
43 49
44 50
45 #ifndef X86_TIMER
46
47 /* generic ISO C timer */ 51 /* generic ISO C timer */
48 ulong64 LBL_T; 52 uint64_t LBL_T;
49 void t_start(void) { LBL_T = TIMFUNC(); } 53 void t_start(void) { LBL_T = TIMFUNC(); }
50 ulong64 t_read(void) { return TIMFUNC() - LBL_T; } 54 uint64_t t_read(void) { return TIMFUNC() - LBL_T; }
51 55
52 #else 56 #else
53 extern void t_start(void); 57 extern void t_start(void);
54 extern ulong64 t_read(void); 58 extern uint64_t t_read(void);
55 #endif 59 #endif
56 60
57 ulong64 time_mult(int size, int s) 61 uint64_t time_mult(int size, int s)
58 { 62 {
59 unsigned long x; 63 unsigned long x;
60 mp_int a, b, c; 64 mp_int a, b, c;
61 ulong64 t1; 65 uint64_t t1;
62 66
63 mp_init (&a); 67 mp_init (&a);
64 mp_init (&b); 68 mp_init (&b);
65 mp_init (&c); 69 mp_init (&c);
66 70
67 mp_rand (&a, size); 71 mp_rand (&a, size);
68 mp_rand (&b, size); 72 mp_rand (&b, size);
69 73
70 if (s == 1) { 74 if (s == 1) {
71 KARATSUBA_MUL_CUTOFF = size; 75 KARATSUBA_MUL_CUTOFF = size;
72 } else { 76 } else {
73 KARATSUBA_MUL_CUTOFF = 100000; 77 KARATSUBA_MUL_CUTOFF = 100000;
74 } 78 }
75 79
82 mp_clear (&b); 86 mp_clear (&b);
83 mp_clear (&c); 87 mp_clear (&c);
84 return t1; 88 return t1;
85 } 89 }
86 90
87 ulong64 time_sqr(int size, int s) 91 uint64_t time_sqr(int size, int s)
88 { 92 {
89 unsigned long x; 93 unsigned long x;
90 mp_int a, b; 94 mp_int a, b;
91 ulong64 t1; 95 uint64_t t1;
92 96
93 mp_init (&a); 97 mp_init (&a);
94 mp_init (&b); 98 mp_init (&b);
95 99
96 mp_rand (&a, size); 100 mp_rand (&a, size);
97 101
98 if (s == 1) { 102 if (s == 1) {
99 KARATSUBA_SQR_CUTOFF = size; 103 KARATSUBA_SQR_CUTOFF = size;
100 } else { 104 } else {
101 KARATSUBA_SQR_CUTOFF = 100000; 105 KARATSUBA_SQR_CUTOFF = 100000;
102 } 106 }
103 107
112 } 116 }
113 117
114 int 118 int
115 main (void) 119 main (void)
116 { 120 {
117 ulong64 t1, t2; 121 uint64_t t1, t2;
118 int x, y; 122 int x, y;
119 123
120 for (x = 8; ; x += 2) { 124 for (x = 8; ; x += 2) {
121 t1 = time_mult(x, 0); 125 t1 = time_mult(x, 0);
122 t2 = time_mult(x, 1); 126 t2 = time_mult(x, 1);
123 printf("%d: %9llu %9llu, %9llu\n", x, t1, t2, t2 - t1); 127 printf("%d: %9llu %9llu, %9llu\n", x, t1, t2, t2 - t1);
124 if (t2 < t1) break; 128 if (t2 < t1) break;
125 } 129 }
126 y = x; 130 y = x;
127 131
128 for (x = 8; ; x += 2) { 132 for (x = 8; ; x += 2) {
129 t1 = time_sqr(x, 0); 133 t1 = time_sqr(x, 0);
130 t2 = time_sqr(x, 1); 134 t2 = time_sqr(x, 1);
131 printf("%d: %9llu %9llu, %9llu\n", x, t1, t2, t2 - t1); 135 printf("%d: %9llu %9llu, %9llu\n", x, t1, t2, t2 - t1);
132 if (t2 < t1) break; 136 if (t2 < t1) break;
133 } 137 }
135 printf("KARATSUBA_SQR_CUTOFF = %d\n", x); 139 printf("KARATSUBA_SQR_CUTOFF = %d\n", x);
136 140
137 return 0; 141 return 0;
138 } 142 }
139 143
140 /* $Source: /cvs/libtom/libtommath/etc/tune.c,v $ */ 144 /* $Source$ */
141 /* $Revision: 1.3 $ */ 145 /* $Revision$ */
142 /* $Date: 2006/03/31 14:18:47 $ */ 146 /* $Date$ */