Gentoo Archives: gentoo-amd64

From: Frank Peters <frank.peters@×××××××.net>
To: gentoo-amd64@l.g.o
Subject: [gentoo-amd64] GCC-4.5.2 Has Serious Problems
Date: Thu, 30 Jun 2011 21:47:28

After banging my head for a while over some strange results, I began
to suspect GCC-4.5.2, the latest version in portage, was creating
faulty code.

It seems to a correct suspicion.

What follows is a short C program that reproduces my particular problem,
but there are likely many other situations where GCC could fail.  The code
may be a difficult for some people to follow but only the output is what
actually matters.  Simpler examples will certainly exist but I have not
had the chance to develop them.

The program is included here as both in-line text and as a file attachment
(gcc_test.c).  The problem is described below.


#include <stdio.h>
#include <stdlib.h>

int main(void)
int n;
double x;
unsigned long int arg;
unsigned long int *px = (unsigned long int*)&x;

x=0.0; n=0; arg=0x4010000000000000;

printf("%lx %g\n",arg, x);


printf("%lx %g\n\n",arg, x);
n++; arg=arg+0x0010000000000000;



What this code does is not very important (it uses pointers to enter
hexadecimal values into a floating point variable within a short loop).
The output is what matters.

Compiling with "gcc -O2 -march=native -o gcc_test gcc_test.c"
produces this output:

Loop: 0
Val = 4010000000000000, X before =  0
Val = 4010000000000000, X after =   0   -->  X should be 4!!! 

Loop: 1
Val = 4020000000000000, X before =  0
Val = 4020000000000000, X after =   4

Loop: 2
Val = 4030000000000000, X before =  4
Val = 4030000000000000, X after =   8

The printf statements are included before and after the variable, x,
is assigned a value.  Notice how in the first iteration of the loop
the x variable does *not* get assigned a value.  The value of x during
the first iteration should be 4.

The problem can be fixed by compiling the program with "O1"

gcc -O1 -march=native -o gcc_test gcc_test.c

Using "O1" the output now becomes:

Loop: 0
Val = 4010000000000000, X before =  0
Val = 4010000000000000, X after =   4  --> Now it works!!!

Loop: 1
Val = 4020000000000000, X before =  4
Val = 4020000000000000, X after =   8

Loop: 2
Val = 4030000000000000, X before =  8
Val = 4030000000000000, X after =   16

This is now the correct output.  In the first iteration the X variable
is assigned the proper value.

For anyone who wants to try to duplicate these results, please feel
free to do so.

So I will have to conclude that GCC-4.5.2 has serious problems.
This kind of erroneous behavior could appear anywhere.
Frank Peters


File name MIME type
gcc_test.c text/x-csrc


Subject Author
Re: [gentoo-amd64] GCC-4.5.2 Has Serious Problems Alex Schuster <wonko@×××××××××.org>
[gentoo-amd64] Re: GCC-4.5.2 Has Serious Problems Nikos Chantziaras <realnc@×××××.de>