* Possible gcc bug?
@ 2000-06-11 5:03 Geoff Hutchison
2000-06-11 11:00 ` Martin Costabel
0 siblings, 1 reply; 3+ messages in thread
From: Geoff Hutchison @ 2000-06-11 5:03 UTC (permalink / raw)
To: linuxppc-dev
[-- Attachment #1: Type: text/plain, Size: 926 bytes --]
Before I send this message off to gcc-bugs, I wanted to check with
the list to see if it's a problem with the particular gcc RPM I'm
using (Franz's latest of gcc-2.95.3-2a.src.rpm). I also need to pare
the code down a bit more to see if I can come up with a smaller
testcase.
When compiled with optimization, x and y loops run correctly
gcc -O -o loop-bug loop-bug.c -lm
When compiled WITHOUT optimization, x and y loops run incorrectly
gcc -o loop-bug loop-bug.c -lm
Here's the problem. I'm doing some numerical calculations and main
loop isn't being executed! It seems like something is clobbering one
of my const declarations internally. There are other strange problems
with other parts of my original code when optimization is turned on,
but I'd rather one step at a time.
If I'm being a complete moron and doing something silly, please let
me know and I'll eat my words.
Thanks,
[-- Attachment #2.1: %loop-bug.c --]
[-- Type: application/applefile, Size: 514 bytes --]
[-- Attachment #2.2: loop-bug.c --]
[-- Type: application/octet-stream, Size: 1994 bytes --]
/*
Test program to expose loop bug in gcc
Tested on ppc-unknown-linux-gnu w/
gcc version 2.95.3 19991030 (prerelease/franzo)
( gcc-2.95.3-2a.src.rpm )
Also seems to have problems on IRIX 6.5 w/gcc-2.8.1
When compiled with optimization, x and y loops run correctly
gcc -O -o loop-bug loop-bug.c -lm
When compiled WITHOUT optimization, x and y loops run incorrectly
gcc -o loop-bug loop-bug.c -lm
by Geoff Hutchison <ghutchis@wso.williams.edu>
originally intended to numerically estimate electric fields across a surface
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
int main()
{
/* Size of the grid */
const double grid = 200.0;
const int maxX = grid / (2.0 * 2.295);
const int maxY = maxX;
const int loopX = maxX - 1;
const int loopY = maxY - 1;
const int maxPts = maxX*maxY;
const int loopPts = maxPts - 1;
const int shuffles = maxPts * 3;
int count, x, y;
double r, total = 0.0;
double field[loopX][loopY];
struct point {
int ptX;
int ptY;
} points[loopPts];
int tempx, tempy, rand1, rand2;
srand(time(0));
/* Initialize arrays */
for (x = 0; x <= loopX; x++)
for (y = 0; y <= loopY; y++)
{
field[x][y] = 0.0;
points[maxX * x + y].ptX = x;
points[maxX * x + y].ptY = y;
}
/* Shuffle points array */
for (count = 0; count <= shuffles; count++)
{
rand1 = rand() % maxPts;
rand2 = rand() % maxPts;
tempx = points[rand1].ptX;
tempy = points[rand1].ptY;
points[rand1].ptX = points[rand2].ptX;
points[rand1].ptY = points[rand2].ptY;
points[rand2].ptX = tempx;
points[rand2].ptY = tempy;
}
/* Main loop. Add another point and recalculate field */
for (count = 0; count <= loopPts; count++)
{
total = 0.0;
for (x = 0; x <= loopX; x++)
for (y = 0; y <= loopY; y++)
{
printf("%d\t%d\n", x, y);
}
}
return 0;
}
[-- Attachment #3: Type: text/plain, Size: 69 bytes --]
--
-Geoff Hutchison
Williams Students Online
http://wso.williams.edu/
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Possible gcc bug?
2000-06-11 5:03 Possible gcc bug? Geoff Hutchison
@ 2000-06-11 11:00 ` Martin Costabel
2000-06-11 14:08 ` Geoff Hutchison
0 siblings, 1 reply; 3+ messages in thread
From: Martin Costabel @ 2000-06-11 11:00 UTC (permalink / raw)
To: Geoff Hutchison; +Cc: linuxppc-dev
Geoff Hutchison wrote:
> If I'm being a complete moron and doing something silly, please let
> me know and I'll eat my words.
I think you do: You are passing over the array bounds. If I remember my
C basics (which I never really learned either) correctly,
double field[loopX][loopY];
allows indices running from 0 to loopX-1 and from 0 to loopY-1.
In your first loop
/* Initialize arrays */
for (x = 0; x <= loopX; x++)
for (y = 0; y <= loopY; y++)
{
field[x][y] = 0.0;
points[maxX * x + y].ptX = x;
points[maxX * x + y].ptY = y;
}
you are clobbering random memory locations that may be different for
different compiler optimisation levels.
Replace "<=" by "<", and your errors will go away.
--
Martin
** Sent via the linuxppc-dev mail list. See http://lists.linuxppc.org/
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Possible gcc bug?
2000-06-11 11:00 ` Martin Costabel
@ 2000-06-11 14:08 ` Geoff Hutchison
0 siblings, 0 replies; 3+ messages in thread
From: Geoff Hutchison @ 2000-06-11 14:08 UTC (permalink / raw)
To: Martin Costabel; +Cc: linuxppc-dev
At 1:00 PM +0200 6/11/00, Martin Costabel wrote:
>I think you do: You are passing over the array bounds. If I remember my
>C basics (which I never really learned either) correctly,
>
>double field[loopX][loopY];
Doh! Yes, as I said, I'll eat my words. This should fix things.
Thanks,
--
-Geoff Hutchison
Williams Students Online
http://wso.williams.edu/
** Sent via the linuxppc-dev mail list. See http://lists.linuxppc.org/
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2000-06-11 14:08 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2000-06-11 5:03 Possible gcc bug? Geoff Hutchison
2000-06-11 11:00 ` Martin Costabel
2000-06-11 14:08 ` Geoff Hutchison
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).