public inbox for gentoo-amd64@lists.gentoo.org
 help / color / mirror / Atom feed
* [gentoo-amd64] GCC-4.5.2 Has Serious Problems
@ 2011-06-30 21:45 Frank Peters
  2011-06-30 22:30 ` Alex Schuster
  2011-06-30 23:35 ` [gentoo-amd64] " Nikos Chantziaras
  0 siblings, 2 replies; 20+ messages in thread
From: Frank Peters @ 2011-06-30 21:45 UTC (permalink / raw
  To: gentoo-amd64

[-- Attachment #1: Type: text/plain, Size: 2548 bytes --]

Hello,

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;
while(n<5)
{

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

*px=arg;

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

exit(0);
}

-------------------------

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"
optimization:

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


[-- Attachment #2: gcc_test.c --]
[-- Type: text/x-csrc, Size: 393 bytes --]

#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;
while(n<3)
{

printf("%s %d\n%s %lx, %s %g\n", "Loop:", n,"Val =", arg, "X before = ", x);

*px=arg;

printf("%s %lx, %s %g\n\n", "Val =", arg, "X after =  ", x);

n++; arg=arg+0x0010000000000000;
}

exit(0);
}

^ permalink raw reply	[flat|nested] 20+ messages in thread

end of thread, other threads:[~2011-07-01 16:56 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-06-30 21:45 [gentoo-amd64] GCC-4.5.2 Has Serious Problems Frank Peters
2011-06-30 22:30 ` Alex Schuster
2011-06-30 23:34   ` Frank Peters
2011-06-30 23:35 ` [gentoo-amd64] " Nikos Chantziaras
2011-06-30 23:44   ` Nikos Chantziaras
2011-07-01  0:04     ` Frank Peters
2011-07-01  0:11       ` Nikos Chantziaras
2011-07-01  0:58         ` Duncan
2011-07-01  1:23           ` Frank Peters
2011-07-01  1:52             ` Duncan
2011-07-01  2:15             ` Ian McCulloch
2011-07-01  4:36             ` Mark Knecht
2011-07-01  5:22               ` Frank Peters
2011-07-01  0:25     ` Frank Peters
2011-07-01  1:04     ` Frank Peters
2011-07-01  2:18       ` Duncan
2011-07-01  2:22       ` Barry Schwartz
2011-07-01  2:43         ` Frank Peters
2011-07-01 11:53       ` Nikos Chantziaras
2011-07-01 16:53         ` Frank Peters

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox