Results 1 to 2 of 2

Thread: Faster code possible?

  1. #1
    Join Date
    Apr 2007
    Posts
    62
    Thanks
    43
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Faster code possible?

    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 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Faster code possible?

    Have you tried using bit fields?

Similar Threads

  1. Qt 4.3.0 lots of Bugs!
    By VireX in forum Qt Programming
    Replies: 69
    Last Post: 20th June 2007, 22:05
  2. Qt Cryptographic Architecture
    By vermarajeev in forum Qt Programming
    Replies: 6
    Last Post: 9th February 2007, 13:15
  3. Problem closing a QMainWindow in Qt4.2
    By ian in forum Qt Programming
    Replies: 11
    Last Post: 17th October 2006, 00:49
  4. problem with linking
    By mickey in forum Qt Programming
    Replies: 49
    Last Post: 12th August 2006, 21:41

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.