fast trigonometric functions

I’m having to use trigonometric functions in my opengl game, so I started looking for fast functions. I found some neat code here:
Fast Trig Functions
It uses some assembly code so I assumed it would be faster than the standard math library functions. However, to my surprise the standard math library was much faster. So, right now I’m using atan2, sin, and cos in my game and the performance hasn’t noticable suffered. My question is, are there any faster trig functions than the ones in the standard math library? That is, on a relatively modern processor, P4. My laptop has a Mobile Intel Pentium 4 M 1.8GHz.

It’s going to be hard, if not impossible, to beat a good look up table. Try comparing them again, this time in a release build (full optimizations).

wow, you were right. I must have run it in debug mode, even though I could have sworn I didn’t. It makes a major difference. Here’s my output in debug mode:
10000000 sin and cos computed in 2924 ticks with standard math funcs
10000000 sin and cos computed in 5708 ticks with fast[cos/sin]

And here is my output in release mode:
10000000 sin and cos computed in 2383 ticks with standard math funcs
10000000 sin and cos computed in 571 ticks with fast[cos/sin]

So the “fast” trig functions are actually faster, about 4x. cool.
thanks,
Chris Naulty

Yeah, that’s more like it :slight_smile: . Strange, I wouldn’t expect it to be slower in debug, on any system. But 2x slower, that’s really a surprise. On my system, the LUT is marginally faster in debug, but about 5x faster with optimizations as well. shrug

Here is something I always wanted to try for myself!

Instead of building regular table-lookups, code sine
and cosine based on a few terms in the Taylor series. It should be fast if you have the coefficients
precomputed.

Something like that. Give it a try if you have time.
You will want a high “accuracy to speed ratio.”

Hi, it’s a very goog idea to precompute and store discrete results for sin, cos, atan2…, then you can access to data at high speed. Probe it!

Originally posted by cirityone:
Hi, it’s a very goog idea to precompute and store discrete results for sin, cos, atan2…, then you can access to data at high speed. Probe it!
I find strange that a lookup table beats a direct calculation method with todays’ extremely high memory latencies. I am currently using the plain ANSI C library functions and the performance seems nice. Roughly 170 ticks for sin or cos (this is on a 1 GHz G4 with gcc 3.3.2, double precision). Since on my machine a cache miss and the subsequent memory access can cost 95 cycles I can’t see how a lookup-table based trig function can beat those ones…

Originally posted by crystall:
[quote]Originally posted by cirityone:
Hi, it’s a very goog idea to precompute and store discrete results for sin, cos, atan2…, then you can access to data at high speed. Probe it!
I find strange that a lookup table beats a direct calculation method with todays’ extremely high memory latencies. I am currently using the plain ANSI C library functions and the performance seems nice. Roughly 170 ticks for sin or cos (this is on a 1 GHz G4 with gcc 3.3.2, double precision). Since on my machine a cache miss and the subsequent memory access can cost 95 cycles I can’t see how a lookup-table based trig function can beat those ones…
[/QUOTE]I think you really need to consider the application. If you’re computing performing a lot of sin/cos coputations in a tight loop, (which is really the only time you’re going to care about how fast they are), then the lookup table is most likely to be in the cache after the first few computations, so you will see a siginificant performance increase over using the built-in version.