This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: g77 vs gcc: address calculation of arrays in loops


Nicola Zingirian wrote:

> I take a simple SPEC95 loop written in Fortran that contains loads and
> stores from/to arrays and compile it using the g77 -O3. I obtain an
> assembly loop body which reduces the address computation to one
> instruction for each memory access (i.e. one add).
> Then, I compile the *same* loop, written in C, using gcc -O3. The
> assembly program generated now needs two instructions to calculate
> each address (i.e., one add + one shift).  The execution time
> difference is not negligible. (~ 28%)
> Why?

Because Fortran is faster than C !

....

[ After the dust settles due to the flame war just invoked, I'll try
  a serious attempt to answer this question: ]

>         DO 100 K = 0, LK - 1
>           X11 = X(I11+K)
>           X12 = X(I11+K+N)
>           X21 = X(I12+K)
>           X22 = X(I12+K+N)
>           T1 = X11 - X21
>           T2 = X12 - X22
>           Y(I21+K) = X11 + X21
>           Y(I21+K+N) = X12 + X22
>           Y(I22+K) = U1 * T1 - U2 * T2
>           Y(I22+K+N) = U1 * T2 + U2 * T1
>  100  CONTINUE

>  for (k = 0; k <= i__2; ++k) {
>             x11 = x[i11 + k];
>             x12 = x[i11 + k + n];
>             x21 = x[i12 + k];
>             x22 = x[i12 + k + n];
>             t1 = x11 - x21;
>             t2 = x12 - x22;
>             y[i21 + k] = x11 + x21;
>             y[i21 + k + n] = x12 + x22;
>             y[i22 + k] = u1 * t1 - u2 * t2;
>             y[i22 + k + n] = u1 * t2 + u2 * t1;
> /* L100: */
>         }

[ Two wrong guesses elided ... ]

>  * what else?

I bet x and y are arguments to the subroutine / function this loop is
in.  In that case Fortran's alias rules kick in:  A Fortran processor
(Standard-Speak for compiler+run-time) is allowed to assume that x and y
do not overlap.  A C compiler cannot assume that (short of using the C
'99 feature `restrict').

Hope this helps,

-- 
Toon Moene - mailto:toon@moene.indiv.nluug.nl - phoneto: +31 346 214290
Saturnushof 14, 3738 XG  Maartensdijk, The Netherlands
GNU Fortran 77: http://gcc.gnu.org/onlinedocs/g77_news.html
GNU Fortran 95: http://g95.sourceforge.net/ (under construction)

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]