From 11155eb9f152347d757522df82608167364341dc Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Wed, 27 Sep 2023 13:48:53 +0100 Subject: [PATCH] Fixed alignment functionality --- include/etl/alignment.h | 17 ++++++++++++++--- test/test_alignment.cpp | 12 ++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/include/etl/alignment.h b/include/etl/alignment.h index 78f10405..dae08aa9 100644 --- a/include/etl/alignment.h +++ b/include/etl/alignment.h @@ -204,13 +204,24 @@ namespace etl { public: -#if ETL_NOT_USING_64BIT_TYPES - typedef typename private_alignment::type_with_alignment_helper::type type; +#if ETL_USING_CPP11 + typedef struct { alignas(Alignment) char dummy; } type; #else - typedef typename private_alignment::type_with_alignment_helper::type type; + #if ETL_NOT_USING_64BIT_TYPES + typedef typename private_alignment::type_with_alignment_helper::type type; + #else + typedef typename private_alignment::type_with_alignment_helper::type type; + #endif #endif + + ETL_STATIC_ASSERT(etl::alignment_of::value == Alignment, "Unable to create the type with the specified alignment"); }; +#if ETL_USING_CPP11 + template + using type_with_alignment_t = typename type_with_alignment::type; +#endif + //*************************************************************************** /// Aligned storage /// Length should be determined in terms of sizeof() diff --git a/test/test_alignment.cpp b/test/test_alignment.cpp index 39e5dc20..29de827a 100644 --- a/test/test_alignment.cpp +++ b/test/test_alignment.cpp @@ -143,5 +143,17 @@ namespace CHECK_FALSE(etl::is_aligned(p)); CHECK_FALSE(etl::is_aligned(p)); } + + //************************************************************************* + TEST(test_type_with_alignment) + { + CHECK_EQUAL(1, alignof(etl::type_with_alignment_t<1>)); + CHECK_EQUAL(2, alignof(etl::type_with_alignment_t<2>)); + CHECK_EQUAL(4, alignof(etl::type_with_alignment_t<4>)); + CHECK_EQUAL(8, alignof(etl::type_with_alignment_t<8>)); + CHECK_EQUAL(16, alignof(etl::type_with_alignment_t<16>)); + CHECK_EQUAL(32, alignof(etl::type_with_alignment_t<32>)); + CHECK_EQUAL(64, alignof(etl::type_with_alignment_t<64>)); + } }; }