Bitwise logic gmpxx.h problems

Torbjorn Granlund tg at swox.com
Thu Jul 24 17:37:01 CEST 2008


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


More information about the gmp-bugs mailing list