This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [Patch] sext_hwi: Avoid left shift of negative value undefined behaviour


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.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]