Bitwise logic gmpxx.h problems

Joppe Bos jwbos at science.uva.nl
Thu Jul 24 20:22:05 CEST 2008

```I am not an experienced C++ hacker but the problem of these
compiler errors are the following lines in gmpxx.h:

__GMPP_DECLARE_COMPOUND_OPERATOR(operator&=)
__GMPP_DECLARE_COMPOUND_OPERATOR(operator|=)
__GMPP_DECLARE_COMPOUND_OPERATOR(operator^=)

__GMPZZ_DEFINE_COMPOUND_OPERATOR(operator&=, __gmp_binary_and)
__GMPZZ_DEFINE_COMPOUND_OPERATOR(operator|=, __gmp_binary_ior)
__GMPZZ_DEFINE_COMPOUND_OPERATOR(operator^=, __gmp_binary_xor)

Replacing the double PP and ZZ by single ones solves the problem.

Best regards,

Joppe Bos

On Thu, 24 Jul 2008, Torbjorn Granlund wrote:

> This code snippet,
>
>   #include "gmpxx.h"
>   int
>   main ()
>   {
>     mpz_class x, y;
>     x = 3;
>     y = x & 2;
>     y = 2 & x;
>     y = x | 2;
>     y = 2 | x;
>     y = x ^ 2;
>     y = 2 ^ x;
>   }
>
> creates a screenful off C++ error messages.
>
> As a C++ illiterate, I don't see why.  But I had some joy with this
> patch:
>
> *************** struct __gmp_binary_and
> *** 660,663 ****
> --- 660,681 ----
>     static void eval(mpz_ptr z, mpz_srcptr w, mpz_srcptr v)
>     { mpz_and(z, w, v); }
> +   static void eval(mpz_ptr z, mpz_srcptr w, unsigned long int i)
> +   {
> +     mpz_t temp;
> +     mp_limb_t limbs[2];
> +     temp->_mp_d = limbs;
> +     temp->_mp_alloc = 2;
> +     mpz_set_ui (temp, i);
> +     mpz_and (z, w, temp);
> +   }
> +   static void eval(mpz_ptr z, unsigned long int i, mpz_srcptr w)
> +   {
> +     mpz_t temp;
> +     mp_limb_t limbs[2];
> +     temp->_mp_d = limbs;
> +     temp->_mp_alloc = 2;
> +     mpz_set_ui (temp, i);
> +     mpz_and (z, temp, w);
> +   }
>   };
>
> *************** struct __gmp_binary_ior
> *** 666,669 ****
> --- 684,705 ----
>     static void eval(mpz_ptr z, mpz_srcptr w, mpz_srcptr v)
>     { mpz_ior(z, w, v); }
> +   static void eval(mpz_ptr z, mpz_srcptr w, unsigned long int i)
> +   {
> +     mpz_t temp;
> +     mp_limb_t limbs[2];
> +     temp->_mp_d = limbs;
> +     temp->_mp_alloc = 2;
> +     mpz_set_ui (temp, i);
> +     mpz_ior (z, w, temp);
> +   }
> +   static void eval(mpz_ptr z, unsigned long int i, mpz_srcptr w)
> +   {
> +     mpz_t temp;
> +     mp_limb_t limbs[2];
> +     temp->_mp_d = limbs;
> +     temp->_mp_alloc = 2;
> +     mpz_set_ui (temp, i);
> +     mpz_ior (z, temp, w);
> +   }
>   };
>
> *************** struct __gmp_binary_xor
> *** 672,675 ****
> --- 708,729 ----
>     static void eval(mpz_ptr z, mpz_srcptr w, mpz_srcptr v)
>     { mpz_xor(z, w, v); }
> +   static void eval(mpz_ptr z, mpz_srcptr w, unsigned long int i)
> +   {
> +     mpz_t temp;
> +     mp_limb_t limbs[2];
> +     temp->_mp_d = limbs;
> +     temp->_mp_alloc = 2;
> +     mpz_set_ui (temp, i);
> +     mpz_xor (z, w, temp);
> +   }
> +   static void eval(mpz_ptr z, unsigned long int i, mpz_srcptr w)
> +   {
> +     mpz_t temp;
> +     mp_limb_t limbs[2];
> +     temp->_mp_d = limbs;
> +     temp->_mp_alloc = 2;
> +     mpz_set_ui (temp, i);
> +     mpz_xor (z, temp, w);
> +   }
>   };
>
>
> But then a modified test case still causes errors:
>
>   int
>   main ()
>   {
>     mpz_class x, y;
>     x &= 3;
>     x |= 3;
>     x ^= 3;
>   }
>
> Could some C++ hacker please suggest solutions?  Do you see other
> similar problems with gmpxx.h that we ought to fix at the same time?
>
> --
> Torbjörn
> _______________________________________________
> gmp-bugs mailing list
> gmp-bugs at swox.com
> https://gmplib.org/mailman/listinfo/gmp-bugs
>
```