github.com/aergoio/aergo@v1.3.1/libtool/src/gmp-6.1.2/mini-gmp/tests/t-logops.c (about) 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 https://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 void 30 testlogops (int count) 31 { 32 int i; 33 mpz_t a, b, res, ref; 34 mp_bitcnt_t c; 35 36 mpz_init (a); 37 mpz_init (b); 38 mpz_init (res); 39 mpz_init (ref); 40 41 for (i = 0; i < count; i++) 42 { 43 mini_random_op3 (OP_AND, MAXBITS, a, b, ref); 44 mpz_and (res, a, b); 45 if (mpz_cmp (res, ref)) 46 { 47 fprintf (stderr, "mpz_and failed:\n"); 48 dump ("a", a); 49 dump ("b", b); 50 dump ("r", res); 51 dump ("ref", ref); 52 abort (); 53 } 54 55 mini_random_op3 (OP_IOR, MAXBITS, a, b, ref); 56 mpz_ior (res, a, b); 57 if (mpz_cmp (res, ref)) 58 { 59 fprintf (stderr, "mpz_ior failed:\n"); 60 dump ("a", a); 61 dump ("b", b); 62 dump ("r", res); 63 dump ("ref", ref); 64 abort (); 65 } 66 67 mini_random_op3 (OP_XOR, MAXBITS, a, b, ref); 68 mpz_xor (res, a, b); 69 if (mpz_cmp (res, ref)) 70 { 71 fprintf (stderr, "mpz_xor failed:\n"); 72 dump ("a", a); 73 dump ("b", b); 74 dump ("r", res); 75 dump ("ref", ref); 76 abort (); 77 } 78 79 if (i % 8) { 80 c = 0; 81 mpz_mul_2exp (res, res, i % 8); 82 } else if (mpz_sgn (res) >= 0) { 83 c = mpz_odd_p (res) != 0; 84 mpz_tdiv_q_2exp (res, res, 1); 85 } else { 86 c = (~ (mp_bitcnt_t) 0) - 3; 87 mpz_set_ui (res, 11 << ((i >> 3)%4)); /* set 3 bits */ 88 } 89 90 if (mpz_popcount (res) + c != mpz_hamdist (a, b)) 91 { 92 fprintf (stderr, "mpz_popcount(r) + %lu and mpz_hamdist(a,b) differ:\n", c); 93 dump ("a", a); 94 dump ("b", b); 95 dump ("r", res); 96 fprintf (stderr, "mpz_popcount(r) = %lu:\n", mpz_popcount (res)); 97 fprintf (stderr, "mpz_hamdist(a,b) = %lu:\n", mpz_hamdist (a, b)); 98 abort (); 99 } 100 } 101 mpz_clear (a); 102 mpz_clear (b); 103 mpz_clear (res); 104 mpz_clear (ref); 105 } 106 107 void 108 testmain (int argc, char **argv) 109 { 110 testhalves (COUNT*2/3, testlogops); 111 testlogops (COUNT/3); 112 }