简体   繁体   English

ANSI-C的编译时查找数组创建?

[英]Compile-time lookup array creation for ANSI-C?

A previous programmer preferred to generate large lookup tables (arrays of constants) to save runtime CPU cycles rather than calculating values on the fly. 以前的程序员首选生成大型查找表(常量数组)以节省运行时CPU周期,而不是动态计算值。 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. 他通过创建自定义的Visual C ++项目来实现这一点,这些项目对于每个单独的查找表都是唯一的......它们生成的数组文件然后被#included到一个完全独立的ANSI-C微控制器(Renesas)项目中。

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. 这种方法适用于他原来的计算假设,但在需要修改输入参数时变得乏味,需要我重新编译所有Visual C ++项目并将这些文件重新导入ANSI-C项目。 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. 我想要做的是将Visual C ++源代码直接移植到ANSI-C微控制器项目中,让编译器创建数组表。

So, my question is: Can ANSI-C compilers compute and generate lookup arrays during compile time? 所以,我的问题是:ANSI-C编译器可以在编译期间计算和生成查找数组吗? And if so, how should I go about it? 如果是这样,我应该怎么做呢?

Thanks in advance for your help! 在此先感谢您的帮助!

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: 我的意思是,在make中我可能会考虑这样的事情:

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 . 其中每个table_*包含一个完整的代码生成项目,其唯一目的是构建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. 在每个表目录中还有一个名为makefile.inc的makefile片段,它为生成的包含文件提供依赖行,现在我已经删除了递归。

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. 做得好(这个实现没有完成,部分是因为这一点更清楚,但主要是因为我很懒),你可以编辑table_3/table_3.input ,在主目录中键入make并获取table_3 / table_3.h重建和程序逐步重新编译。

I guess that depends on the types of value you need to look up. 我想这取决于你需要查找的值的类型。 If the processing to compute each value demands more than eg constant-expression evaluation can deliver, you're going to have problems. 如果计算每个值的处理需要的不仅仅是例如常量表达式评估可以提供,那么您将遇到问题。

Check out the Boost preprocessor library. 查看Boost预处理器库。 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. 它是为C ++编写的,但据我所知,这两个预处理器几乎完全相同,它可以做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM