Sign in to follow this  
Great_White

Why are sinf() or cosf() functions slow?

Recommended Posts

ToohrVyk    1595
If the library implementation that you use doesn't map sinf and cosf to the FPU instructions whenever available, your best solution is to get yourself a better library implementation from the web.

Share this post


Link to post
Share on other sites
SiCrane    11839
Depending on the compiler, you may have the option to automatically replace calls to sinf() and related functions directly with FPU calls. For example, with MSVC you should be able to specify /Oi /Og to get the trigonometric intrinsics.

Share this post


Link to post
Share on other sites
Great_White    148
Quote:
Original post by SiCrane
Depending on the compiler, you may have the option to automatically replace calls to sinf() and related functions directly with FPU calls. For example, with MSVC you should be able to specify /Oi /Og to get the trigonometric intrinsics.


Oh I didnt know that. Is there any preprocessor directive for this?

Share this post


Link to post
Share on other sites
fpsgamer    856
Quote:
Original post by Great_White
Quote:
Original post by SiCrane
Depending on the compiler, you may have the option to automatically replace calls to sinf() and related functions directly with FPU calls. For example, with MSVC you should be able to specify /Oi /Og to get the trigonometric intrinsics.


Oh I didnt know that. Is there any preprocessor directive for this?


Compiler optimizations are in your project settings.

Right click on your project name in the project tree.

edit: Note I'm talking about Visual Studio.

Share this post


Link to post
Share on other sites
Zahlman    1682
When you say "they are slow" and ask why:

1) how did you come to the conclusion that they are slow?
2) exactly how slow do you think they are?
3) is it really a problem?

Share this post


Link to post
Share on other sites
ToohrVyk    1595
Quote:
Original post by ViperG
If you use visual studio 6 they are slow. Especially sqrt function, super slow.


On the other hand, using Visual Studio 6 is just begging for trouble...

Share this post


Link to post
Share on other sites
C0D1F1ED    452
They are slow because they are complex to compute. Simple as that.

If I ask you to compute 1 + 1 and sin(1) on paper you quickly realize that the latter just isn't trivial to write down. And neither is it for a computer. In fact computing a sine or cosine consists of many additions and multiplications. Just look at the Taylor series expansion.

When you don't need the exact result there are fast approximations though: Fast and accurate sine/cosine.

Share this post


Link to post
Share on other sites
Spoonbender    1258
Quote:
Original post by C0D1F1ED
When you don't need the exact result there are fast approximations though: Fast and accurate sine/cosine.


Which may or may not be faster than the regular FPU instruction, whose may vary a lot from cpu to cpu.

So don't go looking for "faster" alternatives until you've gone through Zahlman's checklist.
Make sure that the functions you're using actually *are* slow, and that they're actually slow enough to be a problem.
Then look for alternative implementations, verify that they perform faster on more CPU's than just your own, and that the loss of precision from the isn't a problem.

Share this post


Link to post
Share on other sites
Jan Wassenberg    999
Incidentally, the way you've got that worded is one example of a strong case against using the libc functions (with or without intrinsics): if you need both sin AND cos, large gains are to be had by using the FSINCOS instruction.

But hey, even that is irrelevant. I would lay money on the table that Nick's clever code will completely leave behind any IA-32 implementation of sine/cosine. Continue to ignore his excellent writeup/derivation if you do not care about fast code.

Share this post


Link to post
Share on other sites
ViperG    206
I'm curious to try it since my game is using cos and sin all over the place, however I'm pretty sure I won't see a performance increase.

However I could do a controlled benchmark and see the difference between commenting out all my sin and cosine functions then one with them on to see if they are even affecting my game performance at all...

Share this post


Link to post
Share on other sites
ToohrVyk    1595
Quote:
Original post by ViperG
However I could do a controlled benchmark and see the difference between commenting out all my sin and cosine functions then one with them on to see if they are even affecting my game performance at all...


You would gather more relevant data by profiling your program and determining what percentage of time is spent evaluating those.

Share this post


Link to post
Share on other sites
Promit    13246
Quote:
Original post by ToohrVyk
You would gather more relevant data by profiling your program and determining what percentage of time is spent evaluating those.
Except that if intrinsics are on, they won't even show up in the profile. So if you really want to know how much time you spend in trig functions, that won't help at all.

Share this post


Link to post
Share on other sites
Zahlman    1682
Quote:
Original post by ViperG
I'm curious to try it since my game is using cos and sin all over the place, however I'm pretty sure I won't see a performance increase.


Well, are you sure you need to be using them as much as you are? Perhaps there are algebraic simplifications to be made, or maybe there's a better way to handle how velocities etc. are stored.

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