This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Re: c/7284: incorrectly simplifies leftshift followed by signed power-of-2 division
- From: Falk Hueffner <falk dot hueffner at student dot uni-tuebingen dot de>
- To: "Al Grant" <AlGrant at myrealbox dot com>
- Cc: nathan at gcc dot gnu dot org, algrant at acm dot org, gcc-bugs at gcc dot gnu dot org, gcc-prs at gcc dot gnu dot org, nobody at gcc dot gnu dot org, gcc-gnats at gcc dot gnu dot org
- Date: 12 Jul 2002 17:13:48 +0200
- Subject: Re: c/7284: incorrectly simplifies leftshift followed by signed power-of-2 division
- References: <1026486034.69836ff8AlGrant@myrealbox.com>
"Al Grant" <AlGrant@myrealbox.com> writes:
> On 12/07/2002 15:12:01 nathan wrote:
> >Synopsis: incorrectly simplifies leftshift followed by signed power-of-2
> >division
> >
> >State-Changed-From-To: open->closed
> >State-Changed-By: nathan
> >State-Changed-When: Fri Jul 12 07:12:01 2002
> >State-Changed-Why:
> >not a bug. for signed types, if 'n << c' overflows, the
> >behaviour is undefined.
>
> There is no "overflow" in my sample code. The operation of shifting 128 24 bits to the left on a
> 32-bit machine produces the bit pattern 0x80000000.
> No bits overflow.
>
> The fact that a positive number may become negative when
> left-shifted is a property of the twos complement representation.
> The standard does not define signed left shift in terms of
> multiplication and certainly doesn't say that it is undefined when
> the apparently equivalent multiplication would be undefined.
Before refering to the standard, you should probably read it.
6.5.7.4:
"The result of E1 << E2 is E1 left-shifted E2 bit positions; vacated
bits are filled with zeros. If E1 has a signed type and nonnegative
value, and E1 * 2^E2 is representable in the result type, then that
is the resulting value; otherwise, the behavior is undefined."
--
Falk