Commit a4f28954 by Martin Liska Committed by Martin Liska

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
parent 32ff7e39
2016-12-02 Martin Liska <mliska@suse.cz>
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.
2016-12-02 Dominik Vogt <vogt@linux.vnet.ibm.com> 2016-12-02 Dominik Vogt <vogt@linux.vnet.ibm.com>
* combine.c (combine_simplify_rtx): Suppress replacement of * combine.c (combine_simplify_rtx): Suppress replacement of
...@@ -102,14 +102,14 @@ sreal::shift_right (int s) ...@@ -102,14 +102,14 @@ sreal::shift_right (int s)
int64_t int64_t
sreal::to_int () const sreal::to_int () const
{ {
int64_t sign = m_sig < 0 ? -1 : 1; int64_t sign = SREAL_SIGN (m_sig);
if (m_exp <= -SREAL_BITS) if (m_exp <= -SREAL_BITS)
return 0; return 0;
if (m_exp >= SREAL_PART_BITS) if (m_exp >= SREAL_PART_BITS)
return sign * INTTYPE_MAXIMUM (int64_t); return sign * INTTYPE_MAXIMUM (int64_t);
if (m_exp > 0) if (m_exp > 0)
return m_sig << m_exp; return sign * (SREAL_ABS (m_sig) << m_exp);
if (m_exp < 0) if (m_exp < 0)
return m_sig >> -m_exp; return m_sig >> -m_exp;
return m_sig; return m_sig;
...@@ -229,7 +229,8 @@ sreal::operator/ (const sreal &other) const ...@@ -229,7 +229,8 @@ sreal::operator/ (const sreal &other) const
{ {
gcc_checking_assert (other.m_sig != 0); gcc_checking_assert (other.m_sig != 0);
sreal r; 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.m_exp = m_exp - other.m_exp - SREAL_PART_BITS;
r.normalize (); r.normalize ();
return r; return r;
...@@ -334,6 +335,18 @@ sreal_verify_shifting (void) ...@@ -334,6 +335,18 @@ sreal_verify_shifting (void)
verify_shifting (values[i]); 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. */ /* Run all of the selftests within this file. */
void sreal_c_tests () void sreal_c_tests ()
...@@ -341,6 +354,7 @@ void sreal_c_tests () ...@@ -341,6 +354,7 @@ void sreal_c_tests ()
sreal_verify_basics (); sreal_verify_basics ();
sreal_verify_arithmetics (); sreal_verify_arithmetics ();
sreal_verify_shifting (); sreal_verify_shifting ();
sreal_verify_negative_division ();
} }
} // namespace selftest } // namespace selftest
......
...@@ -31,6 +31,9 @@ along with GCC; see the file COPYING3. If not see ...@@ -31,6 +31,9 @@ along with GCC; see the file COPYING3. If not see
#define SREAL_BITS SREAL_PART_BITS #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. */ /* Structure for holding a simple real number. */
class sreal class sreal
{ {
...@@ -193,7 +196,6 @@ inline sreal operator>> (const sreal &a, int exp) ...@@ -193,7 +196,6 @@ inline sreal operator>> (const sreal &a, int exp)
inline void inline void
sreal::normalize_up () sreal::normalize_up ()
{ {
int64_t s = m_sig < 0 ? -1 : 1;
unsigned HOST_WIDE_INT sig = absu_hwi (m_sig); unsigned HOST_WIDE_INT sig = absu_hwi (m_sig);
int shift = SREAL_PART_BITS - 2 - floor_log2 (sig); int shift = SREAL_PART_BITS - 2 - floor_log2 (sig);
...@@ -208,7 +210,7 @@ sreal::normalize_up () ...@@ -208,7 +210,7 @@ sreal::normalize_up ()
m_exp = -SREAL_MAX_EXP; m_exp = -SREAL_MAX_EXP;
sig = 0; sig = 0;
} }
if (s == -1) if (SREAL_SIGN (m_sig) == -1)
m_sig = -sig; m_sig = -sig;
else else
m_sig = sig; m_sig = sig;
...@@ -221,7 +223,6 @@ sreal::normalize_up () ...@@ -221,7 +223,6 @@ sreal::normalize_up ()
inline void inline void
sreal::normalize_down () sreal::normalize_down ()
{ {
int64_t s = m_sig < 0 ? -1 : 1;
int last_bit; int last_bit;
unsigned HOST_WIDE_INT sig = absu_hwi (m_sig); unsigned HOST_WIDE_INT sig = absu_hwi (m_sig);
int shift = floor_log2 (sig) - SREAL_PART_BITS + 2; int shift = floor_log2 (sig) - SREAL_PART_BITS + 2;
...@@ -246,7 +247,7 @@ sreal::normalize_down () ...@@ -246,7 +247,7 @@ sreal::normalize_down ()
m_exp = SREAL_MAX_EXP; m_exp = SREAL_MAX_EXP;
sig = SREAL_MAX_SIG; sig = SREAL_MAX_SIG;
} }
if (s == -1) if (SREAL_SIGN (m_sig) == -1)
m_sig = -sig; m_sig = -sig;
else else
m_sig = sig; m_sig = sig;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment