I'm not sure, if this can be called a bug, but it is at least a really bad case
of poor optimization.
The following program calls the function 'Square' several times, either with
x=1000 or x=i*2-i-i+1000 (which is also 1000). The second version is executed
much FASTER. I see no reason, why this should be so. I tested it with gcc 4.1.1
and 4.1.2. The timings are more or less equal.
> gcc -O2 f_demo.c f_demo2.c -o f_demo
> time f_demo
real 0m1.537s
user 0m1.183s
sys 0m0.345s
> gcc -D VARIABLE_PAR -O2 f_demo.c f_demo2.c -o f_demo
> time f_demo
real 0m0.700s
user 0m0.368s
sys 0m0.329s
--- f_demo.c -----------------------------------------------------------
#include <stdlib.h>
double Square(double x);
#ifdef VARIABLE_PAR
#define PAR i*2-i-i+1000
#else
#define PAR 1000
#endif
int main()
{
const int iSize=50000000;
int i;
double *pdA=malloc(iSize*sizeof(double));
for(i=0;i<iSize;i++) {
pdA[i]=Square(PAR);
}
}
--- f_demo2.c ----------------------------------------------------
double Square(double x)
{
return x*x;
}
--
Summary: Misoptimization of constant function expressions
Product: gcc
Version: 4.1.2
Status: UNCONFIRMED
Severity: minor
Priority: P3
Component: c
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: o dot mangold at gmx dot de
GCC build triplet: i386-redhat-linux
GCC host triplet: i386-pc-linux-gnu
GCC target triplet: i386-redhat-linux
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31263