[Gmp-commit] /var/hg/gmp: (ABS_CAST): New macro.

mercurial at gmplib.org mercurial at gmplib.org
Sat Feb 11 12:52:08 CET 2012


details:   /var/hg/gmp/rev/19d7b40269eb
changeset: 14621:19d7b40269eb
user:      Torbjorn Granlund <tege at gmplib.org>
date:      Sat Feb 11 12:51:21 2012 +0100
description:
(ABS_CAST): New macro.

diffstat:

 ChangeLog     |  14 ++++++++++++++
 gmp-impl.h    |   1 +
 mpf/cmp_si.c  |  11 ++++++-----
 mpf/get_si.c  |   2 +-
 mpf/iset_si.c |   2 +-
 mpf/set_si.c  |   2 +-
 mpq/set_si.c  |   2 +-
 mpz/cmp_si.c  |   7 ++++---
 mpz/get_si.c  |   4 ++--
 mpz/iset_si.c |   2 +-
 mpz/mul_i.h   |   2 +-
 mpz/set_si.c  |   2 +-
 12 files changed, 34 insertions(+), 17 deletions(-)

diffs (206 lines):

diff -r 422bb4bc9ce9 -r 19d7b40269eb ChangeLog
--- a/ChangeLog	Sat Feb 11 10:57:57 2012 +0100
+++ b/ChangeLog	Sat Feb 11 12:51:21 2012 +0100
@@ -13,6 +13,20 @@
 	* gmp-impl.h (mpz_oddfac_1): Update signature.
 	* mpz/fac_ui.c (mpz_fac_ui): Update call to mpz_oddfac_1.
 
+2012-02-09  Marc Glisse  <marc.glisse at inria.fr>
+
+	* gmp-impl.h (ABS_CAST): New macro.
+	* mpf/cmp_si.c: Use ABS_CAST.
+	* mpf/get_si.c: Use ABS_CAST.
+	* mpf/iset_si.c: Use ABS_CAST.
+	* mpf/set_si.c: Use ABS_CAST.
+	* mpq/set_si.c: Use ABS_CAST.
+	* mpz/cmp_si.c: Use ABS_CAST.
+	* mpz/get_si.c: Use ABS_CAST.
+	* mpz/iset_si.c: Use ABS_CAST.
+	* mpz/mul_i.h: Use ABS_CAST.
+	* mpz/set_si.c: Use ABS_CAST.
+
 2012-02-08  Torbjorn Granlund  <tege at gmplib.org>
 
 	* mpn/powerpc32/divrem_2.asm: Fix off-by-one condition in invert_limb
diff -r 422bb4bc9ce9 -r 19d7b40269eb gmp-impl.h
--- a/gmp-impl.h	Sat Feb 11 10:57:57 2012 +0100
+++ b/gmp-impl.h	Sat Feb 11 12:51:21 2012 +0100
@@ -498,6 +498,7 @@
 #define UNLIKELY(cond)                 __GMP_UNLIKELY(cond)
 
 #define ABS(x) ((x) >= 0 ? (x) : -(x))
+#define ABS_CAST(T,x) ((x) >= 0 ? (T)(x) : -((T)((x) + 1) - 1))
 #undef MIN
 #define MIN(l,o) ((l) < (o) ? (l) : (o))
 #undef MAX
diff -r 422bb4bc9ce9 -r 19d7b40269eb mpf/cmp_si.c
--- a/mpf/cmp_si.c	Sat Feb 11 10:57:57 2012 +0100
+++ b/mpf/cmp_si.c	Sat Feb 11 12:51:21 2012 +0100
@@ -29,6 +29,7 @@
   mp_exp_t uexp;
   mp_limb_t ulimb;
   int usign;
+  unsigned long abs_vval;
 
   uexp = u->_mp_exp;
   usize = u->_mp_size;
