[Gmp-commit] /var/hg/gmp: Use __builtin_constant_p in mpz_class::init_* and m...

mercurial at gmplib.org mercurial at gmplib.org
Fri Feb 15 14:50:50 CET 2013


details:   /var/hg/gmp/rev/538dbbb66bb1
changeset: 15439:538dbbb66bb1
user:      Marc Glisse <marc.glisse at inria.fr>
date:      Fri Feb 15 14:49:43 2013 +0100
description:
Use __builtin_constant_p in mpz_class::init_* and mpz_class::assign_*.
Introduce macro __GMPXX_CONSTANT_TRUE (to be removed if it makes its way
into gmp.h).

diffstat:

 ChangeLog |   1 +
 gmpxx.h   |  56 +++++++++++++++++++++++++++++++++++++++++++++++++-------
 2 files changed, 50 insertions(+), 7 deletions(-)

diffs (91 lines):

diff -r cdd4f0b22589 -r 538dbbb66bb1 ChangeLog
--- a/ChangeLog	Fri Feb 15 14:02:02 2013 +0100
+++ b/ChangeLog	Fri Feb 15 14:49:43 2013 +0100
@@ -5,6 +5,7 @@
 	(mpz_class, mpq_class, mpf_class) [init_ui, init_si, init_d,
 	assign_ui, assign_si, assign_d]: New functions.
 	(__gmp_expr::__gmp_expr, __gmp_expr::operator=): Replace with macros.
+	(__GMPXX_CONSTANT_TRUE): New macro.
 
 2013-02-15 Marco Bodrato <bodrato at mail.dm.unipi.it>
 
diff -r cdd4f0b22589 -r 538dbbb66bb1 gmpxx.h
--- a/gmpxx.h	Fri Feb 15 14:02:02 2013 +0100
+++ b/gmpxx.h	Fri Feb 15 14:49:43 2013 +0100
@@ -39,6 +39,7 @@
 #else
 #define __GMPXX_CONSTANT(X) false
 #endif
+#define __GMPXX_CONSTANT_TRUE(X) (__GMPXX_CONSTANT(X) && (X))
 
 // Use C++11 features
 #ifndef __GMPXX_USE_CXX11
@@ -1425,13 +1426,53 @@
   value_type mp;
 
   // Helper functions used for all arithmetic types
-  void assign_ui(unsigned long l) { mpz_set_ui(mp, l); }
-  void assign_si(signed long l)   { mpz_set_si(mp, l); }
-  void assign_d (double d)        { mpz_set_d (mp, d); }
-
-  void init_ui(unsigned long l)	{ mpz_init_set_ui(mp, l); }
-  void init_si(signed long l)	{ mpz_init_set_si(mp, l); }
-  void init_d (double d)	{ mpz_init_set_d (mp, d); }
+  void assign_ui(unsigned long l)
+  {
+    if (__GMPXX_CONSTANT_TRUE(l == 0))
+      mp->_mp_size = 0;
+    else
+      mpz_set_ui(mp, l);
+  }
+  void assign_si(signed long l)
+  {
+    if (__GMPXX_CONSTANT_TRUE(l >= 0))
+      assign_ui(l);
+    else if (__GMPXX_CONSTANT_TRUE(l <= 0))
+      {
+	assign_ui(-static_cast<unsigned long>(l));
+	mpz_neg(mp, mp);
+      }
+    else
+      mpz_set_si(mp, l);
+  }
+  void assign_d (double d)
+  {
+    mpz_set_d (mp, d);
+  }
+
+  void init_ui(unsigned long l)
+  {
+    if (__GMPXX_CONSTANT_TRUE(l == 0))
+      mpz_init(mp);
+    else
+      mpz_init_set_ui(mp, l);
+  }
+  void init_si(signed long l)
+  {
+    if (__GMPXX_CONSTANT_TRUE(l >= 0))
+      init_ui(l);
+    else if (__GMPXX_CONSTANT_TRUE(l <= 0))
+      {
+	init_ui(-static_cast<unsigned long>(l));
+	mpz_neg(mp, mp);
+      }
+    else
+      mpz_init_set_si(mp, l);
+  }
+  void init_d (double d)
+  {
+    mpz_init_set_d (mp, d);
+  }
 
 public:
   mp_bitcnt_t get_prec() const { return mpf_get_default_prec(); }
@@ -3337,6 +3378,7 @@
 #undef __GMPQ_DEFINE_INCREMENT_OPERATOR
 #undef __GMPF_DEFINE_INCREMENT_OPERATOR
 
+#undef __GMPXX_CONSTANT_TRUE
 #undef __GMPXX_CONSTANT
 
 #endif /* __GMP_PLUSPLUS__ */


More information about the gmp-commit mailing list