What do you guys think about my 24 bit integer class? I need it for processing audio data. I need it to be as fast as possible to reduce latency so if anybody has some suggestions please tell me.
Code:
#ifndef INT24
#define INT24
#pragma pack(push, 1)
/* __int24 class declaration
__int24 is a class used for 24 bit signed
integers. Most machines do not support 24 bit
integers, so a char array is used to keep up with
the bits. When operations need to be done, the
char array is converted to an integer, and then
the operation is done. 24 bit integers are very
typical when it comes to audio streaming, however,
because the of the lack of support for 24 bit
integers, it may run slightly slower which may
increase latency.
*/
class __int24 {
public:
/* Constructors */
__int24() {}
__int24(const __int24& val) { *this = val; }
__int24(const __int32& val) { *this = val; }
template <class T> __int24(const T& val) { *this = (__int32)val; }
/* Conversions */
operator __int32() {
__int32 val = byte[0] | (byte[1] << 8) | (byte[2] << 16);
if (val & 0x800000) val |= ~0xffffff;
return val;
}
template <class T> operator T () { return (T)this->operator __int32(); }
operator bool() const { return (__int32)*this != 0; }
/* Operators */
__int24& operator = (const __int24& val) {
byte[0] = val.byte[0];
byte[1] = val.byte[1];
byte[2] = val.byte[2];
return *this;
}
__int24& operator = (const __int32& val) {
byte[0] = (val & 0x000000ff);
byte[1] = (val & 0x0000ff00) >> 8;
byte[2] = (val & 0x00ff0000) >> 16;
return *this;
}
template <class T> __int24& operator = (const T& val) { return *this = (__int32)val; }
template <class T> __int24& operator += (const T& val) { return *this + val; }
template <class T> __int24& operator -= (const T& val) { return *this - val; }
template <class T> __int24& operator *= (const T& val) { return *this * val; }
template <class T> __int24& operator /= (const T& val) { return *this / val; }
template <class T> __int24& operator %= (const T& val) { return *this % val; }
__int24 operator + (__int24& val) { return __int24((__int32)*this + (__int32)val); }
template <class T> __int24 operator + (const T& val) { return __int24((__int32)*this + val); }
__int24 operator - (__int24& val) { return __int24((__int32)*this - (__int32)val); }
template <class T> __int24 operator - (const T& val) { return __int24((__int32)*this - val); }
__int24 operator * (__int24& val) { return __int24((__int32)*this * (__int32)val); }
template <class T> __int24 operator * (const T& val) { return __int24((__int32)*this * val); }
__int24 operator / (__int24& val) { return __int24((__int32)*this / (__int32)val); }
template <class T> __int24 operator / (const T& val) { return __int24((__int32)*this / val); }
__int24 operator % (__int24& val) { return __int24((__int32)*this % (__int32)val); }
template <class T> __int24 operator % (const T& val) { return __int24((__int32)*this % val); }
__int24 operator - () { return __int24(-(__int32)*this); }
bool operator ! () const { return this == 0; }
bool operator == (__int24& val) { return (__int32)*this == (__int32)val; }
template <class T> bool operator == (T& val) { return (__int32)*this == val; }
bool operator != (__int24& val) { return (__int32)*this != (__int32)val; }
template <class T> bool operator != (T& val) { return (__int32)*this != val; }
bool operator >= (__int24& val) { return (__int32)*this >= (__int32)val; }
template <class T> bool operator >= (T& val) { return (__int32)*this >= val; }
bool operator <= (__int24& val) { return (__int32)*this <= (__int32)val; }
template <class T> bool operator <= (T& val) { return (__int32)*this <= val; }
bool operator > (__int24& val) { return (__int32)*this > (__int32)val; }
template <class T> bool operator > (T& val) { return (__int32)*this > val; }
bool operator < (__int24& val) { return (__int32)*this < (__int32)val; }
template <class T> bool operator < (T& val) { return (__int32)*this < val; }
private:
unsigned char byte[3];
};
#pragma pack(pop)
#endif
EDIT: I just noticed my +=, -=, etc did not work because I forgot say the value equals it. I'm just used to making the += like operators first and then making the + like operators.