security/nss/lib/freebl/mpi/mulsqr.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

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

mercurial