Create cosine table with gcc preprocessor
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.
source to share
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.
source to share
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]);
}
}
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.
source to share