mirror of
https://github.com/RPCS3/rpcs3.git
synced 2025-07-03 13:31:27 +12:00
be_t constructor implemented, make() eliminated
be_t enums are forbidden, le_t improved, some operators cleaned.
This commit is contained in:
parent
a800d21a31
commit
ef6f9f6ded
47 changed files with 917 additions and 1046 deletions
|
@ -1,6 +1,6 @@
|
|||
#pragma once
|
||||
|
||||
#define IS_LE_MACHINE
|
||||
#define IS_LE_MACHINE // only draft
|
||||
|
||||
union u128
|
||||
{
|
||||
|
@ -334,26 +334,6 @@ union u128
|
|||
return (_u64[0] != right._u64[0]) || (_u64[1] != right._u64[1]);
|
||||
}
|
||||
|
||||
force_inline u128 operator | (const u128& right) const
|
||||
{
|
||||
return fromV(_mm_or_si128(vi, right.vi));
|
||||
}
|
||||
|
||||
force_inline u128 operator & (const u128& right) const
|
||||
{
|
||||
return fromV(_mm_and_si128(vi, right.vi));
|
||||
}
|
||||
|
||||
force_inline u128 operator ^ (const u128& right) const
|
||||
{
|
||||
return fromV(_mm_xor_si128(vi, right.vi));
|
||||
}
|
||||
|
||||
u128 operator ~ () const
|
||||
{
|
||||
return from64(~_u64[0], ~_u64[1]);
|
||||
}
|
||||
|
||||
force_inline bool is_any_1() const // check if any bit is 1
|
||||
{
|
||||
return _u64[0] || _u64[1];
|
||||
|
@ -381,15 +361,32 @@ union u128
|
|||
|
||||
static force_inline u128 byteswap(const u128 val)
|
||||
{
|
||||
u128 ret;
|
||||
ret._u64[0] = _byteswap_uint64(val._u64[1]);
|
||||
ret._u64[1] = _byteswap_uint64(val._u64[0]);
|
||||
return ret;
|
||||
return fromV(_mm_shuffle_epi8(val.vi, _mm_set_epi8(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)));
|
||||
}
|
||||
};
|
||||
|
||||
CHECK_SIZE_ALIGN(u128, 16, 16);
|
||||
|
||||
inline u128 operator |(const u128& left, const u128& right)
|
||||
{
|
||||
return u128::fromV(_mm_or_si128(left.vi, right.vi));
|
||||
}
|
||||
|
||||
inline u128 operator &(const u128& left, const u128& right)
|
||||
{
|
||||
return u128::fromV(_mm_and_si128(left.vi, right.vi));
|
||||
}
|
||||
|
||||
inline u128 operator ^(const u128& left, const u128& right)
|
||||
{
|
||||
return u128::fromV(_mm_xor_si128(left.vi, right.vi));
|
||||
}
|
||||
|
||||
inline u128 operator ~(const u128& other)
|
||||
{
|
||||
return u128::from64(~other._u64[0], ~other._u64[1]);
|
||||
}
|
||||
|
||||
static force_inline u128 sync_val_compare_and_swap(volatile u128* dest, u128 comp, u128 exch)
|
||||
{
|
||||
#if !defined(_MSC_VER)
|
||||
|
@ -583,22 +580,8 @@ template<typename T> struct be_storage<T, 16>
|
|||
|
||||
template<typename T> using be_storage_t = typename be_storage<T>::type;
|
||||
|
||||
template<typename T>
|
||||
struct be_t
|
||||
template<typename T> class be_t
|
||||
{
|
||||
using type = std::remove_cv_t<T>;
|
||||
using stype = be_storage_t<std::remove_cv_t<T>>;
|
||||
|
||||
stype m_data;
|
||||
|
||||
static_assert(!std::is_class<type>::value, "be_t<> error: invalid type (class or structure)");
|
||||
static_assert(!std::is_union<type>::value || std::is_same<type, u128>::value, "be_t<> error: invalid type (union)");
|
||||
static_assert(!std::is_pointer<type>::value, "be_t<> error: invalid type (pointer)");
|
||||
static_assert(!std::is_reference<type>::value, "be_t<> error: invalid type (reference)");
|
||||
static_assert(!std::is_array<type>::value, "be_t<> error: invalid type (array)");
|
||||
static_assert(__alignof(type) == __alignof(stype), "be_t<> error: unexpected alignment");
|
||||
|
||||
private:
|
||||
template<typename Tto, typename Tfrom, int mode>
|
||||
struct _convert
|
||||
{
|
||||
|
@ -624,69 +607,63 @@ private:
|
|||
{
|
||||
static force_inline be_t<Tto>& func(Tfrom& be_value)
|
||||
{
|
||||
Tto res = be_value >> ((sizeof(Tfrom)-sizeof(Tto)) * 8);
|
||||
Tto res = be_value >> ((sizeof(Tfrom) - sizeof(Tto)) * 8);
|
||||
return (be_t<Tto>&)res;
|
||||
}
|
||||
};
|
||||
|
||||
const stype& ToBE() const
|
||||
{
|
||||
return m_data;
|
||||
}
|
||||
|
||||
type ToLE() const
|
||||
{
|
||||
return se_t<type, sizeof(stype)>::from_be(m_data);
|
||||
}
|
||||
|
||||
void FromBE(const stype& value)
|
||||
{
|
||||
m_data = value;
|
||||
}
|
||||
|
||||
void FromLE(const type& value)
|
||||
{
|
||||
m_data = se_t<type, sizeof(stype)>::to_be(value);
|
||||
}
|
||||
|
||||
static be_t MakeFromLE(const type& value)
|
||||
{
|
||||
stype data = se_t<type, sizeof(stype)>::to_be(value);
|
||||
return (be_t&)data;
|
||||
}
|
||||
|
||||
static be_t MakeFromBE(const stype& value)
|
||||
{
|
||||
return (be_t&)value;
|
||||
}
|
||||
|
||||
public:
|
||||
//make be_t from current machine byte ordering
|
||||
static be_t make(const type& value)
|
||||
{
|
||||
using type = std::remove_cv_t<T>;
|
||||
using stype = be_storage_t<std::remove_cv_t<T>>;
|
||||
|
||||
#ifdef IS_LE_MACHINE
|
||||
return MakeFromLE(value);
|
||||
stype m_data;
|
||||
#else
|
||||
return MakeFromBE(value);
|
||||
type m_data;
|
||||
#endif
|
||||
|
||||
static_assert(!std::is_class<type>::value, "be_t<> error: invalid type (class or structure)");
|
||||
static_assert(!std::is_union<type>::value || std::is_same<type, u128>::value, "be_t<> error: invalid type (union)");
|
||||
static_assert(!std::is_pointer<type>::value, "be_t<> error: invalid type (pointer)");
|
||||
static_assert(!std::is_reference<type>::value, "be_t<> error: invalid type (reference)");
|
||||
static_assert(!std::is_array<type>::value, "be_t<> error: invalid type (array)");
|
||||
static_assert(!std::is_enum<type>::value, "be_t<> error: invalid type (enumeration), use integral type instead");
|
||||
static_assert(__alignof(type) == __alignof(stype), "be_t<> error: unexpected alignment");
|
||||
|
||||
be_t() = default;
|
||||
|
||||
be_t(const be_t&) = default;
|
||||
|
||||
template<typename CT, typename = std::enable_if_t<std::is_constructible<type, CT>::value>> be_t(const CT& value)
|
||||
#ifdef IS_LE_MACHINE
|
||||
: m_data(se_t<type, sizeof(stype)>::to_be(value))
|
||||
#else
|
||||
: m_data(value)
|
||||
#endif
|
||||
{
|
||||
}
|
||||
|
||||
//get value in current machine byte ordering
|
||||
// get value in current machine byte ordering
|
||||
force_inline type value() const
|
||||
{
|
||||
#ifdef IS_LE_MACHINE
|
||||
return ToLE();
|
||||
return se_t<type, sizeof(stype)>::from_be(m_data);
|
||||
#else
|
||||
return ToBE();
|
||||
return m_data;
|
||||
#endif
|
||||
}
|
||||
|
||||
// get underlying data without any byte order manipulation
|
||||
const stype& data() const
|
||||
{
|
||||
return ToBE();
|
||||
#ifdef IS_LE_MACHINE
|
||||
return m_data;
|
||||
#else
|
||||
return reinterpret_cast<const stype&>(m_data);
|
||||
#endif
|
||||
}
|
||||
|
||||
be_t& operator =(const be_t& value) = default;
|
||||
be_t& operator =(const be_t&) = default;
|
||||
|
||||
template<typename CT> std::enable_if_t<std::is_assignable<type&, CT>::value, be_t&> operator =(const CT& value)
|
||||
{
|
||||
|
@ -695,7 +672,7 @@ public:
|
|||
return *this;
|
||||
}
|
||||
|
||||
//template<typename CT, std::enable_if_t<std::is_convertible<type, CT>::value>> operator CT() const
|
||||
//template<typename CT, typename = std::enable_if_t<std::is_convertible<type, CT>::value>> operator CT() const
|
||||
//{
|
||||
// return value();
|
||||
//}
|
||||
|
@ -708,56 +685,69 @@ public:
|
|||
// conversion to another be_t type
|
||||
template<typename T1> operator be_t<T1>() const
|
||||
{
|
||||
return be_t<T1>::make(value());
|
||||
return value();
|
||||
|
||||
// TODO (complicated cases like int-float conversions are not handled correctly)
|
||||
//return _convert<T1, T, ((sizeof(T1) > sizeof(T)) ? 1 : (sizeof(T1) < sizeof(T) ? 2 : 0))>::func(m_data);
|
||||
}
|
||||
|
||||
template<typename T1> be_t& operator += (T1 right) { return *this = value() + right; }
|
||||
template<typename T1> be_t& operator -= (T1 right) { return *this = value() - right; }
|
||||
template<typename T1> be_t& operator *= (T1 right) { return *this = value() * right; }
|
||||
template<typename T1> be_t& operator /= (T1 right) { return *this = value() / right; }
|
||||
template<typename T1> be_t& operator %= (T1 right) { return *this = value() % right; }
|
||||
template<typename T1> be_t& operator &= (T1 right) { return *this = value() & right; }
|
||||
template<typename T1> be_t& operator |= (T1 right) { return *this = value() | right; }
|
||||
template<typename T1> be_t& operator ^= (T1 right) { return *this = value() ^ right; }
|
||||
template<typename T1> be_t& operator <<= (T1 right) { return *this = value() << right; }
|
||||
template<typename T1> be_t& operator >>= (T1 right) { return *this = value() >> right; }
|
||||
template<typename T1> be_t& operator +=(const T1& right) { return *this = value() + right; }
|
||||
template<typename T1> be_t& operator -=(const T1& right) { return *this = value() - right; }
|
||||
template<typename T1> be_t& operator *=(const T1& right) { return *this = value() * right; }
|
||||
template<typename T1> be_t& operator /=(const T1& right) { return *this = value() / right; }
|
||||
template<typename T1> be_t& operator %=(const T1& right) { return *this = value() % 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 T1& right) { return *this = value() << right; }
|
||||
template<typename T1> be_t& operator >>=(const T1& right) { return *this = value() >> right; }
|
||||
|
||||
template<typename T1> be_t operator & (const be_t<T1>& right) const { be_t res; res.FromBE(ToBE() & right.ToBE()); return res; }
|
||||
template<typename T1> be_t operator | (const be_t<T1>& right) const { be_t res; res.FromBE(ToBE() | right.ToBE()); return res; }
|
||||
template<typename T1> be_t operator ^ (const be_t<T1>& right) const { be_t res; res.FromBE(ToBE() ^ right.ToBE()); return res; }
|
||||
template<typename T1> be_t& operator &=(const T1& right) { return m_data &= be_t(right).data(), *this; }
|
||||
template<typename T1> be_t& operator |=(const T1& right) { return m_data |= be_t(right).data(), *this; }
|
||||
template<typename T1> be_t& operator ^=(const T1& right) { return m_data ^= be_t(right).data(), *this; }
|
||||
|
||||
template<typename T1> bool operator == (T1 right) const { return (T1)ToLE() == right; }
|
||||
template<typename T1> bool operator != (T1 right) const { return !(*this == 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; }
|
||||
template<typename T1> bool operator <= (T1 right) const { return (T1)ToLE() <= right; }
|
||||
|
||||
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); }
|
||||
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(); }
|
||||
|
||||
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; }
|
||||
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; }
|
||||
};
|
||||
|
||||
template<typename T1, typename T2> inline std::enable_if_t<std::is_same<T1, T2>::value, bool> operator ==(const be_t<T1>& left, const be_t<T2>& right)
|
||||
{
|
||||
return left.data() == right.data();
|
||||
}
|
||||
|
||||
template<typename T1, typename T2> inline std::enable_if_t<std::is_same<T1, T2>::value, bool> operator !=(const be_t<T1>& left, const be_t<T2>& right)
|
||||
{
|
||||
return left.data() != right.data();
|
||||
}
|
||||
|
||||
template<typename T1, typename T2> inline std::enable_if_t<std::is_same<T1, T2>::value, be_t<T1>> operator &(const be_t<T1>& left, const be_t<T2>& right)
|
||||
{
|
||||
be_t<T1> result;
|
||||
result.m_data = left.data() & right.data();
|
||||
return result;
|
||||
}
|
||||
|
||||
template<typename T1, typename T2> inline std::enable_if_t<std::is_same<T1, T2>::value, be_t<T1>> operator |(const be_t<T1>& left, const be_t<T2>& right)
|
||||
{
|
||||
be_t<T1> result;
|
||||
result.m_data = left.data() | right.data();
|
||||
return result;
|
||||
}
|
||||
|
||||
template<typename T1, typename T2> inline std::enable_if_t<std::is_same<T1, T2>::value, be_t<T1>> operator ^(const be_t<T1>& left, const be_t<T2>& right)
|
||||
{
|
||||
be_t<T1> result;
|
||||
result.m_data = left.data() ^ right.data();
|
||||
return result;
|
||||
}
|
||||
|
||||
template<typename T1> inline std::enable_if_t<true, be_t<T1>> operator ~(const be_t<T1>& other)
|
||||
{
|
||||
be_t<T1> result;
|
||||
result.m_data = ~other.data();
|
||||
return result;
|
||||
}
|
||||
|
||||
template<typename T> struct is_be_t : public std::integral_constant<bool, false>
|
||||
{
|
||||
};
|
||||
|
@ -804,95 +794,79 @@ template<> struct to_be<s8> { using type = s8; };
|
|||
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
|
||||
|
||||
template<typename Tto, typename Tfrom>
|
||||
struct convert_le_be_t
|
||||
{
|
||||
static Tto func(Tfrom value)
|
||||
{
|
||||
return (Tto)value;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Tt, typename Tfrom>
|
||||
struct convert_le_be_t<be_t<Tt>, Tfrom>
|
||||
{
|
||||
static be_t<Tt> func(Tfrom value)
|
||||
{
|
||||
return be_t<Tt>::make(value);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Tt, typename Tf>
|
||||
struct convert_le_be_t<be_t<Tt>, be_t<Tf>>
|
||||
{
|
||||
static be_t<Tt> func(be_t<Tf> value)
|
||||
{
|
||||
return value;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Tto, typename Tf>
|
||||
struct convert_le_be_t<Tto, be_t<Tf>>
|
||||
{
|
||||
static Tto func(be_t<Tf> value)
|
||||
{
|
||||
return value.value();
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Tto, typename Tfrom>
|
||||
force_inline Tto convert_le_be(Tfrom value)
|
||||
{
|
||||
return convert_le_be_t<Tto, Tfrom>::func(value);
|
||||
}
|
||||
|
||||
template<typename Tto, typename Tfrom>
|
||||
force_inline void convert_le_be(Tto& dst, Tfrom src)
|
||||
{
|
||||
dst = convert_le_be_t<Tto, Tfrom>::func(src);
|
||||
}
|
||||
|
||||
template<typename T> struct le_t
|
||||
template<typename T> class le_t
|
||||
{
|
||||
public:
|
||||
using type = std::remove_cv_t<T>;
|
||||
using stype = be_storage_t<std::remove_cv_t<T>>;
|
||||
|
||||
stype m_data;
|
||||
type m_data;
|
||||
|
||||
static_assert(!std::is_class<type>::value, "le_t<> error: invalid type (class or structure)");
|
||||
static_assert(!std::is_union<type>::value || std::is_same<type, u128>::value, "le_t<> error: invalid type (union)");
|
||||
static_assert(!std::is_pointer<type>::value, "le_t<> error: invalid type (pointer)");
|
||||
static_assert(!std::is_reference<type>::value, "le_t<> error: invalid type (reference)");
|
||||
static_assert(!std::is_array<type>::value, "le_t<> error: invalid type (array)");
|
||||
static_assert(!std::is_enum<type>::value, "le_t<> error: invalid type (enumeration), use integral type instead");
|
||||
static_assert(__alignof(type) == __alignof(stype), "le_t<> error: unexpected alignment");
|
||||
|
||||
le_t() = default;
|
||||
|
||||
le_t(const le_t&) = default;
|
||||
|
||||
template<typename CT, typename = std::enable_if_t<std::is_constructible<type, CT>::value>> le_t(const CT& value)
|
||||
: m_data(value)
|
||||
{
|
||||
}
|
||||
|
||||
type value() const
|
||||
{
|
||||
return reinterpret_cast<const type&>(m_data);
|
||||
return m_data;
|
||||
}
|
||||
|
||||
const stype& data() const
|
||||
{
|
||||
return reinterpret_cast<const stype&>(m_data);
|
||||
}
|
||||
|
||||
le_t& operator =(const le_t& value) = default;
|
||||
|
||||
template<typename CT> std::enable_if_t<std::is_assignable<type&, CT>::value, le_t&> operator =(const CT& value)
|
||||
{
|
||||
m_data = reinterpret_cast<const stype&>(value);
|
||||
|
||||
m_data = value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
//template<typename CT> operator std::enable_if_t<std::is_convertible<type, CT>::value, CT>() const
|
||||
//{
|
||||
// return value();
|
||||
//}
|
||||
|
||||
operator type() const
|
||||
{
|
||||
return value();
|
||||
}
|
||||
|
||||
// conversion to another le_t type
|
||||
//template<typename T1> operator const le_t<T1>() const
|
||||
//{
|
||||
// return le_t<T1>::make(value());
|
||||
//}
|
||||
template<typename T1> operator le_t<T1>() const
|
||||
{
|
||||
return value();
|
||||
}
|
||||
|
||||
template<typename T1> le_t& operator +=(const T1& right) { return *this = value() + right; }
|
||||
template<typename T1> le_t& operator -=(const T1& right) { return *this = value() - right; }
|
||||
template<typename T1> le_t& operator *=(const T1& right) { return *this = value() * right; }
|
||||
template<typename T1> le_t& operator /=(const T1& right) { return *this = value() / right; }
|
||||
template<typename T1> le_t& operator %=(const T1& right) { return *this = value() % right; }
|
||||
|
||||
template<typename T1> le_t& operator <<=(const T1& right) { return *this = value() << right; }
|
||||
template<typename T1> le_t& operator >>=(const T1& right) { return *this = value() >> right; }
|
||||
|
||||
template<typename T1> le_t& operator &=(const T1& right) { return m_data &= le_t(right).data(), *this; }
|
||||
template<typename T1> le_t& operator |=(const T1& right) { return m_data |= le_t(right).data(), *this; }
|
||||
template<typename T1> le_t& operator ^=(const T1& right) { return m_data ^= le_t(right).data(), *this; }
|
||||
|
||||
le_t operator ++(int) { le_t res = *this; *this += 1; return res; }
|
||||
le_t operator --(int) { le_t res = *this; *this -= 1; return res; }
|
||||
le_t& operator ++() { *this += 1; return *this; }
|
||||
le_t& operator --() { *this -= 1; return *this; }
|
||||
};
|
||||
|
||||
template<typename T> struct is_le_t : public std::integral_constant<bool, false>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue