Module: ppl/ppl Branch: bounded_arithmetic Commit: 6c8a266be0f01266599542b7cdc3d3c2a47e62d2 URL: http://www.cs.unipr.it/git/gitweb.cgi?p=ppl/ppl.git;a=commit;h=6c8a266be0f01... Author: Abramo Bagnara <abramo.bagnara@gmail.com> Date: Thu May 14 09:41:30 2009 +0200 Silenced bogus comparison warnings. --- src/checked.inlines.hh | 31 ++++++++++++++++++++++++++----- src/checked_int.inlines.hh | 18 +++++++++--------- 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/src/checked.inlines.hh b/src/checked.inlines.hh index a98c153..0c96381 100644 --- a/src/checked.inlines.hh +++ b/src/checked.inlines.hh @@ -28,6 +28,27 @@ site: http://www.cs.unipr.it/ppl/ . */ #include "C_Integer.hh" #include <cassert> +#if defined(__GNUC__) +/*! \brief + Performs the test <CODE>a < b</CODE> avoiding the warning + about comparison with min or max of the type. +*/ +#define PPL_LT_SILENT(a, b) \ + ({ \ + __typeof__(a) _a = (a); \ + __typeof__(b) _b = (b); \ + _a <= _b && _a != _b; \ + }) +/*! \brief + Performs the test <CODE>a > b</CODE> avoiding the warning + about comparison with min or max of the type. +*/ +#define PPL_GT_SILENT(a, b) PPL_LT_SILENT(b, a) +#else +#define PPL_LT_SILENT(a, b) ((a) < (b)) +#define PPL_GT_SILENT(a, b) ((a) > (b)) +#endif + namespace Parma_Polyhedra_Library { namespace Checked { @@ -439,7 +460,7 @@ inline typename Enable_If<(!Safe_Int_Comparison<S, U>::value && C_Integer<U>::value && C_Integer<S>::is_signed), bool>::type lt(const U& x, const S& y) { - return y >= 0 && x < y; + return y >= 0 && x < static_cast<typename C_Integer<S>::other_type>(y); } template <typename S, typename U> @@ -455,7 +476,7 @@ inline typename Enable_If<(!Safe_Int_Comparison<S, U>::value && C_Integer<U>::value && C_Integer<S>::is_signed), bool>::type le(const U& x, const S& y) { - return y >= 0 && x <= y; + return y >= 0 && x <= static_cast<typename C_Integer<S>::other_type>(y); } template <typename S, typename U> @@ -463,7 +484,7 @@ inline typename Enable_If<(!Safe_Int_Comparison<S, U>::value && C_Integer<U>::value && C_Integer<S>::is_signed), bool>::type eq(const S& x, const U& y) { - return x >= 0 && x == y; + return x >= 0 && static_cast<typename C_Integer<S>::other_type>(x) == y; } template <typename U, typename S> @@ -471,7 +492,7 @@ inline typename Enable_If<(!Safe_Int_Comparison<S, U>::value && C_Integer<U>::value && C_Integer<S>::is_signed), bool>::type eq(const U& x, const S& y) { - return y >= 0 && x == y; + return y >= 0 && x == static_cast<typename C_Integer<S>::other_type>(y); } template <typename T1, typename T2> @@ -480,7 +501,7 @@ inline typename Enable_If<(!Safe_Conversion<T1, T2>::value && (!C_Integer<T1>::value || !C_Integer<T2>::value)), bool>::type eq(const T1& x, const T2& y) { PPL_DIRTY_TEMP(T1, tmp); - Result r = assign_r(tmp, y, static_cast<Rounding_Dir>(ROUND_DIRECT | ROUND_FPU_CHECK_INEXACT)); + Result r = assign_r(tmp, y, ROUND_CHECK); // FIXME: We can do this also without fpu inexact check using a // conversion back and forth and then testing equality. We should // code this in checked_float.inlines.hh, probably it's faster also diff --git a/src/checked_int.inlines.hh b/src/checked_int.inlines.hh index d74cba2..95676fd 100644 --- a/src/checked_int.inlines.hh +++ b/src/checked_int.inlines.hh @@ -347,10 +347,10 @@ assign_signed_int_signed_int(To& to, const From from, Rounding_Dir dir) { && (Extended_Int<To_Policy, To>::min > Extended_Int<From_Policy, From>::min || Extended_Int<To_Policy, To>::max < Extended_Int<From_Policy, From>::max))) { if (CHECK_P(To_Policy::check_overflow, - from < From(Extended_Int<To_Policy, To>::min))) + PPL_LT_SILENT(from, From(Extended_Int<To_Policy, To>::min)))) return set_neg_overflow_int<To_Policy>(to, dir); if (CHECK_P(To_Policy::check_overflow, - from > From(Extended_Int<To_Policy, To>::max))) + PPL_GT_SILENT(from, From(Extended_Int<To_Policy, To>::max)))) return set_pos_overflow_int<To_Policy>(to, dir); } to = To(from); @@ -390,7 +390,7 @@ assign_unsigned_int_unsigned_int(To& to, const From from, Rounding_Dir dir) { || (sizeof(To) == sizeof(From) && Extended_Int<To_Policy, To>::max < Extended_Int<From_Policy, From>::max)) { if (CHECK_P(To_Policy::check_overflow, - from > From(Extended_Int<To_Policy, To>::max))) + PPL_GT_SILENT(from, From(Extended_Int<To_Policy, To>::max)))) return set_pos_overflow_int<To_Policy>(to, dir); } to = To(from); @@ -561,9 +561,9 @@ assign_signed_int_mpz(To& to, const mpz_class& from, Rounding_Dir dir) { } if (from.fits_slong_p()) { signed long v = from.get_si(); - if (v < Extended_Int<To_Policy, To>::min) + if (PPL_LT_SILENT(v, (Extended_Int<To_Policy, To>::min))) return set_neg_overflow_int<To_Policy>(to, dir); - if (v > Extended_Int<To_Policy, To>::max) + if (PPL_GT_SILENT(v, (Extended_Int<To_Policy, To>::max))) return set_pos_overflow_int<To_Policy>(to, dir); to = v; return V_EQ; @@ -610,7 +610,7 @@ assign_unsigned_int_mpz(To& to, const mpz_class& from, Rounding_Dir dir) { } if (from.fits_ulong_p()) { unsigned long v = from.get_ui(); - if (v > Extended_Int<To_Policy, To>::max) + if (PPL_GT_SILENT(v, (Extended_Int<To_Policy, To>::max))) return set_pos_overflow_int<To_Policy>(to, dir); to = v; return V_EQ; @@ -1230,7 +1230,7 @@ mul_2exp_unsigned_int(Type& to, const Type x, unsigned int exp, if (x & (((Type(1) << exp) - 1) << (sizeof(Type) * CHAR_BIT - exp))) return set_pos_overflow_int<To_Policy>(to, dir); Type n = x << exp; - if (n > Extended_Int<To_Policy, Type>::max) + if (PPL_GT_SILENT(n, (Extended_Int<To_Policy, Type>::max))) return set_pos_overflow_int<To_Policy>(to, dir); to = n; return V_EQ; @@ -1261,14 +1261,14 @@ mul_2exp_signed_int(Type& to, const Type x, unsigned int exp, if ((x & mask) != mask) return set_neg_overflow_int<To_Policy>(to, dir); n = x << exp; - if (n < Extended_Int<To_Policy, Type>::min) + if (PPL_LT_SILENT(n, (Extended_Int<To_Policy, Type>::min))) return set_neg_overflow_int<To_Policy>(to, dir); } else { if (x & mask) return set_pos_overflow_int<To_Policy>(to, dir); n = x << exp; - if (n > Extended_Int<To_Policy, Type>::max) + if (PPL_GT_SILENT(n, (Extended_Int<To_Policy, Type>::max))) return set_pos_overflow_int<To_Policy>(to, dir); } to = n;