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

     1  /* Test mpn_mod_1 variants.
     2  
     3  Copyright 2010, 2013 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 <stdio.h>
    21  #include <stdlib.h>
    22  
    23  #include "gmp.h"
    24  #include "gmp-impl.h"
    25  #include "tests.h"
    26  
    27  static void
    28  check_one (mp_srcptr ap, mp_size_t n, mp_limb_t b)
    29  {
    30    mp_limb_t r_ref = refmpn_mod_1 (ap, n, b);
    31    mp_limb_t r;
    32  
    33    if (n >= 2)
    34      {
    35        mp_limb_t pre[4];
    36        mpn_mod_1_1p_cps (pre, b);
    37        r = mpn_mod_1_1p (ap, n, b << pre[1], pre);
    38        if (r != r_ref)
    39  	{
    40  	  printf ("mpn_mod_1_1p failed\n");
    41  	  goto fail;
    42  	}
    43      }
    44    if ((b & GMP_NUMB_HIGHBIT) == 0)
    45      {
    46        mp_limb_t pre[5];
    47        mpn_mod_1s_2p_cps (pre, b);
    48        r = mpn_mod_1s_2p (ap, n, b << pre[1], pre);
    49        if (r != r_ref)
    50  	{
    51  	  printf ("mpn_mod_1s_2p failed\n");
    52  	  goto fail;
    53  	}
    54      }
    55    if (b <= GMP_NUMB_MASK / 3)
    56      {
    57        mp_limb_t pre[6];
    58        mpn_mod_1s_3p_cps (pre, b);
    59        r = mpn_mod_1s_3p (ap, n, b << pre[1], pre);
    60        if (r != r_ref)
    61  	{
    62  	  printf ("mpn_mod_1s_3p failed\n");
    63  	  goto fail;
    64  	}
    65      }
    66    if (b <= GMP_NUMB_MASK / 4)
    67      {
    68        mp_limb_t pre[7];
    69        mpn_mod_1s_4p_cps (pre, b);
    70        r = mpn_mod_1s_4p (ap, n, b << pre[1], pre);
    71        if (r != r_ref)
    72  	{
    73  	  printf ("mpn_mod_1s_4p failed\n");
    74  	  goto fail;
    75  	}
    76      }
    77    r = mpn_mod_1 (ap, n, b);
    78    if (r != r_ref)
    79      {
    80        printf ("mpn_mod_1 failed\n");
    81      fail:
    82        printf ("an = %d, a: ", (int) n); mpn_dump (ap, n);
    83        printf ("b           : "); mpn_dump (&b, 1);
    84        printf ("r (expected): "); mpn_dump (&r_ref, 1);
    85        printf ("r (bad)     : "); mpn_dump (&r, 1);
    86        abort();
    87      }
    88  }
    89  
    90  int
    91  main (int argc, char **argv)
    92  {
    93    gmp_randstate_ptr rands;
    94    int i;
    95    unsigned a_bits;
    96    unsigned b_bits;
    97    mpz_t a;
    98    mpz_t b;
    99  
   100    tests_start ();
   101    rands = RANDS;
   102    mpz_init (a);
   103    mpz_init (b);
   104  
   105    for (i = 0; i < 300; i++)
   106      {
   107        mp_size_t asize;
   108        a_bits = 1 + gmp_urandomm_ui (rands, 1000);
   109        b_bits = 1 + gmp_urandomm_ui (rands, GMP_NUMB_BITS);
   110  
   111        mpz_rrandomb (a, rands, a_bits);
   112        mpz_rrandomb (b, rands, b_bits);
   113  
   114        asize = SIZ(a);
   115        if (!asize)
   116  	asize = 1;
   117        if (mpz_sgn (b) == 0)
   118  	mpz_set_ui (b, 1);
   119  
   120        check_one (PTR(a), asize, PTR(b)[0]);
   121      }
   122  
   123    mpz_clear (a);
   124    mpz_clear (b);
   125  
   126    tests_end ();
   127    return 0;
   128  }
   129