Bitwise logic gmpxx.h problems

Torbjorn Granlund tg at swox.com
Sun Jul 27 23:08:40 CEST 2008


Here are the new code I am considering for gmpxx.h in GMP 4.2.3.
Comments?

I will not improve the mpz_init/mpz_clear of other functions for
4.2.3, since it is not a release for speed improvements, just bug
fixes.

// Max allocations for plain types when converted to mpz_t
// FIXME: how do we get the proper max "double" exponent?
#define __GMP_DBL_LIMBS (2 + (1025 - 1) / GMP_NUMB_BITS)
#define __GMP_ULI_LIMBS (1 + (8 * sizeof (long) - 1) / GMP_NUMB_BITS)

#define __GMPXX_LOGOP_UI(OP)						\
  mpz_t temp;								\
  mp_limb_t limbs[__GMP_ULI_LIMBS];					\
  temp->_mp_d = limbs;							\
  temp->_mp_alloc = __GMP_ULI_LIMBS;					\
  mpz_set_ui (temp, l);							\
  mpz_##OP (z, w, temp)
#define __GMPXX_LOGOP_SI(OP)						\
  mpz_t temp;								\
  mp_limb_t limbs[__GMP_ULI_LIMBS];					\
  temp->_mp_d = limbs;							\
  temp->_mp_alloc = __GMP_ULI_LIMBS;					\
  mpz_set_si (temp, l);							\
  mpz_##OP (z, w, temp)
#define __GMPXX_LOGOP_D(OP)						\
  mpz_t temp;								\
  mp_limb_t limbs[__GMP_DBL_LIMBS];					\
  temp->_mp_d = limbs;							\
  temp->_mp_alloc = __GMP_DBL_LIMBS;					\
  mpz_set_d (temp, d);							\
  mpz_##OP (z, w, temp)

struct __gmp_binary_and
{
  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 l)
  {  __GMPXX_LOGOP_UI(and);  }
  static void eval(mpz_ptr z, unsigned long int l, mpz_srcptr w)
  {  __GMPXX_LOGOP_UI(and);  }
  static void eval(mpz_ptr z, mpz_srcptr w, signed long int l)
  {  __GMPXX_LOGOP_SI(and);  }
  static void eval(mpz_ptr z, signed long int l, mpz_srcptr w)
  {  __GMPXX_LOGOP_SI(and);  }
  static void eval(mpz_ptr z, mpz_srcptr w, double d)
  {  __GMPXX_LOGOP_D(and); }
  static void eval(mpz_ptr z, double d, mpz_srcptr w)
  {  __GMPXX_LOGOP_D(and); }
};

struct __gmp_binary_ior
{
  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 l)
  {  __GMPXX_LOGOP_UI(ior);  }
  static void eval(mpz_ptr z, unsigned long int l, mpz_srcptr w)
  {  __GMPXX_LOGOP_UI(ior);  }
  static void eval(mpz_ptr z, mpz_srcptr w, signed long int l)
  {  __GMPXX_LOGOP_SI(ior);  }
  static void eval(mpz_ptr z, signed long int l, mpz_srcptr w)
  {  __GMPXX_LOGOP_SI(ior);  }
  static void eval(mpz_ptr z, mpz_srcptr w, double d)
  {  __GMPXX_LOGOP_D(ior); }
  static void eval(mpz_ptr z, double d, mpz_srcptr w)
  {  __GMPXX_LOGOP_D(ior); }
};

struct __gmp_binary_xor
{
  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 l)
  {  __GMPXX_LOGOP_UI(xor);  }
  static void eval(mpz_ptr z, unsigned long int l, mpz_srcptr w)
  {  __GMPXX_LOGOP_UI(xor);  }
  static void eval(mpz_ptr z, mpz_srcptr w, signed long int l)
  {  __GMPXX_LOGOP_SI(xor);  }
  static void eval(mpz_ptr z, signed long int l, mpz_srcptr w)
  {  __GMPXX_LOGOP_SI(xor);  }
  static void eval(mpz_ptr z, mpz_srcptr w, double d)
  {  __GMPXX_LOGOP_D(xor); }
  static void eval(mpz_ptr z, double d, mpz_srcptr w)
  {  __GMPXX_LOGOP_D(xor); }
};

-- 
Torbjörn


More information about the gmp-bugs mailing list