@@ -55,13 +56,13 @@
 
   usign = usize >= 0 ? 1 : -1;
   usize = ABS (usize);
-  vval = ABS (vval);
+  abs_vval = ABS_CAST (unsigned long, vval);
 
   /* 2. Are the exponents different (V's exponent == 1)?  */
 #if GMP_NAIL_BITS != 0
-  if (uexp > 1 + ((unsigned long) vval > GMP_NUMB_MAX))
+  if (uexp > 1 + (abs_vval > GMP_NUMB_MAX))
     return usign;
-  if (uexp < 1 + ((unsigned long) vval > GMP_NUMB_MAX))
+  if (uexp < 1 + (abs_vval > GMP_NUMB_MAX))
     return -usign;
 #else
   if (uexp > 1)
@@ -85,9 +86,9 @@
   usize--;
 
   /* 3. Compare the most significant mantissa limb with V.  */
-  if (ulimb > (unsigned long) vval)
+  if (ulimb > abs_vval)
     return usign;
-  else if (ulimb < (unsigned long) vval)
+  else if (ulimb < abs_vval)
     return -usign;
 
   /* Ignore zeroes at the low end of U.  */
diff -r 422bb4bc9ce9 -r 19d7b40269eb mpf/get_si.c
--- a/mpf/get_si.c	Sat Feb 11 10:57:57 2012 +0100
+++ b/mpf/get_si.c	Sat Feb 11 12:51:21 2012 +0100
@@ -72,5 +72,5 @@
     return fl & LONG_MAX;
   else
     /* this form necessary to correctly handle -0x80..00 */
-    return ~ ((fl - 1) & LONG_MAX);
+    return -1 - (long) ((fl - 1) & LONG_MAX);
 }
diff -r 422bb4bc9ce9 -r 19d7b40269eb mpf/iset_si.c
--- a/mpf/iset_si.c	Sat Feb 11 10:57:57 2012 +0100
+++ b/mpf/iset_si.c	Sat Feb 11 12:51:21 2012 +0100
@@ -31,7 +31,7 @@
   r->_mp_prec = prec;
   r->_mp_d = (mp_ptr) (*__gmp_allocate_func) ((prec + 1) * BYTES_PER_MP_LIMB);
 
-  vl = (mp_limb_t) (unsigned long int) (val >= 0 ? val : -val);
+  vl = (mp_limb_t) ABS_CAST (unsigned long int, val);
 
   r->_mp_d[0] = vl & GMP_NUMB_MASK;
   size = vl != 0;
diff -r 422bb4bc9ce9 -r 19d7b40269eb mpf/set_si.c
--- a/mpf/set_si.c	Sat Feb 11 10:57:57 2012 +0100
+++ b/mpf/set_si.c	Sat Feb 11 12:51:21 2012 +0100
@@ -27,7 +27,7 @@
   mp_size_t size;
   mp_limb_t vl;
 
-  vl = (mp_limb_t) (unsigned long int) (val >= 0 ? val : -val);
+  vl = (mp_limb_t) ABS_CAST (unsigned long int, val);
 
   dest->_mp_d[0] = vl & GMP_NUMB_MASK;
   size = vl != 0;
diff -r 422bb4bc9ce9 -r 19d7b40269eb mpq/set_si.c
--- a/mpq/set_si.c	Sat Feb 11 10:57:57 2012 +0100
+++ b/mpq/set_si.c	Sat Feb 11 12:51:21 2012 +0100
@@ -35,7 +35,7 @@
       return;
     }
 
