github.com/aergoio/aergo@v1.3.1/libtool/src/gmp-6.1.2/tests/devel/aors_n.c (about)

     1  /*
     2  Copyright 1996-2004, 2009, 2011 Free Software Foundation, Inc.
     3  
     4  This file is part of the GNU MP Library test suite.
     5  
     6  The GNU MP Library test suite is free software; you can redistribute it
     7  and/or modify it under the terms of the GNU General Public License as
     8  published by the Free Software Foundation; either version 3 of the License,
     9  or (at your option) any later version.
    10  
    11  The GNU MP Library test suite is distributed in the hope that it will be
    12  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
    14  Public License for more details.
    15  
    16  You should have received a copy of the GNU General Public License along with
    17  the GNU MP Library test suite.  If not, see https://www.gnu.org/licenses/.  */
    18  
    19  #include <stdlib.h>
    20  #include <string.h>
    21  #include <stdio.h>
    22  #include "gmp.h"
    23  #include "gmp-impl.h"
    24  #include "tests.h"
    25  
    26  #ifdef OPERATION_add_n
    27  #define func __gmpn_add_n
    28  #define reffunc refmpn_add_n
    29  #define funcname "mpn_add_n"
    30  #endif
    31  
    32  #ifdef OPERATION_sub_n
    33  #define func __gmpn_sub_n
    34  #define reffunc refmpn_sub_n
    35  #define funcname "mpn_sub_n"
    36  #endif
    37  
    38  #ifdef OPERATION_addlsh1_n
    39  #define func __gmpn_addlsh1_n
    40  #define reffunc refmpn_addlsh1_n
    41  #define funcname "mpn_addlsh1_n"
    42  #endif
    43  
    44  #ifdef OPERATION_sublsh1_n
    45  #define func __gmpn_sublsh1_n
    46  #define reffunc refmpn_sublsh1_n
    47  #define funcname "mpn_sublsh1_n"
    48  #endif
    49  
    50  #ifdef OPERATION_rsh1add_n
    51  #define func __gmpn_rsh1add_n
    52  #define reffunc refmpn_rsh1add_n
    53  #define funcname "mpn_rsh1add_n"
    54  #endif
    55  
    56  #ifdef OPERATION_rsh1sub_n
    57  #define func __gmpn_rsh1sub_n
    58  #define reffunc refmpn_rsh1sub_n
    59  #define funcname "mpn_rsh1sub_n"
    60  #endif
    61  
    62  #if defined (USG) || defined (__SVR4) || defined (_UNICOS) || defined (__hpux)
    63  #include <time.h>
    64  
    65  int
    66  cputime ()
    67  {
    68    if (CLOCKS_PER_SEC < 100000)
    69      return clock () * 1000 / CLOCKS_PER_SEC;
    70    return clock () / (CLOCKS_PER_SEC / 1000);
    71  }
    72  #else
    73  #include <sys/types.h>
    74  #include <sys/time.h>
    75  #include <sys/resource.h>
    76  
    77  int
    78  cputime ()
    79  {
    80    struct rusage rus;
    81  
    82    getrusage (0, &rus);
    83    return rus.ru_utime.tv_sec * 1000 + rus.ru_utime.tv_usec / 1000;
    84  }
    85  #endif
    86  
    87  static void print_posneg (mp_limb_t);
    88  static void mpn_print (mp_ptr, mp_size_t);
    89  
    90  #define LXW ((int) (2 * sizeof (mp_limb_t)))
    91  #define M * 1000000
    92  
    93  #ifndef CLOCK
    94  #error "Don't know CLOCK of your machine"
    95  #endif
    96  
    97  #ifndef OPS
    98  #define OPS (CLOCK/5)
    99  #endif
   100  #ifndef SIZE
   101  #define SIZE 328
   102  #endif
   103  #ifndef TIMES
   104  #define TIMES OPS/(SIZE+1)
   105  #endif
   106  
   107  int
   108  main (int argc, char **argv)
   109  {
   110    mp_ptr s1, s2, dx, dy;
   111    mp_limb_t cyx, cyy;
   112    int i;
   113  #if TIMES != 1
   114    long t0, t;
   115  #endif
   116    unsigned int test;
   117    mp_size_t size;
   118    unsigned int ntests;
   119  
   120    s1 = malloc (SIZE * sizeof (mp_limb_t));
   121    s2 = malloc (SIZE * sizeof (mp_limb_t));
   122    dx = malloc ((SIZE + 2) * sizeof (mp_limb_t));
   123    dy = malloc ((SIZE + 2) * sizeof (mp_limb_t));
   124  
   125    ntests = ~(unsigned) 0;
   126    if (argc == 2)
   127      ntests = strtol (argv[1], 0, 0);
   128  
   129    for (test = 1; test <= ntests; test++)
   130      {
   131  #if TIMES == 1 && ! defined (PRINT)
   132        if (test % (SIZE > 100000 ? 1 : 100000 / SIZE) == 0)
   133  	{
   134  	  printf ("\r%u", test);
   135  	  fflush (stdout);
   136  	}
   137  #endif
   138  
   139  #ifdef RANDOM
   140        size = random () % SIZE + 1;
   141  #else
   142        size = SIZE;
   143  #endif
   144  
   145        dx[0] = 0x87654321;
   146        dy[0] = 0x87654321;
   147        dx[size+1] = 0x12345678;
   148        dy[size+1] = 0x12345678;
   149  
   150  #if TIMES != 1
   151        mpn_random (s1, size);
   152        mpn_random (s2, size);
   153  
   154        t0 = cputime();
   155        for (i = 0; i < TIMES; i++)
   156  	func (dx+1, s1, s2, size);
   157        t = cputime() - t0;
   158        printf (funcname ":    %5ldms (%.3f cycles/limb)\n",
   159  	      t, ((double) t * CLOCK) / (TIMES * size * 1000.0));
   160  #endif
   161  
   162  #ifndef NOCHECK
   163        mpn_random2 (s1, size);
   164        mpn_random2 (s2, size);
   165  
   166  #ifdef PRINT
   167        mpn_print (s1, size);
   168        mpn_print (s2, size);
   169  #endif
   170  
   171        /* Put garbage in the destination.  */
   172        for (i = 0; i < size; i++)
   173  	{
   174  	  dx[i+1] = 0xdead;
   175  	  dy[i+1] = 0xbeef;
   176  	}
   177  
   178        cyx = reffunc (dx+1, s1, s2, size);
   179        cyy = func (dy+1, s1, s2, size);
   180  
   181  #ifdef PRINT
   182        mpn_print (&cyx, 1);
   183        mpn_print (dx+1, size);
   184        mpn_print (&cyy, 1);
   185        mpn_print (dy+1, size);
   186  #endif
   187  
   188        if (cyx != cyy || mpn_cmp (dx, dy, size+2) != 0
   189  	  || dx[0] != 0x87654321 || dx[size+1] != 0x12345678)
   190  	{
   191  	  mp_size_t s, e;
   192  	  for (s = 0;; s++)
   193  	    if ((unsigned long long) (dx+1)[s] != (unsigned long long) (dy+1)[s])
   194  	      break;
   195  	  for (e = size - 1;; e--)
   196  	    if ((unsigned long long) (dx+1)[e] != (unsigned long long) (dy+1)[e])
   197  	      break;
   198  #ifndef PRINT
   199  	  for (i = s; i <= e; i++)
   200  	    {
   201  	      printf ("%6d: ", i);
   202  	      printf ("%0*llX ", LXW, (unsigned long long) (dx+1)[i]);
   203  	      printf ("%0*llX ", LXW, (unsigned long long) (dy+1)[i]);
   204  	      print_posneg ((dy+1)[i] - (dx+1)[i]);
   205  	      printf ("\n");
   206  	    }
   207  	  printf ("%6s: ", "retval");
   208  	  printf ("%0*llX ", LXW, (unsigned long long) cyx);
   209  	  printf ("%0*llX ", LXW, (unsigned long long) cyy);
   210  	  print_posneg (cyx - cyy);
   211  #endif
   212  	  printf ("\n");
   213  	  if (dy[0] != 0x87654321)
   214  	    printf ("clobbered at low end\n");
   215  	  if (dy[size+1] != 0x12345678)
   216  	    printf ("clobbered at high end\n");
   217  	  printf ("TEST NUMBER %u\n", test);
   218  	  abort();
   219  	}
   220  #endif
   221      }
   222    exit (0);
   223  }
   224  
   225  static void
   226  print_posneg (mp_limb_t d)
   227  {
   228    char buf[LXW + 2];
   229    if (d == 0)
   230      printf (" %*X", LXW, 0);
   231    else if (-d < d)
   232      {
   233        sprintf (buf, "%llX", (unsigned long long) -d);
   234        printf ("%*s-%s", LXW - (int) strlen (buf), "", buf);
   235      }
   236    else
   237      {
   238        sprintf (buf, "%llX", (unsigned long long) d);
   239        printf ("%*s+%s", LXW - (int) strlen (buf), "", buf);
   240      }
   241  }
   242  
   243  static void
   244  mpn_print (mp_ptr p, mp_size_t size)
   245  {
   246    mp_size_t i;
   247  
   248    for (i = size - 1; i >= 0; i--)
   249      {
   250  #ifdef _LONG_LONG_LIMB
   251        printf ("%0*lX%0*lX", (int) (sizeof(mp_limb_t)),
   252  	      (unsigned long) (p[i] >> (GMP_LIMB_BITS/2)),
   253  	      (int) (sizeof(mp_limb_t)), (unsigned long) (p[i]));
   254  #else
   255        printf ("%0*lX", (int) (2 * sizeof(mp_limb_t)), p[i]);
   256  #endif
   257  #ifdef SPACE
   258        if (i != 0)
   259  	printf (" ");
   260  #endif
   261      }
   262    puts ("");
   263  }