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

Started by
16 comments, last by Zahlman 16 years, 8 months ago
Hi, I am wondering why these functions are slow in C++ lib. Should I use FPU versions?
"A human being is a part of a whole, called by us,universe, a part limited in time and space. He experiences himself, his thoughts and feelings as something separated from the rest... a kind of optical delusion of his consciousness. This delusion is a kind of prison for us, restricting us to our personal desires and to affection for a few persons nearest to us. Our task must be to free ourselves from this prison by widening our circle of compassion to embrace all living creatures and the whole of nature in its beauty."A. Einstein
Advertisement
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.
Are they slow? You can always precalc them, and them not have to worry about speed.
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.
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?
"A human being is a part of a whole, called by us,universe, a part limited in time and space. He experiences himself, his thoughts and feelings as something separated from the rest... a kind of optical delusion of his consciousness. This delusion is a kind of prison for us, restricting us to our personal desires and to affection for a few persons nearest to us. Our task must be to free ourselves from this prison by widening our circle of compassion to embrace all living creatures and the whole of nature in its beauty."A. Einstein
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.
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?
If you use visual studio 6 they are slow. Especially sqrt function, super slow.
Black Sky A Star Control 2/Elite like game
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...

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.

This topic is closed to new replies.

Advertisement