[Gmp-commit] /var/hg/gmp: Make sure we never compute -LONG_MIN.

mercurial at gmplib.org mercurial at gmplib.org
Sat Jan 28 22:35:52 CET 2012


details:   /var/hg/gmp/rev/fba311ff3884
changeset: 14588:fba311ff3884
user:      Marc Glisse <marc.glisse at inria.fr>
date:      Sat Jan 28 22:35:44 2012 +0100
description:
Make sure we never compute -LONG_MIN.

diffstat:

 ChangeLog |   2 ++
 gmpxx.h   |  43 ++++++++++++++++++++++++-------------------
 2 files changed, 26 insertions(+), 19 deletions(-)

diffs (196 lines):

diff -r fbcd89f7a6ad -r fba311ff3884 ChangeLog
--- a/ChangeLog	Sat Jan 28 21:33:12 2012 +0100
+++ b/ChangeLog	Sat Jan 28 22:35:44 2012 +0100
@@ -1,5 +1,7 @@
 2012-01-28  Marc Glisse  <marc.glisse at inria.fr>
 
+	* gmpxx.h: Don't compute -LONG_MIN.
+
 	* doc/gmp.texi (gmp_randclass::get_z_bits): Use mp_bitcnt_t.
 	* gmpxx.h: Replace unsigned long with mp_bitcnt_t.
 
diff -r fbcd89f7a6ad -r fba311ff3884 gmpxx.h
--- a/gmpxx.h	Sat Jan 28 21:33:12 2012 +0100
+++ b/gmpxx.h	Sat Jan 28 22:35:44 2012 +0100
@@ -72,7 +72,7 @@
 {
   if(l < 0)
   {
-    __mpz_set_ui_safe(p, -l);
+    __mpz_set_ui_safe(p, -static_cast<unsigned long>(l));
     mpz_neg(p, p);
   }
   else
@@ -115,6 +115,11 @@
   mpq_denref(temp)->_mp_size = 1;					\
   mpq_denref(temp)->_mp_d[0] = 1
 
+inline unsigned long __gmpxx_abs_ui (signed long l)
+{
+  return l >= 0 ? static_cast<unsigned long>(l)
+	  : -static_cast<unsigned long>(l);
+}
 
 /**************** Function objects ****************/
 /* Any evaluation of a __gmp_expr ends up calling one of these functions
@@ -164,7 +169,7 @@
     if (l >= 0)
       eval(z, w, static_cast<unsigned long>(l));
     else
-      mpz_sub_ui(z, w, -l);
+      mpz_sub_ui(z, w, -static_cast<unsigned long>(l));
   }
   static void eval(mpz_ptr z, signed long int l, mpz_srcptr w)
   { eval(z, w, l); }
@@ -237,7 +242,7 @@
     if (l >= 0)
       mpf_add_ui(f, g, l);
     else
-      mpf_sub_ui(f, g, -l);
+      mpf_sub_ui(f, g, -static_cast<unsigned long>(l));
   }
   static void eval(mpf_ptr f, signed long int l, mpf_srcptr g)
   { eval(f, g, l); }
@@ -281,7 +286,7 @@
     if (l >= 0)
       eval(z, w, static_cast<unsigned long>(l));
     else
-      mpz_add_ui(z, w, -l);
+      mpz_add_ui(z, w, -static_cast<unsigned long>(l));
   }
   static void eval(mpz_ptr z, signed long int l, mpz_srcptr w)
   {
@@ -289,7 +294,7 @@
       eval(z, static_cast<unsigned long>(l), w);
     else
       {
-        mpz_add_ui(z, w, -l);
+        mpz_add_ui(z, w, -static_cast<unsigned long>(l));
         mpz_neg(z, z);
       }
   }
@@ -326,7 +331,7 @@
     if (l >= 0)
       eval(q, r, static_cast<unsigned long>(l));
     else
-      __gmp_binary_plus::eval(q, r, static_cast<unsigned long>(-l));
+      __gmp_binary_plus::eval(q, r, -static_cast<unsigned long>(l));
   }
   static void eval(mpq_ptr q, signed long int l, mpq_srcptr r)
   { eval(q, r, l); mpq_neg(q, q); }
@@ -373,14 +378,14 @@
     if (l >= 0)
       mpf_sub_ui(f, g, l);
     else
-      mpf_add_ui(f, g, -l);
+      mpf_add_ui(f, g, -static_cast<unsigned long>(l));
   }
   static void eval(mpf_ptr f, signed long int l, mpf_srcptr g)
   {
     if (l >= 0)
       mpf_sub_ui(f, g, l);
     else
-      mpf_add_ui(f, g, -l);
+      mpf_add_ui(f, g, -static_cast<unsigned long>(l));
     mpf_neg(f, f);
   }
   static void eval(mpf_ptr f, mpf_srcptr g, double d)
@@ -408,7 +413,7 @@
   if (l >= 0)
     eval(q, r, static_cast<unsigned long>(l));
   else
-    __gmp_binary_minus::eval(q, r, static_cast<unsigned long>(-l));
+    __gmp_binary_minus::eval(q, r, -static_cast<unsigned long>(l));
 }
 
 struct __gmp_binary_lshift
@@ -493,7 +498,7 @@
         eval(z, w, static_cast<unsigned long>(l));
       else
       {
-        eval(z, w, static_cast<unsigned long>(-l));
+        eval(z, w, -static_cast<unsigned long>(l));
 	mpz_neg(z, z);
       }
     }
@@ -541,7 +546,7 @@
         eval(q, r, static_cast<unsigned long>(l));
       else
       {
-        eval(q, r, static_cast<unsigned long>(-l));
+        eval(q, r, -static_cast<unsigned long>(l));
 	mpq_neg(q, q);
       }
     }
@@ -577,7 +582,7 @@
       mpf_mul_ui(f, g, l);
     else
       {
-	mpf_mul_ui(f, g, -l);
+	mpf_mul_ui(f, g, -static_cast<unsigned long>(l));
 	mpf_neg(f, f);
       }
   }
@@ -645,7 +650,7 @@
       eval(z, w, static_cast<unsigned long>(l));
     else
       {
-	eval(z, w, static_cast<unsigned long>(-l));
+	eval(z, w, -static_cast<unsigned long>(l));
 	mpz_neg(z, z);
       }
   }
@@ -657,7 +662,7 @@
       {
         /* if w is bigger than a long then the quotient must be zero, unless
            l==LONG_MIN and w==-LONG_MIN in which case the quotient is -1 */
-        mpz_set_si (z, (mpz_cmpabs_ui (w, (l >= 0 ? l : -l)) == 0 ? -1 : 0));
+        mpz_set_si (z, (mpz_cmpabs_ui (w, __gmpxx_abs_ui(l)) == 0 ? -1 : 0));
       }
   }
   static void eval(mpz_ptr z, mpz_srcptr w, double d)
