[Gmp-commit] /var/hg/gmp: Remove a temporary in expressions like q=q*q+z*z: t...

mercurial at gmplib.org mercurial at gmplib.org
Sat Feb 18 21:16:37 CET 2012


details:   /var/hg/gmp/rev/13f6af182a66
changeset: 14643:13f6af182a66
user:      Marc Glisse <marc.glisse at inria.fr>
date:      Sat Feb 18 21:16:26 2012 +0100
description:
Remove a temporary in expressions like q=q*q+z*z: tmp=z*z, q=q*q, q+=tmp.
Note that we could do the same for q=q*q+z, but that would require checking if z is the numerator or denominator of q.

diffstat:

 ChangeLog             |   4 ++++
 gmpxx.h               |  24 ++++++++++++------------
 tests/cxx/t-binary.cc |   4 ++++
 3 files changed, 20 insertions(+), 12 deletions(-)

diffs (125 lines):

diff -r 65e411ed6ca0 -r 13f6af182a66 ChangeLog
--- a/ChangeLog	Sat Feb 18 14:11:51 2012 +0100
+++ b/ChangeLog	Sat Feb 18 21:16:26 2012 +0100
@@ -5,6 +5,10 @@
 
 	* gmpxx.h: Don't declare long double functions that are never defined.
 
+	* gmpxx.h (__gmp_binary_expr): Let things happen in place: q=q*q+z*z
+	becomes tmp=z*z, q=q*q, q+=tmp.
+	* tests/cxx/t-binary.cc: More variable reuse tests.
+
 2012-02-17  Marc Glisse  <marc.glisse at inria.fr>
 
 	* gmp-h.in (__GMP_WITHIN_GMP): Test with #ifdef instead of #if, for
diff -r 65e411ed6ca0 -r 13f6af182a66 gmpxx.h
--- a/gmpxx.h	Sat Feb 18 14:11:51 2012 +0100
+++ b/gmpxx.h	Sat Feb 18 21:16:26 2012 +0100
@@ -2576,7 +2576,7 @@
   { eval_fun::eval(q, expr.val1.get_mpz_t(), expr.val2.get_mpq_t()); }      \
   const val1_type & get_val1() const { return expr.val1; }                  \
   const val2_type & get_val2() const { return expr.val2; }                  \
-  mp_bitcnt_t get_prec() const { return mpf_get_default_prec(); }     \
+  mp_bitcnt_t get_prec() const { return mpf_get_default_prec(); }           \
 };                                                                          \
                                                                             \
 template <>                                                                 \
@@ -2594,7 +2594,7 @@
   { eval_fun::eval(q, expr.val1.get_mpq_t(), expr.val2.get_mpz_t()); }      \
   const val1_type & get_val1() const { return expr.val1; }                  \
   const val2_type & get_val2() const { return expr.val2; }                  \
-  mp_bitcnt_t get_prec() const { return mpf_get_default_prec(); }     \
+  mp_bitcnt_t get_prec() const { return mpf_get_default_prec(); }           \
 };                                                                          \
                                                                             \
 template <class T>                                                          \
@@ -2616,7 +2616,7 @@
   }                                                                         \
   const val1_type & get_val1() const { return expr.val1; }                  \
   const val2_type & get_val2() const { return expr.val2; }                  \
-  mp_bitcnt_t get_prec() const { return mpf_get_default_prec(); }     \
+  mp_bitcnt_t get_prec() const { return mpf_get_default_prec(); }           \
 };                                                                          \
                                                                             \
 template <class T>                                                          \
@@ -2638,7 +2638,7 @@
   }                                                                         \
   const val1_type & get_val1() const { return expr.val1; }                  \
   const val2_type & get_val2() const { return expr.val2; }                  \
-  mp_bitcnt_t get_prec() const { return mpf_get_default_prec(); }     \
+  mp_bitcnt_t get_prec() const { return mpf_get_default_prec(); }           \
 };                                                                          \
                                                                             \
 template <class T>                                                          \
