• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
EWClay

Fast sin and cos functions

9 posts in this topic

I was looking around for fast sin and cos functions, especially for calculating the two together using sse.

 

I found an old post on devmaster.net describing a fast approximation for sin, but the code was incomplete. So I took that and adapted it for sse intrinsics, adding a SinCos function so I could get the second one for free.

 

The code is here:

 

http://dtosoftware.wordpress.com/2013/01/07/fast-sin-and-cos-functions/

 

Two points:

  1. Feel free to use it, though don't bother unless you have a pressing performance reason; the standard sin and cos functions are actually pretty good.
  2. Can you do better? Can a look up table beat this? Or can the code be optimised even more?
0

Share this post


Link to post
Share on other sites

I was looking around for fast sin and cos functions, especially for calculating the two together using sse.

 

I found an old post on devmaster.net describing a fast approximation for sin, but the code was incomplete. So I took that and adapted it for sse intrinsics, adding a SinCos function so I could get the second one for free.

 

The code is here:

 

http://dtosoftware.wordpress.com/2013/01/07/fast-sin-and-cos-functions/

 

Two points:

  1. Feel free to use it, though don't bother unless you have a pressing performance reason; the standard sin and cos functions are actually pretty good.
  2. Can you do better? Can a look up table beat this? Or can the code be optimised even more?

Maybe you could build a Set with the most used angles. If you need an angle and it isn't in the set then calculate it with that algorithm and store the angle ->result into the set.

 

But this could only improve your app if you only need few different results...

1

Share this post


Link to post
Share on other sites
Note that your compiler may already supply a sincos() implementation. MSVC 2012 has Concurrency::precise_math::sincos() and Concurrency::fast_math::sincos(). And glibc has had sincos() since glibc 2.1.
1

Share this post


Link to post
Share on other sites

Recently I wrote some SIMD/AVX assembly language functions that compute:

 

 - two sines (in xmm registers)

 - two cosines (in xmm registers)

 - four sines (in ymm registers)

 - four cosines (in ymm registers)

 - four sines or cosines (in ymm registers) --- an arbitrary mix of sines and cosines

 

The last one takes an integer argument that specifies whether you want the sine or cosine for each argument.  The input angles and results are all f64 values (64-bit "double precision" floating point).

 

Writing those routines was quite the learning experience, in more ways than one!!!

 

The main purpose was to get very good at the newest AVX/XOP/FMA* and related instructions.  I haven't done a lot of completely parallel routines with SIMD instructions, and these were definitely completely parallel, since they compute the sines and cosines of up to four arguments exactly simultaneously.  Believe it or not, the entire routine to compute a random mix of 4 sines and cosines has zero jmps (conditional or unconditional).  The code just flows all the way through from start to finish!  It was quite satisfying to find that I could write the code that way, and major congrats to the guys at AMD [and Intel] for having just the right instructions to support this kind of flow-through code.

 

The routines are fast, especially when you consider you're getting 4 results for the price of one.  I was able to organize the instructions so the code pretty much never needed to access the result of an instruction until at least 4 instructions later, which seems to be the latency of 256-bit wide ymm instructions.

 

However, there is one downside.  Since the results are computed in the SIMD registers, not the 80-bit wide FPU registers, there are certain angles where the results are not quite as precise as the FPU sine or sine-cosine routines.  The results are always correct to 16-digits from the decimal point, but at certain angles (pi/2 if I recall correctly), where the result is almost zero, some significant bits are lost (even though the result is still correct to 16-digits from the decimal point).  I was able to definitively pin down the problem to a place in the routine (and all similar cheby or taylor routines) where a subtraction of similar-size numbers occurs.

 

I organized my algorithm to minimize the error, but it was impossible to eliminate.  Of course, most people don't give a damn about this precision loss, since they still have 16-digits of precision from the decimal point.  But I'm one of those people who sweats the very last bits in some of the computations I do, for example astrodynamics (solar-system orbit computations).  I made absolutely sure I knew where the problem was, by writing a version of the routines that performed the precision-losing subtracts on the mantissas alone in 64-bit integers... and that solved the "problem".

 

I actually have 5 variations with 7, 8, 9, 10, 11 chebyshev coefficients. The longer ones are supposed to be more precise, but it appear like 7 coefficiencts is sufficient (and is obviously a bit faster).

 

Anyway, if you're interested in the nitty gritty, I'll post one or two of these routines here.  Or if you prefer, PM or email them to you.

Edited by maxgpgpu
1

Share this post


Link to post
Share on other sites
You can achieve better precision (while not increasing the order of the approximation) by utilizing the fact sine is an odd function and by using the relative min-max approximation on [0,pi/2]. Also, you can achieve better performance by utilizing OOOE and the pipelined nature of modern CPUs (your code is highly sequential).

Here is the outline:
1. Bring x to [-pi,pi] just like you've done already.
2. Save the sign of x, and use the absolute value for the next operation (this is implemented by an 'and' followed by 'xor').
3. Compute y=0.3938708705960881*(pi/2 - abs(x - pi/2))
4. Compute z=y*(y-2.750773512942674)*(y-1.1527016230201428)*(y+0.800422269980487)
5. Apply the sign of x to z (this is a simple 'or' operation with the saved sign bit).
6. Return z

Note that in step 4 all three additions/subtractions don't depend on each other (this means that it'll take less time than two additions). Also you have three multiplication, two of which are pipelined and the third relies on the other two, this means that it'll cost you as about two multiplications. Just multiply in this order: a=1*2, b=3*4, z=a*b.
0

Share this post


Link to post
Share on other sites

For my purposes, an approximate solution is good enough, but full-precision is possibly a better approach in general, because it doesn't seem to add that much to the cost, certainly less than a factor of two. My code is not suitable for any kind of scientific work.

 

For a straight look up table, I would need around 1000 entries to get the same precision. Not that many, but the output wouldn't be smooth, and I think that's important. An interpolated look up would need fewer entries, but I suspect it may be slower than calculating.

 

And yes, I do need sin and cos, one way or another.

0

Share this post


Link to post
Share on other sites
BTW, a follow up to my previous post.
I totally omitted it, but you can obtain even more precision by employing the same divide&conquer technique to the seventh degree Taylor approximation of sin(x) (essentially it's an eighth degree approximation) restricted to [-pi/2,pi/2].
Just factor the approximation into:
(531.7555982144694 + x^2 * (x^2 - 32.521961561056756))*(x^2 - 9.47803843894324)*x/5040
Now perform all independent operations first. This includes computing x^2 very early and reusing it. Sadly, in this way the quadratic term (after x^2=y) cannot be factored into linear terms, but it's still quite efficient. Edited by max343
0

Share this post


Link to post
Share on other sites

I like it, it gives a couple of bits greater precision than my original version, and runs just as fast.

 

I find though, that if the original is inlined into the calling function, it's about 20% faster, perhaps because the processor is able to fit other instructions into the stalls.

 

I can certainly afford to inline, as I call this about twice in my entire program.

0

Share this post


Link to post
Share on other sites
[...] Can a look up table beat this?

I'd be surprised if a look up table beat it, simply because it'll likely result in a cache miss. If your look up table is in the cache, then perhaps, but that's really a case-by-case issue.

 

Cool stuff though.

1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0