[Gmp-commit] /var/hg/gmp: Replace e=a*b-c*d with tmp=c*d, e=a*b, e-=tmp, whic...

mercurial at gmplib.org mercurial at gmplib.org
Thu Feb 16 21:41:47 CET 2012


details:   /var/hg/gmp/rev/7e4f37685265
changeset: 14639:7e4f37685265
user:      Marc Glisse <marc.glisse at inria.fr>
date:      Thu Feb 16 21:41:39 2012 +0100
description:
Replace e=a*b-c*d with tmp=c*d, e=a*b, e-=tmp, which uses one less temporary.

diffstat:

 ChangeLog             |   6 ++++++
 gmpxx.h               |  15 +++++++++------
 tests/cxx/t-binary.cc |   8 +++++++-
 3 files changed, 22 insertions(+), 7 deletions(-)

diffs (96 lines):

diff -r c4caec4dbfb2 -r 7e4f37685265 ChangeLog
--- a/ChangeLog	Thu Feb 16 14:31:42 2012 +0100
+++ b/ChangeLog	Thu Feb 16 21:41:39 2012 +0100
@@ -1,3 +1,9 @@
+2012-02-16  Marc Glisse  <marc.glisse at inria.fr>
+
+	* gmpxx.h (__gmp_binary_expr): Let things happen in place: e=a*b-c*d
+	becomes tmp=c*d, e=a*b, e-=tmp.
+	* tests/cxx/t-binary.cc: More variable reuse tests.
+
 2012-02-15  Niels Möller  <nisse at lysator.liu.se>
 
 	* tune/tuneup.c (mul_toom43_to_toom54_threshold): New global.
diff -r c4caec4dbfb2 -r 7e4f37685265 gmpxx.h
--- a/gmpxx.h	Thu Feb 16 14:31:42 2012 +0100
+++ b/gmpxx.h	Thu Feb 16 21:41:39 2012 +0100
@@ -2441,8 +2441,9 @@
     : expr(val1, val2) { }
   void eval(typename __gmp_resolve_expr<T>::ptr_type p) const
   {
-    __gmp_expr<T, T> temp1(expr.val1), temp2(expr.val2);
-    Op::eval(p, temp1.__get_mp(), temp2.__get_mp());
+    __gmp_expr<T, T> temp2(expr.val2);
+    expr.val1.eval(p);
+    Op::eval(p, p, temp2.__get_mp());
   }
   void eval(typename __gmp_resolve_expr<T>::ptr_type p,
 	    mp_bitcnt_t prec) const
@@ -2474,8 +2475,9 @@
     : expr(val1, val2) { }
   void eval(typename __gmp_resolve_expr<T>::ptr_type p) const
   {
-    __gmp_expr<T, T> temp1(expr.val1), temp2(expr.val2);
-    Op::eval(p, temp1.__get_mp(), temp2.__get_mp());
+    __gmp_expr<T, T> temp1(expr.val1);
+    expr.val2.eval(p);
+    Op::eval(p, temp1.__get_mp(), p);
   }
   void eval(typename __gmp_resolve_expr<T>::ptr_type p,
 	    mp_bitcnt_t prec) const
@@ -2507,8 +2509,9 @@
     : expr(val1, val2) { }
   void eval(typename __gmp_resolve_expr<T>::ptr_type p) const
   {
-    __gmp_expr<T, T> temp1(expr.val1), temp2(expr.val2);
-    Op::eval(p, temp1.__get_mp(), temp2.__get_mp());
+    __gmp_expr<T, T> temp2(expr.val2);
+    expr.val1.eval(p);
+    Op::eval(p, p, temp2.__get_mp());
   }
   void eval(typename __gmp_resolve_expr<T>::ptr_type p,
 	    mp_bitcnt_t prec) const
diff -r c4caec4dbfb2 -r 7e4f37685265 tests/cxx/t-binary.cc
--- a/tests/cxx/t-binary.cc	Thu Feb 16 14:31:42 2012 +0100
+++ b/tests/cxx/t-binary.cc	Thu Feb 16 21:41:39 2012 +0100
@@ -1,6 +1,6 @@
 /* Test mp*_class binary expressions.
 
-Copyright 2001, 2002, 2003, 2008 Free Software Foundation, Inc.
+Copyright 2001, 2002, 2003, 2008, 2012 Free Software Foundation, Inc.
 
 This file is part of the GNU MP Library.
 
@@ -148,6 +148,8 @@
     mpz_class a(3), b(5), c(7);
     mpz_class d;
     d = (a - b) * (-c); ASSERT_ALWAYS(d == 14);
+    d = (b - d) * (-a); ASSERT_ALWAYS(d == 27);
+    d = (a - b) * (-d); ASSERT_ALWAYS(d == 54);
   }
 
   {
@@ -275,12 +277,14 @@
     mpq_class a(1, 2), b(1, 4);
     mpz_class c(1);
     mpq_class d((a + b) - c); ASSERT_ALWAYS(d == -0.25);
+    d = (a + d) - c; ASSERT_ALWAYS(d == -0.75);
   }
   {
     mpq_class a(1, 3), b(3, 2);
     mpz_class c(2), d(4);
     mpq_class e;
     e = (a * b) / (c - d); ASSERT_ALWAYS(e == -0.25);
+    e = (2 * e) / (c - d); ASSERT_ALWAYS(e ==  0.25);
   }
 
   // template <class T, class U, class V, class W, class Op>
@@ -304,6 +308,8 @@
     mpq_class a(1, 3), b(3, 4), c(2, 5);
     mpq_class d;
     d = (a * b) / (-c); ASSERT_ALWAYS(d == -0.625);
+    d = (c * d) / (-b); ASSERT_ALWAYS(3 * d == 1);
+    d = (a * c) / (-d); ASSERT_ALWAYS(5 * d == -2);
   }
 }
 


More information about the gmp-commit mailing list