From: "Martin Liška" <mliska@suse.cz>
To: GCC Patches <gcc-patches@gcc.gnu.org>
Cc: markus@trippelsdorf.de
Subject: [PATCH] Fix runtime error: left shift of negative value (PR, ipa/78555).
Date: Thu, 01 Dec 2016 09:55:00 -0000 [thread overview]
Message-ID: <0e1ac154-4e1c-d2b7-645d-7a007a9d4e53@suse.cz> (raw)
[-- Attachment #1: Type: text/plain, Size: 234 bytes --]
As described in the PR, we do couple of shifts of a negative value. Fixed in the patch
and couple of new unit tests are added.
Patch can bootstrap on ppc64le-redhat-linux and survives regression tests.
Ready to be installed?
Martin
[-- Attachment #2: 0001-Fix-runtime-error-left-shift-of-negative-value-PR-ip.patch --]
[-- Type: text/x-patch, Size: 3644 bytes --]
From 61a6b5e0c973bd77341a1053609c7ad331691a9e Mon Sep 17 00:00:00 2001
From: marxin <mliska@suse.cz>
Date: Wed, 30 Nov 2016 15:24:48 +0100
Subject: [PATCH] Fix runtime error: left shift of negative value (PR
ipa/78555).
gcc/ChangeLog:
2016-11-30 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.
---
gcc/sreal.c | 20 +++++++++++++++++---
gcc/sreal.h | 9 +++++----
2 files changed, 22 insertions(+), 7 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
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;
--
2.10.2
next reply other threads:[~2016-12-01 9:55 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-12-01 9:55 Martin Liška [this message]
2016-12-01 22:36 ` Jeff Law
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=0e1ac154-4e1c-d2b7-645d-7a007a9d4e53@suse.cz \
--to=mliska@suse.cz \
--cc=gcc-patches@gcc.gnu.org \
--cc=markus@trippelsdorf.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).