[Gmp-commit] /var/hg/gmp: Inline mpz_set_*i where it is safe

mercurial at gmplib.org mercurial at gmplib.org
Wed Mar 16 19:55:18 CET 2011


details:   /var/hg/gmp/rev/3127bba7037a
changeset: 14066:3127bba7037a
user:      Marc Glisse <marc.glisse at inria.fr>
date:      Wed Mar 16 19:55:13 2011 +0100
description:
Inline mpz_set_*i where it is safe

diffstat:

 ChangeLog           |  10 ++++++++++
 gmpxx.h             |  35 +++++++++++++++++++++++++++--------
 tests/cxx/t-ops2.cc |   1 +
 3 files changed, 38 insertions(+), 8 deletions(-)

diffs (96 lines):

diff -r c1537591cbd9 -r 3127bba7037a ChangeLog
--- a/ChangeLog	Tue Mar 15 19:22:57 2011 +0100
+++ b/ChangeLog	Wed Mar 16 19:55:13 2011 +0100
@@ -1,3 +1,13 @@
+2011-03-16  Marc Glisse  <marc.glisse at inria.fr>
+
+	* gmpxx.h (__mpz_set_ui_safe): New inline function.
+	(__mpz_set_si_safe): Likewise.
+	(__GMPXX_TMPZ_UI): Use the new function.
+	(__GMPXX_TMPZ_SI): Likewise.
+	(__GMPXX_TMPQ_UI): Likewise.
+	(__GMPXX_TMPQ_SI): Likewise.
+	* tests/cxx/t-ops2.cc: test converting 0 to stack mpq_t.
+
 2011-03-15  Marc Glisse  <marc.glisse at inria.fr>
 
 	* gmpxx.h (__GMPXX_TMPQ_UI): New macro.
diff -r c1537591cbd9 -r 3127bba7037a gmpxx.h
--- a/gmpxx.h	Tue Mar 15 19:22:57 2011 +0100
+++ b/gmpxx.h	Wed Mar 16 19:55:13 2011 +0100
@@ -49,19 +49,40 @@
 #define __GMPZ_ULI_LIMBS 1
 #endif
 
+inline void __mpz_set_ui_safe(mpz_ptr p, unsigned long l)
+{
+  p->_mp_size = (l != 0);
+  p->_mp_d[0] = l & GMP_NUMB_MASK;
+#if __GMPZ_ULI_LIMBS > 1
+  l >>= GMP_NUMB_BITS;
+  p->_mp_d[1] = l;
+  p->_mp_size += (l != 0);
+#endif
+}
+
+inline void __mpz_set_si_safe(mpz_ptr p, long l)
+{
+  if(l < 0)
+  {
+    __mpz_set_ui_safe(p, -l);
+    mpz_neg(p, p);
+  }
+  else
+    __mpz_set_ui_safe(p, l);
+    // Note: we know the high bit of l is 0 so we could do slightly better
+}
+
 // Fake temporary variables
 #define __GMPXX_TMPZ_UI							\
   mpz_t temp;								\
   mp_limb_t limbs[__GMPZ_ULI_LIMBS];					\
   temp->_mp_d = limbs;							\
-  temp->_mp_alloc = __GMPZ_ULI_LIMBS;					\
-  mpz_set_ui (temp, l)
+  __mpz_set_ui_safe (temp, l)
 #define __GMPXX_TMPZ_SI							\
   mpz_t temp;								\
   mp_limb_t limbs[__GMPZ_ULI_LIMBS];					\
   temp->_mp_d = limbs;							\
-  temp->_mp_alloc = __GMPZ_ULI_LIMBS;					\
-  mpz_set_si (temp, l)
+  __mpz_set_si_safe (temp, l)
 #define __GMPXX_TMPZ_D							\
   mpz_t temp;								\
   mp_limb_t limbs[__GMPZ_DBL_LIMBS];					\
@@ -73,8 +94,7 @@
   mpq_t temp;								\
   mp_limb_t limbs[__GMPZ_ULI_LIMBS+1];					\
   mpq_numref(temp)->_mp_d = limbs;					\
-  mpq_numref(temp)->_mp_alloc = __GMPZ_ULI_LIMBS;			\
-  mpz_set_ui (mpq_numref(temp), l);					\
+  __mpz_set_ui_safe (mpq_numref(temp), l);				\
   mpq_denref(temp)->_mp_d = limbs + __GMPZ_ULI_LIMBS;			\
   mpq_denref(temp)->_mp_size = 1;					\
   mpq_denref(temp)->_mp_d[0] = 1
@@ -82,8 +102,7 @@
   mpq_t temp;								\
   mp_limb_t limbs[__GMPZ_ULI_LIMBS+1];					\
   mpq_numref(temp)->_mp_d = limbs;					\
-  mpq_numref(temp)->_mp_alloc = __GMPZ_ULI_LIMBS;			\
-  mpz_set_si (mpq_numref(temp), l);					\
+  __mpz_set_si_safe (mpq_numref(temp), l);				\
   mpq_denref(temp)->_mp_d = limbs + __GMPZ_ULI_LIMBS;			\
   mpq_denref(temp)->_mp_size = 1;					\
   mpq_denref(temp)->_mp_d[0] = 1
diff -r c1537591cbd9 -r 3127bba7037a tests/cxx/t-ops2.cc
--- a/tests/cxx/t-ops2.cc	Tue Mar 15 19:22:57 2011 +0100
+++ b/tests/cxx/t-ops2.cc	Wed Mar 16 19:55:13 2011 +0100
@@ -136,6 +136,7 @@
   CHECK_ALL_SIGNS(T,11.,3,*);
   CHECK_ALL_SIGNS(T,11.,4,/);
   CHECK_SI(T,LONG_MIN,1,*);
+  CHECK_SI(T,0,3,*);
   CHECK_ALL_COMPARISONS(T,5.,2);
   CHECK_ALL_SIGNS_COMPARISONS(T,11.,3);
   CHECK_MPZ(T,5,-2,<); 


More information about the gmp-commit mailing list