Executing code at compilation time

Fernando <pronesto@gmail.com>
Mon, 15 Mar 2010 18:53:02 -0700 (PDT)

          From comp.compilers

Related articles
Executing code at compilation time pronesto@gmail.com (Fernando) (2010-03-15)
Re: Executing code at compilation time gah@ugcs.caltech.edu (glen herrmannsfeldt) (2010-03-16)
Re: Executing code at compilation time torbenm@diku.dk (2010-03-16)
Re: Executing code at compilation time dnovillo@acm.org (Diego Novillo) (2010-03-16)
Re: Executing code at compilation time bartc@freeuk.com (Bartc) (2010-03-16)
Re: Executing code at compilation time quinn_jackson2004@yahoo.ca (Quinn Tyler Jackson) (2010-03-16)
Re: Executing code at compilation time apoelstra@localhost.localdomain (Andrew Poelstra) (2010-03-16)
[13 later articles]
| List of all articles for this month |

From: Fernando <pronesto@gmail.com>
Newsgroups: comp.compilers
Date: Mon, 15 Mar 2010 18:53:02 -0700 (PDT)
Organization: Compilers Central
Keywords: optimize, question
Posted-Date: 16 Mar 2010 01:50:43 EDT

Dear all,


GCC does a pretty good job at optimizing a program like this one
below:


#include <stdio.h>


int main(int argc, char** argv) {
    int i = 0;
    int sum = 0;
    for (; i < 10; i++) {
        sum += i;
    }
    printf("The sum is %d\n", sum);
}


GCC -O1 produces an assembly that simply prints the answer, 45. It
completely resolves the loop.


I would like to know what kind of optimizations are used to be able to
completely resolve the loop. Could you give me some source? E.g,
chapter in textbook, or paper?


Additionally, GCC does not resolve the loop in the program below:


#include <stdio.h>


int main(int argc, char **argv) {
    int i, j, k, t = 0;
    for(i = 0; i < 1000; i++)
        for(j = 0; j < 1000; j++)
            for(k = 0; k < 1000; k++)
                if(i * i + j * j + k * k % 7 == 0)
                    t++;
    printf("%d\n", t);
    return 0;
}


But it is "solvable" at compilation time. Is there any sort of non-
standard optimization that does the job, in this case?


Thank you very much,


Fernando


Post a followup to this message

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