Create cosine table with gcc preprocessor

I want to make a cosine table at compile time. Is there a way to do this without hardcoding?

+2


source to share


8 answers


Why not make it hard? I am not aware of any changes as a result of the cosine function they are planning, well, not for another 100 years or so.



+12


source


I'm not sure if pre-calculating the sine table will result in performance improvement. I suggest:

  • Monitor your application by calling fcos () to decide if it's good enough. If so, stop here.
  • If this is really too slow, consider using -ffast-math if that's acceptable for your use.

Look-up tables, especially large ones, will increase the size of your program, which must be stored in the CPU cache, which reduces the hit rate. This, in turn, will slow down other parts of your application.



I am assuming that you are doing this in an incredibly tight loop, as this is the only case that could matter anyway.

If you've actually found using a lookup table to be helpful, why not just calculate it at runtime? It is unlikely to affect the startup time (unless it is huuuuuge). In fact, it can be faster at runtime because your processor can execute sines faster than your disk can load floats.

+9


source


With C ++, you can use template metaprogramming to generate your lookup table at runtime.

Now, this is a standard C trick that may or may not do what you want.

  • Write a program (say cosgen) that generates the C cosine table instruction (i.e. the code you desire).
  • Run cosgen and dump the output (c-code) to a file, say cos_table.c
  • In your main program, use #include "cos_table.c" to insert the table you want.
+8


source


You can create it in any scripting language you like and include the result. Use make to let the scripting language do its thing anytime you change source. It's hardcoded in C, but not you, really.

+5


source


With the magic of computers, the impossible becomes impossible:

#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

+2


source


Since you are targeting Cell, you are probably targeting SPE? They have proper FP support, vectorization really is, but they don't have great working memories. For this reason, BAD IDEA actually uses tables - you are sacrificing a very limited resource.

+2


source


I would create a hardcoded lookup table - once with a scripting language, but I'm not sure if that would be faster than just using the standard math library.

I think it depends on the size of the table, but I suspect FPU for computation might be faster than memory access. So when you have a table solution, I would put it in a comparison to see if it is faster than the standard function.

+1


source


Wavetables are the way to go. You can program it as prompted, or launch it during application startup.

0


source







All Articles