Re: Why C is much slower than Fortran

Reid Tatge <reid@micro.ti.com>
20 May 1999 01:43:49 -0400

          From comp.compilers

Related articles
Re: Why C is much slower than Fortran sokal@holyrood.ed.ac.uk (Daniel Barker) (1999-04-18)
Re: Why C is much slower than Fortran telken@sgi.com (Thomas Elken) (1999-04-29)
Re: Why C is much slower than Fortran sokal@holyrood.ed.ac.uk (Daniel Barker) (1999-04-30)
Re: Why C is much slower than Fortran harley@corton.inria.fr (Robert Harley) (1999-05-03)
Re: Why C is much slower than Fortran hrubin@stat.purdue.edu (1999-05-09)
Re: Why C is much slower than Fortran terryg@uswest.net (1999-05-16)
Re: Why C is much slower than Fortran gneuner@dyn.com (1999-05-16)
Re: Why C is much slower than Fortran reid@micro.ti.com (Reid Tatge) (1999-05-20)
Re: Why C is much slower than Fortran jhallen@world.std.com (1999-05-29)
Re: Why C is much slower than Fortran hwstock@wizard.com (H.W. Stockman) (1999-06-02)
Re: Why C is much slower than Fortran erik@arbat.com (Erik Corry) (1999-06-02)
Re: Why C is much slower than Fortran lindahl@pbm.com (1999-06-02)
Re: Why C is much slower than Fortran sokal@holyrood.ed.ac.uk (Daniel Barker) (1999-06-02)
Re: Why C is much slower than Fortran djb@koobera.math.uic.edu (1999-06-02)
[5 later articles]
| List of all articles for this month |

From: Reid Tatge <reid@micro.ti.com>
Newsgroups: comp.lang.c++,comp.compilers
Date: 20 May 1999 01:43:49 -0400
Organization: Texas Instruments, Stafford
References: <3710584B.1C0F05F5@hotmail.com> 99-04-105 99-04-107 99-05-011 99-05-037 99-05-044
Keywords: C, performance

I agree that C has some "difficult" aliasing behavior.


However, in this kind of context, "p" generally can't alias "i" if "n" is
greater than 1. (!!) Here's the deal: the compiler is allowed to assume
that the program doesn't write past the end of any declared object. So if
the loop iterates more than once, writing to p[i] can't modify any scalar
int. Generally, if it iterates "x" times, it can't modify any declared
object of size ((x - 1) * sizeof(p[i])).


So, in the below example, where we don't know the trip count of the
loop, p[i] can only alias "i" in the first iteration. So the obvious
thing to do is peel the first iteration, and then promote "i" to a
register for the remaining iterations:


                i = 0;
                p[0] = p[0] + 1;
   for ( i++; i < n; i++ )
                        p[i] = p[i] + 1;


A useful piece of information when you're trying to get loop counters into
registers. More useful when you know "n" (like its a literal constant).


-Reid


On 16 May 1999, Terry Greyzck wrote:


> hrubin@stat.purdue.edu (Herman Rubin) wrote:
>
> Here's a simple ANSI C routine that illustrates the problem:
>
> int i;
>
> void
> pointer_alias( int * const p, const int n )
> {
> for ( i = 0; i < n; i++ ) {
> p[i] = p[i] + 1;
> }
> }
>
> It looks harmless, but as 'i' is global, the write through 'p' can
> change its value, changing the character of the loop. I've actually
> seen this type of code appear in 'real' applications.
>
> The new 'restrict' qualifier does help.


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.