unaligned and endianess experiments

This commit is contained in:
John Wellbelove 2022-01-06 10:18:58 +00:00
parent 2cf77bf837
commit 79c659b0bb
5 changed files with 261 additions and 57 deletions

View File

@ -86,9 +86,9 @@ namespace etl
private:
ETL_CONSTEXPR static etl::endian get()
static etl::endian get()
{
static ETL_CONSTANT union U
static union U
{
U()
: ui32(0x12345678UL)
@ -101,6 +101,23 @@ namespace etl
return (u.ui16[0] == 0x5678U) ? etl::endian::little : etl::endian::big;
}
// constexpr static auto check()
// {
//#if defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
//
// return etl::endian::big;
//
//#elif defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
//
// return etl::endian::little;
//
//#else
//
// return Endian::UNKNOWN;
//
//#endif
// }
};
//***************************************************************************

View File

@ -39,16 +39,19 @@ SOFTWARE.
#include "type_traits.h"
#include "endianness.h"
#include <string.h>
namespace etl
{
template <typename T, etl::endian Endian, typename = typename etl::enable_if<etl::is_fundamental<T>::value, void>::type>
template <typename T, int Endian, typename = typename etl::enable_if<etl::is_fundamental<T>::value && (Endian != etl::endian::native), void>::type>
class unaligned_type
{
public:
typedef type T;
typedef T type;
static ETL_CONSTANT size_t Size = sizeof(T);
static ETL_CONSTANT int endian_type = Endian;
//*************************************************************************
/// Default constructor
@ -57,7 +60,7 @@ namespace etl
{
T value = T(0);
mempy(storage, &value, sizeof(T));
memcpy(storage, &value, Size);
}
//*************************************************************************
@ -65,13 +68,13 @@ namespace etl
//*************************************************************************
unaligned_type(T value)
{
// Same as host?
if (Endian != etl::endianness::get())
// Not same as host?
if (Endian != etl::endianness::value())
{
value = etl::reverse_bytes(value);
}
mempy(storage, &value, sizeof(T));
memcpy(storage, &value, Size);
}
//*************************************************************************
@ -79,7 +82,7 @@ namespace etl
//*************************************************************************
unaligned_type(const unaligned_type& other)
{
mempy(storage, other.storage, sizeof(T));
memcpy(storage, other.storage, Size);
}
//*************************************************************************
@ -87,7 +90,24 @@ namespace etl
//*************************************************************************
unaligned_type& operator =(const unaligned_type& other)
{
mempy(storage, other.storage, sizeof(T));
memcpy(storage, other.storage, Size);
return *this;
}
//*************************************************************************
/// Assignment operator from other endianness.
//*************************************************************************
template <int Endian_Other>
unaligned_type& operator =(const unaligned_type<T, Endian_Other>& other)
{
memcpy(storage, other.storage, Size);
// Not same?
if (Endian != Endian_Other)
{
etl::reverse(storage, storage + Size);
}
}
//*************************************************************************
@ -95,7 +115,7 @@ namespace etl
//*************************************************************************
friend bool operator ==(const unaligned_type& lhs, const unaligned_type& rhs)
{
return memcmp(lhs.storage, rhs.storage, sizeof(T)) == 0;
return (memcmp(lhs.storage, rhs.storage, Size) == 0);
}
//*************************************************************************
@ -113,10 +133,10 @@ namespace etl
{
T value;
mempy(&value, storage, sizeof(T));
memcpy(&value, storage, Size);
// Same as host?
if (Endian != etl::endianness::get())
if (Endian != etl::endianness::value())
{
value = etl::reverse_bytes(value);
}
@ -126,70 +146,173 @@ namespace etl
private:
char storage[sizeof(T)];
char storage[Size];
};
//template <typename T, typename D = typename etl::enable_if<etl::is_fundamental<T>::value, void>::type>
//class unaligned_type<T, etl::endian::native, D>
//{
//public:
// typedef T type;
// static ETL_CONSTANT size_t Size = Size;
// static ETL_CONSTANT etl::endianness endian_type = etl::endianness::get();
// //*************************************************************************
// /// Default constructor
// //*************************************************************************
// unaligned_type()
// {
// T value = T(0);
// memcpy(storage, &value, Size);
// }
// //*************************************************************************
// /// Constructor
// //*************************************************************************
// unaligned_type(T value)
// {
// memcpy(storage, &value, Size);
// }
// //*************************************************************************
// /// Copy constructor
// //*************************************************************************
// unaligned_type(const unaligned_type& other)
// {
// memcpy(storage, other.storage, Size);
// }
// //*************************************************************************
// /// Assignment operator
// //*************************************************************************
// unaligned_type& operator =(const unaligned_type& other)
// {
// memcpy(storage, other.storage, Size);
// }
// //*************************************************************************
// /// Equality operator
// //*************************************************************************
// friend bool operator ==(const unaligned_type& lhs, const unaligned_type& rhs)
// {
// return (memcmp(lhs.storage, rhs.storage, Size) == 0);
// }
// //*************************************************************************
// /// Inequality operator
// //*************************************************************************
// friend bool operator !=(const unaligned_type& lhs, const unaligned_type& rhs)
// {
// return !(lhs == rhs);
// }
// //*************************************************************************
// /// Conversion operator
// //*************************************************************************
// operator T() const
// {
// T value;
// memcpy(&value, storage, Size);
// return value;
// }
//private:
// char storage[Size];
//};
// Network Order
typedef unaligned_type<char, etl::endian::big> net_char_t;
typedef unaligned_type<signed char, etl::endian::big> net_schar_t;
typedef unaligned_type<unsigned char, etl::endian::big> net_uchar_t;
typedef unaligned_type<short, etl::endian::big> net_short_t;
typedef unaligned_type<unsigned short, etl::endian::big> net_unsigned_short_t;
typedef unaligned_type<int, etl::endian::big> net_int_t;
typedef unaligned_type<unsigned int, etl::endian::big> net_unsigned_int_t;
typedef unaligned_type<short, etl::endian::big> net_shor_t;
typedef unaligned_type<unsigned short, etl::endian::big> net_unsigned_shor_t;
typedef unaligned_type<int, etl::endian::big> net_in_t;
typedef unaligned_type<unsigned int, etl::endian::big> net_unsigned_in_t;
typedef unaligned_type<long, etl::endian::big> net_long_t;
typedef unaligned_type<unsigned long, etl::endian::big> net_unsigned_long_t;
typedef unaligned_type<long long, etl::endian::big> net_long_long_t;
typedef unaligned_type<unsigned long long, etl::endian::big> net_unsigned_long_long_t;
#if ETL_8BIT_SUPPORT
typedef unaligned_type<int8_t, etl::endian::big> net_int8_t_t;
typedef unaligned_type<uint8_t, etl::endian::big> net_uint8_t_t;
typedef unaligned_type<int8_t, etl::endian::big> net_int8_t;
typedef unaligned_type<uint8_t, etl::endian::big> net_uint8_t;
#endif
typedef unaligned_type<int16_t, etl::endian::big> net_int16_t_t;
typedef unaligned_type<uint16_t, etl::endian::big> net_uint16_t_t;
typedef unaligned_type<int32_t, etl::endian::big> net_int32_t_t;
typedef unaligned_type<uint32_t, etl::endian::big> net_uint32_t_t;
typedef unaligned_type<int16_t, etl::endian::big> net_int16_t;
typedef unaligned_type<uint16_t, etl::endian::big> net_uint16_t;
typedef unaligned_type<int32_t, etl::endian::big> net_int32_t;
typedef unaligned_type<uint32_t, etl::endian::big> net_uint32_t;
#if ETL_USING_64BIT_TYPES
typedef unaligned_type<int64_t, etl::endian::big> net_int64_t_t;
typedef unaligned_type<uint64_t, etl::endian::big> net_uint64_t_t;
typedef unaligned_type<int64_t, etl::endian::big> net_int64_t;
typedef unaligned_type<uint64_t, etl::endian::big> net_uint64_t;
#endif
typedef unaligned_type<float, etl::endian::big> net_float_t;
typedef unaligned_type<float, etl::endian::big> net_floa_t;
typedef unaligned_type<double, etl::endian::big> net_double_t;
typedef unaligned_type<long double, etl::endian::big> net_long_double_t;
#if ETL_CPP11_SUPPORTED
// Host order
typedef unaligned_type<char, etl::endian::get()> host_char_t;
typedef unaligned_type<signed char, etl::endian::get()> host_schar_t;
typedef unaligned_type<unsigned char, etl::endian::get()> host_uchar_t;
typedef unaligned_type<short, etl::endian::get()> host_short_t;
typedef unaligned_type<unsigned short, etl::endian::get()> host_unsigned_short_t;
typedef unaligned_type<int, etl::endian::get()> host_int_t;
typedef unaligned_type<unsigned int, etl::endian::get()> host_unsigned_int_t;
typedef unaligned_type<long, etl::endian::get()> host_long_t;
typedef unaligned_type<unsigned long, etl::endian::get()> host_unsigned_long_t;
typedef unaligned_type<long long, etl::endian::get()> host_long_long_t;
typedef unaligned_type<unsigned long long, etl::endian::get()> host_unsigned_long_long_t;
#if ETL_8BIT_SUPPORT
typedef unaligned_type<int8_t, etl::endian::get()> host_int8_t_t;
typedef unaligned_type<uint8_t, etl::endian::get()> host_uint8_t_t;
#endif
typedef unaligned_type<int16_t, etl::endian::get()> host_int16_t_t;
typedef unaligned_type<uint16_t, etl::endian::get()> host_uint16_t_t;
typedef unaligned_type<int32_t, etl::endian::get()> host_int32_t_t;
typedef unaligned_type<uint32_t, etl::endian::get()> host_uint32_t_t;
#if ETL_USING_64BIT_TYPES
typedef unaligned_type<int64_t, etl::endian::get()> host_int64_t_t;
typedef unaligned_type<uint64_t, etl::endian::get()> host_uint64_t_t;
#endif
typedef unaligned_type<float, etl::endian::get()> host_float_t;
typedef unaligned_type<double, etl::endian::get()> host_double_t;
typedef unaligned_type<long double, etl::endian::get()> host_long_double_t;
#endif
//#if ETL_CPP11_SUPPORTED
// // Host order
// typedef unaligned_type<char, int(etl::endianness::get())> host_char_t;
// typedef unaligned_type<signed char, etl::endianness::get()> host_schar_t;
// typedef unaligned_type<unsigned char, etl::endianness::get()> host_uchar_t;
// typedef unaligned_type<short, etl::endianness::get()> host_shor_t;
// typedef unaligned_type<unsigned short, etl::endianness::get()> host_unsigned_shor_t;
// typedef unaligned_type<int, etl::endianness::get()> host_in_t;
// typedef unaligned_type<unsigned int, etl::endianness::get()> host_unsigned_in_t;
// typedef unaligned_type<long, etl::endianness::get()> host_long_t;
// typedef unaligned_type<unsigned long, etl::endianness::get()> host_unsigned_long_t;
// typedef unaligned_type<long long, etl::endianness::get()> host_long_long_t;
// typedef unaligned_type<unsigned long long, etl::endianness::get()> host_unsigned_long_long_t;
//#if ETL_8BIT_SUPPORT
// typedef unaligned_type<int8_t, etl::endianness::get()> host_int8_t;
// typedef unaligned_type<uint8_t, etl::endianness::get()> host_uint8_t;
//#endif
// typedef unaligned_type<int16_t, etl::endianness::get()> host_int16_t;
// typedef unaligned_type<uint16_t, etl::endianness::get()> host_uint16_t;
// typedef unaligned_type<int32_t, etl::endianness::get()> host_int32_t;
// typedef unaligned_type<uint32_t, etl::endianness::get()> host_uint32_t;
//#if ETL_USING_64BIT_TYPES
// typedef unaligned_type<int64_t, etl::endianness::get()> host_int64_t;
// typedef unaligned_type<uint64_t, etl::endianness::get()> host_uint64_t;
//#endif
// typedef unaligned_type<float, etl::endianness::get()> host_floa_t;
// typedef unaligned_type<double, etl::endianness::get()> host_double_t;
// typedef unaligned_type<long double, etl::endianness::get()> host_long_double_t;
//#endif
//template <>
//unaligned_type<char, etl::endian::native>
#if ETL_CPP11_SUPPORTED
template <typename T, int Endian>
using unaligned_type_t = typename etl::unaligned_type<T, Endian>::type;
#endif
#if ETL_CPP17_SUPPORTED
template <typename T>
using unaligned_type_t = typename etl::unaligned_type<T>::type;
constexpr size_t unaligned_type_v = etl::unaligned_type<T, Endian>::Size;
#endif
}

View File

@ -0,0 +1,60 @@
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2022 jwellbelove
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/
#include "unit_test_framework.h"
#include "etl/unaligned_type.h"
#include "etl/type_traits.h"
//#include <type_traits>
//#include <ostream>
//
//#if defined(ETL_COMPILER_MICROSOFT)
// #pragma warning(disable : 4996)
//#endif
//
//#ifdef ETL_COMPILER_GCC
// #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
//#endif
namespace
{
SUITE(test_unaligned_type)
{
TEST(test_host)
{
etl::net_int32_t n32;
int a = 0x12345678;
n32 = a;
int b = n32;
}
};
}

View File

@ -8462,6 +8462,7 @@
<ClCompile Include="..\test_type_traits.cpp" />
<ClCompile Include="..\test_string_stream_u16.cpp" />
<ClCompile Include="..\test_string_stream_u32.cpp" />
<ClCompile Include="..\test_unaligned_type.cpp" />
<ClCompile Include="..\test_unordered_map.cpp" />
<ClCompile Include="..\test_unordered_multimap.cpp" />
<ClCompile Include="..\test_unordered_multiset.cpp" />

View File

@ -3083,6 +3083,9 @@
<ClCompile Include="..\test_delegate_service_cpp03.cpp">
<Filter>Tests</Filter>
</ClCompile>
<ClCompile Include="..\test_unaligned_type.cpp">
<Filter>Tests</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="..\..\library.properties">