2013-10-06 12:07:42 +00:00
|
|
|
#pragma once
|
|
|
|
|
2014-05-02 06:30:32 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
using std::min;
|
|
|
|
using std::max;
|
|
|
|
|
2014-08-22 21:15:02 +00:00
|
|
|
#define re16(val) _byteswap_ushort(val)
|
|
|
|
#define re32(val) _byteswap_ulong(val)
|
|
|
|
#define re64(val) _byteswap_uint64(val)
|
|
|
|
#define re128(val) u128::byteswap(val)
|
2014-05-02 06:30:32 +00:00
|
|
|
|
2013-11-19 21:10:23 +00:00
|
|
|
template<typename T, int size = sizeof(T)> struct se_t;
|
|
|
|
template<typename T> struct se_t<T, 1> { static __forceinline void func(T& dst, const T src) { (u8&)dst = (u8&)src; } };
|
|
|
|
template<typename T> struct se_t<T, 2> { static __forceinline void func(T& dst, const T src) { (u16&)dst = _byteswap_ushort((u16&)src); } };
|
|
|
|
template<typename T> struct se_t<T, 4> { static __forceinline void func(T& dst, const T src) { (u32&)dst = _byteswap_ulong((u32&)src); } };
|
|
|
|
template<typename T> struct se_t<T, 8> { static __forceinline void func(T& dst, const T src) { (u64&)dst = _byteswap_uint64((u64&)src); } };
|
|
|
|
|
2014-05-02 06:30:32 +00:00
|
|
|
template<typename T> T re(const T val) { T res; se_t<T>::func(res, val); return res; }
|
|
|
|
template<typename T1, typename T2> void re(T1& dst, const T2 val) { se_t<T1>::func(dst, val); }
|
2014-02-22 02:53:06 +00:00
|
|
|
|
|
|
|
template<typename T, s64 _value, int size = sizeof(T)> struct const_se_t;
|
2013-11-19 10:30:58 +00:00
|
|
|
template<typename T, s64 _value> struct const_se_t<T, _value, 1>
|
2013-11-19 21:10:23 +00:00
|
|
|
{
|
|
|
|
static const T value = (T)_value;
|
|
|
|
};
|
|
|
|
|
2013-11-19 10:30:58 +00:00
|
|
|
template<typename T, s64 _value> struct const_se_t<T, _value, 2>
|
2013-11-19 21:10:23 +00:00
|
|
|
{
|
|
|
|
static const T value = ((_value >> 8) & 0xff) | ((_value << 8) & 0xff00);
|
|
|
|
};
|
|
|
|
|
2013-11-19 10:30:58 +00:00
|
|
|
template<typename T, s64 _value> struct const_se_t<T, _value, 4>
|
2013-11-19 21:10:23 +00:00
|
|
|
{
|
|
|
|
static const T value =
|
|
|
|
((_value >> 24) & 0x000000ff) |
|
|
|
|
((_value >> 8) & 0x0000ff00) |
|
|
|
|
((_value << 8) & 0x00ff0000) |
|
|
|
|
((_value << 24) & 0xff000000);
|
|
|
|
};
|
|
|
|
|
2013-11-19 10:30:58 +00:00
|
|
|
template<typename T, s64 _value> struct const_se_t<T, _value, 8>
|
2013-11-19 21:10:23 +00:00
|
|
|
{
|
|
|
|
static const T value =
|
|
|
|
((_value >> 56) & 0x00000000000000ff) |
|
|
|
|
((_value >> 40) & 0x000000000000ff00) |
|
|
|
|
((_value >> 24) & 0x0000000000ff0000) |
|
|
|
|
((_value >> 8) & 0x00000000ff000000) |
|
|
|
|
((_value << 8) & 0x000000ff00000000) |
|
|
|
|
((_value << 24) & 0x0000ff0000000000) |
|
|
|
|
((_value << 40) & 0x00ff000000000000) |
|
|
|
|
((_value << 56) & 0xff00000000000000);
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T, int size=sizeof(T)>
|
2013-10-06 12:07:42 +00:00
|
|
|
class be_t
|
|
|
|
{
|
2013-11-19 21:10:23 +00:00
|
|
|
static_assert(size == 1 || size == 2 || size == 4 || size == 8, "Bad be_t type");
|
2013-10-06 12:07:42 +00:00
|
|
|
T m_data;
|
|
|
|
|
|
|
|
public:
|
2014-02-22 02:53:06 +00:00
|
|
|
typedef T type;
|
2014-04-09 22:54:32 +00:00
|
|
|
|
2014-01-19 16:05:27 +00:00
|
|
|
const T& ToBE() const
|
2013-10-06 12:07:42 +00:00
|
|
|
{
|
|
|
|
return m_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
T ToLE() const
|
|
|
|
{
|
|
|
|
T res;
|
|
|
|
|
2013-11-19 21:10:23 +00:00
|
|
|
se_t<T>::func(res, m_data);
|
2013-10-06 12:07:42 +00:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FromBE(const T& value)
|
|
|
|
{
|
|
|
|
m_data = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FromLE(const T& value)
|
|
|
|
{
|
2013-11-19 21:10:23 +00:00
|
|
|
se_t<T>::func(m_data, value);
|
2013-10-06 12:07:42 +00:00
|
|
|
}
|
|
|
|
|
2014-02-22 02:53:06 +00:00
|
|
|
static be_t MakeFromLE(const T value)
|
|
|
|
{
|
|
|
|
be_t res;
|
|
|
|
res.FromLE(value);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static be_t MakeFromBE(const T value)
|
|
|
|
{
|
|
|
|
be_t res;
|
|
|
|
res.FromBE(value);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2013-10-06 12:07:42 +00:00
|
|
|
//template<typename T1>
|
|
|
|
operator const T() const
|
|
|
|
{
|
|
|
|
return ToLE();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T1>
|
|
|
|
operator const be_t<T1>() const
|
|
|
|
{
|
|
|
|
be_t<T1> res;
|
2014-07-18 16:55:26 +00:00
|
|
|
if (sizeof(T1) < sizeof(T))
|
|
|
|
{
|
|
|
|
res.FromBE(ToBE() >> ((sizeof(T)-sizeof(T1)) * 8));
|
|
|
|
}
|
|
|
|
else if (sizeof(T1) > sizeof(T))
|
|
|
|
{
|
2014-07-18 17:04:12 +00:00
|
|
|
res.FromLE(ToLE());
|
2014-07-18 16:55:26 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
res.FromBE(ToBE());
|
|
|
|
}
|
2013-10-06 12:07:42 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
be_t& operator = (const T& right)
|
|
|
|
{
|
|
|
|
FromLE(right);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2014-04-09 22:54:32 +00:00
|
|
|
be_t<T,size>& operator = (const be_t<T,size>& right) = default;
|
2013-10-06 12:07:42 +00:00
|
|
|
|
|
|
|
template<typename T1> be_t& operator += (T1 right) { return *this = T(*this) + right; }
|
|
|
|
template<typename T1> be_t& operator -= (T1 right) { return *this = T(*this) - right; }
|
|
|
|
template<typename T1> be_t& operator *= (T1 right) { return *this = T(*this) * right; }
|
|
|
|
template<typename T1> be_t& operator /= (T1 right) { return *this = T(*this) / right; }
|
|
|
|
template<typename T1> be_t& operator %= (T1 right) { return *this = T(*this) % right; }
|
|
|
|
template<typename T1> be_t& operator &= (T1 right) { return *this = T(*this) & right; }
|
|
|
|
template<typename T1> be_t& operator |= (T1 right) { return *this = T(*this) | right; }
|
|
|
|
template<typename T1> be_t& operator ^= (T1 right) { return *this = T(*this) ^ right; }
|
|
|
|
template<typename T1> be_t& operator <<= (T1 right) { return *this = T(*this) << right; }
|
|
|
|
template<typename T1> be_t& operator >>= (T1 right) { return *this = T(*this) >> right; }
|
|
|
|
|
|
|
|
template<typename T1> be_t& operator += (const be_t<T1>& right) { return *this = ToLE() + right.ToLE(); }
|
|
|
|
template<typename T1> be_t& operator -= (const be_t<T1>& right) { return *this = ToLE() - right.ToLE(); }
|
|
|
|
template<typename T1> be_t& operator *= (const be_t<T1>& right) { return *this = ToLE() * right.ToLE(); }
|
|
|
|
template<typename T1> be_t& operator /= (const be_t<T1>& right) { return *this = ToLE() / right.ToLE(); }
|
|
|
|
template<typename T1> be_t& operator %= (const be_t<T1>& right) { return *this = ToLE() % right.ToLE(); }
|
|
|
|
template<typename T1> be_t& operator &= (const be_t<T1>& right) { return *this = ToBE() & right.ToBE(); }
|
|
|
|
template<typename T1> be_t& operator |= (const be_t<T1>& right) { return *this = ToBE() | right.ToBE(); }
|
|
|
|
template<typename T1> be_t& operator ^= (const be_t<T1>& right) { return *this = ToBE() ^ right.ToBE(); }
|
|
|
|
|
|
|
|
template<typename T1> be_t operator & (const be_t<T1>& right) const { be_t<T> res; res.FromBE(ToBE() & right.ToBE()); return res; }
|
|
|
|
template<typename T1> be_t operator | (const be_t<T1>& right) const { be_t<T> res; res.FromBE(ToBE() | right.ToBE()); return res; }
|
|
|
|
template<typename T1> be_t operator ^ (const be_t<T1>& right) const { be_t<T> res; res.FromBE(ToBE() ^ right.ToBE()); return res; }
|
|
|
|
|
2013-11-16 00:38:22 +00:00
|
|
|
template<typename T1> bool operator == (T1 right) const { return (T1)ToLE() == right; }
|
2013-10-06 12:07:42 +00:00
|
|
|
template<typename T1> bool operator != (T1 right) const { return !(*this == right); }
|
2013-11-16 00:38:22 +00:00
|
|
|
template<typename T1> bool operator > (T1 right) const { return (T1)ToLE() > right; }
|
|
|
|
template<typename T1> bool operator < (T1 right) const { return (T1)ToLE() < right; }
|
|
|
|
template<typename T1> bool operator >= (T1 right) const { return (T1)ToLE() >= right; }
|
|
|
|
template<typename T1> bool operator <= (T1 right) const { return (T1)ToLE() <= right; }
|
2013-10-06 12:07:42 +00:00
|
|
|
|
|
|
|
template<typename T1> bool operator == (const be_t<T1>& right) const { return ToBE() == right.ToBE(); }
|
|
|
|
template<typename T1> bool operator != (const be_t<T1>& right) const { return !(*this == right); }
|
2013-11-16 00:38:22 +00:00
|
|
|
template<typename T1> bool operator > (const be_t<T1>& right) const { return (T1)ToLE() > right.ToLE(); }
|
|
|
|
template<typename T1> bool operator < (const be_t<T1>& right) const { return (T1)ToLE() < right.ToLE(); }
|
|
|
|
template<typename T1> bool operator >= (const be_t<T1>& right) const { return (T1)ToLE() >= right.ToLE(); }
|
|
|
|
template<typename T1> bool operator <= (const be_t<T1>& right) const { return (T1)ToLE() <= right.ToLE(); }
|
2014-02-22 02:53:06 +00:00
|
|
|
|
|
|
|
be_t operator++ (int) { be_t res = *this; *this += 1; return res; }
|
|
|
|
be_t operator-- (int) { be_t res = *this; *this -= 1; return res; }
|
|
|
|
be_t& operator++ () { *this += 1; return *this; }
|
|
|
|
be_t& operator-- () { *this -= 1; return *this; }
|
2013-11-19 21:10:23 +00:00
|
|
|
};
|
2014-02-22 02:53:06 +00:00
|
|
|
|
|
|
|
template<typename T, typename T1, T1 value> struct _se : public const_se_t<T, value> {};
|
|
|
|
template<typename T, typename T1, T1 value> struct _se<be_t<T>, T1, value> : public const_se_t<T, value> {};
|
|
|
|
|
|
|
|
#define se(t, x) _se<decltype(t), decltype(x), x>::value
|
|
|
|
#define se16(x) _se<u16, decltype(x), x>::value
|
|
|
|
#define se32(x) _se<u32, decltype(x), x>::value
|
|
|
|
#define se64(x) _se<u64, decltype(x), x>::value
|