@@ -690,7 +695,7 @@
         eval(q, r, static_cast<unsigned long>(l));
       else
       {
-        eval(q, r, static_cast<unsigned long>(-l));
+        eval(q, r, -static_cast<unsigned long>(l));
 	mpq_neg(q, q);
       }
     }
@@ -732,7 +737,7 @@
       mpf_div_ui(f, g, l);
     else
       {
-	mpf_div_ui(f, g, -l);
+	mpf_div_ui(f, g, -static_cast<unsigned long>(l));
 	mpf_neg(f, f);
       }
   }
@@ -742,7 +747,7 @@
       mpf_ui_div(f, l, g);
     else
       {
-	mpf_ui_div(f, -l, g);
+	mpf_ui_div(f, -static_cast<unsigned long>(l), g);
 	mpf_neg(f, f);
       }
   }
@@ -791,7 +796,7 @@
   }
   static void eval(mpz_ptr z, mpz_srcptr w, signed long int l)
   {
-    mpz_tdiv_r_ui (z, w, (l >= 0 ? l : -l));
+    mpz_tdiv_r_ui (z, w, __gmpxx_abs_ui(l));
   }
   static void eval(mpz_ptr z, signed long int l, mpz_srcptr w)
   {
@@ -801,7 +806,7 @@
       {
         /* if w is bigger than a long then the remainder is l unchanged,
            unless l==LONG_MIN and w==-LONG_MIN in which case it's 0 */
-        mpz_set_si (z, mpz_cmpabs_ui (w, (l >= 0 ? l : -l)) == 0 ? 0 : l);
+        mpz_set_si (z, mpz_cmpabs_ui (w, __gmpxx_abs_ui(l)) == 0 ? 0 : l);
       }
   }
   static void eval(mpz_ptr z, mpz_srcptr w, double d)


More information about the gmp-commit mailing list