View Full Version : fast trigonometric functions

fodder

06-03-2004, 11:29 AM

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 (http://www.flipcode.com/cgi-bin/msg.cgi?showThread=COTD-CosSin&forum=cotd&id=-1)

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.

plasmonster

06-03-2004, 12:31 PM

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).

fodder

06-03-2004, 01:44 PM

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

plasmonster

06-04-2004, 11:59 AM

Yeah, that's more like it :) . 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*

iznogoud

06-10-2004, 10:27 AM

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."

cirityone

06-13-2004, 11:40 PM

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!

crystall

06-14-2004, 08:07 AM

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...

dslater

06-20-2004, 04:24 AM

Originally posted by crystall:

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...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.

Powered by vBulletin® Version 4.2.2 Copyright © 2015 vBulletin Solutions, Inc. All rights reserved.