xref: /netbsd-src/external/lgpl3/gmp/dist/mini-gmp/tests/t-logops.c (revision 63aea4bd5b445e491ff0389fe27ec78b3099dba3)
1 /*
2 
3 Copyright 2012, 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 http://www.gnu.org/licenses/.  */
19 
20 #include <limits.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 
24 #include "testutils.h"
25 
26 #define MAXBITS 400
27 #define COUNT 10000
28 
29 static void
30 dump (const char *label, const mpz_t x)
31 {
32   char *buf = mpz_get_str (NULL, 16, x);
33   fprintf (stderr, "%s: %s\n", label, buf);
34   testfree (buf);
35 }
36 
37 void
38 testlogops (int count)
39 {
40   unsigned i;
41   mpz_t a, b, res, ref;
42   mp_bitcnt_t c;
43 
44   mpz_init (a);
45   mpz_init (b);
46   mpz_init (res);
47   mpz_init (ref);
48 
49   for (i = 0; i < count; i++)
50     {
51       mini_random_op3 (OP_AND, MAXBITS, a, b, ref);
52       mpz_and (res, a, b);
53       if (mpz_cmp (res, ref))
54 	{
55 	  fprintf (stderr, "mpz_and failed:\n");
56 	  dump ("a", a);
57 	  dump ("b", b);
58 	  dump ("r", res);
59 	  dump ("ref", ref);
60 	  abort ();
61 	}
62 
63       mini_random_op3 (OP_IOR, MAXBITS, a, b, ref);
64       mpz_ior (res, a, b);
65       if (mpz_cmp (res, ref))
66 	{
67 	  fprintf (stderr, "mpz_ior failed:\n");
68 	  dump ("a", a);
69 	  dump ("b", b);
70 	  dump ("r", res);
71 	  dump ("ref", ref);
72 	  abort ();
73 	}
74 
75       mini_random_op3 (OP_XOR, MAXBITS, a, b, ref);
76       mpz_xor (res, a, b);
77       if (mpz_cmp (res, ref))
78 	{
79 	  fprintf (stderr, "mpz_xor failed:\n");
80 	  dump ("a", a);
81 	  dump ("b", b);
82 	  dump ("r", res);
83 	  dump ("ref", ref);
84 	  abort ();
85 	}
86 
87       if (i % 8) {
88 	c = 0;
89 	mpz_mul_2exp (res, res, i % 8);
90       } else if (mpz_sgn (res) >= 0) {
91 	c = mpz_odd_p (res) != 0;
92 	mpz_tdiv_q_2exp (res, res, 1);
93       } else {
94 	c = (~ (mp_bitcnt_t) 0) - 3;
95 	mpz_set_ui (res, 11 << ((i >> 3)%4)); /* set 3 bits */
96       }
97 
98       if (mpz_popcount (res) + c != mpz_hamdist (a, b))
99 	{
100 	  fprintf (stderr, "mpz_popcount(r) + %lu and mpz_hamdist(a,b) differ:\n", c);
101 	  dump ("a", a);
102 	  dump ("b", b);
103 	  dump ("r", res);
104 	  fprintf (stderr, "mpz_popcount(r) = %lu:\n", mpz_popcount (res));
105 	  fprintf (stderr, "mpz_hamdist(a,b) = %lu:\n", mpz_hamdist (a, b));
106 	  abort ();
107 	}
108     }
109   mpz_clear (a);
110   mpz_clear (b);
111   mpz_clear (res);
112   mpz_clear (ref);
113 }
114 
115 void
116 testmain (int argc, char **argv)
117 {
118   testhalves (COUNT*2/3, testlogops);
119   testlogops (COUNT/3);
120 }
121