|
1 /* |
|
2 * Test whether to include squaring code given the current settings |
|
3 * |
|
4 * This Source Code Form is subject to the terms of the Mozilla Public |
|
5 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
7 |
|
8 #include <stdio.h> |
|
9 #include <stdlib.h> |
|
10 #include <limits.h> |
|
11 #include <time.h> |
|
12 |
|
13 #define MP_SQUARE 1 /* make sure squaring code is included */ |
|
14 |
|
15 #include "mpi.h" |
|
16 #include "mpprime.h" |
|
17 |
|
18 int main(int argc, char *argv[]) |
|
19 { |
|
20 int ntests, prec, ix; |
|
21 unsigned int seed; |
|
22 clock_t start, stop; |
|
23 double multime, sqrtime; |
|
24 mp_int a, c; |
|
25 |
|
26 seed = (unsigned int)time(NULL); |
|
27 |
|
28 if(argc < 3) { |
|
29 fprintf(stderr, "Usage: %s <ntests> <nbits>\n", argv[0]); |
|
30 return 1; |
|
31 } |
|
32 |
|
33 if((ntests = abs(atoi(argv[1]))) == 0) { |
|
34 fprintf(stderr, "%s: must request at least 1 test.\n", argv[0]); |
|
35 return 1; |
|
36 } |
|
37 if((prec = abs(atoi(argv[2]))) < CHAR_BIT) { |
|
38 fprintf(stderr, "%s: must request at least %d bits.\n", argv[0], |
|
39 CHAR_BIT); |
|
40 return 1; |
|
41 } |
|
42 |
|
43 prec = (prec + (DIGIT_BIT - 1)) / DIGIT_BIT; |
|
44 |
|
45 mp_init_size(&a, prec); |
|
46 mp_init_size(&c, 2 * prec); |
|
47 |
|
48 /* Test multiplication by self */ |
|
49 srand(seed); |
|
50 start = clock(); |
|
51 for(ix = 0; ix < ntests; ix++) { |
|
52 mpp_random_size(&a, prec); |
|
53 mp_mul(&a, &a, &c); |
|
54 } |
|
55 stop = clock(); |
|
56 |
|
57 multime = (double)(stop - start) / CLOCKS_PER_SEC; |
|
58 |
|
59 /* Test squaring */ |
|
60 srand(seed); |
|
61 start = clock(); |
|
62 for(ix = 0; ix < ntests; ix++) { |
|
63 mpp_random_size(&a, prec); |
|
64 mp_sqr(&a, &c); |
|
65 } |
|
66 stop = clock(); |
|
67 |
|
68 sqrtime = (double)(stop - start) / CLOCKS_PER_SEC; |
|
69 |
|
70 printf("Multiply: %.4f\n", multime); |
|
71 printf("Square: %.4f\n", sqrtime); |
|
72 if(multime < sqrtime) { |
|
73 printf("Speedup: %.1f%%\n", 100.0 * (1.0 - multime / sqrtime)); |
|
74 printf("Prefer: multiply\n"); |
|
75 } else { |
|
76 printf("Speedup: %.1f%%\n", 100.0 * (1.0 - sqrtime / multime)); |
|
77 printf("Prefer: square\n"); |
|
78 } |
|
79 |
|
80 mp_clear(&a); mp_clear(&c); |
|
81 return 0; |
|
82 |
|
83 } |