Trig functions and lookup tables

Started by
18 comments, last by MENTAL 24 years ago
quote:Original post by s9801758

I learned that lookup tables for sine and cosine are done with. They take an enormous cache hit, that slows your program more down then using the standard sin and cosine methods.

Jaap Suter


Whatever, man. It all depends how you do it. If you are simply using 360 floats, I don''t think that''ll translate into a enormous cache hit. If you are doing a million levels of precision, then yeah, you would probably run into problems. I''ve actually never really looked into how sin() cos() are implemented, but the only way that it would be faster than a small lookup table is if it was built into hardware, and even then I''m not sure how much faster it would be.

Advertisement
Ahem, for sin and cos, if you have 360 different angles/entries, look-up tables are ALWAYS FASTER!!!!
I KNOW THIS!!
Just make a demo that plots cirlces using sin and cos, and then make the same demo with sin and cos look-up tables. Tables with make it zoom by much faster. Someone said that tables make the program take a performance hit, and I don''t what they were smoking, unless they meant a HUGE table.
RE: performance

Here''s a little rule of tumb.

When in doubt, make a little demo to see the results.
quote:
Ahem, for sin and cos, if you have 360 different angles/entries, look-up tables are ALWAYS FASTER!!!!

You know, there are very few true absolute statements in life....

Anyways, sin and cosine are implemented in the FPU (FSIN, FCOS instructions for you asm heads. See Sec 7.5.7 in the "Intel Architecture Software Developer''s Manual, Volume 1" if you don''t believe me.) A call to FSIN takes about 20 clock cycles. In comparison an L2 cache miss can take 40-50 cycles. (Exact numbers depend on which processor you have.) So for sufficiently infrequent calls of trig functions (or sufficiently bad memory layouts), there *is* a performance hit in using lookup tables, no matter what size they are.
The gods have spoken - now let it be!

Thanks SiCrane.

Zipster - Next time you compile your ''demo'', try setting the processor to Pentium.....

Bye now

Matt




Check out my project at: www.btinternet.com/~Matthew.Bennett
Now, let''s be fair, Matt, the demo he described is very sin/cos intensive. It will be faster to use table lookup, if all you are doing is drawing a circle. But, if your program requires only a single sin/cos every frame then it will probably be slower to use a table lookup, even if you only have 360 entries in the table.
Hey, i read it in a book. Don''t blame me, blame Andre!!
Argh! this wasn''t ment to be a flame war!

I only used 360 degrees as an example, and for a simple value for the variable i. I am infact using 1000 possible angles, and have found that lookup tables are much faster.

Thank you derek day for actually answering the question .

If I was just plotting circle it would be ok, but some of the meshes that need rotating consist of hundreds of verticies (one has well over 1000 - damn does it look good though), so the lookup tables are much quicker.

One more problem: I have found that some angles need to be rotated by the Z axis. would the by any chance:

x = ( cos_lookup_table( angle ) * radius ) + mid_x;
y = ( sin_lookup_table( angle ) * radius ) + mid_y;
z = ( tan_lookup_table( angle ) * radius ) + mid_z;

I''m only guessing because it''s the last trig function left

anyhelp appriciated.

MENTAL

PS: STOP FLAMING EACH OTHER!!!
P.P.S: I wouldn''t trust those intel books if i were you, unless it''s not actually written by someone at intel - they have a habit of lying (ok ok, i''m a hypocrite. now we can all stop flaming)
I didn''t see anyone flaming anyone, so I donno about that one...

Anywayz, I always use look-up tables for trig functions, since I''m always using hundreds of calulations per frame whenever i use sin and cos, tan and sec, csc and cot, etc.
Nothing I said was meant as a flame, but *I* was amazed at the difference it made when I was told to do that!!

OK it''s a very obvious step, but for me it wasn''t...

Anyway, I still hold with my original post. Use cos() and sin() first until you''re sure everything is working....

Cheers

Matt



Check out my project at: www.btinternet.com/~Matthew.Bennett

This topic is closed to new replies.

Advertisement