be_storage_t

BE data is internally stored as u8, u16, u32, u64, u128 value (decreases
amount of reinterpret casts in be<>le conversions)
This commit is contained in:
Nekotekina 2015-01-11 12:33:05 +03:00
parent afe221c29d
commit c36e692411
2 changed files with 118 additions and 50 deletions

View File

@ -378,65 +378,97 @@ template<typename T, int size = sizeof(T)> struct se_t;
template<typename T> struct se_t<T, 1>
{
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<typename T> struct se_t<T, 2>
{
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<typename T> struct se_t<T, 4>
{
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<typename T> struct se_t<T, 8>
{
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<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); }
template<typename T, u64 _value, int size = sizeof(T)> struct const_se_t;
template<typename T, u64 _value> struct const_se_t<T, _value, 1>
template<typename T> struct se_t<T, 16>
{
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<typename T, u64 _value> struct const_se_t<T, _value, 2>
template<typename T, T _value, size_t size = sizeof(T)> struct const_se_t;
template<u8 _value> struct const_se_t<u8, _value, 1>
{
static const T value = ((_value >> 8) & 0xff) | ((_value << 8) & 0xff00);
static const u8 value = _value;
};
template<typename T, u64 _value> struct const_se_t<T, _value, 4>
template<u16 _value> struct const_se_t<u16, _value, 2>
{
static const T value =
static const u16 value = ((_value >> 8) & 0xff) | ((_value << 8) & 0xff00);
};
template<u32 _value> struct const_se_t<u32, _value, 4>
{
static const u32 value =
((_value >> 24) & 0x000000ff) |
((_value >> 8) & 0x0000ff00) |
((_value << 8) & 0x00ff0000) |
((_value << 24) & 0xff000000);
};
template<typename T, u64 _value> struct const_se_t<T, _value, 8>
template<u64 _value> struct const_se_t<u64, _value, 8>
{
static const T value =
static const u64 value =
((_value >> 56) & 0x00000000000000ff) |
((_value >> 40) & 0x000000000000ff00) |
((_value >> 24) & 0x0000000000ff0000) |
@ -447,17 +479,53 @@ template<typename T, u64 _value> struct const_se_t<T, _value, 8>
((_value << 56) & 0xff00000000000000);
};
template<typename T, size_t size = sizeof(T)>
struct be_storage_t
{
static_assert(!size, "Bad be_storage_t type");
};
template<typename T>
struct be_storage_t<T, 1>
{
typedef u8 type;
};
template<typename T>
struct be_storage_t<T, 2>
{
typedef u16 type;
};
template<typename T>
struct be_storage_t<T, 4>
{
typedef u32 type;
};
template<typename T>
struct be_storage_t<T, 8>
{
typedef u64 type;
};
template<typename T>
struct be_storage_t<T, 16>
{
typedef u128 type;
};
#define IS_LE_MACHINE
template<typename T, typename T2 = T>
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<T>::type type;
static const bool is_le_machine = true;
typedef typename be_storage_t<T2>::type stype;
private:
type m_data;
stype m_data;
template<typename Tto, typename Tfrom, int mode>
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<type, sizeof(T2)>::func(m_data);
return se_t<type, sizeof(T2)>::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<type, sizeof(T2)>::func(value);
m_data = se_t<type, sizeof(T2)>::to_be(value);
}
static be_t MakeFromLE(const type value)
static be_t MakeFromLE(const type& value)
{
type data = se_t<type, sizeof(T2)>::func(value);
stype data = se_t<type, sizeof(T2)>::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<type, sizeof(T2)>::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<type, sizeof(T2)>::func(value);
m_data = se_t<type, sizeof(T2)>::to_be(value);
return *this;
}
@ -821,4 +889,4 @@ template<typename Tto, typename Tfrom>
__forceinline void convert_le_be(Tto& dst, Tfrom&& src)
{
dst = convert_le_be_t<Tto, Tfrom>::func(src);
}
}

View File

@ -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<be_t<u32>>();
vm::var<be_t<u64>> 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<unsigned char[]> png((u32)fileSize);
vm::var<be_t<u64>> 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):