1 1.1 mrg /* 2 1.1 mrg 3 1.1 mrg Copyright 2012, 2013 Free Software Foundation, Inc. 4 1.1 mrg 5 1.1 mrg This file is part of the GNU MP Library test suite. 6 1.1 mrg 7 1.1 mrg The GNU MP Library test suite is free software; you can redistribute it 8 1.1 mrg and/or modify it under the terms of the GNU General Public License as 9 1.1 mrg published by the Free Software Foundation; either version 3 of the License, 10 1.1 mrg or (at your option) any later version. 11 1.1 mrg 12 1.1 mrg The GNU MP Library test suite is distributed in the hope that it will be 13 1.1 mrg useful, but WITHOUT ANY WARRANTY; without even the implied warranty of 14 1.1 mrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 15 1.1 mrg Public License for more details. 16 1.1 mrg 17 1.1 mrg You should have received a copy of the GNU General Public License along with 18 1.1.1.2 mrg the GNU MP Library test suite. If not, see https://www.gnu.org/licenses/. */ 19 1.1 mrg 20 1.1 mrg #include <limits.h> 21 1.1 mrg #include <stdlib.h> 22 1.1 mrg #include <stdio.h> 23 1.1 mrg 24 1.1 mrg #include "testutils.h" 25 1.1 mrg 26 1.1 mrg #define MAXBITS 400 27 1.1 mrg #define COUNT 10000 28 1.1 mrg 29 1.1 mrg void 30 1.1 mrg testlogops (int count) 31 1.1 mrg { 32 1.1.1.2 mrg int i; 33 1.1 mrg mpz_t a, b, res, ref; 34 1.1 mrg mp_bitcnt_t c; 35 1.1 mrg 36 1.1 mrg mpz_init (a); 37 1.1 mrg mpz_init (b); 38 1.1 mrg mpz_init (res); 39 1.1 mrg mpz_init (ref); 40 1.1 mrg 41 1.1 mrg for (i = 0; i < count; i++) 42 1.1 mrg { 43 1.1 mrg mini_random_op3 (OP_AND, MAXBITS, a, b, ref); 44 1.1 mrg mpz_and (res, a, b); 45 1.1 mrg if (mpz_cmp (res, ref)) 46 1.1 mrg { 47 1.1 mrg fprintf (stderr, "mpz_and failed:\n"); 48 1.1 mrg dump ("a", a); 49 1.1 mrg dump ("b", b); 50 1.1 mrg dump ("r", res); 51 1.1 mrg dump ("ref", ref); 52 1.1 mrg abort (); 53 1.1 mrg } 54 1.1 mrg 55 1.1 mrg mini_random_op3 (OP_IOR, MAXBITS, a, b, ref); 56 1.1 mrg mpz_ior (res, a, b); 57 1.1 mrg if (mpz_cmp (res, ref)) 58 1.1 mrg { 59 1.1 mrg fprintf (stderr, "mpz_ior failed:\n"); 60 1.1 mrg dump ("a", a); 61 1.1 mrg dump ("b", b); 62 1.1 mrg dump ("r", res); 63 1.1 mrg dump ("ref", ref); 64 1.1 mrg abort (); 65 1.1 mrg } 66 1.1 mrg 67 1.1 mrg mini_random_op3 (OP_XOR, MAXBITS, a, b, ref); 68 1.1 mrg mpz_xor (res, a, b); 69 1.1 mrg if (mpz_cmp (res, ref)) 70 1.1 mrg { 71 1.1 mrg fprintf (stderr, "mpz_xor failed:\n"); 72 1.1 mrg dump ("a", a); 73 1.1 mrg dump ("b", b); 74 1.1 mrg dump ("r", res); 75 1.1 mrg dump ("ref", ref); 76 1.1 mrg abort (); 77 1.1 mrg } 78 1.1 mrg 79 1.1 mrg if (i % 8) { 80 1.1 mrg c = 0; 81 1.1 mrg mpz_mul_2exp (res, res, i % 8); 82 1.1 mrg } else if (mpz_sgn (res) >= 0) { 83 1.1 mrg c = mpz_odd_p (res) != 0; 84 1.1 mrg mpz_tdiv_q_2exp (res, res, 1); 85 1.1 mrg } else { 86 1.1 mrg c = (~ (mp_bitcnt_t) 0) - 3; 87 1.1 mrg mpz_set_ui (res, 11 << ((i >> 3)%4)); /* set 3 bits */ 88 1.1 mrg } 89 1.1 mrg 90 1.1 mrg if (mpz_popcount (res) + c != mpz_hamdist (a, b)) 91 1.1 mrg { 92 1.1 mrg fprintf (stderr, "mpz_popcount(r) + %lu and mpz_hamdist(a,b) differ:\n", c); 93 1.1 mrg dump ("a", a); 94 1.1 mrg dump ("b", b); 95 1.1 mrg dump ("r", res); 96 1.1 mrg fprintf (stderr, "mpz_popcount(r) = %lu:\n", mpz_popcount (res)); 97 1.1 mrg fprintf (stderr, "mpz_hamdist(a,b) = %lu:\n", mpz_hamdist (a, b)); 98 1.1 mrg abort (); 99 1.1 mrg } 100 1.1 mrg } 101 1.1 mrg mpz_clear (a); 102 1.1 mrg mpz_clear (b); 103 1.1 mrg mpz_clear (res); 104 1.1 mrg mpz_clear (ref); 105 1.1 mrg } 106 1.1 mrg 107 1.1 mrg void 108 1.1 mrg testmain (int argc, char **argv) 109 1.1 mrg { 110 1.1 mrg testhalves (COUNT*2/3, testlogops); 111 1.1 mrg testlogops (COUNT/3); 112 1.1 mrg } 113