xref: /netbsd-src/external/lgpl3/gmp/dist/tests/mpn/logic.c (revision 72c7faa4dbb41dbb0238d6b4a109da0d4b236dd4)
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-impl.h"
39 #include "tests.h"
40 
41 
42 void
check_one(mp_srcptr refp,mp_srcptr rp,mp_srcptr ap,mp_srcptr bp,mp_size_t n,const char * funcname)43 check_one (mp_srcptr refp, mp_srcptr rp, mp_srcptr ap, mp_srcptr bp, mp_size_t n, const char *funcname)
44 {
45   if (mpn_cmp (refp, rp, n))
46     {
47       printf ("ERROR in mpn_%s\n", funcname);
48       printf ("a: "); mpn_dump (ap, n);
49       printf ("b: "); mpn_dump (bp, n);
50       printf ("r:   "); mpn_dump (rp, n);
51       printf ("ref: "); mpn_dump (refp, n);
52       abort();
53     }
54 }
55 
56 int
main(int argc,char ** argv)57 main (int argc, char **argv)
58 {
59   mpz_t a, b;
60   mp_ptr ap, bp, rp, refp;
61   mp_size_t max_n, n, i;
62   gmp_randstate_ptr rands;
63   long test, reps = 1000;
64   TMP_DECL;
65   TMP_MARK;
66 
67   tests_start ();
68   TESTS_REPS (reps, argv, argc);
69 
70   mpz_inits (a, b, NULL);
71 
72   rands = RANDS;		/* FIXME: not used */
73 
74   max_n = 100;
75 
76   rp = TMP_ALLOC_LIMBS (1 + max_n * 8 / GMP_LIMB_BITS);
77   refp = TMP_ALLOC_LIMBS (1 + max_n * 8 / GMP_LIMB_BITS);
78 
79   for (test = 0; test < reps; test++)
80     {
81       for (i = 1; i <= max_n; i++)
82 	{
83 	  mpz_rrandomb (a, rands, i * 8);
84 	  mpz_rrandomb (b, rands, i * 8);
85 	  mpz_setbit (a, i * 8 - 1);
86 	  mpz_setbit (b, i * 8 - 1);
87 	  ap = PTR(a);
88 	  bp = PTR(b);
89 	  n = SIZ(a);
90 
91 	  refmpn_and_n (refp, ap, bp, n);
92 	  mpn_and_n (rp, ap, bp, n);
93 	  check_one (refp, rp, ap, bp, n, "and_n");
94 
95 	  refmpn_ior_n (refp, ap, bp, n);
96 	  mpn_ior_n (rp, ap, bp, n);
97 	  check_one (refp, rp, ap, bp, n, "ior_n");
98 
99 	  refmpn_xor_n (refp, ap, bp, n);
100 	  mpn_xor_n (rp, ap, bp, n);
101 	  check_one (refp, rp, ap, bp, n, "xor_n");
102 
103 	  refmpn_andn_n (refp, ap, bp, n);
104 	  mpn_andn_n (rp, ap, bp, n);
105 	  check_one (refp, rp, ap, bp, n, "andn_n");
106 
107 	  refmpn_iorn_n (refp, ap, bp, n);
108 	  mpn_iorn_n (rp, ap, bp, n);
109 	  check_one (refp, rp, ap, bp, n, "iorn_n");
110 
111 	  refmpn_nand_n (refp, ap, bp, n);
112 	  mpn_nand_n (rp, ap, bp, n);
113 	  check_one (refp, rp, ap, bp, n, "nand_n");
114 
115 	  refmpn_nior_n (refp, ap, bp, n);
116 	  mpn_nior_n (rp, ap, bp, n);
117 	  check_one (refp, rp, ap, bp, n, "nior_n");
118 
119 	  refmpn_xnor_n (refp, ap, bp, n);
120 	  mpn_xnor_n (rp, ap, bp, n);
121 	  check_one (refp, rp, ap, bp, n, "xnor_n");
122 
123 	  refmpn_com (refp, ap, n);
124 	  mpn_com (rp, ap, n);
125 	  check_one (refp, rp, ap, bp, n, "com");
126 	}
127     }
128 
129   TMP_FREE;
130   mpz_clears (a, b, NULL);
131   tests_end ();
132   return 0;
133 }
134