Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 8 of 8

Thread: fast trigonometric functions

  1. #1
    Junior Member Newbie
    Join Date
    May 2004
    Location
    Kentucky, USA
    Posts
    12

    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.

  2. #2
    Advanced Member Frequent Contributor plasmonster's Avatar
    Join Date
    Mar 2004
    Posts
    739

    Re: fast trigonometric functions

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

  3. #3
    Junior Member Newbie
    Join Date
    May 2004
    Location
    Kentucky, USA
    Posts
    12

    Re: fast trigonometric functions

    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

  4. #4
    Advanced Member Frequent Contributor plasmonster's Avatar
    Join Date
    Mar 2004
    Posts
    739

    Re: fast trigonometric functions

    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*

  5. #5
    Junior Member Regular Contributor
    Join Date
    Jun 2004
    Location
    Minneapolis, USA
    Posts
    100

    Re: fast trigonometric functions

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

  6. #6
    Intern Newbie
    Join Date
    Mar 2003
    Location
    SPAIN
    Posts
    42

    Re: fast trigonometric functions

    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!

  7. #7
    Intern Newbie
    Join Date
    Feb 2004
    Location
    Milan, Italy
    Posts
    47

    Re: fast trigonometric functions

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

  8. #8
    Intern Newbie
    Join Date
    Mar 2001
    Location
    Boston,MA USA
    Posts
    37

    Re: fast trigonometric functions

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •