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

Dan Bernstein brnstnd at kramden.acf.nyu.edu
Sat Mar 2 13:47:42 AEST 1991


In article <1991Mar2.010049.21044 at grebyn.com> ckp at grebyn.com (Checkpoint Technologies) writes:
> >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?  :-)

Actually, one of the few optimizations my q2c supports is loop
unrolling. If you tell it to, it will even go all the way to 101
separate statements, and there are probably some C compilers that can
collapse x += (1 + 5); x += (1 + 6)  into  x += 13.

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

In Q you can use any pure expression as an initializer. (And arrays are
first-class objects so a Fibonacci array initialization can be made
pure.) Right now I have q2c just stick the initializations at the top of
main(), but I'm thinking of changing it to a two-level system where it
first compiles the initializer code, then runs it to produce the C
initializers.

q2c is slowly improving... Expect to see a usable Q language by the end
of the summer.

---Dan



More information about the Comp.lang.c mailing list