Blame tests/mpn/logic.c

Packit 5c3484
/* Test mpn_and, mpn_ior, mpn_xor, mpn_andn, mpn_iorn, mpn_xnor, mpn_nand, and
Packit 5c3484
   mpn_nior.
Packit 5c3484
Packit 5c3484
Copyright 2011-2013 Free Software Foundation, Inc.
Packit 5c3484
Packit 5c3484
This file is part of the GNU MP Library test suite.
Packit 5c3484
Packit 5c3484
The GNU MP Library test suite is free software; you can redistribute it
Packit 5c3484
and/or modify it under the terms of the GNU General Public License as
Packit 5c3484
published by the Free Software Foundation; either version 3 of the License,
Packit 5c3484
or (at your option) any later version.
Packit 5c3484
Packit 5c3484
The GNU MP Library test suite is distributed in the hope that it will be
Packit 5c3484
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 5c3484
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
Packit 5c3484
Public License for more details.
Packit 5c3484
Packit 5c3484
You should have received a copy of the GNU General Public License along with
Packit 5c3484
the GNU MP Library test suite.  If not, see https://www.gnu.org/licenses/.  */
Packit 5c3484
Packit 5c3484
Packit 5c3484
#include <stdlib.h>
Packit 5c3484
#include <stdio.h>
Packit 5c3484
Packit 5c3484
/* Fake native prevalence of the tested operations, so that we actually test
Packit 5c3484
   the compiled functions, i.e., the ones which users will reach.  The inlined
Packit 5c3484
   variants will be tested through tests/mpz/logic.c.  */
