From: Frank Peters <frank.peters@comcast.net>
To: gentoo-amd64@lists.gentoo.org
Subject: [gentoo-amd64] GCC-4.5.2 Has Serious Problems
Date: Thu, 30 Jun 2011 17:45:30 -0400 [thread overview]
Message-ID: <20110630174530.9bcbcd47.frank.peters@comcast.net> (raw)
[-- 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);
}
next reply other threads:[~2011-06-30 21:47 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-06-30 21:45 Frank Peters [this message]
2011-06-30 22:30 ` [gentoo-amd64] GCC-4.5.2 Has Serious Problems 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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20110630174530.9bcbcd47.frank.peters@comcast.net \
--to=frank.peters@comcast.net \
--cc=gentoo-amd64@lists.gentoo.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox