title: How to make GCC evaluate functions at compile time?
body: <p>I am thinking about the following problem: I want to program a microcontroller (let's say an AVR mega type) with a program that uses some sort of look-up tables.</p>
  
  <p>The first attempt would be to locate the table in a separate file and create it using any other scripting language/program/.... In this case there is quite some effort in creating the necessary source files for C.</p>
  
  <p>My thought was now to use the preprocessor and compiler to handle things. I tried to implement this with a table of sine values (just as an example):</p>
  
  <pre><code>#include &lt;avr/io.h&gt;
  #include &lt;math.h&gt;
  
  #define S1(i,n) ((uint8_t) sin(M_PI*(i)/n*255))
  #define S4(i,n) S1(i,n), S1(i+1,n), S1(i+2,n), S1(i+3,n)
  
  uint8_t lut[] = {S4(0,4)};
  
  void main()
  {
      uint8_t val, i;
  
      for(i=0; i&lt;4; i++)
      {
          val = lut[i];
      }
  }
  </code></pre>
  
  <p>If I compile this code I get warnings about the <code>sin</code> function. Further in the assembly there is nothing in the section <code>.data</code>. If I just remove the <code>sin</code> in the third line I get the data in the assembly. Clearly all information are available at compile time.</p>
  
  <p>Can you tell me if there is a way to achieve what I intent: The compiler calculates as many values as offline possible? Or is the best way to go using an external script/program/... to calculate the table entries and add these to a separate file that will just be <code>#include</code>d?</p>