Heroic constant folding (was micro-optimizing loops etc.)

Checkpoint Technologies ckp at grebyn.com
Sat Mar 2 12:00:49 AEST 1991


>Of course, the best optimization for:
>	for (i = 1; i < 100; i++)
>		x += i;
>is:
>	x += 4950;

OK, so how many more years must it be before a compiler will do this
optimization for you?  :-)

When I did a lot of assembly language, I used to look for constant
processing that could be done at assembly time, and do that with macros.
(I think it's important to illustrate the algorithm by which you arrive
at your constants.) This is ok if you have a powerful enough macro
language, but C doesn't.

So, as I think of it I was just doing manual "constant folding".  The C
compiler does some of that.  But I'd sure like to see a compiler that
could change the following:

--------------------------------
int fib_array[1024];

void init_fib(void)
{
    register int i;

    fib_array[0] = fib_array[2] = 1;
    for(i = 2; i < 1024; i++)
        fib_array[i] = fib_array[i-1] + fib_array[i-2];
}

int main(int argc, char **argv)
{
init_fib();
...
--------------------------------

...into...

--------------------------------
int fib_array[1024] = {1, 1, 2, 3, 5, 8, 13, 21, /* you get the idea */
--------------------------------

I know, this is asking a LOT. But hey, I can ask, can't I?
-- 
First comes the logo: C H E C K P O I N T  T E C H N O L O G I E S      / /  
                                                                    \\ / /    
Then, the disclaimer:  All expressed opinions are, indeed, opinions. \  / o
Now for the witty part:    I'm pink, therefore, I'm spam!             \/



More information about the Comp.lang.c mailing list