🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.gcc.help
Article #52125

A bug in gcc? or bad code

#52125
From: Marcos Mondragon
Date: Fri, 05 Feb 1993 09:44
115 lines
3797 bytes
This was presented to me and I was unable to determine
whether or not this was a bug or just bad code.  Any
Ideas?  The gcc versions that have been tested are:

gcc 1.36 -- oxygen below
gcc 2.3.1 -- phebos below

I have also tried it out on another machine where we have
gcc-2.3.3 loaded and it gives the same results as version
2.3.1.  Not being real familiar with the macro definition
style he is using(ie tmpsqr), I wonder if this is not the
problem.  I would appreciate any insight that I may recieve.

In advance, thanks

Marc Mondragon


-------------------------------------------------------------------
Marcos A. Mondragon			mam@oxygen.aps1.anl.gov
Advanced Photon Source			Argonne National Laboratory
9700 South Cass Avenue			Argonne, Illinois  60439
-------------------------------------------------------------------


---------- Begin Forwarded Message ----------
Message 23:
>From borland Thu Feb  4 16:57:20 1993
To: mam, frl
Subject: bug in gcc
Cc: borland, emery, evans
Content-Type: X-sun-attachment

----------
X-Sun-Data-Type: text
X-Sun-Data-Description: text
X-Sun-Data-Name: text
X-Sun-Content-Lines: 46

I've discovered a bug in gcc.  The attached program illustrates the bug.
Oddly enough, the bug is different on oxygen and phebos (which have different
version of the compiler).  I found the bug by tracking down a problem on
phebos related to my "sqr" macro (which squares its argument).

The output from the program when compiled and run on phebos is

sqr(a) + sqr(b) = 8             ! wrong
a*a + b*b = 20
sqr(a) = 4
sqr(b) = 16
sqr(a), sqr(b) = 4, 16
a, b, a+b = 2, 4, 6
a, b, a+b = 2, 2, 4             ! wrong

The output from the program when compiled and run on oxygen is

sqr(a) + sqr(b) = 20
a*a + b*b = 20
sqr(a) = 4
sqr(b) = 16
sqr(a), sqr(b) = 4, 16
a, b, a+b = 2, 4, 6
a, b, a+b = 4, 4, 8             ! wrong

The acc compiler on oxygen gets the following (which is all correct, as I see 
it):

sqr(a) + sqr(b) = 20
a*a + b*b = 20
sqr(a) = 4
sqr(b) = 16
sqr(a), sqr(b) = 4, 16
a, b, a+b = 2, 4, 6
a, b, a+b = 2, 4, 6

The SUN C compiler (cc) on oxygen gets the same result as acc.

The VAX C compiler gets the last two lines wrong. (It gives 2, 4, 8.)

I don't know enough about the assembly language to readily figure out what's
happening here in any detail.

--Michael


----------
X-Sun-Data-Type: c-file
X-Sun-Data-Description: c-file
X-Sun-Data-Name: test1.c
X-Sun-Content-Lines: 23

#include <stdio.h>
#include <math.h>

static double tmpsqr;
#define sqr(x) (tmpsqr=(x),tmpsqr*tmpsqr)

main()
{
    double a, b;

    a = 2;
    b = 4;

    printf("sqr(a) + sqr(b) = %g\n", sqr(a)+sqr(b));
    printf("a*a + b*b = %g\n", a*a+b*b);
    printf("sqr(a) = %g\n", sqr(a));
    printf("sqr(b) = %g\n", sqr(b));
    printf("sqr(a), sqr(b) = %g, %g\n", sqr(a), sqr(b));
    printf("a, b, a+b = %g, %g, %g\n", (tmpsqr=a), (tmpsqr=b), (tmpsqr=a)+(tmpsqr=b));
    printf("a, b, a+b = %g, %g, %g\n", (tmpsqr=a,tmpsqr), (tmpsqr=b,tmpsqr), (tmpsqr=a,tmpsqr)+(tmpsqr=b,tmpsqr));
    }


----------- End Forwarded Message -----------
Technical Details
Message-ID: <9302051940.AA06923@oxygen.aps1.anl.gov>
Path: arc1-ca.pugleaf.net!sparky!uunet!ogicse!uwm.edu!linac!pacific.mps.ohio-state.edu!cis.ohio-state.edu!oxygen.aps1.anl.gov!mam

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 11318c,cea452db2347a44c,start
X-Google-Attributes: gid11318c,public
X-Google-ArrivalTime: 1993-02-05 01:44:24 PST
Path: arc1-ca.pugleaf.net!sparky!uunet!ogicse!uwm.edu!linac!pacific.mps.ohio-state.edu!cis.ohio-state.edu!oxygen.aps1.anl.gov!mam
From: mam@oxygen.aps1.anl.gov (Marcos Mondragon)
Newsgroups: gnu.gcc.help
Subject: A bug in gcc? or bad code
Message-ID: <9302051940.AA06923@oxygen.aps1.anl.gov>
Date: 5 Feb 93 09:44:24 GMT
Article-I.D.: oxygen.9302051940.AA06923
Sender: daemon@cis.ohio-state.edu
Distribution: gnu
Organization: GNUs Not Usenet
Lines: 115