From c36e6924119ed5eed85e72f481fcf704abb9b4b7 Mon Sep 17 00:00:00 2001 From: Nekotekina Date: Sun, 11 Jan 2015 12:33:05 +0300 Subject: [PATCH] be_storage_t BE data is internally stored as u8, u16, u32, u64, u128 value (decreases amount of reinterpret casts in be<>le conversions) --- Utilities/BEType.h | 158 ++++++++++++++++------ rpcs3/Emu/SysCalls/Modules/cellPngDec.cpp | 10 +- 2 files changed, 118 insertions(+), 50 deletions(-) diff --git a/Utilities/BEType.h b/Utilities/BEType.h index 768e25770c..d8a8c5998e 100644 --- a/Utilities/BEType.h +++ b/Utilities/BEType.h @@ -378,65 +378,97 @@ template struct se_t; template struct se_t { - static __forceinline T func(const T src) + static __forceinline u8 to_be(const T& src) { - return src; + return (u8&)src; + } + + static __forceinline T from_be(const u8 src) + { + return (T&)src; } }; template struct se_t { - static __forceinline T func(const T src) + static __forceinline u16 to_be(const T& src) { - const u16 res = _byteswap_ushort((u16&)src); + return _byteswap_ushort((u16&)src); + } + + static __forceinline T from_be(const u16 src) + { + const u16 res = _byteswap_ushort(src); return (T&)res; } }; template struct se_t { - static __forceinline T func(const T src) + static __forceinline u32 to_be(const T& src) { - const u32 res = _byteswap_ulong((u32&)src); + return _byteswap_ulong((u32&)src); + } + + static __forceinline T from_be(const u32 src) + { + const u32 res = _byteswap_ulong(src); return (T&)res; } }; template struct se_t { - static __forceinline T func(const T src) + static __forceinline u64 to_be(const T& src) { - const u64 res = _byteswap_uint64((u64&)src); + return _byteswap_uint64((u64&)src); + } + + static __forceinline T from_be(const u64 src) + { + const u64 res = _byteswap_uint64(src); return (T&)res; } }; -//template T re(const T val) { T res; se_t::func(res, val); return res; } -//template void re(T1& dst, const T2 val) { se_t::func(dst, val); } - -template struct const_se_t; -template struct const_se_t +template struct se_t { - static const T value = (T)_value; + static __forceinline u128 to_be(const T& src) + { + return u128::byteswap((u128&)src); + } + + static __forceinline T from_be(const u128& src) + { + const u128 res = u128::byteswap(src); + return (T&)res; + } }; -template struct const_se_t +template struct const_se_t; + +template struct const_se_t { - static const T value = ((_value >> 8) & 0xff) | ((_value << 8) & 0xff00); + static const u8 value = _value; }; -template struct const_se_t +template struct const_se_t { - static const T value = + static const u16 value = ((_value >> 8) & 0xff) | ((_value << 8) & 0xff00); +}; + +template struct const_se_t +{ + static const u32 value = ((_value >> 24) & 0x000000ff) | ((_value >> 8) & 0x0000ff00) | ((_value << 8) & 0x00ff0000) | ((_value << 24) & 0xff000000); }; -template struct const_se_t +template struct const_se_t { - static const T value = + static const u64 value = ((_value >> 56) & 0x00000000000000ff) | ((_value >> 40) & 0x000000000000ff00) | ((_value >> 24) & 0x0000000000ff0000) | @@ -447,17 +479,53 @@ template struct const_se_t ((_value << 56) & 0xff00000000000000); }; +template +struct be_storage_t +{ + static_assert(!size, "Bad be_storage_t type"); +}; + +template +struct be_storage_t +{ + typedef u8 type; +}; + +template +struct be_storage_t +{ + typedef u16 type; +}; + +template +struct be_storage_t +{ + typedef u32 type; +}; + +template +struct be_storage_t +{ + typedef u64 type; +}; + +template +struct be_storage_t +{ + typedef u128 type; +}; + +#define IS_LE_MACHINE + template class be_t { - static_assert(sizeof(T2) == 1 || sizeof(T2) == 2 || sizeof(T2) == 4 || sizeof(T2) == 8, "Bad be_t type"); - public: typedef typename std::remove_cv::type type; - static const bool is_le_machine = true; + typedef typename be_storage_t::type stype; private: - type m_data; + stype m_data; template struct _convert @@ -490,62 +558,62 @@ private: }; public: - const type& ToBE() const + const stype& ToBE() const { return m_data; } type ToLE() const { - return se_t::func(m_data); + return se_t::from_be(m_data); } - void FromBE(const type& value) + void FromBE(const stype& value) { m_data = value; } void FromLE(const type& value) { - m_data = se_t::func(value); + m_data = se_t::to_be(value); } - static be_t MakeFromLE(const type value) + static be_t MakeFromLE(const type& value) { - type data = se_t::func(value); + stype data = se_t::to_be(value); return (be_t&)data; } - static be_t MakeFromBE(const type value) + static be_t MakeFromBE(const stype& value) { return (be_t&)value; } //make be_t from current machine byte ordering - static be_t make(const type value) + static be_t make(const type& value) { - return is_le_machine ? MakeFromLE(value) : MakeFromBE(value); +#ifdef IS_LE_MACHINE + return MakeFromLE(value); +#else + return MakeFromBE(value); +#endif } //get value in current machine byte ordering __forceinline type value() const { - return is_le_machine ? ToLE() : ToBE(); +#ifdef IS_LE_MACHINE + return ToLE(); +#else + return ToBE(); +#endif } - - //be_t() = default; - //be_t(const be_t& value) = default; - - //be_t(type value) - //{ - // m_data = se_t::func(value); - //} be_t& operator = (const be_t& value) = default; - be_t& operator = (type value) + be_t& operator = (const type& value) { - m_data = se_t::func(value); + m_data = se_t::to_be(value); return *this; } @@ -821,4 +889,4 @@ template __forceinline void convert_le_be(Tto& dst, Tfrom&& src) { dst = convert_le_be_t::func(src); -} \ No newline at end of file +} diff --git a/rpcs3/Emu/SysCalls/Modules/cellPngDec.cpp b/rpcs3/Emu/SysCalls/Modules/cellPngDec.cpp index 973c1dff2b..ad1edb197e 100644 --- a/rpcs3/Emu/SysCalls/Modules/cellPngDec.cpp +++ b/rpcs3/Emu/SysCalls/Modules/cellPngDec.cpp @@ -77,7 +77,7 @@ s64 pngDecOpen( stream->fd = 0; stream->src = *src; - switch ((u32)src->srcSelect.ToBE()) + switch (src->srcSelect.ToBE()) { case se32(CELL_PNGDEC_BUFFER): stream->fileSize = src->streamSize.ToLE(); @@ -145,7 +145,7 @@ s64 pngReadHeader( auto buffer_32 = buffer.To>(); vm::var> pos, nread; - switch ((u32)stream->src.srcSelect.ToBE()) + switch (stream->src.srcSelect.ToBE()) { case se32(CELL_PNGDEC_BUFFER): memmove(buffer.begin(), stream->src.streamPtr.get_ptr(), buffer.size()); @@ -206,7 +206,7 @@ s64 pngDecSetParameter( current_outParam.outputHeight = current_info.imageHeight; current_outParam.outputColorSpace = inParam->outputColorSpace; - switch ((u32)current_outParam.outputColorSpace.ToBE()) + switch (current_outParam.outputColorSpace.ToBE()) { case se32(CELL_PNGDEC_PALETTE): case se32(CELL_PNGDEC_GRAYSCALE): @@ -254,7 +254,7 @@ s64 pngDecodeData( vm::var png((u32)fileSize); vm::var> pos, nread; - switch ((u32)stream->src.srcSelect.ToBE()) + switch (stream->src.srcSelect.ToBE()) { case se32(CELL_PNGDEC_BUFFER): memmove(png.begin(), stream->src.streamPtr.get_ptr(), png.size()); @@ -283,7 +283,7 @@ s64 pngDecodeData( const int bytesPerLine = (u32)dataCtrlParam->outputBytesPerLine; uint image_size = width * height; - switch ((u32)current_outParam.outputColorSpace.ToBE()) + switch (current_outParam.outputColorSpace.ToBE()) { case se32(CELL_PNGDEC_RGB): case se32(CELL_PNGDEC_RGBA):