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

     1  /* Test mpn_and, mpn_ior, mpn_xor, mpn_andn, mpn_iorn, mpn_xnor, mpn_nand, and
     2     mpn_nior.
     3  
     4  Copyright 2011-2013 Free 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  
    22  #include <stdlib.h>
    23  #include <stdio.h>
    24  
    25  /* Fake native prevalence of the tested operations, so that we actually test
    26     the compiled functions, i.e., the ones which users will reach.  The inlined
    27     variants will be tested through tests/mpz/logic.c.  */
    28  #define HAVE_NATIVE_mpn_com    1
    29  #define HAVE_NATIVE_mpn_and_n  1
    30  #define HAVE_NATIVE_mpn_andn_n 1
    31  #define HAVE_NATIVE_mpn_nand_n 1
    32  #define HAVE_NATIVE_mpn_ior_n  1
    33  #define HAVE_NATIVE_mpn_iorn_n 1
    34  #define HAVE_NATIVE_mpn_nior_n 1
    35  #define HAVE_NATIVE_mpn_xor_n  1
    36  #define HAVE_NATIVE_mpn_xnor_n 1
    37  
    38  #include "gmp.h"
    39  #include "gmp-impl.h"
    40  #include "tests.h"
    41  
    42  
    43  void
    44  check_one (mp_srcptr refp, mp_srcptr rp, mp_srcptr ap, mp_srcptr bp, mp_size_t n, const char *funcname)
    45  {
    46    if (mpn_cmp (refp, rp, n))
    47      {
    48        printf ("ERROR in mpn_%s\n", funcname);
    49        printf ("a: "); mpn_dump (ap, n);
    50        printf ("b: "); mpn_dump (bp, n);
    51        printf ("r:   "); mpn_dump (rp, n);
    52        printf ("ref: "); mpn_dump (refp, n);
    53        abort();
    54      }
    55  }
    56  
    57  int
    58  main (int argc, char **argv)
    59  {
    60    mpz_t a, b;
    61    mp_ptr ap, bp, rp, refp;
    62    mp_size_t max_n, n, i;
    63    gmp_randstate_ptr rands;
    64    long test, reps = 1000;
    65    TMP_DECL;
    66    TMP_MARK;
    67  
    68    tests_start ();
    69    TESTS_REPS (reps, argv, argc);
    70  
    71    mpz_inits (a, b, NULL);
    72  
    73    rands = RANDS;		/* FIXME: not used */
    74  
    75    max_n = 100;
    76  
    77    rp = TMP_ALLOC_LIMBS (1 + max_n * 8 / GMP_LIMB_BITS);
    78    refp = TMP_ALLOC_LIMBS (1 + max_n * 8 / GMP_LIMB_BITS);
    79  
    80    for (test = 0; test < reps; test++)
    81      {
    82        for (i = 1; i <= max_n; i++)
    83  	{
    84  	  mpz_rrandomb (a, rands, i * 8);
    85  	  mpz_rrandomb (b, rands, i * 8);
    86  	  mpz_setbit (a, i * 8 - 1);
    87  	  mpz_setbit (b, i * 8 - 1);
    88  	  ap = PTR(a);
    89  	  bp = PTR(b);
    90  	  n = SIZ(a);
    91  
    92  	  refmpn_and_n (refp, ap, bp, n);
    93  	  mpn_and_n (rp, ap, bp, n);
    94  	  check_one (refp, rp, ap, bp, n, "and_n");
    95  
    96  	  refmpn_ior_n (refp, ap, bp, n);
    97  	  mpn_ior_n (rp, ap, bp, n);
    98  	  check_one (refp, rp, ap, bp, n, "ior_n");
    99  
   100  	  refmpn_xor_n (refp, ap, bp, n);
   101  	  mpn_xor_n (rp, ap, bp, n);
   102  	  check_one (refp, rp, ap, bp, n, "xor_n");
   103  
   104  	  refmpn_andn_n (refp, ap, bp, n);
   105  	  mpn_andn_n (rp, ap, bp, n);
   106  	  check_one (refp, rp, ap, bp, n, "andn_n");
   107  
   108  	  refmpn_iorn_n (refp, ap, bp, n);
   109  	  mpn_iorn_n (rp, ap, bp, n);
   110  	  check_one (refp, rp, ap, bp, n, "iorn_n");
   111  
   112  	  refmpn_nand_n (refp, ap, bp, n);
   113  	  mpn_nand_n (rp, ap, bp, n);
   114  	  check_one (refp, rp, ap, bp, n, "nand_n");
   115  
   116  	  refmpn_nior_n (refp, ap, bp, n);
   117  	  mpn_nior_n (rp, ap, bp, n);
   118  	  check_one (refp, rp, ap, bp, n, "nior_n");
   119  
   120  	  refmpn_xnor_n (refp, ap, bp, n);
   121  	  mpn_xnor_n (rp, ap, bp, n);
   122  	  check_one (refp, rp, ap, bp, n, "xnor_n");
   123  
   124  	  refmpn_com (refp, ap, n);
   125  	  mpn_com (rp, ap, n);
   126  	  check_one (refp, rp, ap, bp, n, "com");
   127  	}
   128      }
   129  
   130    TMP_FREE;
   131    mpz_clears (a, b, NULL);
   132    tests_end ();
   133    return 0;
   134  }