**Type :** floating point functions

**References :** Posted by tobybear[AT]web[DOT]de

**Notes :**

Haven't seen this elsewhere, probably because it is too obvious? Anyway, these functions are intended for 32-bit floating point numbers only and should work a bit faster than the regular ones.

fastabs() gives you the absolute value of a float

fastneg() gives you the negative number (faster than multiplying with -1)

fastsgn() gives back +1 for 0 or positive numbers, -1 for negative numbers

Comments are welcome (tobybear[AT]web[DOT]de)

Cheers

Toby (www.tobybear.de)

**Code :**

// C/C++ code:

float fastabs(float f)

{int i=((*(int*)&f)&0x7fffffff);return (*(float*)&i);}

float fastneg(float f)

{int i=((*(int*)&f)^0x80000000);return (*(float*)&i);}

int fastsgn(float f)

{return 1+(((*(int*)&f)>>31)<<1);}

//Delphi/Pascal code:

function fastabs(f:single):single;

begin i:=longint((@f)^) and $7FFFFFFF;result:=single((@i)^) end;

function fastneg(f:single):single;

begin i:=longint((@f)^) xor $80000000;result:=single((@i)^) end;

function fastsgn(f:single):longint;

begin result:=1+((longint((@f)^) shr 31)shl 1) end;

**Comments**

__from__ : tobybear[AT]web[DOT]de

__comment__ : Matthias (bekkah[AT]web[DOT]de) wrote me a mail with the following further improvements for the C++ parts of the code:
// C++ code:
inline float fastabs(const float f)
{int i=((*(int*)&f)&0x7fffffff);return (*(float*)&i);}
inline float fastneg(const float f)
{int i=((*(int*)&f)^0x80000000);return (*(float*)&i);}
inline int fastsgn(const float f)
{return 1+(((*(int*)&f)>>31)<<1);}
Thanks!

__from__ : picoder[AT]mail[DOT]ru

__comment__ : Too bad these 'tricks' need two additional FWAITs to work in a raw FPU code. Maybe standard fabs and fneg are better? Although, that fastsgn() could be useful since there's no FPU equivalent for it.
Cheers,
Aleksey.

__from__ : picoder[AT]mail[DOT]ru

__comment__ : I meant 'fchs' in place of 'fneg'.

__from__ : david[AT]brannvall[DOT]net

__comment__ : I don't know if this is any faster, but atleast you can avoid some typecasting.
function fastabs(f: Single): Single; var i: Integer absolute f;
begin i := i and $7fffffff; Result := f; end;

__from__ : chris[AT]m-audio[DOT]com

__comment__ : Note that a reasonable compiler should be able to perform these optimizations for you. I seem to recall that GCC in particular has the capability to replace calls to [f]abs() with instructions optimized for the platform.

__from__ : kaleja[AT]estarcion[DOT]com

__comment__ : On MS compilers for x86, just do:
#pragma intrinsic(fabs)
...and then use fabs() for doubles, fabsf() for floats. The compiler will generate the FABS instruction, which is generally 1 cycle on modern x86 FPUs. (Internally, the FPU just masks the bit.)