PATCH: Bad signed int to long long conversion

Marc Glisse marc.glisse at inria.fr
Sun Jan 29 13:10:06 CET 2012


On Thu, 1 Dec 2011, Torbjorn Granlund wrote:

> I don't mind that somebody patches up all occurences of signed negation
> where the smallest negative value could hypothetically appear.

I don't know about "all occurences", but if there aren't any objections I 
plan to commit the patch attached.

(I checked and gcc produces the same code for -1-(long)l as for (long)~l, 
even at -O0)

-- 
Marc Glisse
-------------- next part --------------
diff -r fba311ff3884 gmp-impl.h
--- a/gmp-impl.h	Sat Jan 28 22:35:44 2012 +0100
+++ b/gmp-impl.h	Sun Jan 29 12:57:42 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))
 #undef MIN
 #define MIN(l,o) ((l) < (o) ? (l) : (o))
 #undef MAX
diff -r fba311ff3884 mpf/cmp_si.c
--- a/mpf/cmp_si.c	Sat Jan 28 22:35:44 2012 +0100
+++ b/mpf/cmp_si.c	Sun Jan 29 12:57:42 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 fba311ff3884 mpf/get_si.c
--- a/mpf/get_si.c	Sat Jan 28 22:35:44 2012 +0100
+++ b/mpf/get_si.c	Sun Jan 29 12:57:42 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 fba311ff3884 mpf/iset_si.c
--- a/mpf/iset_si.c	Sat Jan 28 22:35:44 2012 +0100
+++ b/mpf/iset_si.c	Sun Jan 29 12:57:42 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 fba311ff3884 mpf/set_si.c
--- a/mpf/set_si.c	Sat Jan 28 22:35:44 2012 +0100
+++ b/mpf/set_si.c	Sun Jan 29 12:57:42 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 fba311ff3884 mpq/set_si.c
--- a/mpq/set_si.c	Sat Jan 28 22:35:44 2012 +0100
+++ b/mpq/set_si.c	Sun Jan 29 12:57:42 2012 +0100
@@ -35,7 +35,7 @@
       return;
     }
 
-  abs_num = ABS (num);
+  abs_num = ABS_CAST (unsigned long, num);
 
   if (num == 0)
     {
diff -r fba311ff3884 mpz/cmp_si.c
--- a/mpz/cmp_si.c	Sat Jan 28 22:35:44 2012 +0100
+++ b/mpz/cmp_si.c	Sun Jan 29 12:57:42 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 fba311ff3884 mpz/get_si.c
--- a/mpz/get_si.c	Sat Jan 28 22:35:44 2012 +0100
+++ b/mpz/get_si.c	Sun Jan 29 12:57:42 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 fba311ff3884 mpz/iset_si.c
--- a/mpz/iset_si.c	Sat Jan 28 22:35:44 2012 +0100
+++ b/mpz/iset_si.c	Sun Jan 29 12:57:42 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 fba311ff3884 mpz/mul_i.h
--- a/mpz/mul_i.h	Sat Jan 28 22:35:44 2012 +0100
+++ b/mpz/mul_i.h	Sun Jan 29 12:57:42 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 fba311ff3884 mpz/set_si.c
--- a/mpz/set_si.c	Sat Jan 28 22:35:44 2012 +0100
+++ b/mpz/set_si.c	Sun Jan 29 12:57:42 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-bugs mailing list