github.com/aergoio/aergo@v1.3.1/libtool/src/gmp-6.1.2/tests/mpz/t-remove.c (about)

     1  /* Test mpz_remove.
     2  
     3  Copyright 1991, 1993, 1994, 1996, 1997, 2000, 2001, 2009, 2012, 2013 Free
     4  Software Foundation, Inc.
     5  
     6  This file is part of the GNU MP Library test suite.
     7  
     8  The GNU MP Library test suite is free software; you can redistribute it
     9  and/or modify it under the terms of the GNU General Public License as
    10  published by the Free Software Foundation; either version 3 of the License,
    11  or (at your option) any later version.
    12  
    13  The GNU MP Library test suite is distributed in the hope that it will be
    14  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
    15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
    16  Public License for more details.
    17  
    18  You should have received a copy of the GNU General Public License along with
    19  the GNU MP Library test suite.  If not, see https://www.gnu.org/licenses/.  */
    20  
    21  #include <stdio.h>
    22  #include <stdlib.h>
    23  
    24  #include "gmp.h"
    25  #include "gmp-impl.h"
    26  #include "tests.h"
    27  
    28  void debug_mp (mpz_t);
    29  unsigned long int mpz_refremove (mpz_t, const mpz_t, const mpz_t);
    30  
    31  int
    32  main (int argc, char **argv)
    33  {
    34    unsigned long int exp;
    35    mpz_t t, dest, refdest, dividend, divisor;
    36    mp_size_t dividend_size, divisor_size;
    37    int i;
    38    int reps = 1000;
    39    unsigned long int pwr, refpwr;
    40    gmp_randstate_ptr rands;
    41    mpz_t bs;
    42    unsigned long size_range;
    43  
    44    tests_start ();
    45    rands = RANDS;
    46  
    47    if (argc == 2)
    48      reps = atoi (argv[1]);
    49  
    50    mpz_inits (bs, t, dest, refdest, dividend, divisor, NULL);
    51  
    52    for (i = 0; i < reps; i++)
    53      {
    54        mpz_urandomb (bs, rands, 32);
    55        size_range = mpz_get_ui (bs) % 18 + 1; /* 1..524288 bit operands */
    56  
    57        do
    58  	{
    59  	  mpz_urandomb (bs, rands, size_range);
    60  	  divisor_size = mpz_get_ui (bs);
    61  	  mpz_rrandomb (divisor, rands, divisor_size);
    62  	}
    63        while (mpz_sgn (divisor) == 0);
    64  
    65        mpz_urandomb (bs, rands, size_range);
    66        dividend_size = mpz_get_ui (bs) + divisor_size;
    67        mpz_rrandomb (dividend, rands, dividend_size);
    68  
    69        mpz_urandomb (bs, rands, 32);
    70        exp = mpz_get_ui (bs) % (5 + 10000 / mpz_sizeinbase (divisor, 2));
    71        if (mpz_get_ui (bs) & 2)
    72  	mpz_neg (divisor, divisor);
    73        mpz_pow_ui (t, divisor, exp);
    74        mpz_mul (dividend, dividend, t);
    75  
    76        refpwr = mpz_refremove (refdest, dividend, divisor);
    77        pwr = mpz_remove (dest, dividend, divisor);
    78  
    79        if (refpwr != pwr || mpz_cmp (refdest, dest) != 0)
    80  	{
    81  	  fprintf (stderr, "ERROR after %d tests\n", i);
    82  	  fprintf (stderr, "refpower = %lu\n", refpwr);
    83  	  fprintf (stderr, "   power = %lu\n", pwr);
    84  	  fprintf (stderr, "    op1 = "); debug_mp (dividend);
    85  	  fprintf (stderr, "    op2 = "); debug_mp (divisor);
    86  	  fprintf (stderr, "refdest = "); debug_mp (refdest);
    87  	  fprintf (stderr, "   dest = "); debug_mp (dest);
    88  	  abort ();
    89  	}
    90      }
    91  
    92    mpz_clears (bs, t, dest, refdest, dividend, divisor, NULL);
    93  
    94    tests_end ();
    95    exit (0);
    96  }
    97  
    98  unsigned long int
    99  mpz_refremove (mpz_t dest, const mpz_t src, const mpz_t f)
   100  {
   101    unsigned long int pwr;
   102  
   103    pwr = 0;
   104  
   105    mpz_set (dest, src);
   106    if (mpz_cmpabs_ui (f, 1) > 0)
   107      {
   108        mpz_t rem, x;
   109  
   110        mpz_init (x);
   111        mpz_init (rem);
   112  
   113        for (;; pwr++)
   114  	{
   115  	  mpz_tdiv_qr (x, rem, dest, f);
   116  	  if (mpz_cmp_ui (rem, 0) != 0)
   117  	    break;
   118  	  mpz_swap (dest, x);
   119  	}
   120  
   121        mpz_clear (x);
   122        mpz_clear (rem);
   123      }
   124  
   125    return pwr;
   126  }
   127  
   128  void
   129  debug_mp (mpz_t x)
   130  {
   131    size_t siz = mpz_sizeinbase (x, 16);
   132  
   133    if (siz > 65)
   134      {
   135        mpz_t q;
   136        mpz_init (q);
   137        mpz_tdiv_q_2exp (q, x, 4 * (mpz_sizeinbase (x, 16) - 25));
   138        gmp_fprintf (stderr, "%ZX...", q);
   139        mpz_tdiv_r_2exp (q, x, 4 * 25);
   140        gmp_fprintf (stderr, "%025ZX [%d]\n", q, (int) siz);
   141        mpz_clear (q);
   142      }
   143    else
   144      {
   145        gmp_fprintf (stderr, "%ZX\n", x);
   146      }
   147  }