Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sun-barr!lll-winken!elroy.jpl.nasa.gov!swrinde!zaphod.mps.ohio-state.edu!rpi!uupsi!grebyn!ckp From: ckp@grebyn.com (Checkpoint Technologies) Newsgroups: comp.lang.c Subject: Heroic constant folding (was micro-optimizing loops etc.) Message-ID: <1991Mar2.010049.21044@grebyn.com> Date: 2 Mar 91 01:00:49 GMT References: <10191@dog.ee.lbl.gov> <14522@ganymede.inmos.co.uk> <10450@dog.ee.lbl.gov> Organization: Grebyn Timesharing Lines: 49 >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! \/