Packit 5c3484
#define HAVE_NATIVE_mpn_com    1
Packit 5c3484
#define HAVE_NATIVE_mpn_and_n  1
Packit 5c3484
#define HAVE_NATIVE_mpn_andn_n 1
Packit 5c3484
#define HAVE_NATIVE_mpn_nand_n 1
Packit 5c3484
#define HAVE_NATIVE_mpn_ior_n  1
Packit 5c3484
#define HAVE_NATIVE_mpn_iorn_n 1
Packit 5c3484
#define HAVE_NATIVE_mpn_nior_n 1
Packit 5c3484
#define HAVE_NATIVE_mpn_xor_n  1
Packit 5c3484
#define HAVE_NATIVE_mpn_xnor_n 1
Packit 5c3484
Packit 5c3484
#include "gmp.h"
Packit 5c3484
#include "gmp-impl.h"
Packit 5c3484
#include "tests.h"
Packit 5c3484
Packit 5c3484
Packit 5c3484
void
Packit 5c3484
check_one (mp_srcptr refp, mp_srcptr rp, mp_srcptr ap, mp_srcptr bp, mp_size_t n, const char *funcname)
Packit 5c3484
{
Packit 5c3484
  if (mpn_cmp (refp, rp, n))
Packit 5c3484
    {
Packit 5c3484
      printf ("ERROR in mpn_%s\n", funcname);
Packit 5c3484
      printf ("a: "); mpn_dump (ap, n);
Packit 5c3484
      printf ("b: "); mpn_dump (bp, n);
Packit 5c3484
      printf ("r:   "); mpn_dump (rp, n);
Packit 5c3484
      printf ("ref: "); mpn_dump (refp, n);
Packit 5c3484
      abort();
Packit 5c3484
    }
Packit 5c3484
}
Packit 5c3484
Packit 5c3484
int
Packit 5c3484
main (int argc, char **argv)
Packit 5c3484
{
Packit 5c3484
  mpz_t a, b;
Packit 5c3484
  mp_ptr ap, bp, rp, refp;
Packit 5c3484
  mp_size_t max_n, n, i;
Packit 5c3484
  gmp_randstate_ptr rands;
Packit 5c3484
  long test, reps = 1000;
Packit 5c3484
  TMP_DECL;
Packit 5c3484
  TMP_MARK;
Packit 5c3484
Packit 5c3484
  tests_start ();
Packit 5c3484
  TESTS_REPS (reps, argv, argc);
Packit 5c3484
Packit 5c3484
  mpz_inits (a, b, NULL);
Packit 5c3484
Packit 5c3484
  rands = RANDS;		/* FIXME: not used */
Packit 5c3484
Packit 5c3484
  max_n = 100;
Packit 5c3484
Packit 5c3484
  rp = TMP_ALLOC_LIMBS (1 + max_n * 8 / GMP_LIMB_BITS);
Packit 5c3484
  refp = TMP_ALLOC_LIMBS (1 + max_n * 8 / GMP_LIMB_BITS);
Packit 5c3484
Packit 5c3484
  for (test = 0; test < reps; test++)
Packit 5c3484
    {
Packit 5c3484
      for (i = 1; i <= max_n; i++)
Packit 5c3484
	{
Packit 5c3484
	  mpz_rrandomb (a, rands, i * 8);
Packit 5c3484
	  mpz_rrandomb (b, rands, i * 8);
Packit 5c3484
	  mpz_setbit (a, i * 8 - 1);
Packit 5c3484
	  mpz_setbit (b, i * 8 - 1);
Packit 5c3484
	  ap = PTR(a);
Packit 5c3484
	  bp = PTR(b);
Packit 5c3484
	  n = SIZ(a);
Packit 5c3484
Packit 5c3484
	  refmpn_and_n (refp, ap, bp, n);
Packit 5c3484
	  mpn_and_n (rp, ap, bp, n);
Packit 5c3484
	  check_one (refp, rp, ap, bp, n, "and_n");
Packit 5c3484
Packit 5c3484
	  refmpn_ior_n (refp, ap, bp, n);
Packit 5c3484
	  mpn_ior_n (rp, ap, bp, n);
Packit 5c3484
	  check_one (refp, rp, ap, bp, n, "ior_n");
Packit 5c3484
Packit 5c3484
	  refmpn_xor_n (refp, ap, bp, n);
Packit 5c3484
	  mpn_xor_n (rp, ap, bp, n);
Packit 5c3484
	  check_one (refp, rp, ap, bp, n, "xor_n");
Packit 5c3484
Packit 5c3484
	  refmpn_andn_n (refp, ap, bp, n);
Packit 5c3484
	  mpn_andn_n (rp, ap, bp, n);
Packit 5c3484
	  check_one (refp, rp, ap, bp, n, "andn_n");
Packit 5c3484
Packit 5c3484
	  refmpn_iorn_n (refp, ap, bp, n);
Packit 5c3484
	  mpn_iorn_n (rp, ap, bp, n);
Packit 5c3484
	  check_one (refp, rp, ap, bp, n, "iorn_n");
Packit 5c3484
Packit 5c3484
	  refmpn_nand_n (refp, ap, bp, n);
Packit 5c3484
	  mpn_nand_n (rp, ap, bp, n);
Packit 5c3484
	  check_one (refp, rp, ap, bp, n, "nand_n");
Packit 5c3484
Packit 5c3484
	  refmpn_nior_n (refp, ap, bp, n);
Packit 5c3484
	  mpn_nior_n (rp, ap, bp, n);
Packit 5c3484
	  check_one (refp, rp, ap, bp, n, "nior_n");
Packit 5c3484
Packit 5c3484
	  refmpn_xnor_n (refp, ap, bp, n);
Packit 5c3484
	  mpn_xnor_n (rp, ap, bp, n);
Packit 5c3484
	  check_one (refp, rp, ap, bp, n, "xnor_n");
Packit 5c3484
Packit 5c3484
	  refmpn_com (refp, ap, n);
Packit 5c3484
	  mpn_com (rp, ap, n);
Packit 5c3484
	  check_one (refp, rp, ap, bp, n, "com");
Packit 5c3484
	}
Packit 5c3484
    }
Packit 5c3484
Packit 5c3484
  TMP_FREE;
Packit 5c3484
  mpz_clears (a, b, NULL);
Packit 5c3484
  tests_end ();
Packit 5c3484
  return 0;
Packit 5c3484
}