How to make GCC evaluate functions at compile time?

后端 未结 3 711
广开言路
广开言路 2021-01-19 00:34

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.

Th

相关标签:
3条回答
  • 2021-01-19 00:48

    What you are trying is not part of the C language. In situations like this, I have written code following this pattern:

    #if GENERATE_SOURCECODE
    int main (void)
    {
        ... Code that uses printf to write C code to stdout
    }
    #else
        // Source code generated by the code above
        ... Here I paste in what the code above generated
    
        // The rest of the program
    #endif
    

    Every time you need to change it, you run the code with GENERATE_SOURCECODE defined, and paste in the output. Works well if your code is self contained and the generated output only ever changes if the code generating it changes.

    0 讨论(0)
  • 2021-01-19 00:56

    First of all, it should go without saying that you should evaluate (probably by experiment) whether this is worth doing. Your lookup table is going to increase your data size and programmer effort, but may or may not provide a runtime speed increase that you need.

    If you still want to do it, I don't think the C preprocessor can do it straightforwardly, because it has no facilities for iteration or recursion.

    The most robust way to go about this would be to write a program in C or some other language to print out C source for the table, and then include that file in your program using the preprocessor. If you are using a tool like make, you can create a rule to generate the table file and have your .c file depend on that file.

    On the other hand, if you are sure you are never going to change this table, you could write a program to generate it once and just paste it in.

    0 讨论(0)
  • 2021-01-19 01:12

    The general problem here is that sin call makes this initialization de facto illegal, according to rules of C language, as it's not constant expression per se and you're initializing array of static storage duration, which requires that. This also explains why your array is not in .data section.

    C11 (N1570) §6.6/2,3 Constant expressions (emphasis mine)

    A constant expression can be evaluated during translation rather than runtime, and accordingly may be used in any place that a constant may be.

    Constant expressions shall not contain assignment, increment, decrement, function-call, or comma operators, except when they are contained within a subexpression that is not evaluated.115)

    However as by @ShafikYaghmour's comment GCC will replace sin function call with its built-in counterpart (unless -fno-builtin option is present), that is likely to be treated as constant expression. According to 6.57 Other Built-in Functions Provided by GCC:

    GCC includes built-in versions of many of the functions in the standard C library. The versions prefixed with __builtin_ are always treated as having the same meaning as the C library function even if you specify the -fno-builtin option.

    0 讨论(0)
提交回复
热议问题