security/nss/lib/freebl/mpi/tests/mptest-5a.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/nss/lib/freebl/mpi/tests/mptest-5a.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,132 @@
     1.4 +/*
     1.5 + * Simple test driver for MPI library
     1.6 + *
     1.7 + * Test 5a: Greatest common divisor speed test, binary vs. Euclid
     1.8 + *
     1.9 + * This Source Code Form is subject to the terms of the Mozilla Public
    1.10 + * License, v. 2.0. If a copy of the MPL was not distributed with this
    1.11 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
    1.12 +
    1.13 +#include <stdio.h>
    1.14 +#include <stdlib.h>
    1.15 +#include <string.h>
    1.16 +#include <ctype.h>
    1.17 +#include <limits.h>
    1.18 +#include <time.h>
    1.19 +
    1.20 +#include <sys/time.h>
    1.21 +
    1.22 +#include "mpi.h"
    1.23 +#include "mpprime.h"
    1.24 +
    1.25 +typedef struct {
    1.26 +  unsigned int  sec;
    1.27 +  unsigned int  usec;
    1.28 +} instant_t;
    1.29 +
    1.30 +instant_t now(void)
    1.31 +{
    1.32 +  struct timeval clk;
    1.33 +  instant_t      res;
    1.34 +
    1.35 +  res.sec = res.usec = 0;
    1.36 +
    1.37 +  if(gettimeofday(&clk, NULL) != 0)
    1.38 +    return res;
    1.39 +
    1.40 +  res.sec = clk.tv_sec;
    1.41 +  res.usec = clk.tv_usec;
    1.42 +
    1.43 +  return res;
    1.44 +}
    1.45 +
    1.46 +#define PRECISION 16
    1.47 +
    1.48 +int main(int argc, char *argv[])
    1.49 +{
    1.50 +  int          ix, num, prec = PRECISION;
    1.51 +  mp_int       a, b, c, d;
    1.52 +  instant_t    start, finish;
    1.53 +  time_t       seed;
    1.54 +  unsigned int d1, d2;
    1.55 +
    1.56 +  seed = time(NULL);
    1.57 +
    1.58 +  if(argc < 2) {
    1.59 +    fprintf(stderr, "Usage: %s <num-tests>\n", argv[0]);
    1.60 +    return 1;
    1.61 +  }
    1.62 +
    1.63 +  if((num = atoi(argv[1])) < 0)
    1.64 +    num = -num;
    1.65 +
    1.66 +  printf("Test 5a: Euclid vs. Binary, a GCD speed test\n\n"
    1.67 +	 "Number of tests: %d\n"
    1.68 +	 "Precision:       %d digits\n\n", num, prec);
    1.69 +
    1.70 +  mp_init_size(&a, prec);
    1.71 +  mp_init_size(&b, prec);
    1.72 +  mp_init(&c);
    1.73 +  mp_init(&d);
    1.74 +
    1.75 +  printf("Verifying accuracy ... \n");
    1.76 +  srand((unsigned int)seed);
    1.77 +  for(ix = 0; ix < num; ix++) {
    1.78 +    mpp_random_size(&a, prec);
    1.79 +    mpp_random_size(&b, prec);
    1.80 +
    1.81 +    mp_gcd(&a, &b, &c);
    1.82 +    mp_bgcd(&a, &b, &d);
    1.83 +
    1.84 +    if(mp_cmp(&c, &d) != 0) {
    1.85 +      printf("Error!  Results not accurate:\n");
    1.86 +      printf("a = "); mp_print(&a, stdout); fputc('\n', stdout);
    1.87 +      printf("b = "); mp_print(&b, stdout); fputc('\n', stdout);
    1.88 +      printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
    1.89 +      printf("d = "); mp_print(&d, stdout); fputc('\n', stdout);
    1.90 +
    1.91 +      mp_clear(&a); mp_clear(&b); mp_clear(&c); mp_clear(&d);
    1.92 +      return 1;
    1.93 +    }
    1.94 +  }
    1.95 +  mp_clear(&d);
    1.96 +  printf("Accuracy confirmed for the %d test samples\n", num);
    1.97 +
    1.98 +  printf("Testing Euclid ... \n");
    1.99 +  srand((unsigned int)seed);
   1.100 +  start = now();
   1.101 +  for(ix = 0; ix < num; ix++) {
   1.102 +    mpp_random_size(&a, prec);
   1.103 +    mpp_random_size(&b, prec);
   1.104 +    mp_gcd(&a, &b, &c);
   1.105 +
   1.106 +  }
   1.107 +  finish = now();
   1.108 +
   1.109 +  d1 = (finish.sec - start.sec) * 1000000;
   1.110 +  d1 -= start.usec; d1 += finish.usec;
   1.111 +
   1.112 +  printf("Testing binary ... \n");
   1.113 +  srand((unsigned int)seed);
   1.114 +  start = now();
   1.115 +  for(ix = 0; ix < num; ix++) {
   1.116 +    mpp_random_size(&a, prec);
   1.117 +    mpp_random_size(&b, prec);
   1.118 +    mp_bgcd(&a, &b, &c);
   1.119 +  }
   1.120 +  finish = now();
   1.121 +
   1.122 +  d2 = (finish.sec - start.sec) * 1000000;
   1.123 +  d2 -= start.usec; d2 += finish.usec;
   1.124 +
   1.125 +  printf("Euclidean algorithm time: %u usec\n", d1);
   1.126 +  printf("Binary algorithm time:    %u usec\n", d2);
   1.127 +  printf("Improvement:              %.2f%%\n",
   1.128 +         (1.0 - ((double)d2 / (double)d1)) * 100.0);
   1.129 +
   1.130 +  mp_clear(&c);
   1.131 +  mp_clear(&b);
   1.132 +  mp_clear(&a);
   1.133 +
   1.134 +  return 0;
   1.135 +}

mercurial