Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | * Simple test driver for MPI library |
michael@0 | 3 | * |
michael@0 | 4 | * Test 3a: Multiplication vs. squaring timing test |
michael@0 | 5 | * |
michael@0 | 6 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 7 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 8 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 9 | |
michael@0 | 10 | #include <stdio.h> |
michael@0 | 11 | #include <stdlib.h> |
michael@0 | 12 | #include <string.h> |
michael@0 | 13 | #include <ctype.h> |
michael@0 | 14 | #include <limits.h> |
michael@0 | 15 | |
michael@0 | 16 | #include <time.h> |
michael@0 | 17 | |
michael@0 | 18 | #include "mpi.h" |
michael@0 | 19 | #include "mpprime.h" |
michael@0 | 20 | |
michael@0 | 21 | int main(int argc, char *argv[]) |
michael@0 | 22 | { |
michael@0 | 23 | int ix, num, prec = 8; |
michael@0 | 24 | double d1, d2; |
michael@0 | 25 | clock_t start, finish; |
michael@0 | 26 | time_t seed; |
michael@0 | 27 | mp_int a, c, d; |
michael@0 | 28 | |
michael@0 | 29 | seed = time(NULL); |
michael@0 | 30 | |
michael@0 | 31 | if(argc < 2) { |
michael@0 | 32 | fprintf(stderr, "Usage: %s <num-tests> [<precision>]\n", argv[0]); |
michael@0 | 33 | return 1; |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | if((num = atoi(argv[1])) < 0) |
michael@0 | 37 | num = -num; |
michael@0 | 38 | |
michael@0 | 39 | if(!num) { |
michael@0 | 40 | fprintf(stderr, "%s: must perform at least 1 test\n", argv[0]); |
michael@0 | 41 | return 1; |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | if(argc > 2) { |
michael@0 | 45 | if((prec = atoi(argv[2])) <= 0) |
michael@0 | 46 | prec = 8; |
michael@0 | 47 | else |
michael@0 | 48 | prec = (prec + (DIGIT_BIT - 1)) / DIGIT_BIT; |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | printf("Test 3a: Multiplication vs squaring timing test\n" |
michael@0 | 52 | "Precision: %d digits (%u bits)\n" |
michael@0 | 53 | "# of tests: %d\n\n", prec, prec * DIGIT_BIT, num); |
michael@0 | 54 | |
michael@0 | 55 | mp_init_size(&a, prec); |
michael@0 | 56 | |
michael@0 | 57 | mp_init(&c); mp_init(&d); |
michael@0 | 58 | |
michael@0 | 59 | printf("Verifying accuracy ... \n"); |
michael@0 | 60 | srand((unsigned int)seed); |
michael@0 | 61 | for(ix = 0; ix < num; ix++) { |
michael@0 | 62 | mpp_random_size(&a, prec); |
michael@0 | 63 | mp_mul(&a, &a, &c); |
michael@0 | 64 | mp_sqr(&a, &d); |
michael@0 | 65 | |
michael@0 | 66 | if(mp_cmp(&c, &d) != 0) { |
michael@0 | 67 | printf("Error! Results not accurate:\n"); |
michael@0 | 68 | printf("a = "); mp_print(&a, stdout); fputc('\n', stdout); |
michael@0 | 69 | printf("c = "); mp_print(&c, stdout); fputc('\n', stdout); |
michael@0 | 70 | printf("d = "); mp_print(&d, stdout); fputc('\n', stdout); |
michael@0 | 71 | mp_sub(&c, &d, &d); |
michael@0 | 72 | printf("dif "); mp_print(&d, stdout); fputc('\n', stdout); |
michael@0 | 73 | mp_clear(&c); mp_clear(&d); |
michael@0 | 74 | mp_clear(&a); |
michael@0 | 75 | return 1; |
michael@0 | 76 | } |
michael@0 | 77 | } |
michael@0 | 78 | printf("Accuracy is confirmed for the %d test samples\n", num); |
michael@0 | 79 | mp_clear(&d); |
michael@0 | 80 | |
michael@0 | 81 | printf("Testing squaring ... \n"); |
michael@0 | 82 | srand((unsigned int)seed); |
michael@0 | 83 | start = clock(); |
michael@0 | 84 | for(ix = 0; ix < num; ix++) { |
michael@0 | 85 | mpp_random_size(&a, prec); |
michael@0 | 86 | mp_sqr(&a, &c); |
michael@0 | 87 | } |
michael@0 | 88 | finish = clock(); |
michael@0 | 89 | |
michael@0 | 90 | d2 = (double)(finish - start) / CLOCKS_PER_SEC; |
michael@0 | 91 | |
michael@0 | 92 | printf("Testing multiplication ... \n"); |
michael@0 | 93 | srand((unsigned int)seed); |
michael@0 | 94 | start = clock(); |
michael@0 | 95 | for(ix = 0; ix < num; ix++) { |
michael@0 | 96 | mpp_random(&a); |
michael@0 | 97 | mp_mul(&a, &a, &c); |
michael@0 | 98 | } |
michael@0 | 99 | finish = clock(); |
michael@0 | 100 | |
michael@0 | 101 | d1 = (double)(finish - start) / CLOCKS_PER_SEC; |
michael@0 | 102 | |
michael@0 | 103 | printf("Multiplication time: %.3f sec (%.3f each)\n", d1, d1 / num); |
michael@0 | 104 | printf("Squaring time: %.3f sec (%.3f each)\n", d2, d2 / num); |
michael@0 | 105 | printf("Improvement: %.2f%%\n", (1.0 - (d2 / d1)) * 100.0); |
michael@0 | 106 | |
michael@0 | 107 | mp_clear(&c); |
michael@0 | 108 | mp_clear(&a); |
michael@0 | 109 | |
michael@0 | 110 | return 0; |
michael@0 | 111 | } |