aboutsummaryrefslogtreecommitdiff
path: root/gcc/sreal.c
diff options
context:
space:
mode:
authorMartin Liska <mliska@suse.cz>2016-12-02 09:36:01 +0100
committerMartin Liska <marxin@gcc.gnu.org>2016-12-02 08:36:01 +0000
commita4f2895465da4c8856b119a5787b95db345567a9 (patch)
treed78ade7d79b89f366b780063e24f411faa024243 /gcc/sreal.c
parent32ff7e39c15206dada1406bdc06e18db9e02d248 (diff)
downloadgcc-a4f2895465da4c8856b119a5787b95db345567a9.zip
gcc-a4f2895465da4c8856b119a5787b95db345567a9.tar.gz
gcc-a4f2895465da4c8856b119a5787b95db345567a9.tar.bz2
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
Diffstat (limited to 'gcc/sreal.c')
-rw-r--r--gcc/sreal.c20
1 files changed, 17 insertions, 3 deletions
diff --git a/gcc/sreal.c b/gcc/sreal.c
index 9c43b4e..52e530d 100644
--- a/gcc/sreal.c
+++ b/gcc/sreal.c
@@ -102,14 +102,14 @@ sreal::shift_right (int s)
int64_t
sreal::to_int () const
{
- int64_t sign = m_sig < 0 ? -1 : 1;
+ int64_t sign = SREAL_SIGN (m_sig);
if (m_exp <= -SREAL_BITS)
return 0;
if (m_exp >= SREAL_PART_BITS)
return sign * INTTYPE_MAXIMUM (int64_t);
if (m_exp > 0)
- return m_sig << m_exp;
+ return sign * (SREAL_ABS (m_sig) << m_exp);
if (m_exp < 0)
return m_sig >> -m_exp;
return m_sig;
@@ -229,7 +229,8 @@ sreal::operator/ (const sreal &other) const
{
gcc_checking_assert (other.m_sig != 0);
sreal r;
- r.m_sig = (m_sig << SREAL_PART_BITS) / other.m_sig;
+ r.m_sig
+ = SREAL_SIGN (m_sig) * (SREAL_ABS (m_sig) << SREAL_PART_BITS) / other.m_sig;
r.m_exp = m_exp - other.m_exp - SREAL_PART_BITS;
r.normalize ();
return r;
@@ -334,6 +335,18 @@ sreal_verify_shifting (void)
verify_shifting (values[i]);
}
+/* Verify division by (of) a negative value. */
+
+static void
+sreal_verify_negative_division (void)
+{
+ ASSERT_EQ (sreal (1) / sreal (1), sreal (1));
+ ASSERT_EQ (sreal (-1) / sreal (-1), sreal (1));
+ ASSERT_EQ (sreal (-1234567) / sreal (-1234567), sreal (1));
+ ASSERT_EQ (sreal (-1234567) / sreal (1234567), sreal (-1));
+ ASSERT_EQ (sreal (1234567) / sreal (-1234567), sreal (-1));
+}
+
/* Run all of the selftests within this file. */
void sreal_c_tests ()
@@ -341,6 +354,7 @@ void sreal_c_tests ()
sreal_verify_basics ();
sreal_verify_arithmetics ();
sreal_verify_shifting ();
+ sreal_verify_negative_division ();
}
} // namespace selftest