This is the mail archive of the gcc-bugs@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]

c/6677: GCC 3.1 has a bug in signed char treatment



>Number:         6677
>Category:       c
>Synopsis:       GCC 3.1 has a bug in signed char treatment
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          wrong-code
>Submitter-Id:   net
>Arrival-Date:   Thu May 16 07:36:02 PDT 2002
>Closed-Date:
>Last-Modified:
>Originator:     FreeBSD-gnats-submit@freebsd.org
>Release:        gcc (GCC) 3.1 [FreeBSD] 20020509 (prerelease)
>Organization:
>Environment:
  FreeBSD archon.local.idaemons.org 5.0-CURRENT FreeBSD 5.0-CURRENT #7: Mon May  6 01:19:19 JST 2002     root@archon.local.idaemons.org:/villa/work/obj/freebsd/src/villa/src/sys/ARCHON  i386

  gcc (GCC) 3.1 [FreeBSD] 20020509 (prerelease)
>Description:
GCC 3.1 has a bug in signed char treatment.  See below.
>How-To-Repeat:
knu@archon[2]% cat a.c
int main(void)
{
   unsigned char i = 127;
   char j;

   printf("%d\n", ((char)(i << 1)));
   j = ((char)(i << 1)) / 2;
   printf("%d\n", j);
   j = ((char)(i << 1));
   printf("%d\n", j / 2);
   return 0;
}
knu@archon[2]% cc a.c
knu@archon[2]% ./a.out
-2
127
-1

The second value should be equal to the third (-1). (char is signed on
the platform)

I can reproduce this with any of -O[0-6].

With gcc 2.95.x, this test code properly results in -2, -1 and -1.
>Fix:
Changing the division from `/ 2' to `>> 1' seems to work around the
problem, but I don't know why.
>Release-Note:
>Audit-Trail:
>Unformatted:


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