 |
fast abs/neg/sign for 32bit floats
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
Added on : 05/01/03 by 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!
Added on : 11/01/03 by 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.
Added on : 11/01/03 by picoder[ AT ]mail[ DOT ]ru Comment : I meant 'fchs' in place of 'fneg'.
Added on : 05/05/03 by 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;
Added on : 29/07/03 by 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.
Added on : 25/05/05 by 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.)
|
Add your own comment
Comments are displayed in fixed width, no HTML code allowed! |
|
|
 |
|