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
>Organization:
-- 
                                Michael van Elst
Internet: mlelstv%serpens.de@localhost
                                "A potential Snark may lurk in every tree."
>Environment:
        
        
System: NetBSD henery 4.0_STABLE NetBSD 4.0_STABLE (HENERY) #0: Tue Jul 22 
20:55:01 CEST 2008 
mlelstv@henery:/home/netbsd4/obj.i386/home/netbsd4/src/sys/arch/i386/compile/HENERY
 i386
Architecture: i386
Machine: i386
>Description:
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>

main()
{
        signed char x = -1;
        int y;

        y = (x < 0) << 7;

        printf("%d\n",y);
}

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

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.


>How-To-Repeat:
Verified on i386/amd64/m68k.

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


>Unformatted:
        
        


Home | Main Index | Thread Index | Old Index