public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [Patch] sext_hwi: Avoid left shift of negative value undefined behaviour
@ 2015-08-06 10:26 Mikael Morin
  2015-08-11 19:49 ` Jeff Law
  0 siblings, 1 reply; 26+ messages in thread
From: Mikael Morin @ 2015-08-06 10:26 UTC (permalink / raw)
  To: gcc-patches

[-- Attachment #1: Type: text/plain, Size: 128 bytes --]

Hello,

this avoids an error found with bootstrap-ubsan.
Regression tested on x86_64-unknown-linux-gnu.  OK for trunk?

Mikael


[-- Attachment #2: noub_sext.CL --]
[-- Type: text/plain, Size: 125 bytes --]

2015-08-05  Mikael Morin  <mikael@gcc.gnu.org>

	* hwint.h (sext_hwi): Rewrite without undefined behaviour on
	negative SRC.

[-- Attachment #3: noub_sext.diff --]
[-- Type: text/x-patch, Size: 557 bytes --]

diff --git a/gcc/hwint.h b/gcc/hwint.h
index 3793986..9c3eda0 100644
--- a/gcc/hwint.h
+++ b/gcc/hwint.h
@@ -246,8 +246,9 @@ sext_hwi (HOST_WIDE_INT src, unsigned int prec)
   else
     {
       gcc_checking_assert (prec < HOST_BITS_PER_WIDE_INT);
-      int shift = HOST_BITS_PER_WIDE_INT - prec;
-      return (src << shift) >> shift;
+      HOST_WIDE_INT sign_mask = HOST_WIDE_INT_1 << (prec - 1);
+      HOST_WIDE_INT value_mask = (HOST_WIDE_INT_1U << prec) - HOST_WIDE_INT_1U;
+      return (((src & value_mask) ^ sign_mask) - sign_mask);
     }
 }
 


^ permalink raw reply	[flat|nested] 26+ messages in thread

end of thread, other threads:[~2015-08-18 17:47 UTC | newest]

Thread overview: 26+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-08-06 10:26 [Patch] sext_hwi: Avoid left shift of negative value undefined behaviour Mikael Morin
2015-08-11 19:49 ` Jeff Law
2015-08-12 11:01   ` Richard Biener
2015-08-12 11:07     ` Markus Trippelsdorf
2015-08-12 11:09       ` Richard Biener
2015-08-12 13:34         ` Mikael Morin
2015-08-12 17:02           ` Jeff Law
2015-08-12 17:12             ` Richard Biener
2015-08-12 18:07               ` Jeff Law
2015-08-12 18:32                 ` Richard Biener
2015-08-12 18:41                   ` Jeff Law
2015-08-12 20:07                     ` Richard Sandiford
2015-08-12 20:53                       ` Mike Stump
2015-08-13 10:11                         ` Richard Biener
2015-08-13 18:01                           ` Mike Stump
2015-08-14  7:31                             ` Richard Biener
2015-08-18 15:35                               ` Mikael Morin
2015-08-18 17:51                                 ` Jeff Law
2015-08-13 11:03                       ` Mikael Morin
2015-08-13 11:06                         ` Mikael Morin
2015-08-13 11:09                         ` Markus Trippelsdorf
2015-08-13 11:20                           ` Richard Biener
2015-08-13 13:48                             ` Marek Polacek
2015-08-13 13:55                               ` Markus Trippelsdorf
2015-08-13 13:57                                 ` Marek Polacek
2015-08-12 19:20                 ` Mike Stump

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).