This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [Patch] sext_hwi: Avoid left shift of negative value undefined behaviour
- From: Richard Biener <richard dot guenther at gmail dot com>
- To: Mike Stump <mikestump at comcast dot net>
- Cc: Richard Sandiford <rdsandiford at googlemail dot com>, Jeff Law <law at redhat dot com>, Mikael Morin <mikael dot morin at sfr dot fr>, Markus Trippelsdorf <markus at trippelsdorf dot de>, gcc-patches <gcc-patches at gcc dot gnu dot org>
- Date: Thu, 13 Aug 2015 12:05:46 +0200
- Subject: Re: [Patch] sext_hwi: Avoid left shift of negative value undefined behaviour
- Authentication-results: sourceware.org; auth=none
- References: <55C33636 dot 7020907 at sfr dot fr> <55CA51C4 dot 3050601 at redhat dot com> <CAFiYyc1pM=BT06yLY=L9EDzDCSiUHeXL0D26ic-g8bnYBmqYNA at mail dot gmail dot com> <20150812110724 dot GB403 at x4> <CAFiYyc3G2NM2iqM5b5TKs+yWZJZ5-S13n30Bz51a2uMYL7N6KA at mail dot gmail dot com> <55CB4B40 dot 6040902 at sfr dot fr> <55CB7C0C dot 7030203 at redhat dot com> <F89F5949-3938-4F58-9C31-8BC0B217533B at gmail dot com> <55CB8B51 dot 5080908 at redhat dot com> <21C33A3D-9976-4632-ACCC-082F2D618ED5 at gmail dot com> <55CB9343 dot 6000103 at redhat dot com> <87k2t06zkk dot fsf at googlemail dot com> <2E5F6175-B03B-491D-A71A-A44A59412140 at comcast dot net>
On Wed, Aug 12, 2015 at 10:52 PM, Mike Stump <mikestump@comcast.net> wrote:
> On Aug 12, 2015, at 1:07 PM, Richard Sandiford <rdsandiford@googlemail.com> wrote:
>>> I don't think the right shifts are an issue at all.
>>
>> Well, they're implementation-defined, at least in C.
>
>> The C11 wording for E1 >> E2 is "If E1 has a signed type and a negative value, the
>> resulting value is implementation-definedâ.
>
>> Is C++ different?
>
> No, it is the same:
>
> 3 The value of E1 >> E2 is E1 right-shifted E2 bit positions. If E1 has
> an unsigned type or if E1 has a signed type and a nonnegative value,
> the value of the result is the integral part of the quotient of E1
> divided by the quantity 2 raised to the power E2. If E1 has a signed
> type and a negative value, the resulting value is implementation-
> defined.
Ok, then guard the << >> with __GCC__ and do the expensive bit stuff
otherwise. Just to cater for other host compilers doing sth unsensibly
implementation defined.
This thing _is_ performance critical.
Richard.
>> (I don't have the standard handy.)
>
> Google is your friend.