[PPL-devel] [GIT] ppl/ppl(master): Avoid implicit conversions changing integer type signedness.

Abramo Bagnara abramo.bagnara at gmail.com
Thu Feb 23 10:48:15 CET 2012


Module: ppl/ppl
Branch: master
Commit: 279f44d4d2ad2e818be866ff37b79336ab0333e7
URL:    http://www.cs.unipr.it/git/gitweb.cgi?p=ppl/ppl.git;a=commit;h=279f44d4d2ad2e818be866ff37b79336ab0333e7

Author: Abramo Bagnara <abramo.bagnara at gmail.com>
Date:   Thu Feb 23 10:47:58 2012 +0100

Avoid implicit conversions changing integer type signedness.
Detected by ECLAIR service utypflag.

---

 src/Bit_Row.inlines.hh       |   10 +++++-----
 src/Float.defs.hh            |    2 +-
 src/Row_Flags.defs.hh        |    2 +-
 src/checked.cc               |   20 +++++++++++---------
 src/checked_float.inlines.hh |   10 +++++-----
 5 files changed, 23 insertions(+), 21 deletions(-)

diff --git a/src/Bit_Row.inlines.hh b/src/Bit_Row.inlines.hh
index da1824e..46a1236 100644
--- a/src/Bit_Row.inlines.hh
+++ b/src/Bit_Row.inlines.hh
@@ -58,13 +58,13 @@ Bit_Row::Bit_Row(const Bit_Row& y, const Bit_Row& z) {
   if (y_size < z_size) {
     PPL_ASSERT(static_cast<unsigned long>(z_size)
            <= ULONG_MAX / PPL_BITS_PER_GMP_LIMB);
-    mpz_init2(vec, z_size * PPL_BITS_PER_GMP_LIMB);
+    mpz_init2(vec, static_cast<mp_bitcnt_t>(z_size) * PPL_BITS_PER_GMP_LIMB);
     union_helper(y, z);
   }
   else {
     PPL_ASSERT(static_cast<unsigned long>(y_size)
            <= ULONG_MAX / PPL_BITS_PER_GMP_LIMB);
-    mpz_init2(vec, y_size * PPL_BITS_PER_GMP_LIMB);
+    mpz_init2(vec, static_cast<mp_bitcnt_t>(y_size) * PPL_BITS_PER_GMP_LIMB);
     union_helper(z, y);
   }
 }
@@ -119,7 +119,7 @@ Bit_Row::clear() {
 
 inline memory_size_type
 Bit_Row::external_memory_in_bytes() const {
-  return vec[0]._mp_alloc * PPL_SIZEOF_MP_LIMB_T;
+  return static_cast<memory_size_type>(vec[0]._mp_alloc) * PPL_SIZEOF_MP_LIMB_T;
 }
 
 inline memory_size_type
@@ -136,13 +136,13 @@ Bit_Row::union_assign(const Bit_Row& x, const Bit_Row& y) {
   if (x_size < y_size) {
     PPL_ASSERT(static_cast<unsigned long>(y_size)
                <= ULONG_MAX / PPL_BITS_PER_GMP_LIMB);
-    mpz_realloc2(vec, y_size * PPL_BITS_PER_GMP_LIMB);
+    mpz_realloc2(vec, static_cast<mp_bitcnt_t>(y_size) * PPL_BITS_PER_GMP_LIMB);
     union_helper(x, y);
   }
   else {
     PPL_ASSERT(static_cast<unsigned long>(x_size)
            <= ULONG_MAX / PPL_BITS_PER_GMP_LIMB);
-    mpz_realloc2(vec, x_size * PPL_BITS_PER_GMP_LIMB);
+    mpz_realloc2(vec, static_cast<mp_bitcnt_t>(x_size) * PPL_BITS_PER_GMP_LIMB);
     union_helper(y, x);
   }
 }
diff --git a/src/Float.defs.hh b/src/Float.defs.hh
index fa60ff0..e2f0a44 100644
--- a/src/Float.defs.hh
+++ b/src/Float.defs.hh
@@ -263,7 +263,7 @@ struct float_ieee754_quad {
   static const unsigned int BASE = 2;
   static const unsigned int EXPONENT_BITS = 15;
   static const unsigned int MANTISSA_BITS = 112;
-  static const int EXPONENT_MAX = (1U << (EXPONENT_BITS - 1)) - 1;
+  static const int EXPONENT_MAX = (1 << (EXPONENT_BITS - 1)) - 1;
   static const int EXPONENT_BIAS = EXPONENT_MAX;
   static const int EXPONENT_MIN = -EXPONENT_MAX + 1;
   static const int EXPONENT_MIN_DENORM = EXPONENT_MIN
diff --git a/src/Row_Flags.defs.hh b/src/Row_Flags.defs.hh
index bf8e013..cfff314 100644
--- a/src/Row_Flags.defs.hh
+++ b/src/Row_Flags.defs.hh
@@ -67,7 +67,7 @@ protected:
   static const unsigned first_free_bit = 0;
 
   //! Total number of bits that can be stored.
-  static const unsigned num_bits = std::numeric_limits<base_type>::digits;
+  static const unsigned num_bits = sizeof_to_bits(sizeof(base_type));
 
   //! Constructs an object with flags set as in \p n.
   explicit Row_Flags(base_type n);
diff --git a/src/checked.cc b/src/checked.cc
index b024861..12a725e 100644
--- a/src/checked.cc
+++ b/src/checked.cc
@@ -51,8 +51,8 @@ struct number_struct {
   association; returns \f$-1\f$ otherwise.
 */
 inline int
-get_digit(char c, int base = 10) {
-  int n;
+get_digit(char c, unsigned int base = 10) {
+  unsigned int n;
   switch (c) {
   case '0': n = 0; break;
   case '1': n = 1; break;
@@ -95,7 +95,7 @@ get_digit(char c, int base = 10) {
   }
   if (n >= base)
     return -1;
-  return n;
+  return static_cast<int>(n);
 }
 
 /*! \brief
@@ -136,7 +136,7 @@ parse_number_part(std::istream& is, number_struct& numer) {
   bool empty_exponent = true;
   bool empty_mantissa = true;
   long exponent_offset = 0;
-  long exponent_offset_scale = 1;
+  unsigned exponent_offset_scale = 1;
   numer.base = 10;
   numer.base_for_exponent = 10;
   numer.neg_mantissa = false;
@@ -223,7 +223,7 @@ parse_number_part(std::istream& is, number_struct& numer) {
         for (std::string::const_iterator i = numer.mantissa.begin();
              i != numer.mantissa.end();
              i++) {
-          numer.base = numer.base * 10 + get_digit(*i, 10);
+          numer.base = numer.base * 10 + static_cast<unsigned>(get_digit(*i, 10));
           if (numer.base > 36)
             goto unexpected;
         }
@@ -301,7 +301,7 @@ parse_number_part(std::istream& is, number_struct& numer) {
         if (numer.exponent > max_exp_div
             || (numer.exponent == max_exp_div && d > max_exp_rem))
           return V_CVT_STR_UNK;
-        numer.exponent = 10*numer.exponent + d;
+        numer.exponent = 10 * numer.exponent + static_cast<unsigned int>(d);
         break;
       }
       if (empty_exponent)
@@ -331,7 +331,7 @@ parse_number_part(std::istream& is, number_struct& numer) {
     else
       neg = false;
     sum_sign(numer.neg_exponent, numer.exponent,
-             neg, exponent_offset * exponent_offset_scale);
+             neg, static_cast<unsigned long>(exponent_offset) * exponent_offset_scale);
     return V_EQ;
   }
 
@@ -405,11 +405,13 @@ input_mpq(mpq_class& to, std::istream& is) {
   }
   mpz_ptr numer = to.get_num().get_mpz_t();
   mpz_ptr denom = to.get_den().get_mpz_t();
-  mpz_set_str(numer, numer_struct.mantissa.c_str(), numer_struct.base);
+  mpz_set_str(numer, numer_struct.mantissa.c_str(),
+              static_cast<int>(numer_struct.base));
   if (denom_struct.base != 0) {
     if (numer_struct.neg_mantissa != denom_struct.neg_mantissa)
       mpz_neg(numer, numer);
-    mpz_set_str(denom, denom_struct.mantissa.c_str(), denom_struct.base);
+    mpz_set_str(denom, denom_struct.mantissa.c_str(),
+                static_cast<int>(denom_struct.base));
     if (numer_struct.exponent != 0 || denom_struct.exponent != 0) {
       // Multiply the exponents into the numerator and denominator.
       mpz_t z;
diff --git a/src/checked_float.inlines.hh b/src/checked_float.inlines.hh
index 44679e6..029bc6e 100644
--- a/src/checked_float.inlines.hh
+++ b/src/checked_float.inlines.hh
@@ -862,8 +862,8 @@ assign_float_mpq(T& to, const mpq_class& from, Rounding_Dir dir) {
   mpz_srcptr numer_z = numer.get_mpz_t();
   mpz_srcptr denom_z = denom.get_mpz_t();
   int sign = sgn(numer);
-  signed long exponent
-    = mpz_sizeinbase(numer_z, 2) - mpz_sizeinbase(denom_z, 2);
+  long exponent = static_cast<long>(mpz_sizeinbase(numer_z, 2))
+    - static_cast<long>(mpz_sizeinbase(denom_z, 2));
   if (exponent < Float<T>::Binary::EXPONENT_MIN_DENORM) {
     to = 0;
   inexact:
@@ -884,13 +884,13 @@ assign_float_mpq(T& to, const mpq_class& from, Rounding_Dir dir) {
     needed_bits -= Float<T>::Binary::EXPONENT_MIN - exponent;
   mpz_t mantissa;
   mpz_init(mantissa);
-  signed long shift = needed_bits - exponent;
+  long shift = static_cast<long>(needed_bits) - exponent;
   if (shift > 0) {
-    mpz_mul_2exp(mantissa, numer_z, shift);
+    mpz_mul_2exp(mantissa, numer_z, static_cast<mp_bitcnt_t>(shift));
     numer_z = mantissa;
   }
   else if (shift < 0) {
-    mpz_mul_2exp(mantissa, denom_z, -shift);
+    mpz_mul_2exp(mantissa, denom_z, static_cast<mp_bitcnt_t>(-shift));
     denom_z = mantissa;
   }
   mpz_t r;




More information about the PPL-devel mailing list