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

     1  /*
     2  Copyright 1996-1998, 2000, 2001, 2007, 2009 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 <stdio.h>
    20  #include <stdlib.h>
    21  #include "gmp.h"
    22  #include "gmp-impl.h"
    23  
    24  #if defined (USG) || defined (__SVR4) || defined (_UNICOS) || defined (__hpux)
    25  #include <time.h>
    26  
    27  int
    28  cputime ()
    29  {
    30    if (CLOCKS_PER_SEC < 100000)
    31      return clock () * 1000 / CLOCKS_PER_SEC;
    32    return clock () / (CLOCKS_PER_SEC / 1000);
    33  }
    34  #else
    35  #include <sys/types.h>
    36  #include <sys/time.h>
    37  #include <sys/resource.h>
    38  
    39  int
    40  cputime ()
    41  {
    42    struct rusage rus;
    43  
    44    getrusage (0, &rus);
    45    return rus.ru_utime.tv_sec * 1000 + rus.ru_utime.tv_usec / 1000;
    46  }
    47  #endif
    48  
    49  #define M * 1000000
    50  
    51  #ifndef CLOCK
    52  #error "Don't know CLOCK of your machine"
    53  #endif
    54  
    55  #ifndef OPS
    56  #define OPS 20000000
    57  #endif
    58  #ifndef SIZE
    59  #define SIZE 100
    60  #endif
    61  #ifndef TIMES
    62  #define TIMES OPS/(SIZE+1)
    63  #endif
    64  
    65  int
    66  main ()
    67  {
    68    mp_limb_t nptr[2 * SIZE];
    69    mp_limb_t dptr[2 * SIZE];
    70    mp_limb_t qptr[2 * SIZE];
    71    mp_limb_t pptr[2 * SIZE + 1];
    72    mp_limb_t rptr[2 * SIZE];
    73    mp_size_t nsize, dsize, qsize, rsize, psize;
    74    int test;
    75    mp_limb_t qlimb;
    76  
    77    for (test = 0; ; test++)
    78      {
    79        printf ("%d\n", test);
    80  #ifdef RANDOM
    81        nsize = random () % (2 * SIZE) + 1;
    82        dsize = random () % nsize + 1;
    83  #else
    84        nsize = 2 * SIZE;
    85        dsize = SIZE;
    86  #endif
    87  
    88        mpn_random2 (nptr, nsize);
    89        mpn_random2 (dptr, dsize);
    90        dptr[dsize - 1] |= (mp_limb_t) 1 << (GMP_LIMB_BITS - 1);
    91  
    92        MPN_COPY (rptr, nptr, nsize);
    93        qlimb = mpn_divrem (qptr, (mp_size_t) 0, rptr, nsize, dptr, dsize);
    94        rsize = dsize;
    95        qsize = nsize - dsize;
    96        qptr[qsize] = qlimb;
    97        qsize += qlimb;
    98        if (qsize == 0 || qsize > 2 * SIZE)
    99  	{
   100  	  continue;		/* bogus */
   101  	}
   102        else
   103  	{
   104  	  mp_limb_t cy;
   105  	  if (qsize > dsize)
   106  	    mpn_mul (pptr, qptr, qsize, dptr, dsize);
   107  	  else
   108  	    mpn_mul (pptr, dptr, dsize, qptr, qsize);
   109  	  psize = qsize + dsize;
   110  	  psize -= pptr[psize - 1] == 0;
   111  	  cy = mpn_add (pptr, pptr, psize, rptr, rsize);
   112  	  pptr[psize] = cy;
   113  	  psize += cy;
   114  	}
   115  
   116        if (nsize != psize || mpn_cmp (nptr, pptr, nsize) != 0)
   117  	abort ();
   118      }
   119  }