简体   繁体   English

用gcc预处理器制作余弦表

[英]Make a cosine table with the gcc preprocessor

I wish to make a cosine table at compile time. 我希望在编译时制作一个余弦表。 Is there a way to do this without hard coding anything? 有没有一种方法可以在不进行任何硬编码的情况下做到这一点?

Why not hardcode it? 为什么不硬编码呢? I am not aware of any changes in the result of the cosine function that they are planning, well not for another 100 years or so. 我不知道他们正在计划的余弦函数的结果会发生任何变化,最好不要再持续100年左右。

I am not convinced that precalculating a sine table would result in a performance improvement. 我不相信预先计算正弦表会导致性能提高。 I suggest: 我建议:

  1. Benchmark your application calling fcos() to decide whether it's fast enough. 对调用fcos()的应用程序进行基准测试,以确定它是否足够快。 If it is, stop here. 如果是,请在此处停止。
  2. If it really is too slow, consider using -ffast-math if it is acceptable for your usage. 如果确实太慢,请考虑使用-ffast-math(如果您的用法可以接受)。

Lookup tables, particularly large ones, will increase the size of your program that needs to be held in the CPU cache, which reduces its hit rate. 查找表(特别是大的查找表)将增加需要保留在CPU高速缓存中的程序的大小,从而降低其命中率。 This in turn will slow other parts of your application down. 反过来,这会使应用程序的其他部分变慢。

I am assuming you're doing this in an incredibly tight loop, as that's the only case it could possibly matter in anyway. 我假设您正在一个难以置信的紧密循环中执行此操作,因为这是无论如何都可能很重要的唯一情况。

If you actually DID discover that using a lookup table was beneficial, why not just precalculate it at runtime? 如果实际上DID发现使用查找表是有好处的,为什么不只在运行时对其进行预先计算呢? It's going to have hardly any impact on startup time (unless it's a huuuuuge one). 它几乎不会对启动时间产生任何影响(除非这是一个很大的挑战)。 It may actually be faster to do it at runtime, because your CPU may be able to do sines faster than your disc can load floats in. 实际上,在运行时这样做可能会更快,因为您的CPU可能比磁盘加载浮点的速度更快。

With C++, you can use templates metaprogramming to generate your lookup table at runtime. 使用C ++,您可以使用模板元编程在运行时生成查找表。

Now, here is a standard C trick that may or may not accomplish what you want. 现在,这是一个标准的C技巧,可能会或可能不会实现您想要的。

  1. Write a program (say, cosgen) that generates the cosine table C statement (ie, the code that you desire). 编写一个生成余弦表C语句(即所需代码)的程序(例如cosgen)。
  2. Run cosgen and dump the output (c code) to a file, say cos_table.c 运行cosgen并将输出(C代码)转储到文件中,例如cos_table.c
  3. In your main program, use a #include "cos_table.c" to insert the table where you want. 在主程序中,使用#include“ cos_table.c”将表插入所需的位置。

You could generate it with any scripting language you liked and include the result. 您可以使用任何喜欢的脚本语言来生成它,并包含结果。 Use make to have the scripting language do its thing anytime you change the source. 每当更改源时,都可以使用make来使脚本语言执行其操作。 It's hard coded to C but not to you, really. 确实,它是硬编码到C的,但不是硬编码的。

With the magic of computers, the apparently impossible becomes possible: 借助计算机的魔力,似乎不可能的事情成为可能:

#include <stdio.h>
#include <math.h>
#define MAX_ANGLE 90
double kinopiko_krazy_kosines[MAX_ANGLE];
int main ()
{
    int i;
    for (i = 0; i <= 90; i++) {
        double angle = (M_PI * i) / (2.0*90.0);
        kinopiko_krazy_kosines[i] = cos (angle);
        printf ("#define cos_%d %f\n", i, kinopiko_krazy_kosines[i]);
    }
}

http://codepad.org/G6JTATne http://codepad.org/G6JTATne

Since you're targetting Cell, you're probably targetting the SPE's? 由于您以Cell为目标,因此您可能以SPE为目标? They do have proper FP support, vectorised in fact, but do not have large working memories. 他们确实有适当的FP支持,实际上是矢量化的,但没有很大的工作记忆。 For that reason it's in fact a BAD IDEA to use tables - you're sacrificing a very limited resource. 因此,使用表实际上是一个糟糕的想法-您牺牲了非常有限的资源。

I'd create a hard-coded lookup table - once with a scripting language - but I'm not sure it'll be faster than just using the standard math library. 我将使用脚本语言创建一个硬编码的查找表,但是我不确定它会比仅使用标准数学库更快。

I guess it depends on the size of the table, but I would suspect getting the FPU to do the calculation might be faster than accessing memory. 我猜这取决于表的大小,但是我怀疑让FPU进行计算可能比访问内存更快。 So once you've got your table solution, I'd benchmark it to see if it's faster than the standard function. 因此,一旦您有了表解决方案,就对其进行基准测试,看看它是否比标准功能要快。

Wave tables are the way to go. 波浪表是必经之路。 You can hard code it as suggested, or run it during application start up. 您可以按照建议对它进行硬编码,或者在应用程序启动期间运行它。

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

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