[PPL-devel] [GIT] ppl/ppl(master): Avoid explicit integral conversions that change the signedness of the underlying type of complex expressions .

Roberto Bagnara bagnara at cs.unipr.it
Sat Oct 29 15:05:42 CEST 2011


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

Author: Roberto Bagnara <bagnara at cs.unipr.it>
Date:   Sat Oct 29 15:04:04 2011 +0200

Avoid explicit integral conversions that change the signedness of the underlying type of complex expressions.
Detected by ECLAIR service utypflag.

---

 src/Float.inlines.hh |   26 ++++++++++++++++++--------
 1 files changed, 18 insertions(+), 8 deletions(-)

diff --git a/src/Float.inlines.hh b/src/Float.inlines.hh
index 34281b3..9ae573f 100644
--- a/src/Float.inlines.hh
+++ b/src/Float.inlines.hh
@@ -85,7 +85,9 @@ float_ieee754_half::build(bool negative, mpz_t mantissa, int exponent) {
   word = mpz_get_ui(mantissa) & ((1UL << MANTISSA_BITS) - 1);
   if (negative)
     word |= SGN_MASK;
-  word |= static_cast<uint16_t>(exponent + EXPONENT_BIAS) << MANTISSA_BITS;
+  int exponent_repr = exponent + EXPONENT_BIAS;
+  PPL_ASSERT(exponent_repr >= 0 && exponent_repr < (1 << EXPONENT_BITS));
+  word |= static_cast<uint16_t>(exponent_repr) << MANTISSA_BITS;
 }
 
 inline int
@@ -143,7 +145,9 @@ float_ieee754_single::build(bool negative, mpz_t mantissa, int exponent) {
   word = mpz_get_ui(mantissa) & ((1UL << MANTISSA_BITS) - 1);
   if (negative)
     word |= SGN_MASK;
-  word |= static_cast<uint32_t>(exponent + EXPONENT_BIAS) << MANTISSA_BITS;
+  int exponent_repr = exponent + EXPONENT_BIAS;
+  PPL_ASSERT(exponent_repr >= 0 && exponent_repr < (1 << EXPONENT_BITS));
+  word |= static_cast<uint32_t>(exponent_repr) << MANTISSA_BITS;
 }
 
 inline int
@@ -226,8 +230,9 @@ float_ieee754_double::build(bool negative, mpz_t mantissa, int exponent) {
   msp = m & ((1UL << (MANTISSA_BITS - 32)) - 1);
   if (negative)
     msp |= MSP_SGN_MASK;
-  msp |= static_cast<uint32_t>(exponent + EXPONENT_BIAS)
-    << (MANTISSA_BITS - 32);
+  int exponent_repr = exponent + EXPONENT_BIAS;
+  PPL_ASSERT(exponent_repr >= 0 && exponent_repr < (1 << EXPONENT_BITS));
+  msp |= static_cast<uint32_t>(exponent_repr) << (MANTISSA_BITS - 32);
 }
 
 inline int
@@ -285,7 +290,9 @@ float_ibm_single::build(bool negative, mpz_t mantissa, int exponent) {
   word = mpz_get_ui(mantissa) & ((1UL << MANTISSA_BITS) - 1);
   if (negative)
     word |= SGN_MASK;
-  word |= static_cast<uint32_t>(exponent + EXPONENT_BIAS) << MANTISSA_BITS;
+  int exponent_repr = exponent + EXPONENT_BIAS;
+  PPL_ASSERT(exponent_repr >= 0 && exponent_repr < (1 << EXPONENT_BITS));
+  word |= static_cast<uint32_t>(exponent_repr) << MANTISSA_BITS;
 }
 
 inline int
@@ -365,7 +372,9 @@ float_intel_double_extended::build(bool negative,
   lsp = mpz_get_ui(mantissa);
 #endif
   msp = (negative ? MSP_SGN_MASK : 0);
-  msp |= static_cast<uint32_t>(exponent + EXPONENT_BIAS);
+  int exponent_repr = exponent + EXPONENT_BIAS;
+  PPL_ASSERT(exponent_repr >= 0 && exponent_repr < (1 << EXPONENT_BITS));
+  msp |= static_cast<uint32_t>(exponent_repr);
 }
 
 inline int
@@ -443,8 +452,9 @@ float_ieee754_quad::build(bool negative, mpz_t mantissa, int exponent) {
   msp &= ((1ULL << (MANTISSA_BITS - 64)) - 1);
   if (negative)
     msp |= MSP_SGN_MASK;
-  msp |= static_cast<uint64_t>(exponent + EXPONENT_BIAS)
-    << (MANTISSA_BITS - 64);
+  int exponent_repr = exponent + EXPONENT_BIAS;
+  PPL_ASSERT(exponent_repr >= 0 && exponent_repr < (1 << EXPONENT_BITS));
+  msp |= static_cast<uint64_t>(exponent_repr) << (MANTISSA_BITS - 64);
 }
 
 inline bool




More information about the PPL-devel mailing list