129 lines
2.8 KiB
C++
129 lines
2.8 KiB
C++
/*
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <cinttypes>
|
|
|
|
namespace OpenVulkano
|
|
{
|
|
class int24 final
|
|
{
|
|
uint8_t m_internal[3];
|
|
public:
|
|
constexpr int24() : m_internal{ 0, 0, 0 }
|
|
{}
|
|
|
|
constexpr int24(const int val)
|
|
: m_internal{(uint8_t)(val & 0xff), (uint8_t)((val >> 8) & 0xff), (uint8_t)((val >> 16) & 0xff)}
|
|
{}
|
|
|
|
constexpr int24(const int24& val) : m_internal{ val.m_internal[0], val.m_internal[1], val.m_internal[2] }
|
|
{}
|
|
|
|
operator int() const
|
|
{
|
|
if (m_internal[2] & 0x80)
|
|
{ // Negative numbers
|
|
return (0xff << 24) | (m_internal[2] << 16) | (m_internal[1] << 8) | (m_internal[0] << 0);
|
|
}
|
|
else
|
|
{
|
|
return (m_internal[2] << 16) | (m_internal[1] << 8) | (m_internal[0] << 0);
|
|
}
|
|
}
|
|
|
|
operator float() const
|
|
{
|
|
return (float)this->operator int();
|
|
}
|
|
|
|
int24& operator =(const int24& input)
|
|
{
|
|
m_internal[0] = input.m_internal[0];
|
|
m_internal[1] = input.m_internal[1];
|
|
m_internal[2] = input.m_internal[2];
|
|
return *this;
|
|
}
|
|
|
|
int24& operator =(const int input)
|
|
{
|
|
m_internal[0] = ((unsigned char*)&input)[0];
|
|
m_internal[1] = ((unsigned char*)&input)[1];
|
|
m_internal[2] = ((unsigned char*)&input)[2];
|
|
return *this;
|
|
}
|
|
|
|
#define INT24_OPERATORS(op) \
|
|
int24 operator op(const int24& val) const { return int24( (int)*this op (int)val ); } \
|
|
int24 operator op(const int val) const { return int24( (int)*this op val ); }
|
|
|
|
INT24_OPERATORS(+)
|
|
INT24_OPERATORS(-)
|
|
INT24_OPERATORS(*)
|
|
INT24_OPERATORS(/)
|
|
#undef INT24_OPERATORS
|
|
|
|
#define INT24_OPERATORS(op) \
|
|
int24& operator op##=(const int24& val ) { *this = *this op val; return *this; } \
|
|
int24& operator op##=(const int val ) { *this = *this op val; return *this; }
|
|
|
|
INT24_OPERATORS(+)
|
|
INT24_OPERATORS(-)
|
|
INT24_OPERATORS(*)
|
|
INT24_OPERATORS(/)
|
|
#undef INT24_OPERATORS
|
|
|
|
int24 operator >>(const int val) const
|
|
{
|
|
return int24( (int)*this >> val );
|
|
}
|
|
|
|
int24 operator <<(const int val) const
|
|
{
|
|
return int24( (int)*this << val );
|
|
}
|
|
|
|
int24& operator >>=(const int val )
|
|
{
|
|
*this = *this >> val;
|
|
return *this;
|
|
}
|
|
|
|
int24& operator <<=(const int val )
|
|
{
|
|
*this = *this << val;
|
|
return *this;
|
|
}
|
|
|
|
operator bool() const
|
|
{
|
|
return (int)*this != 0;
|
|
}
|
|
|
|
bool operator !() const
|
|
{
|
|
return !((int)*this);
|
|
}
|
|
|
|
int24 operator -()
|
|
{
|
|
return int24( -(int)*this );
|
|
}
|
|
|
|
#define INT24_OPERATORS(op) \
|
|
bool operator op(const int24& val) const { return (int)*this op (int)val; } \
|
|
bool operator op(const int val) const { return (int)*this op val; }
|
|
|
|
INT24_OPERATORS(==)
|
|
INT24_OPERATORS(!=)
|
|
INT24_OPERATORS(>=)
|
|
INT24_OPERATORS(<=)
|
|
INT24_OPERATORS(>)
|
|
INT24_OPERATORS(<)
|
|
#undef INT24_OPERATORS
|
|
};
|
|
} |