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

     1  /*
     2  Copyright 1999-2001, 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_copyi
    27  #define func MPN_COPY_INCR
    28  #define reffunc refmpn_copyi
    29  #define funcname "MPN_COPY_INCR"
    30  #endif
    31  
    32  #ifdef OPERATION_copyd
    33  #define func MPN_COPY_DECR
    34  #define reffunc refmpn_copyd
    35  #define funcname "MPN_COPY_DECR"
    36  #endif
    37  
    38  #if defined (USG) || defined (__SVR4) || defined (_UNICOS) || defined (__hpux)
    39  #include <time.h>
    40  
    41  int
    42  cputime ()
    43  {
    44    if (CLOCKS_PER_SEC < 100000)
    45      return clock () * 1000 / CLOCKS_PER_SEC;
    46    return clock () / (CLOCKS_PER_SEC / 1000);
    47  }
    48  #else
    49  #include <sys/types.h>
    50  #include <sys/time.h>
    51  #include <sys/resource.h>
    52  
    53  int
    54  cputime ()
    55  {
    56    struct rusage rus;
    57  
    58    getrusage (0, &rus);
    59    return rus.ru_utime.tv_sec * 1000 + rus.ru_utime.tv_usec / 1000;
    60  }
    61  #endif
    62  
    63  static void print_posneg (mp_limb_t);
    64  static void mpn_print (mp_ptr, mp_size_t);
    65  
    66  #define LXW ((int) (2 * sizeof (mp_limb_t)))
    67  #define M * 1000000
    68  
    69  #ifndef CLOCK
    70  #error "Don't know CLOCK of your machine"
    71  #endif
    72  
    73  #ifndef OPS
    74  #define OPS (CLOCK/5)
    75  #endif
    76  #ifndef SIZE
    77  #define SIZE 496
    78  #endif
    79  #ifndef TIMES
    80  #define TIMES OPS/(SIZE+1)
    81  #endif
    82  
    83  int
    84  main (int argc, char **argv)
    85  {
    86    mp_ptr s1, dx, dy;
    87    int i;
    88    long t0, t;
    89    unsigned int test;
    90    mp_size_t size;
    91    unsigned int ntests;
    92  
    93    s1 = malloc (SIZE * sizeof (mp_limb_t));
    94    dx = malloc ((SIZE + 2) * sizeof (mp_limb_t));
    95    dy = malloc ((SIZE + 2) * sizeof (mp_limb_t));
    96  
    97    ntests = ~(unsigned) 0;
    98    if (argc == 2)
    99      ntests = strtol (argv[1], 0, 0);
   100  
   101    for (test = 1; test <= ntests; test++)
   102      {
   103  #if TIMES == 1 && ! defined (PRINT)
   104        if (test % (SIZE > 100000 ? 1 : 100000 / SIZE) == 0)
   105  	{
   106  	  printf ("\r%u", test);
   107  	  fflush (stdout);
   108  	}
   109  #endif
   110  
   111  #ifdef RANDOM
   112        size = random () % SIZE + 1;
   113  #else
   114        size = SIZE;
   115  #endif
   116  
   117        dx[0] = 0x87654321;
   118        dy[0] = 0x87654321;
   119        dx[size+1] = 0x12345678;
   120        dy[size+1] = 0x12345678;
   121  
   122  #if TIMES != 1
   123        mpn_random (s1, size);
   124  
   125        t0 = cputime();
   126        for (i = 0; i < TIMES; i++)
   127  	func (dx+1, s1, size);
   128        t = cputime() - t0;
   129        printf (funcname ":    %5ldms (%.3f cycles/limb)\n",
   130  	      t, ((double) t * CLOCK) / (TIMES * size * 1000.0));
   131  #endif
   132  
   133  #ifndef NOCHECK
   134        mpn_random2 (s1, size);
   135  
   136  #ifdef PRINT
   137        mpn_print (s1, size);
   138  #endif
   139  
   140        /* Put garbage in the destination.  */
   141        for (i = 0; i < size; i++)
   142  	{
   143  	  dx[i+1] = 0xdead;
   144  	  dy[i+1] = 0xbeef;
   145  	}
   146  
   147        reffunc (dx+1, s1, size);
   148        func (dy+1, s1, size);
   149  
   150  #ifdef PRINT
   151        mpn_print (dx+1, size);
   152        mpn_print (dy+1, size);
   153  #endif
   154  
   155        if (mpn_cmp (dx, dy, size+2) != 0
   156  	  || dx[0] != 0x87654321 || dx[size+1] != 0x12345678)
   157  	{
   158  	  mp_size_t s, e;
   159  	  for (s = 0;; s++)
   160  	    if ((unsigned long long) (dx+1)[s] != (unsigned long long) (dy+1)[s])
   161  	      break;
   162  	  for (e = size - 1;; e--)
   163  	    if ((unsigned long long) (dx+1)[e] != (unsigned long long) (dy+1)[e])
   164  	      break;
   165  #ifndef PRINT
   166  	  for (i = s; i <= e; i++)
   167  	    {
   168  	      printf ("%6d: ", i);
   169  	      printf ("%0*llX ", LXW, (unsigned long long) (dx+1)[i]);
   170  	      printf ("%0*llX ", LXW, (unsigned long long) (dy+1)[i]);
   171  	      print_posneg ((dy+1)[i] - (dx+1)[i]);
   172  	      printf ("\n");
   173  	    }
   174  #endif
   175  	  printf ("\n");
   176  	  if (dy[0] != 0x87654321)
   177  	    printf ("clobbered at low end\n");
   178  	  if (dy[size+1] != 0x12345678)
   179  	    printf ("clobbered at high end\n");
   180  	  printf ("TEST NUMBER %u\n", test);
   181  	  abort();
   182  	}
   183  #endif
   184      }
   185    exit (0);
   186  }
   187  
   188  static void
   189  print_posneg (mp_limb_t d)
   190  {
   191    char buf[LXW + 2];
   192    if (d == 0)
   193      printf (" %*X", LXW, 0);
   194    else if (-d < d)
   195      {
   196        sprintf (buf, "%llX", (unsigned long long) -d);
   197        printf ("%*s-%s", LXW - (int) strlen (buf), "", buf);
   198      }
   199    else
   200      {
   201        sprintf (buf, "%llX", (unsigned long long) d);
   202        printf ("%*s+%s", LXW - (int) strlen (buf), "", buf);
   203      }
   204  }
   205  
   206  static void
   207  mpn_print (mp_ptr p, mp_size_t size)
   208  {
   209    mp_size_t i;
   210  
   211    for (i = size - 1; i >= 0; i--)
   212      {
   213  #ifdef _LONG_LONG_LIMB
   214        printf ("%0*lX%0*lX", (int) (sizeof(mp_limb_t)),
   215  	      (unsigned long) (p[i] >> (GMP_LIMB_BITS/2)),
   216  	      (int) (sizeof(mp_limb_t)), (unsigned long) (p[i]));
   217  #else
   218        printf ("%0*lX", (int) (2 * sizeof(mp_limb_t)), p[i]);
   219  #endif
   220  #ifdef SPACE
   221        if (i != 0)
   222  	printf (" ");
   223  #endif
   224      }
   225    puts ("");
   226  }