Sign in to follow this  
eGamer

floating point functions

Recommended Posts

i am writing a Math Library and thinking of writing fast appoximate floating point functions [sqr-sin-cos,...]. but before this i want to ask one question : Are the floating point functions [found in C] fast enough on the nowaday Macnihes ? Also is there any library dedicated for fast calculations of floating point numbers ?

Share this post


Link to post
Share on other sites
From earlier days I still have an assembler solution for "fast square-root" and "fast inverse square-root" as is so usual for vector normalization. My lastest measurings (approx. 3 weeks ago), now done on a Q9550, show that the assembler routine is slower than a libc solution:

timing of ::sqrt = 0.014ms
timing of ::sqrtf = 0.011ms
timing of Float::fsqrt = 0.077ms

timing of 1.0f/::sqrt = 0.141ms
timing of 1.0f/::sqrtf = 0.052ms
timing of Float::finvsqrt = 0.068ms

Another example is the computation of sine and cosine at the same time, because for e.g. rotation matrices and quaternions both values are needed. There is a sincos routine available in some OSs, but unfortunately not on Mac OS (for what I develop as well). So I have an approximation (from http://www.stereopsis.com/computermath101):

timing of ::sin + ::cos = 0.238ms
timing of ::sinf + ::cosf = 0.235ms
timing of Angle::fsincos = 0.191ms

The approximation has also beaten the std::sincos solution of linux (tested just 4 years ago ;).

So the clear answer is: It depends! Especially if you ask for "fast enough" because that also depends on the use case!

Share this post


Link to post
Share on other sites
If you haven't found something to be a performance bottleneck, don't optimize it.

Quote:
Donald Knuth wrote:
“We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%.A good programmer will not be lulled into complacency by such reasoning, he will be wise to look carefully at the critical code; but only after that code has been identified.”


Share this post


Link to post
Share on other sites
If your code is really heavy on trigonometric functions (edit: or, of course, other heavy functions like sqrt), lookup table based solutions can indeed totally beat standard functions. If you even can allow you code to make assumptions on the input domain, you can increase performance even more. I made some tests some months ago, and got dramatic speedups.

But again, only do that if it makes sense and if you can live with the precision loss. An overall performance gain of 0.5% isn't worthful.

Share this post


Link to post
Share on other sites
Quote:
Original post by phresnel
If your code is really heavy on trigonometric functions


...then you probably aren't making effective enough use of linear algebra. :-)

Share this post


Link to post
Share on other sites
Quote:
Original post by Emergent
Quote:
Original post by phresnel
If your code is really heavy on trigonometric functions


...then you probably aren't making effective enough use of linear algebra. :-)


And if you are heavy on your so-called "linear algebra", you can make LUTs for them ... (kidding, but I am also serious)

Share this post


Link to post
Share on other sites
Quote:
Original post by eGamer
but before this i want to ask one question :
Are the floating point functions [found in C] fast enough on the nowaday Macnihes ?


In 90% of cases... yes.

I wouldn't bother optimising for small gains unless it has been identified as a bottleneck for your particular application.

Optimisation should always be a last resort!

Share this post


Link to post
Share on other sites
I used lookup tables for sin of angles [1..360], with a step of 0.5 Degrees, so that it contains 720 entries.
as we want to calculate the sin of an angle T:

if T is available then return The Sin of it.
else
Sin(T) = Sin(T0) + (T-T0)*[Sin(T1)-Sin(T0)]*2

which is linear interpolate equation.

when i made the tests, the performance were nearly equal [ Same Cycles].


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