-  abs_num = ABS (num);
+  abs_num = ABS_CAST (unsigned long, num);
 
   if (num == 0)
     {
diff -r 422bb4bc9ce9 -r 19d7b40269eb mpz/cmp_si.c
--- a/mpz/cmp_si.c	Sat Feb 11 10:57:57 2012 +0100
+++ b/mpz/cmp_si.c	Sat Feb 11 12:51:21 2012 +0100
@@ -28,6 +28,7 @@
   mp_size_t usize = u->_mp_size;
   mp_size_t vsize;
   mp_limb_t u_digit;
+  unsigned long int absv_digit = (unsigned long int) v_digit;
 
 #if GMP_NAIL_BITS != 0
   /* FIXME.  This isn't very pretty.  */
@@ -45,7 +46,7 @@
   else if (v_digit < 0)
     {
       vsize = -1;
-      v_digit = -v_digit;
+      absv_digit = -absv_digit;
     }
 
   if (usize != vsize)
@@ -56,10 +57,10 @@
 
   u_digit = u->_mp_d[0];
 
-  if (u_digit == (mp_limb_t) (unsigned long) v_digit)
+  if (u_digit == (mp_limb_t) absv_digit)
     return 0;
 
-  if (u_digit > (mp_limb_t) (unsigned long) v_digit)
+  if (u_digit > (mp_limb_t) absv_digit)
     return usize;
   else
     return -usize;
diff -r 422bb4bc9ce9 -r 19d7b40269eb mpz/get_si.c
--- a/mpz/get_si.c	Sat Feb 11 10:57:57 2012 +0100
+++ b/mpz/get_si.c	Sat Feb 11 12:51:21 2012 +0100
@@ -34,10 +34,10 @@
 #endif
 
   if (size > 0)
-    return (long) zl & LONG_MAX;
+    return zl & LONG_MAX;
   else if (size < 0)
     /* This expression is necessary to properly handle 0x80000000 */
-    return ~(((long) zl - 1L) & LONG_MAX);
+    return -1 - (long) ((zl - 1) & LONG_MAX);
   else
     return 0;
 }
diff -r 422bb4bc9ce9 -r 19d7b40269eb mpz/iset_si.c
--- a/mpz/iset_si.c	Sat Feb 11 10:57:57 2012 +0100
+++ b/mpz/iset_si.c	Sat Feb 11 12:51:21 2012 +0100
@@ -31,7 +31,7 @@
   dest->_mp_alloc = 1;
   dest->_mp_d = (mp_ptr) (*__gmp_allocate_func) (BYTES_PER_MP_LIMB);
 
-  vl = (mp_limb_t) (unsigned long int) (val >= 0 ? val : -val);
+  vl = (mp_limb_t) ABS_CAST (unsigned long int, val);
 
   dest->_mp_d[0] = vl & GMP_NUMB_MASK;
   size = vl != 0;
diff -r 422bb4bc9ce9 -r 19d7b40269eb mpz/mul_i.h
--- a/mpz/mul_i.h	Sat Feb 11 10:57:57 2012 +0100
+++ b/mpz/mul_i.h	Sat Feb 11 12:51:21 2012 +0100
@@ -26,7 +26,7 @@
 #ifdef OPERATION_mul_si
 #define FUNCTION               mpz_mul_si
 #define MULTIPLICAND_UNSIGNED
-#define MULTIPLICAND_ABS(x)    ((unsigned long) ABS(x))
+#define MULTIPLICAND_ABS(x)    ABS_CAST(unsigned long, (x))
 #endif
 
 #ifdef OPERATION_mul_ui
diff -r 422bb4bc9ce9 -r 19d7b40269eb mpz/set_si.c
--- a/mpz/set_si.c	Sat Feb 11 10:57:57 2012 +0100
+++ b/mpz/set_si.c	Sat Feb 11 12:51:21 2012 +0100
@@ -27,7 +27,7 @@
   mp_size_t size;
   mp_limb_t vl;
 
-  vl = (mp_limb_t) (unsigned long int) (val >= 0 ? val : -val);
+  vl = (mp_limb_t) ABS_CAST (unsigned long int, val);
 
   dest->_mp_d[0] = vl & GMP_NUMB_MASK;
   size = vl != 0;


More information about the gmp-commit mailing list