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

     1  /*
     2  
     3  Copyright 2012, 2014, Free Software Foundation, Inc.
     4  
     5  This file is part of the GNU MP Library test suite.
     6  
     7  The GNU MP Library test suite is free software; you can redistribute it
     8  and/or modify it under the terms of the GNU General Public License as
     9  published by the Free Software Foundation; either version 3 of the License,
    10  or (at your option) any later version.
    11  
    12  The GNU MP Library test suite is distributed in the hope that it will be
    13  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
    14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
    15  Public License for more details.
    16  
    17  You should have received a copy of the GNU General Public License along with
    18  the GNU MP Library test suite.  If not, see https://www.gnu.org/licenses/.  */
    19  
    20  #include <limits.h>
    21  #include <stdlib.h>
    22  #include <stdio.h>
    23  #include <string.h>
    24  
    25  #include "testutils.h"
    26  
    27  #define MAXBITS 400
    28  #define COUNT 10000
    29  
    30  #define GMP_LIMB_BITS (sizeof(mp_limb_t) * CHAR_BIT)
    31  #define MAXLIMBS ((MAXBITS + GMP_LIMB_BITS - 1) / GMP_LIMB_BITS)
    32  
    33  void
    34  testmain (int argc, char **argv)
    35  {
    36    unsigned i;
    37    mpz_t a, b, res, ref;
    38  
    39    mpz_init (a);
    40    mpz_init (b);
    41    mpz_init_set_ui (res, 5);
    42    mpz_init (ref);
    43  
    44    for (i = 0; i < COUNT; i++)
    45      {
    46        mini_random_op3 (OP_MUL, MAXBITS, a, b, ref);
    47        if (i & 1) {
    48  	mpz_add (ref, ref, res);
    49  	if (mpz_fits_ulong_p (b))
    50  	  mpz_addmul_ui (res, a, mpz_get_ui (b));
    51  	else
    52  	  mpz_addmul (res, a, b);
    53        } else {
    54  	mpz_sub (ref, res, ref);
    55  	if (mpz_fits_ulong_p (b))
    56  	  mpz_submul_ui (res, a, mpz_get_ui (b));
    57  	else
    58  	  mpz_submul (res, a, b);
    59        }
    60        if (mpz_cmp (res, ref))
    61  	{
    62  	  if (i & 1)
    63  	    fprintf (stderr, "mpz_addmul failed:\n");
    64  	  else
    65  	    fprintf (stderr, "mpz_submul failed:\n");
    66  	  dump ("a", a);
    67  	  dump ("b", b);
    68  	  dump ("r", res);
    69  	  dump ("ref", ref);
    70  	  abort ();
    71  	}
    72      }
    73    mpz_clear (a);
    74    mpz_clear (b);
    75    mpz_clear (res);
    76    mpz_clear (ref);
    77  }