31#ifndef ETL_UNALIGNED_TYPE_INCLUDED
32#define ETL_UNALIGNED_TYPE_INCLUDED
48 namespace private_unaligned_type
54 template <
size_t Size_>
59 static ETL_CONSTANT
size_t Size = Size_;
61 typedef unsigned char storage_type;
62 typedef storage_type* pointer;
63 typedef const storage_type* const_pointer;
64 typedef storage_type* iterator;
65 typedef const storage_type* const_iterator;
80 ETL_CONSTEXPR
size_t size()
const
96 ETL_CONSTEXPR const_pointer
data()
const
106 return iterator(storage);
112 ETL_CONSTEXPR const_iterator
begin()
const
114 return const_iterator(storage);
120 ETL_CONSTEXPR const_iterator
cbegin()
const
122 return const_iterator(storage);
154 return iterator(storage + Size);
160 ETL_CONSTEXPR const_iterator
end()
const
162 return const_iterator(storage + Size);
168 ETL_CONSTEXPR const_iterator
cend()
const
170 return const_iterator(storage + Size);
215 unsigned char storage[Size];
218 template <
size_t Size_>
219 ETL_CONSTANT
size_t unaligned_type_common<Size_>::Size;
228 template <
typename T,
int Endian_>
235 typedef T value_type;
245 static ETL_CONSTANT
int Endian = Endian_;
266 template <
int Endian_Other>
285 template <
int Endian_Other>
298 return etl::equal(lhs.
data(), lhs.
data() + lhs.Size, rhs.
data());
307 return (lhs_value == rhs);
316 return (lhs == rhs_value);
324 return !(lhs == rhs);
332 return !(lhs == rhs);
340 return !(lhs == rhs);
346 ETL_CONSTEXPR14
operator T()
const
370 template <
typename U,
size_t Size = sizeof(U)>
377 template <
typename U>
381 static ETL_CONSTEXPR14
void copy(T
value, pointer store)
383 store[0] =
static_cast<storage_type
>(
value);
387 static ETL_CONSTEXPR14
void copy(const_pointer store, T&
value)
389 value =
static_cast<T
>(store[0]);
393 static ETL_CONSTEXPR14
void copy(const_pointer src,
int ,
unsigned char* dst)
403 template <
typename U>
407 static ETL_CONSTEXPR14
void copy(T
value,
unsigned char* store)
409 if (Endian == etl::endianness::value())
411 store[0] =
static_cast<storage_type
>(
value);
412 store[1] =
static_cast<storage_type
>(
value >> (1U * CHAR_BIT));
416 store[1] =
static_cast<storage_type
>(
value);
417 store[0] =
static_cast<storage_type
>(
value >> (1U * CHAR_BIT));
422 static ETL_CONSTEXPR14
void copy(const_pointer store, T&
value)
424 if (Endian == etl::endianness::value())
426 value =
static_cast<T
>(
static_cast<unsigned char>(store[0]));
427 value |=
static_cast<T
>(
static_cast<unsigned char>(store[1])) << (1U * CHAR_BIT);
431 value =
static_cast<T
>(
static_cast<unsigned char>(store[1]));
432 value |=
static_cast<T
>(
static_cast<unsigned char>(store[0])) << (1U * CHAR_BIT);
437 static ETL_CONSTEXPR14
void copy(const_pointer src,
int endian_src,
unsigned char* dst)
439 if (Endian == endian_src)
456 template <
typename U>
459 static ETL_CONSTEXPR14
void copy(T
value,
unsigned char* store)
461 if (Endian == etl::endianness::value())
463 store[0] =
static_cast<storage_type
>(
value);
464 store[1] =
static_cast<storage_type
>(
value >> (1U * CHAR_BIT));
465 store[2] =
static_cast<storage_type
>(
value >> (2U * CHAR_BIT));
466 store[3] =
static_cast<storage_type
>(
value >> (3U * CHAR_BIT));
470 store[3] =
static_cast<storage_type
>(
value);
471 store[2] =
static_cast<storage_type
>(
value >> (1U * CHAR_BIT));
472 store[1] =
static_cast<storage_type
>(
value >> (2U * CHAR_BIT));
473 store[0] =
static_cast<storage_type
>(
value >> (3U * CHAR_BIT));
478 static ETL_CONSTEXPR14
void copy(const_pointer store, T&
value)
480 if (Endian == etl::endianness::value())
482 value =
static_cast<T
>(
static_cast<unsigned char>(store[0]));
483 value |=
static_cast<T
>(
static_cast<unsigned char>(store[1])) << (1U * CHAR_BIT);
484 value |=
static_cast<T
>(
static_cast<unsigned char>(store[2])) << (2U * CHAR_BIT);
485 value |=
static_cast<T
>(
static_cast<unsigned char>(store[3])) << (3U * CHAR_BIT);
489 value =
static_cast<T
>(
static_cast<unsigned char>(store[3]));
490 value |=
static_cast<T
>(
static_cast<unsigned char>(store[2])) << (1U * CHAR_BIT);
491 value |=
static_cast<T
>(
static_cast<unsigned char>(store[1])) << (2U * CHAR_BIT);
492 value |=
static_cast<T
>(
static_cast<unsigned char>(store[0])) << (3U * CHAR_BIT);
497 static ETL_CONSTEXPR14
void copy(const_pointer src,
int endian_src,
unsigned char* dst)
499 if (Endian == endian_src)
520 template <
typename U>
523 static ETL_CONSTEXPR14
void copy(T
value,
unsigned char* store)
525 if (Endian == etl::endianness::value())
527 store[0] =
static_cast<storage_type
>(
value);
528 store[1] =
static_cast<storage_type
>(
value >> (1U * CHAR_BIT));
529 store[2] =
static_cast<storage_type
>(
value >> (2U * CHAR_BIT));
530 store[3] =
static_cast<storage_type
>(
value >> (3U * CHAR_BIT));
531 store[4] =
static_cast<storage_type
>(
value >> (4U * CHAR_BIT));
532 store[5] =
static_cast<storage_type
>(
value >> (5U * CHAR_BIT));
533 store[6] =
static_cast<storage_type
>(
value >> (6U * CHAR_BIT));
534 store[7] =
static_cast<storage_type
>(
value >> (7U * CHAR_BIT));
538 store[7] =
static_cast<storage_type
>(
value);
539 store[6] =
static_cast<storage_type
>(
value >> (1U * CHAR_BIT));
540 store[5] =
static_cast<storage_type
>(
value >> (2U * CHAR_BIT));
541 store[4] =
static_cast<storage_type
>(
value >> (3U * CHAR_BIT));
542 store[3] =
static_cast<storage_type
>(
value >> (4U * CHAR_BIT));
543 store[2] =
static_cast<storage_type
>(
value >> (5U * CHAR_BIT));
544 store[1] =
static_cast<storage_type
>(
value >> (6U * CHAR_BIT));
545 store[0] =
static_cast<storage_type
>(
value >> (7U * CHAR_BIT));
550 static ETL_CONSTEXPR14
void copy(const_pointer store, T&
value)
552 if (Endian == etl::endianness::value())
554 value =
static_cast<T
>(
static_cast<unsigned char>(store[0]));
555 value |=
static_cast<T
>(
static_cast<unsigned char>(store[1])) << (1U * CHAR_BIT);
556 value |=
static_cast<T
>(
static_cast<unsigned char>(store[2])) << (2U * CHAR_BIT);
557 value |=
static_cast<T
>(
static_cast<unsigned char>(store[3])) << (3U * CHAR_BIT);
558 value |=
static_cast<T
>(
static_cast<unsigned char>(store[4])) << (4U * CHAR_BIT);
559 value |=
static_cast<T
>(
static_cast<unsigned char>(store[5])) << (5U * CHAR_BIT);
560 value |=
static_cast<T
>(
static_cast<unsigned char>(store[6])) << (6U * CHAR_BIT);
561 value |=
static_cast<T
>(
static_cast<unsigned char>(store[7])) << (7U * CHAR_BIT);
565 value =
static_cast<T
>(
static_cast<unsigned char>(store[7]));
566 value |=
static_cast<T
>(
static_cast<unsigned char>(store[6])) << (1U * CHAR_BIT);
567 value |=
static_cast<T
>(
static_cast<unsigned char>(store[5])) << (2U * CHAR_BIT);
568 value |=
static_cast<T
>(
static_cast<unsigned char>(store[4])) << (3U * CHAR_BIT);
569 value |=
static_cast<T
>(
static_cast<unsigned char>(store[3])) << (4U * CHAR_BIT);
570 value |=
static_cast<T
>(
static_cast<unsigned char>(store[2])) << (5U * CHAR_BIT);
571 value |=
static_cast<T
>(
static_cast<unsigned char>(store[1])) << (6U * CHAR_BIT);
572 value |=
static_cast<T
>(
static_cast<unsigned char>(store[0])) << (7U * CHAR_BIT);
577 static ETL_CONSTEXPR14
void copy(const_pointer src,
int endian_src,
unsigned char* dst)
579 if (Endian == endian_src)
605 template <
typename T,
int Endian_>
608 template <
typename T,
int Endian_>
611#if ETL_HAS_CONSTEXPR_ENDIANNESS
613 typedef unaligned_type<char, etl::endianness::value()> host_char_t;
614 typedef unaligned_type<
signed char, etl::endianness::value()> host_schar_t;
615 typedef unaligned_type<
unsigned char, etl::endianness::value()> host_uchar_t;
616 typedef unaligned_type<short, etl::endianness::value()> host_short_t;
617 typedef unaligned_type<
unsigned short, etl::endianness::value()> host_ushort_t;
619 typedef unaligned_type<
unsigned int, etl::endianness::value()> host_uint_t;
620 typedef unaligned_type<long, etl::endianness::value()> host_long_t;
621 typedef unaligned_type<
unsigned long, etl::endianness::value()> host_ulong_t;
622 typedef unaligned_type<
long long, etl::endianness::value()> host_long_long_t;
623 typedef unaligned_type<
unsigned long long, etl::endianness::value()> host_ulong_long_t;
624#if ETL_USING_8BIT_TYPES
625 typedef unaligned_type<int8_t, etl::endianness::value()> host_int8_t;
626 typedef unaligned_type<uint8_t, etl::endianness::value()> host_uint8_t;
628 typedef unaligned_type<int16_t, etl::endianness::value()> host_int16_t;
629 typedef unaligned_type<uint16_t, etl::endianness::value()> host_uint16_t;
630 typedef unaligned_type<int32_t, etl::endianness::value()> host_int32_t;
631 typedef unaligned_type<uint32_t, etl::endianness::value()> host_uint32_t;
632#if ETL_USING_64BIT_TYPES
633 typedef unaligned_type<int64_t, etl::endianness::value()> host_int64_t;
634 typedef unaligned_type<uint64_t, etl::endianness::value()> host_uint64_t;
636 typedef unaligned_type<float, etl::endianness::value()> host_float_t;
637 typedef unaligned_type<double, etl::endianness::value()> host_double_t;
638 typedef unaligned_type<
long double, etl::endianness::value()> host_long_double_t;
653#if ETL_USING_8BIT_TYPES
661#if ETL_USING_64BIT_TYPES
681#if ETL_USING_8BIT_TYPES
689#if ETL_USING_64BIT_TYPES
709#if ETL_USING_8BIT_TYPES
710 typedef be_int8_t net_int8_t;
711 typedef be_uint8_t net_uint8_t;
717#if ETL_USING_64BIT_TYPES
726 template <
typename T,
int Endian>
731 template <
typename T,
int Endian>
Definition: unaligned_type.h:56
ETL_CONSTEXPR14 const_reverse_iterator crend() const
Const reverse iterator to the end of the storage.
Definition: unaligned_type.h:192
iterator begin()
Iterator to the beginning of the storage.
Definition: unaligned_type.h:104
storage_type & operator[](int i)
Index operator.
Definition: unaligned_type.h:200
ETL_CONSTEXPR const_iterator cbegin() const
Const iterator to the beginning of the storage.
Definition: unaligned_type.h:120
reverse_iterator rend()
Reverse iterator to the end of the storage.
Definition: unaligned_type.h:176
iterator end()
Iterator to the end of the storage.
Definition: unaligned_type.h:152
reverse_iterator rbegin()
Reverse iterator to the beginning of the storage.
Definition: unaligned_type.h:128
ETL_CONSTEXPR unaligned_type_common()
Default constructor.
Definition: unaligned_type.h:72
pointer data()
Pointer to the beginning of the storage.
Definition: unaligned_type.h:88
ETL_CONSTEXPR size_t size() const
Size of the storage.
Definition: unaligned_type.h:80
ETL_CONSTEXPR const_iterator cend() const
Const iterator to the end of the storage.
Definition: unaligned_type.h:168
ETL_CONSTEXPR14 const_reverse_iterator crbegin() const
Const reverse iterator to the beginning of the storage.
Definition: unaligned_type.h:144
Definition: iterator.h:228
Allows an arithmetic type to be stored at an unaligned address.
Definition: unaligned_type.h:230
friend ETL_CONSTEXPR14 bool operator!=(const unaligned_type &lhs, T rhs)
Inequality operator.
Definition: unaligned_type.h:322
ETL_CONSTEXPR14 unaligned_type & operator=(T value)
Assignment operator.
Definition: unaligned_type.h:275
ETL_CONSTEXPR14 T value() const
Get the value.
Definition: unaligned_type.h:358
friend ETL_CONSTEXPR14 bool operator==(const unaligned_type &lhs, const unaligned_type &rhs)
Equality operator.
Definition: unaligned_type.h:296
ETL_CONSTEXPR unaligned_type()
Default constructor.
Definition: unaligned_type.h:251
is_floating_point
Definition: type_traits_generator.h:1031
is_integral
Definition: type_traits_generator.h:1001
bitset_ext
Definition: absolute.h:38
Unaligned copy.
Definition: unaligned_type.h:371