diff --git a/include/etl/endianness.h b/include/etl/endianness.h index 5db22523..4a18c9e5 100644 --- a/include/etl/endianness.h +++ b/include/etl/endianness.h @@ -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 +// } }; //*************************************************************************** diff --git a/include/etl/unaligned_type.h b/include/etl/unaligned_type.h index ef51536b..9de3ccca 100644 --- a/include/etl/unaligned_type.h +++ b/include/etl/unaligned_type.h @@ -39,16 +39,19 @@ SOFTWARE. #include "type_traits.h" #include "endianness.h" +#include + namespace etl { - template ::value, void>::type> + template ::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 + unaligned_type& operator =(const unaligned_type& 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 ::value, void>::type> + //class unaligned_type + //{ + //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 net_char_t; typedef unaligned_type net_schar_t; typedef unaligned_type net_uchar_t; - typedef unaligned_type net_short_t; - typedef unaligned_type net_unsigned_short_t; - typedef unaligned_type net_int_t; - typedef unaligned_type net_unsigned_int_t; + typedef unaligned_type net_shor_t; + typedef unaligned_type net_unsigned_shor_t; + typedef unaligned_type net_in_t; + typedef unaligned_type net_unsigned_in_t; typedef unaligned_type net_long_t; typedef unaligned_type net_unsigned_long_t; typedef unaligned_type net_long_long_t; typedef unaligned_type net_unsigned_long_long_t; #if ETL_8BIT_SUPPORT - typedef unaligned_type net_int8_t_t; - typedef unaligned_type net_uint8_t_t; + typedef unaligned_type net_int8_t; + typedef unaligned_type net_uint8_t; #endif - typedef unaligned_type net_int16_t_t; - typedef unaligned_type net_uint16_t_t; - typedef unaligned_type net_int32_t_t; - typedef unaligned_type net_uint32_t_t; + typedef unaligned_type net_int16_t; + typedef unaligned_type net_uint16_t; + typedef unaligned_type net_int32_t; + typedef unaligned_type net_uint32_t; #if ETL_USING_64BIT_TYPES - typedef unaligned_type net_int64_t_t; - typedef unaligned_type net_uint64_t_t; + typedef unaligned_type net_int64_t; + typedef unaligned_type net_uint64_t; #endif - typedef unaligned_type net_float_t; + typedef unaligned_type net_floa_t; typedef unaligned_type net_double_t; typedef unaligned_type net_long_double_t; -#if ETL_CPP11_SUPPORTED - // Host order - typedef unaligned_type host_char_t; - typedef unaligned_type host_schar_t; - typedef unaligned_type host_uchar_t; - typedef unaligned_type host_short_t; - typedef unaligned_type host_unsigned_short_t; - typedef unaligned_type host_int_t; - typedef unaligned_type host_unsigned_int_t; - typedef unaligned_type host_long_t; - typedef unaligned_type host_unsigned_long_t; - typedef unaligned_type host_long_long_t; - typedef unaligned_type host_unsigned_long_long_t; -#if ETL_8BIT_SUPPORT - typedef unaligned_type host_int8_t_t; - typedef unaligned_type host_uint8_t_t; -#endif - typedef unaligned_type host_int16_t_t; - typedef unaligned_type host_uint16_t_t; - typedef unaligned_type host_int32_t_t; - typedef unaligned_type host_uint32_t_t; -#if ETL_USING_64BIT_TYPES - typedef unaligned_type host_int64_t_t; - typedef unaligned_type host_uint64_t_t; -#endif - typedef unaligned_type host_float_t; - typedef unaligned_type host_double_t; - typedef unaligned_type host_long_double_t; -#endif +//#if ETL_CPP11_SUPPORTED +// // Host order +// typedef unaligned_type host_char_t; +// typedef unaligned_type host_schar_t; +// typedef unaligned_type host_uchar_t; +// typedef unaligned_type host_shor_t; +// typedef unaligned_type host_unsigned_shor_t; +// typedef unaligned_type host_in_t; +// typedef unaligned_type host_unsigned_in_t; +// typedef unaligned_type host_long_t; +// typedef unaligned_type host_unsigned_long_t; +// typedef unaligned_type host_long_long_t; +// typedef unaligned_type host_unsigned_long_long_t; +//#if ETL_8BIT_SUPPORT +// typedef unaligned_type host_int8_t; +// typedef unaligned_type host_uint8_t; +//#endif +// typedef unaligned_type host_int16_t; +// typedef unaligned_type host_uint16_t; +// typedef unaligned_type host_int32_t; +// typedef unaligned_type host_uint32_t; +//#if ETL_USING_64BIT_TYPES +// typedef unaligned_type host_int64_t; +// typedef unaligned_type host_uint64_t; +//#endif +// typedef unaligned_type host_floa_t; +// typedef unaligned_type host_double_t; +// typedef unaligned_type host_long_double_t; +//#endif + + //template <> + //unaligned_type #if ETL_CPP11_SUPPORTED + template + using unaligned_type_t = typename etl::unaligned_type::type; +#endif + +#if ETL_CPP17_SUPPORTED template - using unaligned_type_t = typename etl::unaligned_type::type; + constexpr size_t unaligned_type_v = etl::unaligned_type::Size; #endif } diff --git a/test/test_unaligned_type.cpp b/test/test_unaligned_type.cpp new file mode 100644 index 00000000..5a0f3838 --- /dev/null +++ b/test/test_unaligned_type.cpp @@ -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 +//#include +// +//#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; + } + }; +} diff --git a/test/vs2019/etl.vcxproj b/test/vs2019/etl.vcxproj index 4fe89a50..8d23922c 100644 --- a/test/vs2019/etl.vcxproj +++ b/test/vs2019/etl.vcxproj @@ -8462,6 +8462,7 @@ + diff --git a/test/vs2019/etl.vcxproj.filters b/test/vs2019/etl.vcxproj.filters index 6e3043e7..1550674f 100644 --- a/test/vs2019/etl.vcxproj.filters +++ b/test/vs2019/etl.vcxproj.filters @@ -3083,6 +3083,9 @@ Tests + + Tests +