diff --git a/include/etl/unaligned_type.h b/include/etl/unaligned_type.h index 0db9543a..09acddb7 100644 --- a/include/etl/unaligned_type.h +++ b/include/etl/unaligned_type.h @@ -58,10 +58,11 @@ namespace etl static ETL_CONSTANT size_t Size = Size_; - typedef unsigned char* pointer; - typedef const unsigned char* const_pointer; - typedef unsigned char* iterator; - typedef const unsigned char* const_iterator; + typedef unsigned char storage_type; + typedef storage_type* pointer; + typedef const storage_type* const_pointer; + typedef storage_type* iterator; + typedef const storage_type* const_iterator; typedef etl::reverse_iterator reverse_iterator; typedef etl::reverse_iterator const_reverse_iterator; @@ -196,7 +197,7 @@ namespace etl //************************************************************************* /// Index operator. //************************************************************************* - unsigned char& operator[](int i) + storage_type& operator[](int i) { return storage[i]; } @@ -204,7 +205,7 @@ namespace etl //************************************************************************* /// Const index operator. //************************************************************************* - ETL_CONSTEXPR const unsigned char& operator[](int i) const + ETL_CONSTEXPR const storage_type& operator[](int i) const { return storage[i]; } @@ -217,6 +218,9 @@ namespace etl //************************************************************************* /// unaligned_type + ///\brief Allows an arithmetic type to be stored at an unaligned address. + ///\tparam T The arithmetic type. + ///\tparam Endian The endianness of the arithmetic type. //************************************************************************* template class unaligned_type : public private_unaligned_type::unaligned_type_common @@ -227,7 +231,15 @@ namespace etl typedef T value_type; - static ETL_CONSTANT int Endian = Endian_; + typedef typename private_unaligned_type::unaligned_type_common::storage_type storage_type; + typedef typename private_unaligned_type::unaligned_type_common::pointer pointer; + typedef typename private_unaligned_type::unaligned_type_common::const_pointer const_pointer; + typedef typename private_unaligned_type::unaligned_type_common::iterator iterator; + typedef typename private_unaligned_type::unaligned_type_common::const_iterator const_iterator; + typedef typename private_unaligned_type::unaligned_type_common::reverse_iterator reverse_iterator; + typedef typename private_unaligned_type::unaligned_type_common::const_reverse_iterator const_reverse_iterator; + + static ETL_CONSTANT int Endian = Endian_; static ETL_CONSTANT size_t Size = private_unaligned_type::unaligned_type_common::Size; //************************************************************************* @@ -356,32 +368,34 @@ namespace etl struct unaligned_copy; //******************************************* - // Size == 1 + /// Unaligned copy + /// Size == 1 //******************************************* template struct unaligned_copy { //******************************* - static ETL_CONSTEXPR14 void copy(T value, unsigned char* store) + static ETL_CONSTEXPR14 void copy(T value, pointer store) { - store[0] = static_cast(value); + store[0] = static_cast(value); } //******************************* - static ETL_CONSTEXPR14 void copy(const unsigned char* store, T& value) + static ETL_CONSTEXPR14 void copy(const_pointer store, T& value) { value = static_cast(store[0]); } //******************************* - static ETL_CONSTEXPR14 void copy(const unsigned char* src, int /*endian_src*/, unsigned char* dst) + static ETL_CONSTEXPR14 void copy(const_pointer src, int /*endian_src*/, unsigned char* dst) { dst[0] = src[0]; } }; //******************************************* - // Size == 2 + /// Unaligned copy + /// Size == 2 //******************************************* template struct unaligned_copy @@ -391,33 +405,33 @@ namespace etl { if (Endian == etl::endianness::value()) { - store[0] = static_cast(value); - store[1] = static_cast(value >> (1U * CHAR_BIT)); + store[0] = static_cast(value); + store[1] = static_cast(value >> (1U * CHAR_BIT)); } else { - store[1] = static_cast(value); - store[0] = static_cast(value >> (1U * CHAR_BIT)); + store[1] = static_cast(value); + store[0] = static_cast(value >> (1U * CHAR_BIT)); } } //******************************* - static ETL_CONSTEXPR14 void copy(const unsigned char* store, T& value) + static ETL_CONSTEXPR14 void copy(const_pointer store, T& value) { if (Endian == etl::endianness::value()) { - value = static_cast(store[0]); - value |= static_cast(store[1]) << (1U * CHAR_BIT); + value = static_cast(static_cast(store[0])); + value |= static_cast(static_cast(store[1])) << (1U * CHAR_BIT); } else { - value = static_cast(store[1]); - value |= static_cast(store[0]) << (1U * CHAR_BIT); + value = static_cast(static_cast(store[1])); + value |= static_cast(static_cast(store[0])) << (1U * CHAR_BIT); } } //******************************* - static ETL_CONSTEXPR14 void copy(const unsigned char* src, int endian_src, unsigned char* dst) + static ETL_CONSTEXPR14 void copy(const_pointer src, int endian_src, unsigned char* dst) { if (Endian == endian_src) { @@ -433,7 +447,8 @@ namespace etl }; //******************************************* - // Size == 4 + /// Unaligned copy + /// Size == 4 //******************************************* template struct unaligned_copy @@ -442,41 +457,41 @@ namespace etl { if (Endian == etl::endianness::value()) { - store[0] = static_cast(value); - store[1] = static_cast(value >> (1U * CHAR_BIT)); - store[2] = static_cast(value >> (2U * CHAR_BIT)); - store[3] = static_cast(value >> (3U * CHAR_BIT)); + store[0] = static_cast(value); + store[1] = static_cast(value >> (1U * CHAR_BIT)); + store[2] = static_cast(value >> (2U * CHAR_BIT)); + store[3] = static_cast(value >> (3U * CHAR_BIT)); } else { - store[3] = static_cast(value); - store[2] = static_cast(value >> (1U * CHAR_BIT)); - store[1] = static_cast(value >> (2U * CHAR_BIT)); - store[0] = static_cast(value >> (3U * CHAR_BIT)); + store[3] = static_cast(value); + store[2] = static_cast(value >> (1U * CHAR_BIT)); + store[1] = static_cast(value >> (2U * CHAR_BIT)); + store[0] = static_cast(value >> (3U * CHAR_BIT)); } } //******************************* - static ETL_CONSTEXPR14 void copy(const char* store, T& value) + static ETL_CONSTEXPR14 void copy(const_pointer store, T& value) { if (Endian == etl::endianness::value()) { - value = static_cast(store[0]); - value |= static_cast(store[1]) << (1U * CHAR_BIT); - value |= static_cast(store[2]) << (2U * CHAR_BIT); - value |= static_cast(store[3]) << (3U * CHAR_BIT); + value = static_cast(static_cast(store[0])); + value |= static_cast(static_cast(store[1])) << (1U * CHAR_BIT); + value |= static_cast(static_cast(store[2])) << (2U * CHAR_BIT); + value |= static_cast(static_cast(store[3])) << (3U * CHAR_BIT); } else { - value = static_cast(store[3]); - value |= static_cast(store[2]) << (1U * CHAR_BIT); - value |= static_cast(store[1]) << (2U * CHAR_BIT); - value |= static_cast(store[0]) << (3U * CHAR_BIT); + value = static_cast(static_cast(store[3])); + value |= static_cast(static_cast(store[2])) << (1U * CHAR_BIT); + value |= static_cast(static_cast(store[1])) << (2U * CHAR_BIT); + value |= static_cast(static_cast(store[0])) << (3U * CHAR_BIT); } } //******************************* - static ETL_CONSTEXPR14 void copy(const unsigned char* src, int endian_src, unsigned char* dst) + static ETL_CONSTEXPR14 void copy(const_pointer src, int endian_src, unsigned char* dst) { if (Endian == endian_src) { @@ -496,7 +511,8 @@ namespace etl }; //******************************************* - // Size == 8 + /// Unaligned copy + /// Size == 8 //******************************************* template struct unaligned_copy @@ -505,57 +521,57 @@ namespace etl { if (Endian == etl::endianness::value()) { - store[0] = static_cast(value); - store[1] = static_cast(value >> (1U * CHAR_BIT)); - store[2] = static_cast(value >> (2U * CHAR_BIT)); - store[3] = static_cast(value >> (3U * CHAR_BIT)); - store[4] = static_cast(value >> (4U * CHAR_BIT)); - store[5] = static_cast(value >> (5U * CHAR_BIT)); - store[6] = static_cast(value >> (6U * CHAR_BIT)); - store[7] = static_cast(value >> (7U * CHAR_BIT)); + store[0] = static_cast(value); + store[1] = static_cast(value >> (1U * CHAR_BIT)); + store[2] = static_cast(value >> (2U * CHAR_BIT)); + store[3] = static_cast(value >> (3U * CHAR_BIT)); + store[4] = static_cast(value >> (4U * CHAR_BIT)); + store[5] = static_cast(value >> (5U * CHAR_BIT)); + store[6] = static_cast(value >> (6U * CHAR_BIT)); + store[7] = static_cast(value >> (7U * CHAR_BIT)); } else { - store[7] = static_cast(value); - store[6] = static_cast(value >> (1U * CHAR_BIT)); - store[5] = static_cast(value >> (2U * CHAR_BIT)); - store[4] = static_cast(value >> (3U * CHAR_BIT)); - store[3] = static_cast(value >> (4U * CHAR_BIT)); - store[2] = static_cast(value >> (5U * CHAR_BIT)); - store[1] = static_cast(value >> (6U * CHAR_BIT)); - store[0] = static_cast(value >> (7U * CHAR_BIT)); + store[7] = static_cast(value); + store[6] = static_cast(value >> (1U * CHAR_BIT)); + store[5] = static_cast(value >> (2U * CHAR_BIT)); + store[4] = static_cast(value >> (3U * CHAR_BIT)); + store[3] = static_cast(value >> (4U * CHAR_BIT)); + store[2] = static_cast(value >> (5U * CHAR_BIT)); + store[1] = static_cast(value >> (6U * CHAR_BIT)); + store[0] = static_cast(value >> (7U * CHAR_BIT)); } } //******************************* - static ETL_CONSTEXPR14 void copy(const unsigned char* store, T& value) + static ETL_CONSTEXPR14 void copy(const_pointer store, T& value) { if (Endian == etl::endianness::value()) { - value = static_cast(store[0]); - value |= static_cast(store[1]) << (1U * CHAR_BIT); - value |= static_cast(store[2]) << (2U * CHAR_BIT); - value |= static_cast(store[3]) << (3U * CHAR_BIT); - value |= static_cast(store[4]) << (4U * CHAR_BIT); - value |= static_cast(store[5]) << (5U * CHAR_BIT); - value |= static_cast(store[6]) << (6U * CHAR_BIT); - value |= static_cast(store[7]) << (7U * CHAR_BIT); + value = static_cast(static_cast(store[0])); + value |= static_cast(static_cast(store[1])) << (1U * CHAR_BIT); + value |= static_cast(static_cast(store[2])) << (2U * CHAR_BIT); + value |= static_cast(static_cast(store[3])) << (3U * CHAR_BIT); + value |= static_cast(static_cast(store[4])) << (4U * CHAR_BIT); + value |= static_cast(static_cast(store[5])) << (5U * CHAR_BIT); + value |= static_cast(static_cast(store[6])) << (6U * CHAR_BIT); + value |= static_cast(static_cast(store[7])) << (7U * CHAR_BIT); } else { - value = static_cast(store[7]); - value |= static_cast(store[6]) << (1U * CHAR_BIT); - value |= static_cast(store[5]) << (2U * CHAR_BIT); - value |= static_cast(store[4]) << (3U * CHAR_BIT); - value |= static_cast(store[3]) << (4U * CHAR_BIT); - value |= static_cast(store[2]) << (5U * CHAR_BIT); - value |= static_cast(store[1]) << (6U * CHAR_BIT); - value |= static_cast(store[0]) << (7U * CHAR_BIT); + value = static_cast(static_cast(store[7])); + value |= static_cast(static_cast(store[6])) << (1U * CHAR_BIT); + value |= static_cast(static_cast(store[5])) << (2U * CHAR_BIT); + value |= static_cast(static_cast(store[4])) << (3U * CHAR_BIT); + value |= static_cast(static_cast(store[3])) << (4U * CHAR_BIT); + value |= static_cast(static_cast(store[2])) << (5U * CHAR_BIT); + value |= static_cast(static_cast(store[1])) << (6U * CHAR_BIT); + value |= static_cast(static_cast(store[0])) << (7U * CHAR_BIT); } } //******************************* - static ETL_CONSTEXPR14 void copy(const unsigned char* src, int endian_src, unsigned char* dst) + static ETL_CONSTEXPR14 void copy(const_pointer src, int endian_src, unsigned char* dst) { if (Endian == endian_src) { @@ -585,89 +601,89 @@ namespace etl #if ETL_HAS_CONSTEXPR_ENDIANNESS // 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_ushort_t; - typedef unaligned_type host_int_t; - typedef unaligned_type host_uint_t; - typedef unaligned_type host_long_t; - typedef unaligned_type host_ulong_t; - typedef unaligned_type host_long_long_t; + 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_ushort_t; + typedef unaligned_type host_int_t; + typedef unaligned_type host_uint_t; + typedef unaligned_type host_long_t; + typedef unaligned_type host_ulong_t; + typedef unaligned_type host_long_long_t; typedef unaligned_type host_ulong_long_t; #if ETL_USING_8BIT_TYPES - typedef unaligned_type host_int8_t; - typedef unaligned_type host_uint8_t; + 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; + 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; + typedef unaligned_type host_int64_t; + typedef unaligned_type host_uint64_t; #endif - typedef unaligned_type host_float_t; - typedef unaligned_type host_double_t; - typedef unaligned_type host_long_double_t; + typedef unaligned_type host_float_t; + typedef unaligned_type host_double_t; + typedef unaligned_type host_long_double_t; #endif // Little Endian - typedef unaligned_type le_char_t; - typedef unaligned_type le_schar_t; - typedef unaligned_type le_uchar_t; - typedef unaligned_type le_short_t; - typedef unaligned_type le_ushort_t; - typedef unaligned_type le_int_t; - typedef unaligned_type le_uint_t; - typedef unaligned_type le_long_t; - typedef unaligned_type le_ulong_t; - typedef unaligned_type le_long_long_t; + typedef unaligned_type le_char_t; + typedef unaligned_type le_schar_t; + typedef unaligned_type le_uchar_t; + typedef unaligned_type le_short_t; + typedef unaligned_type le_ushort_t; + typedef unaligned_type le_int_t; + typedef unaligned_type le_uint_t; + typedef unaligned_type le_long_t; + typedef unaligned_type le_ulong_t; + typedef unaligned_type le_long_long_t; typedef unaligned_type le_ulong_long_t; #if ETL_USING_8BIT_TYPES - typedef unaligned_type le_int8_t; - typedef unaligned_type le_uint8_t; + typedef unaligned_type le_int8_t; + typedef unaligned_type le_uint8_t; #endif - typedef unaligned_type le_int16_t; - typedef unaligned_type le_uint16_t; - typedef unaligned_type le_int32_t; - typedef unaligned_type le_uint32_t; + typedef unaligned_type le_int16_t; + typedef unaligned_type le_uint16_t; + typedef unaligned_type le_int32_t; + typedef unaligned_type le_uint32_t; #if ETL_USING_64BIT_TYPES - typedef unaligned_type le_int64_t; - typedef unaligned_type le_uint64_t; + typedef unaligned_type le_int64_t; + typedef unaligned_type le_uint64_t; #endif - typedef unaligned_type le_float_t; - typedef unaligned_type le_double_t; - typedef unaligned_type le_long_double_t; + typedef unaligned_type le_float_t; + typedef unaligned_type le_double_t; + typedef unaligned_type le_long_double_t; // Big Endian - typedef unaligned_type be_char_t; - typedef unaligned_type be_schar_t; - typedef unaligned_type be_uchar_t; - typedef unaligned_type be_short_t; - typedef unaligned_type be_ushort_t; - typedef unaligned_type be_int_t; - typedef unaligned_type be_uint_t; - typedef unaligned_type be_long_t; - typedef unaligned_type be_ulong_t; - typedef unaligned_type be_long_long_t; + typedef unaligned_type be_char_t; + typedef unaligned_type be_schar_t; + typedef unaligned_type be_uchar_t; + typedef unaligned_type be_short_t; + typedef unaligned_type be_ushort_t; + typedef unaligned_type be_int_t; + typedef unaligned_type be_uint_t; + typedef unaligned_type be_long_t; + typedef unaligned_type be_ulong_t; + typedef unaligned_type be_long_long_t; typedef unaligned_type be_ulong_long_t; #if ETL_USING_8BIT_TYPES - typedef unaligned_type be_int8_t; - typedef unaligned_type be_uint8_t; + typedef unaligned_type be_int8_t; + typedef unaligned_type be_uint8_t; #endif - typedef unaligned_type be_int16_t; - typedef unaligned_type be_uint16_t; - typedef unaligned_type be_int32_t; - typedef unaligned_type be_uint32_t; + typedef unaligned_type be_int16_t; + typedef unaligned_type be_uint16_t; + typedef unaligned_type be_int32_t; + typedef unaligned_type be_uint32_t; #if ETL_USING_64BIT_TYPES - typedef unaligned_type be_int64_t; - typedef unaligned_type be_uint64_t; + typedef unaligned_type be_int64_t; + typedef unaligned_type be_uint64_t; #endif - typedef unaligned_type be_float_t; - typedef unaligned_type be_double_t; - typedef unaligned_type be_long_double_t; + typedef unaligned_type be_float_t; + typedef unaligned_type be_double_t; + typedef unaligned_type be_long_double_t; // Network Order typedef be_char_t net_char_t;