[Gmp-commit] /var/hg/gmp: Optimize 1/q to call mpq_inv.

mercurial at gmplib.org mercurial at gmplib.org
Fri Oct 28 19:51:35 UTC 2016


details:   /var/hg/gmp/rev/5766d2c5e2b9
changeset: 17084:5766d2c5e2b9
user:      Marc Glisse <marc.glisse at inria.fr>
date:      Fri Oct 28 21:51:33 2016 +0200
description:
Optimize 1/q to call mpq_inv.

Also handle (-1)/q and 0/q. We could test for l == 1 at runtime and not just with __builtin_constant_p, it might be worth it, especially for non-gcc compilers.

diffstat:

 ChangeLog          |   5 +++++
 gmpxx.h            |  29 +++++++++++++++++++++++++++--
 tests/cxx/t-ops.cc |  10 ++++++++++
 3 files changed, 42 insertions(+), 2 deletions(-)

diffs (78 lines):

diff -r d2b8c42756bb -r 5766d2c5e2b9 ChangeLog
--- a/ChangeLog	Sat Oct 15 14:07:20 2016 +0200
+++ b/ChangeLog	Fri Oct 28 21:51:33 2016 +0200
@@ -1,3 +1,8 @@
+2016-10-28  Marc Glisse  <marc.glisse at inria.fr>
+
+	* gmpxx.h (__gmp_binary_divides): Let 1/q call mpq_inv.
+	* tests/cxx/t-ops.cc (check_mpq): Test it.
+
 2016-08-29  Torbjörn Granlund  <tg at gmplib.org>
 
 	* All C files: Include just gmp-impl.h, make gmp-impl.h grab gmp.h.
diff -r d2b8c42756bb -r 5766d2c5e2b9 gmpxx.h
--- a/gmpxx.h	Sat Oct 15 14:07:20 2016 +0200
+++ b/gmpxx.h	Fri Oct 28 21:51:33 2016 +0200
@@ -689,7 +689,17 @@
     }
   }
   static void eval(mpq_ptr q, unsigned long int l, mpq_srcptr r)
-  {  __GMPXX_TMPQ_UI;   mpq_div (q, temp, r); }
+  {
+    if (__GMPXX_CONSTANT_TRUE(l == 0))
+      mpq_set_ui(q, 0, 1);
+    else if (__GMPXX_CONSTANT_TRUE(l == 1))
+      mpq_inv(q, r);
+    else
+      {
+	__GMPXX_TMPQ_UI;
+	mpq_div (q, temp, r);
+      }
+  }
   static void eval(mpq_ptr q, mpq_srcptr r, signed long int l)
   {
     if (__GMPXX_CONSTANT_TRUE(l >= 0))
@@ -706,7 +716,22 @@
       }
   }
   static void eval(mpq_ptr q, signed long int l, mpq_srcptr r)
-  {  __GMPXX_TMPQ_SI;   mpq_div (q, temp, r); }
+  {
+    if (__GMPXX_CONSTANT_TRUE(l == 0))
+      mpq_set_ui(q, 0, 1);
+    else if (__GMPXX_CONSTANT_TRUE(l == 1))
+      mpq_inv(q, r);
+    else if (__GMPXX_CONSTANT_TRUE(l == -1))
+      {
+	mpq_inv(q, r);
+	mpq_neg(q, q);
+      }
+    else
+      {
+	__GMPXX_TMPQ_SI;
+	mpq_div (q, temp, r);
+      }
+  }
   static void eval(mpq_ptr q, mpq_srcptr r, double d)
   {  __GMPXX_TMPQ_D;    mpq_div (q, r, temp); }
   static void eval(mpq_ptr q, double d, mpq_srcptr r)
diff -r d2b8c42756bb -r 5766d2c5e2b9 tests/cxx/t-ops.cc
--- a/tests/cxx/t-ops.cc	Sat Oct 15 14:07:20 2016 +0200
+++ b/tests/cxx/t-ops.cc	Fri Oct 28 21:51:33 2016 +0200
@@ -502,6 +502,16 @@
     mpq_class c;
     c = a / b; ASSERT_ALWAYS(c == 0.75);
   }
+  {
+    mpq_class one = 1;
+    mpq_class x(2, 5);
+    ASSERT_ALWAYS(1 / x == one / x);
+    ASSERT_ALWAYS(1u / x == one / x);
+    x = (-1) / x;
+    ASSERT_ALWAYS(x == -2.5);
+    ASSERT_ALWAYS(0 / x == 0);
+    ASSERT_ALWAYS(0u / x == 0);
+  }
 
   // operator<<
   // operator>>


More information about the gmp-commit mailing list