From c9bb331f4cec66ff2e08a63a891edb5f8070e995 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Wed, 14 Jun 2023 08:20:36 +0100 Subject: [PATCH] Added C++11 alignment matcher code --- include/etl/alignment.h | 112 ++++++++++++++++++++++++++++------------ 1 file changed, 79 insertions(+), 33 deletions(-) diff --git a/include/etl/alignment.h b/include/etl/alignment.h index 19bc3151..78f10405 100644 --- a/include/etl/alignment.h +++ b/include/etl/alignment.h @@ -102,16 +102,61 @@ namespace etl namespace private_alignment { +#if ETL_USING_CPP11 //*************************************************************************** // Matcher. //*************************************************************************** - template + class type_with_alignment_matcher; + + // Matching alignment. + template + class type_with_alignment_matcher + { + public: + + typedef T1 type; + }; + + // Non-matching alignment + template + class type_with_alignment_matcher + { + public: + + typedef typename type_with_alignment_matcher < Alignment <= etl::alignment_of::value , Alignment, T2, TRest... > ::type type; + }; + + // Non-matching alignment, none left. + template + class type_with_alignment_matcher + { + public: + + typedef char type; + }; + + //*************************************************************************** + // Helper. + //*************************************************************************** + template + class type_with_alignment_helper + { + public: + + typedef typename type_with_alignment_matcher::value, Alignment, T1, T...>::type type; + }; +#else + //*************************************************************************** + // Matcher. + //*************************************************************************** + template class type_with_alignment_matcher; // Matching alignment. - template - class type_with_alignment_matcher + template + class type_with_alignment_matcher { public: @@ -119,17 +164,17 @@ namespace etl }; // Non-matching alignment. - template - class type_with_alignment_matcher + template + class type_with_alignment_matcher { public: - typedef typename type_with_alignment_matcher::value, ALIGNMENT, T2, T3, T4, T5, T6, T7, T8, void>::type type; + typedef typename type_with_alignment_matcher::value, Alignment, T2, T3, T4, T5, T6, T7, T8, void>::type type; }; // Non-matching alignment, none left. - template - class type_with_alignment_matcher + template + class type_with_alignment_matcher { public: @@ -139,38 +184,39 @@ namespace etl //*************************************************************************** // Helper. //*************************************************************************** - template + template class type_with_alignment_helper { public: - typedef typename type_with_alignment_matcher::value, ALIGNMENT, T1, T2, T3, T4, T5, T6, T7, T8>::type type; + typedef typename type_with_alignment_matcher::value, Alignment, T1, T2, T3, T4, T5, T6, T7, T8>::type type; }; +#endif } //*************************************************************************** /// Gets a type that has the same as the specified alignment. ///\ingroup alignment //*************************************************************************** - template + template class type_with_alignment { public: #if ETL_NOT_USING_64BIT_TYPES - typedef typename private_alignment::type_with_alignment_helper::type type; + typedef typename private_alignment::type_with_alignment_helper::type type; #else - typedef typename private_alignment::type_with_alignment_helper::type type; + typedef typename private_alignment::type_with_alignment_helper::type type; #endif }; //*************************************************************************** /// Aligned storage - /// LENGTH should be determined in terms of sizeof() + /// Length should be determined in terms of sizeof() ///\ingroup alignment //*************************************************************************** - template + template struct aligned_storage { struct type @@ -184,7 +230,7 @@ namespace etl template operator T& () { - ETL_STATIC_ASSERT((etl::is_same:: value || ((ALIGNMENT % etl::alignment_of::value) == 0)), "Incompatible alignment"); + ETL_STATIC_ASSERT((etl::is_same:: value || ((Alignment % etl::alignment_of::value) == 0)), "Incompatible alignment"); T* t = *this; return *t; } @@ -193,7 +239,7 @@ namespace etl template operator const T& () const { - ETL_STATIC_ASSERT((etl::is_same:: value || ((ALIGNMENT % etl::alignment_of::value) == 0)), "Incompatible alignment"); + ETL_STATIC_ASSERT((etl::is_same:: value || ((Alignment % etl::alignment_of::value) == 0)), "Incompatible alignment"); const T* t = *this; return *t; } @@ -202,7 +248,7 @@ namespace etl template operator T* () { - ETL_STATIC_ASSERT((etl::is_same:: value || ((ALIGNMENT % etl::alignment_of::value) == 0)), "Incompatible alignment"); + ETL_STATIC_ASSERT((etl::is_same:: value || ((Alignment % etl::alignment_of::value) == 0)), "Incompatible alignment"); return reinterpret_cast(data); } @@ -210,7 +256,7 @@ namespace etl template operator const T* () const { - ETL_STATIC_ASSERT((etl::is_same:: value || ((ALIGNMENT % etl::alignment_of::value) == 0)), "Incompatible alignment"); + ETL_STATIC_ASSERT((etl::is_same:: value || ((Alignment % etl::alignment_of::value) == 0)), "Incompatible alignment"); return reinterpret_cast(data); } @@ -218,7 +264,7 @@ namespace etl template T& get_reference() { - ETL_STATIC_ASSERT((etl::is_same:: value || ((ALIGNMENT % etl::alignment_of::value) == 0)), "Incompatible alignment"); + ETL_STATIC_ASSERT((etl::is_same:: value || ((Alignment % etl::alignment_of::value) == 0)), "Incompatible alignment"); T* t = *this; return *t; } @@ -227,7 +273,7 @@ namespace etl template const T& get_reference() const { - ETL_STATIC_ASSERT((etl::is_same:: value || ((ALIGNMENT % etl::alignment_of::value) == 0)), "Incompatible alignment"); + ETL_STATIC_ASSERT((etl::is_same:: value || ((Alignment % etl::alignment_of::value) == 0)), "Incompatible alignment"); const T* t = *this; return *t; } @@ -236,7 +282,7 @@ namespace etl template T* get_address() { - ETL_STATIC_ASSERT((etl::is_same:: value || ((ALIGNMENT % etl::alignment_of::value) == 0)), "Incompatible alignment"); + ETL_STATIC_ASSERT((etl::is_same:: value || ((Alignment % etl::alignment_of::value) == 0)), "Incompatible alignment"); return reinterpret_cast(data); } @@ -244,39 +290,39 @@ namespace etl template const T* get_address() const { - ETL_STATIC_ASSERT((etl::is_same:: value || ((ALIGNMENT % etl::alignment_of::value) == 0)), "Incompatible alignment"); + ETL_STATIC_ASSERT((etl::is_same:: value || ((Alignment % etl::alignment_of::value) == 0)), "Incompatible alignment"); return reinterpret_cast(data); } #if ETL_USING_CPP11 && !defined(ETL_COMPILER_ARM5) - alignas(ALIGNMENT) char data[LENGTH]; + alignas(Alignment) char data[Length]; #else union { - char data[LENGTH]; - typename etl::type_with_alignment::type etl_alignment_type; // A POD type that has the same alignment as ALIGNMENT. + char data[Length]; + typename etl::type_with_alignment::type etl_alignment_type; // A POD type that has the same alignment as Alignment. }; #endif }; }; #if ETL_USING_CPP11 - template - using aligned_storage_t = typename aligned_storage::type; + template + using aligned_storage_t = typename aligned_storage::type; #endif //*************************************************************************** /// Aligned storage as ///\ingroup alignment //*************************************************************************** - template - struct aligned_storage_as : public etl::aligned_storage::value> + template + struct aligned_storage_as : public etl::aligned_storage::value> { }; #if ETL_USING_CPP11 - template - using aligned_storage_as_t = typename aligned_storage_as::type; + template + using aligned_storage_as_t = typename aligned_storage_as::type; #endif }