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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/nss/lib/freebl/mpi/mulsqr.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,83 @@
     1.4 +/*
     1.5 + * Test whether to include squaring code given the current settings
     1.6 + *
     1.7 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.8 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.9 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
    1.10 +
    1.11 +#include <stdio.h>
    1.12 +#include <stdlib.h>
    1.13 +#include <limits.h>
    1.14 +#include <time.h>
    1.15 +
    1.16 +#define MP_SQUARE 1  /* make sure squaring code is included */
    1.17 +
    1.18 +#include "mpi.h"
    1.19 +#include "mpprime.h"
    1.20 +
    1.21 +int main(int argc, char *argv[])
    1.22 +{
    1.23 +  int           ntests, prec, ix;
    1.24 +  unsigned int  seed;
    1.25 +  clock_t       start, stop;
    1.26 +  double        multime, sqrtime;
    1.27 +  mp_int        a, c;
    1.28 +
    1.29 +  seed = (unsigned int)time(NULL);
    1.30 +
    1.31 +  if(argc < 3) {
    1.32 +    fprintf(stderr, "Usage: %s <ntests> <nbits>\n", argv[0]);
    1.33 +    return 1;
    1.34 +  }
    1.35 +
    1.36 +  if((ntests = abs(atoi(argv[1]))) == 0) {
    1.37 +    fprintf(stderr, "%s: must request at least 1 test.\n", argv[0]);
    1.38 +    return 1;
    1.39 +  }
    1.40 +  if((prec = abs(atoi(argv[2]))) < CHAR_BIT) {
    1.41 +    fprintf(stderr, "%s: must request at least %d bits.\n", argv[0],
    1.42 +	    CHAR_BIT);
    1.43 +    return 1;
    1.44 +  }
    1.45 +
    1.46 +  prec = (prec + (DIGIT_BIT - 1)) / DIGIT_BIT;
    1.47 +
    1.48 +  mp_init_size(&a, prec);
    1.49 +  mp_init_size(&c, 2 * prec);
    1.50 +
    1.51 +  /* Test multiplication by self */
    1.52 +  srand(seed);
    1.53 +  start = clock();
    1.54 +  for(ix = 0; ix < ntests; ix++) {
    1.55 +    mpp_random_size(&a, prec);
    1.56 +    mp_mul(&a, &a, &c);
    1.57 +  }
    1.58 +  stop = clock();
    1.59 +
    1.60 +  multime = (double)(stop - start) / CLOCKS_PER_SEC;
    1.61 +
    1.62 +  /* Test squaring */
    1.63 +  srand(seed);
    1.64 +  start = clock();
    1.65 +  for(ix = 0; ix < ntests; ix++) {
    1.66 +    mpp_random_size(&a, prec);
    1.67 +    mp_sqr(&a, &c);
    1.68 +  }
    1.69 +  stop = clock();
    1.70 +
    1.71 +  sqrtime = (double)(stop - start) / CLOCKS_PER_SEC;
    1.72 +
    1.73 +  printf("Multiply: %.4f\n", multime);
    1.74 +  printf("Square:   %.4f\n", sqrtime);
    1.75 +  if(multime < sqrtime) {
    1.76 +    printf("Speedup:  %.1f%%\n", 100.0 * (1.0 - multime / sqrtime));
    1.77 +    printf("Prefer:   multiply\n");
    1.78 +  } else {
    1.79 +    printf("Speedup:  %.1f%%\n", 100.0 * (1.0 - sqrtime / multime));
    1.80 +    printf("Prefer:   square\n");
    1.81 +  }
    1.82 +
    1.83 +  mp_clear(&a); mp_clear(&c);
    1.84 +  return 0;
    1.85 +
    1.86 +}

mercurial