[Gmp-commit] /var/hg/gmp: mini-gmp: New macro GMP_CMP, simplify comparison fu...

mercurial at gmplib.org mercurial at gmplib.org
Wed Nov 23 20:32:48 UTC 2016


details:   /var/hg/gmp/rev/c76021c9e885
changeset: 17132:c76021c9e885
user:      Niels M?ller <nisse at lysator.liu.se>
date:      Wed Nov 23 21:32:15 2016 +0100
description:
mini-gmp: New macro GMP_CMP, simplify comparison functions.

diffstat:

 mini-gmp/ChangeLog  |   5 +++++
 mini-gmp/mini-gmp.c |  30 ++++++++----------------------
 2 files changed, 13 insertions(+), 22 deletions(-)

diffs (81 lines):

diff -r ca07af6e95ac -r c76021c9e885 mini-gmp/ChangeLog
--- a/mini-gmp/ChangeLog	Wed Nov 23 10:25:13 2016 +0100
+++ b/mini-gmp/ChangeLog	Wed Nov 23 21:32:15 2016 +0100
@@ -1,3 +1,8 @@
+2016-11-23  Niels Möller  <nisse at lysator.liu.se>
+
+	* mini-gmp.c (GMP_CMP): New macro.
+	(mpz_sgn, mpz_cmp_si, mpz_cmp_ui, mpz_cmpabs_ui): Use it.
+
 2016-11-22  Niels Möller  <nisse at lysator.liu.se>
 
 	* tests/t-invert.c (test_3by2, test_2by1): New functions,
diff -r ca07af6e95ac -r c76021c9e885 mini-gmp/mini-gmp.c
--- a/mini-gmp/mini-gmp.c	Wed Nov 23 10:25:13 2016 +0100
+++ b/mini-gmp/mini-gmp.c	Wed Nov 23 21:32:15 2016 +0100
@@ -69,6 +69,8 @@
 #define GMP_MIN(a, b) ((a) < (b) ? (a) : (b))
 #define GMP_MAX(a, b) ((a) > (b) ? (a) : (b))
 
+#define GMP_CMP(a,b) (((a) > (b)) - ((a) < (b)))
+
 #define gmp_assert_nocarry(x) do { \
     mp_limb_t __cy = (x);	   \
     assert (__cy == 0);		   \
@@ -1777,9 +1779,7 @@
 int
 mpz_sgn (const mpz_t u)
 {
-  mp_size_t usize = u->_mp_size;
-
-  return (usize > 0) - (usize < 0);
+  return GMP_CMP (u->_mp_size, 0);
 }
 
 int
@@ -1794,13 +1794,7 @@
   else if (usize >= 0)
     return 1;
   else /* usize == -1 */
-    {
-      mp_limb_t ul = u->_mp_d[0];
-      if ((mp_limb_t)GMP_NEG_CAST (unsigned long int, v) < ul)
-	return -1;
-      else
-	return (mp_limb_t)GMP_NEG_CAST (unsigned long int, v) > ul;
-    }
+    return GMP_CMP (GMP_NEG_CAST (mp_limb_t, v), u->_mp_d[0]);
 }
 
 int
@@ -1813,10 +1807,7 @@
   else if (usize < 0)
     return -1;
   else
-    {
-      mp_limb_t ul = (usize > 0) ? u->_mp_d[0] : 0;
-      return (ul > v) - (ul < v);
-    }
+    return GMP_CMP (mpz_get_ui (u), v);
 }
 
 int
@@ -1836,15 +1827,10 @@
 int
 mpz_cmpabs_ui (const mpz_t u, unsigned long v)
 {
-  mp_size_t un = GMP_ABS (u->_mp_size);
-  mp_limb_t ul;
-
-  if (un > 1)
+  if (GMP_ABS (u->_mp_size) > 1)
     return 1;
-
-  ul = (un == 1) ? u->_mp_d[0] : 0;
-
-  return (ul > v) - (ul < v);
+  else
+    return GMP_CMP (mpz_get_ui (u), v);
 }
 
 int


More information about the gmp-commit mailing list