[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    Re: ARTS_1_1_BRANCH: arts/soundserver
From:       Michael Matz <matz () kde ! org>
Date:       2003-07-31 23:51:36
[Download RAW message or body]

Hi,

On Fri, 1 Aug 2003, Matthias Kretz wrote:

> float a = 1.2;
> float b = fabs( a );
>
> is not equivalent to
>
> float a = -1.2;
> float b = fabsf( a );
>
> right?

It is.  converting to double (for fabs()) is exact, changing the sign too,
hence the (double) return value is still representable in float, hence the
conversion back to float doesn't loose precision.

> The second one should be more efficient (I'm thinking of loops where
> fabs( float ) is called like 4*44100 times per second), no?

Practically not.  Depending on the exact circumstances the fabs is
already carried out with an AND internally.  And an fabs(float) is
converted to an fabsf() already too (since, hmm gcc 3.3 or 3.4).

> float myfabsf( a )
> {
> 	long x = 0x7FFFFFFF & *((long*)&a);
> 	return *((float*)&x);
> }

Yikes!  Aliasing problems.  Don't do that.  Note that the above might
_not_ be faster than a simple fabs opcode (I'm talking about x86 for now).
For instance in this sequence:

float a,f = ...
f = fabs(f);
a = a * f;

With the above method the float f would have to be forced to memory, the
and applied to the memory, and then loaded back into the FPU.  A simple
fabs opcode (taking 1 cycle) simply is faster.

Just simply use fabs() and be done with it.


Ciao,
Michael.

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic