views:

180

answers:

3

A previous programmer preferred to generate large lookup tables (arrays of constants) to save runtime CPU cycles rather than calculating values on the fly. He did this by creating custom Visual C++ projects that were unique for each individual lookup table... which generate array files that are then #included into a completely separate ANSI-C micro-controller (Renesas) project.

This approach is fine for his original calculation assumptions, but has become tedious when the input parameters need to be modified, requiring me to recompile all of the Visual C++ projects and re-import those files into the ANSI-C project. What I would like to do is port the Visual C++ source directly into the ANSI-C microcontroller project and let the compiler create the array tables.

So, my question is: Can ANSI-C compilers compute and generate lookup arrays during compile time? And if so, how should I go about it?

Thanks in advance for your help!

+1  A: 

I guess that depends on the types of value you need to look up. If the processing to compute each value demands more than e.g. constant-expression evaluation can deliver, you're going to have problems.

unwind
+4  A: 

Is there some reason you can't import his code generation architecture to your build system?

I mean, in make I might consider something like:

TABLES:=$(wildcard table_*)
TABLE_INCS:=$(foreach dir,$TABLES,$dir/$dir.h)
include $(foreach dir,$TABLES,dir/makefile.inc)

$MAIN: $(SRS) $(TABLE_INCS)

where each table_* contains a complete code generation project whose sole purpose is tho build table_n/table_n.h. Also in each table directory a makefile fragment named makefile.inc which provides the dependency lines for generated include files, and now I've removed the recursivity.

Done right (and this implementation isn't finished, in part because the point is clearer this way but mostly because I am lazy), you could edit table_3/table_3.input, type make in the main directory and get table_3/table_3.h rebuilt and the program incrementally recompiled.

dmckee
That is certainly one approach I could take. So from your experience, a compiler itself cannot generate calculated array data, thus I need to rely on external script engines or binaries to get the job done? Would any preprocessor directives/macros be able to compute such tables?
multiproximus
@multiproximus: Sometimes you can do something with the preprocessor, but if I read you question right your inherited code comes with an working code generation scheme, no? The idea here is to take advantage of that. So I'd get the build system to deal with the code generation, but you haven't said what you are using to manage your build (doing it by hand, ide builtin, make, scons, etc...).
dmckee
A: 

Check out the Boost preprocessor library. It's written for C++ but as far as I'm aware, the two preprocessors are pretty much identical, and it can do this sort of thing.

DeadMG