@@ -2660,7 +2660,7 @@
   }                                                                         \
   const val1_type & get_val1() const { return expr.val1; }                  \
   const val2_type & get_val2() const { return expr.val2; }                  \
-  mp_bitcnt_t get_prec() const { return mpf_get_default_prec(); }     \
+  mp_bitcnt_t get_prec() const { return mpf_get_default_prec(); }           \
 };                                                                          \
                                                                             \
 template <class T>                                                          \
@@ -2682,7 +2682,7 @@
   }                                                                         \
   const val1_type & get_val1() const { return expr.val1; }                  \
   const val2_type & get_val2() const { return expr.val2; }                  \
-  mp_bitcnt_t get_prec() const { return mpf_get_default_prec(); }     \
+  mp_bitcnt_t get_prec() const { return mpf_get_default_prec(); }           \
 };                                                                          \
                                                                             \
 template <class T, class U>                                                 \
@@ -2700,12 +2700,12 @@
   void eval(mpq_ptr q) const                                                \
   {                                                                         \
     mpz_class temp1(expr.val1);                                             \
-    mpq_class temp2(expr.val2);                                             \
-    eval_fun::eval(q, temp1.get_mpz_t(), temp2.get_mpq_t());                \
+    expr.val2.eval(q);                                                      \
+    eval_fun::eval(q, temp1.get_mpz_t(), q);                                \
   }                                                                         \
   const val1_type & get_val1() const { return expr.val1; }                  \
   const val2_type & get_val2() const { return expr.val2; }                  \
-  mp_bitcnt_t get_prec() const { return mpf_get_default_prec(); }     \
+  mp_bitcnt_t get_prec() const { return mpf_get_default_prec(); }           \
 };                                                                          \
                                                                             \
 template <class T, class U>                                                 \
@@ -2722,13 +2722,13 @@
     : expr(val1, val2) { }                                                  \
   void eval(mpq_ptr q) const                                                \
   {                                                                         \
-    mpq_class temp1(expr.val1);                                             \
     mpz_class temp2(expr.val2);                                             \
-    eval_fun::eval(q, temp1.get_mpq_t(), temp2.get_mpz_t());                \
+    expr.val1.eval(q);                                             \
+    eval_fun::eval(q, q, temp2.get_mpz_t());                \
   }                                                                         \
   const val1_type & get_val1() const { return expr.val1; }                  \
   const val2_type & get_val2() const { return expr.val2; }                  \
-  mp_bitcnt_t get_prec() const { return mpf_get_default_prec(); }     \
+  mp_bitcnt_t get_prec() const { return mpf_get_default_prec(); }           \
 };
 
 
diff -r 65e411ed6ca0 -r 13f6af182a66 tests/cxx/t-binary.cc
--- a/tests/cxx/t-binary.cc	Sat Feb 18 14:11:51 2012 +0100
+++ b/tests/cxx/t-binary.cc	Sat Feb 18 21:16:26 2012 +0100
@@ -278,6 +278,9 @@
     mpz_class c(1);
     mpq_class d((a + b) - c); ASSERT_ALWAYS(d == -0.25);
     d = (a + d) - c; ASSERT_ALWAYS(d == -0.75);
+    d = (a + d) - d.get_num(); ASSERT_ALWAYS(d == 2.75);
+    d = (2 * d) * d.get_den(); ASSERT_ALWAYS(d == 22);
+    d = (b * d) / -d.get_num(); ASSERT_ALWAYS(d == -0.25);
   }
   {
     mpq_class a(1, 3), b(3, 2);
@@ -300,6 +303,7 @@
     signed int d = 4;
     mpq_class e;
     e = (c % d) / (a * b); ASSERT_ALWAYS(e == 10);
+    e = (e.get_num() % d) / (2 / e); ASSERT_ALWAYS(e == 10);
   }
 
   // template <class T, class U, class V, class Op>


More information about the gmp-commit mailing list