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

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     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/. */
     8 #include <stdio.h>
     9 #include <stdlib.h>
    10 #include <limits.h>
    11 #include <time.h>
    13 #define MP_SQUARE 1  /* make sure squaring code is included */
    15 #include "mpi.h"
    16 #include "mpprime.h"
    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;
    26   seed = (unsigned int)time(NULL);
    28   if(argc < 3) {
    29     fprintf(stderr, "Usage: %s <ntests> <nbits>\n", argv[0]);
    30     return 1;
    31   }
    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   }
    43   prec = (prec + (DIGIT_BIT - 1)) / DIGIT_BIT;
    45   mp_init_size(&a, prec);
    46   mp_init_size(&c, 2 * prec);
    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();
    57   multime = (double)(stop - start) / CLOCKS_PER_SEC;
    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();
    68   sqrtime = (double)(stop - start) / CLOCKS_PER_SEC;
    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   }
    80   mp_clear(&a); mp_clear(&c);
    81   return 0;
    83 }

mercurial