From 3ce45057f3da7a31b37ab044a0b1c84683bb9681 Mon Sep 17 00:00:00 2001 From: jwellbelove Date: Mon, 8 Dec 2014 20:09:31 +0000 Subject: [PATCH] Added alignment template helpers --- alignment.h | 107 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 alignment.h diff --git a/alignment.h b/alignment.h new file mode 100644 index 00000000..b4467c4f --- /dev/null +++ b/alignment.h @@ -0,0 +1,107 @@ +///\file + +/****************************************************************************** +The MIT License(MIT) + +Embedded Template Library. + +Copyright(c) 2014 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. +******************************************************************************/ + +#ifndef __ETL_ALIGNEMENT__ +#define __ETL_ALIGNEMENT__ + +#include "type_traits.h" + +///\defgroup align_at align_at +/// Creates a variable of the specified type at the specified alignment. +/// \ingroup utilities + +#if !defined(COMPILER_IAR) + +#if defined(COMPILER_MICROSOFT) +#define ETL_ALIGNMENT_PRE(n) __declspec(align(n)) +#define ETL_ALIGNMENT_POST(n) +#endif + +#if defined(COMPILER_GCC) +#define ETL_ALIGNMENT_PRE(n) +#define ETL_ALIGNMENT_POST(n) __attribute__((aligned(n))) +#endif + +#if defined (COMPILER_KEIL) +#define ETL_ALIGNMENT_PRE(n) +#define ETL_ALIGNMENT_POST(n) __attribute__((aligned(n))) +#endif + +namespace etl +{ + /// Template declaration. + template + struct align_at; + + /// Align 1 + template + struct align_at + { + ETL_ALIGNMENT_PRE(1) T value ETL_ALIGNMENT_POST(1); + }; + + /// Align 2 + template + struct align_at + { + ETL_ALIGNMENT_PRE(2) T value ETL_ALIGNMENT_POST(2); + }; + + /// Align 4 + template + struct align_at + { + ETL_ALIGNMENT_PRE(4) T value ETL_ALIGNMENT_POST(4); + }; + + /// Align 8 + template + struct align_at + { + ETL_ALIGNMENT_PRE(8) T value ETL_ALIGNMENT_POST(8); + }; + + /// Align 16 + template + struct align_at + { + ETL_ALIGNMENT_PRE(16) T value ETL_ALIGNMENT_POST(16); + }; + + /// Align As + template + struct align_as : public align_at::value> + { + }; +} +#endif + +#undef ETL_ALIGNMENT_PRE +#undef ETL_ALIGNMENT_POST + +#endif