[Gmp-commit] /var/hg/gmp: mini-gmp: Simplified mpz_cmp_d.

mercurial at gmplib.org mercurial at gmplib.org
Tue Jan 22 21:34:41 CET 2013


details:   /var/hg/gmp/rev/caefcb543afd
changeset: 15339:caefcb543afd
user:      Niels M?ller <nisse at lysator.liu.se>
date:      Tue Jan 22 21:34:36 2013 +0100
description:
mini-gmp: Simplified mpz_cmp_d.

diffstat:

 ChangeLog           |   3 +++
 mini-gmp/mini-gmp.c |  53 ++++++++++++-----------------------------------------
 2 files changed, 15 insertions(+), 41 deletions(-)

diffs (103 lines):

diff -r 2d7ecf5a6459 -r caefcb543afd ChangeLog
--- a/ChangeLog	Tue Jan 22 21:21:39 2013 +0100
+++ b/ChangeLog	Tue Jan 22 21:34:36 2013 +0100
@@ -1,5 +1,8 @@
 2013-01-22  Niels Möller  <nisse at lysator.liu.se>
 
+	* mini-gmp/mini-gmp.c (mpz_cmp_d): Simplified, just sort out
+	signs, then call mpz_cmpabs_d.
+
 	* mini-gmp/tests/testutils.h: Include stdio.h and stdlib.h.
 	(numberof): New define.
 
diff -r 2d7ecf5a6459 -r caefcb543afd mini-gmp/mini-gmp.c
--- a/mini-gmp/mini-gmp.c	Tue Jan 22 21:21:39 2013 +0100
+++ b/mini-gmp/mini-gmp.c	Tue Jan 22 21:34:36 2013 +0100
@@ -1572,7 +1572,7 @@
   if (d >= B)
     return -1;
 
-  /* Subtract x from d, one limb at a time. */
+  /* Compare floor(d) to top limb, subtract and cancel when equal. */
   for (i = xn; i-- > 0;)
     {
       mp_limb_t f, xl;
@@ -1589,22 +1589,19 @@
   if (d > 0)
     return -1;
   else
-    return 0;  
+    return 0;
 }
 
 int
 mpz_cmp_d (const mpz_t x, double d)
 {
   mp_size_t xn = x->_mp_size;
-  int sign;
-  double B, Bi;
-  mp_size_t i;
 
   if (xn == 0)
     {
-      if (d < 0)
+      if (d < 0.0)
 	return 1;
-      else if (d > 0)
+      else if (d > 0.0)
 	return -1;
       else
 	return 0;
@@ -1612,44 +1609,18 @@
 
   if (xn < 0)
     {
-      xn = -xn;
-      d = -d;
-      sign = -1;
+      if (d >= 0.0)
+	return -1;
+      else
+	return -mpz_cmpabs_d (x, d);
     }
   else
-    sign = 1;
-
-  if (d < 1.0)
-    return sign;
-
-  B = 2.0 * (double) GMP_LIMB_HIGHBIT;
-  Bi = 1.0 / B;
-  for (i = 1; i < xn; i++)
     {
-      d *= Bi;
-      if (d < 1.0)
-	return sign;
+      if (d <= 0.0)
+	return 1;
+      else
+	return mpz_cmpabs_d (x, d);
     }
-  if (d >= B)
-    return -sign;
-
-  for (i = xn; i-- > 0; i)
-    {
-      mp_limb_t f, xl;
-
-      f = (mp_limb_t) d;
-      xl = x->_mp_d[i];
-      if (xl > f)
-	return sign;
-      else if (xl < f)
-	return -sign;
-      d = B * (d - f);
-    }
-
-  if (d > 0)
-    return -sign;
-  else
-    return 0;
 }
 
 


More information about the gmp-commit mailing list