PDA

View Full Version : Faster code possible?



ShaChris23
28th May 2008, 02:12
Hi,

What's the fastest way to extract the sign bit of a floating point value and put it into the sign bit of a 16-bit value?

We are trying to pack a

1) signed float
2) 1-bit value

into a 16-bit value.

The format is as followed:

16th bit (MSB): sign
15th - 1st bit: value (+/- 128)
0th bit (LSB): quality bit


Below is the code that I have; cant think of any other way to make it faster.
We are trying to minimize the use of an if statement. Any suggestions/comments would be greatly appreciated:


#include <math.h>

typedef enum
{
BAD,
GOOD
} BathyQuality;

class BathyAngle
{
public:
float Angle();
BathyQuality Quality();

public:
void SetAngle( float data );
void SetQuality( BathyQuality quality );

private:
short mData;
};



inline float BathyAngle::Angle()
{
if (mData < 0)
return ((mData & 0x7FFF) >> 1) / -128.0f;
else
return ((mData & 0x7FFF) >> 1) / 128.0f;
}
inline BathyQuality BathyAngle::Quality()
{
return static_cast< BathyQuality >( mData & 0x01 );
}

inline void BathyAngle::SetAngle( float data )
{
mData &= 0x1;

mData |= ( static_cast< short > ( fabs(data) * 128.0f ) ) << 1;

if( data < 0.0f )
{
mData |= 0x8000;
}
else
{
mData &= ~0x8000;
}
}
inline void BathyAngle::SetQuality( BathyQuality quality )
{
if( quality == GOOD )
mData |= GOOD;
else
mData &= ~GOOD;
}

wysota
28th May 2008, 07:35
Have you tried using bit fields?