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:

Qt Code:
  1. #include <math.h>
  2.  
  3. typedef enum
  4. {
  5. BAD,
  6. GOOD
  7. } BathyQuality;
  8.  
  9. class BathyAngle
  10. {
  11. public:
  12. float Angle();
  13. BathyQuality Quality();
  14.  
  15. public:
  16. void SetAngle( float data );
  17. void SetQuality( BathyQuality quality );
  18.  
  19. private:
  20. short mData;
  21. };
  22.  
  23.  
  24.  
  25. inline float BathyAngle::Angle()
  26. {
  27. if (mData < 0)
  28. return ((mData & 0x7FFF) >> 1) / -128.0f;
  29. else
  30. return ((mData & 0x7FFF) >> 1) / 128.0f;
  31. }
  32. inline BathyQuality BathyAngle::Quality()
  33. {
  34. return static_cast< BathyQuality >( mData & 0x01 );
  35. }
  36.  
  37. inline void BathyAngle::SetAngle( float data )
  38. {
  39. mData &= 0x1;
  40.  
  41. mData |= ( static_cast< short > ( fabs(data) * 128.0f ) ) << 1;
  42.  
  43. if( data < 0.0f )
  44. {
  45. mData |= 0x8000;
  46. }
  47. else
  48. {
  49. mData &= ~0x8000;
  50. }
  51. }
  52. inline void BathyAngle::SetQuality( BathyQuality quality )
  53. {
  54. if( quality == GOOD )
  55. mData |= GOOD;
  56. else
  57. mData &= ~GOOD;
  58. }
To copy to clipboard, switch view to plain text mode