NetBSD Problem Report #39205

From mlelstv@henery.1st.de  Fri Jul 25 14:12:09 2008
Return-Path: <mlelstv@henery.1st.de>
Received: from mail.netbsd.org (mail.netbsd.org [204.152.190.11])
	by narn.NetBSD.org (Postfix) with ESMTP id 5A91163B91E
	for <gnats-bugs@gnats.NetBSD.org>; Fri, 25 Jul 2008 14:12:09 +0000 (UTC)
Message-Id: <20080725141024.08DA128181@henery.1st.de>
Date: Fri, 25 Jul 2008 16:10:24 +0200 (CEST)
From: mlelstv@serpens.de
Reply-To: mlelstv@serpens.de
To: gnats-bugs@gnats.NetBSD.org
Subject: gcc-4.1.2 shifting a boolean may cause bad sign extension
X-Send-Pr-Version: 3.95

>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:          closed
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Fri Jul 25 14:15:00 +0000 2008
>Closed-Date:    Tue Nov 27 20:38:38 +0000 2018
>Last-Modified:  Tue Nov 27 20:38:38 +0000 2018
>Originator:     Michael van Elst
>Release:        NetBSD 4.0_STABLE
>Organization:
-- 
                                Michael van Elst
Internet: mlelstv@serpens.de
                                "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.


>Release-Note:

>Audit-Trail:

State-Changed-From-To: open->closed
State-Changed-By: maya@NetBSD.org
State-Changed-When: Tue, 27 Nov 2018 20:38:38 +0000
State-Changed-Why:
this also states -current was fixed at the time, so by now all supported releases are fixed.
(Also, can't reproduce)


>Unformatted:

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.39 2013/11/01 18:47:49 spz Exp $
$NetBSD: gnats_config.sh,v 1.9 2014/08/02 14:16:04 spz Exp $
Copyright © 1994-2007 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.