Fast sin and cos functions

Started by
8 comments, last by Cornstalks 11 years, 3 months ago

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?
Advertisement

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

The fastest trigonometric function is the one you don't evaluate. Doing your math in terms of vectors can eliminate most of the uses of sin and cos that I have seen in these forums.
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.

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.

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.

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.

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.

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.

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

[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

This topic is closed to new replies.

Advertisement