Create a Fast Sin() function to improve fps ? Fast sin() function?

I am rendering 500×500 points in real-time. I have to compute the position of points using atan() and sin() functions. By using atan() and sin() I am getting 24 fps (frames per second).

float thetaC = atan(value);
float h = (value) / (sin(thetaC)));

If I don’t use sin() I am getting 52 fps.

and if I dont use atan() I am 30 fps.

So, the big problem is with sin(). How can I use Fast Sin version. Can I create a Look Up Table for that ? I don’t have any specific values to create LUT. what can I do in this situation ?

PS: I have also tried fast sin function of ASM but not getting any difference.

Thanks.

Answer

It depends on the accuracy that you need. The maximum derivative of sin is 1, so if if x1 and x2 are within epsilon of one another, then sin(x1) and sin(x2) are also within epsilon. If you just need accuracy to within, say 0.001, then you can create a lookup table of 1000 * PI = 3142 points, and just look up the value closest to the one you need. This can be faster than what the native code does, since the native code (probably) uses a lookup table for polynomial coefficients, and then interpolates, and since this table can be small enough to stay in cache easily.

If you need complete accuracy over the whole range, then there’s probably nothing better that you can do.

If you wanted, you could also create a lookup table over (1/sin(x)), since that’s your actual function of interest. Either way, you’ll want to be careful around sin(x) = 0, since a small error in sin(x) can cause a big error in 1/sin(x). Defining your error tolerance is important for figuring out what shortcuts you can take.

You’d create the lookup table with something like:

float *table = malloc(1000 * sizeof(float));
for(int i = 0; i < 1000; i++){
  table[i] = sin(i/1000.0);
}

and would access it something like

float fastSin(float x){
  int index = x * 1000.0;
  return table[index];
}

This code isn’t complete (and will crash for anything outside of 0 < x < 1, because of array bounds), but should get you started.

Leave a Reply

Your email address will not be published. Required fields are marked *