NetBSD-Bugs archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

toolchain/39205: gcc-4.1.2 shifting a boolean may cause bad sign extension

>Number:         39205
>Category:       toolchain
>Synopsis:       gcc-4.1.2 shifting a boolean may cause bad sign extension
>Confidential:   no
>Severity:       serious
>Priority:       low
>Responsible:    toolchain-manager
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Fri Jul 25 14:15:00 +0000 2008
>Originator:     Michael van Elst
>Release:        NetBSD 4.0_STABLE
                                Michael van Elst
                                "A potential Snark may lurk in every tree."
System: NetBSD henery 4.0_STABLE NetBSD 4.0_STABLE (HENERY) #0: Tue Jul 22 
20:55:01 CEST 2008 
Architecture: i386
Machine: i386
Shifting a boolean value may cause a bad sign extension with
gcc-4.1.2 as found in netbsd-4.

The following code

#include <stdio.h>

        signed char x = -1;
        int y;

        y = (x < 0) << 7;


prints -128, the correct value is 1<<7 == 128.

Apparently the type of x is used as the type of the value being
shifted instead of 'int' which is the type of the comparison.
If you use a signed short then a shift of 15 causes the bad

However, (x < 0) << 8 seems to work fine again as well as
using a temporary variable for the boolean. I.e.

d = (x < 0);
y = d << 7;

produces the correct result, independent of d being of type
int or signed char.

Verified on i386/amd64/m68k.

gcc-4.1.3 (in -current) and gcc3 from pkgsrc generate good code.


Home | Main Index | Thread Index | Old Index