From a4f2895465da4c8856b119a5787b95db345567a9 Mon Sep 17 00:00:00 2001 From: Martin Liska Date: Fri, 2 Dec 2016 09:36:01 +0100 Subject: Fix runtime error: left shift of negative value (PR PR ipa/78555 * sreal.c (sreal::to_int): Make absolute value before shifting. (sreal::operator/): Likewise. (sreal_verify_negative_division): New test. (void sreal_c_tests): Call the new test. * sreal.h (sreal::normalize_up): Use new SREAL_ABS and SREAL_SIGN macros. (sreal::normalize_down): Likewise. From-SVN: r243163 --- gcc/sreal.h | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'gcc/sreal.h') diff --git a/gcc/sreal.h b/gcc/sreal.h index ce9cdbb..21f14b0 100644 --- a/gcc/sreal.h +++ b/gcc/sreal.h @@ -31,6 +31,9 @@ along with GCC; see the file COPYING3. If not see #define SREAL_BITS SREAL_PART_BITS +#define SREAL_SIGN(v) (v < 0 ? -1: 1) +#define SREAL_ABS(v) (v < 0 ? -v: v) + /* Structure for holding a simple real number. */ class sreal { @@ -193,7 +196,6 @@ inline sreal operator>> (const sreal &a, int exp) inline void sreal::normalize_up () { - int64_t s = m_sig < 0 ? -1 : 1; unsigned HOST_WIDE_INT sig = absu_hwi (m_sig); int shift = SREAL_PART_BITS - 2 - floor_log2 (sig); @@ -208,7 +210,7 @@ sreal::normalize_up () m_exp = -SREAL_MAX_EXP; sig = 0; } - if (s == -1) + if (SREAL_SIGN (m_sig) == -1) m_sig = -sig; else m_sig = sig; @@ -221,7 +223,6 @@ sreal::normalize_up () inline void sreal::normalize_down () { - int64_t s = m_sig < 0 ? -1 : 1; int last_bit; unsigned HOST_WIDE_INT sig = absu_hwi (m_sig); int shift = floor_log2 (sig) - SREAL_PART_BITS + 2; @@ -246,7 +247,7 @@ sreal::normalize_down () m_exp = SREAL_MAX_EXP; sig = SREAL_MAX_SIG; } - if (s == -1) + if (SREAL_SIGN (m_sig) == -1) m_sig = -sig; else m_sig = sig; -- cgit v1.1