1 // (C) Copyright John Maddock 2000.
2 // Use, modification and distribution are subject to the Boost Software License,
3 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt).
6 // See http://www.boost.org/libs/type_traits for most recent version including documentation.
8 #ifndef BOOST_TT_TYPE_WITH_ALIGNMENT_INCLUDED
9 #define BOOST_TT_TYPE_WITH_ALIGNMENT_INCLUDED
11 #include <boost/type_traits/alignment_of.hpp>
12 #include <boost/type_traits/is_pod.hpp>
13 #include <boost/static_assert.hpp>
14 #include <boost/config.hpp>
15 #include <cstddef> // size_t
16 #include <boost/detail/workaround.hpp>
19 # pragma warning(push)
20 # pragma warning(disable: 4121) // alignment is sensitive to packing
24 #include <boost/type_traits/conditional.hpp>
28 #ifndef BOOST_BORLANDC
37 #ifndef BOOST_NO_LONG_LONG
38 boost::long_long_type ll;
40 #ifdef BOOST_HAS_INT128
41 boost::int128_type i128;
46 #ifdef BOOST_HAS_FLOAT128
51 template <std::size_t Target, bool check> struct long_double_alignment{ typedef long double type; };
52 template <std::size_t Target> struct long_double_alignment<Target, false>{ typedef boost::detail::max_align type; };
54 template <std::size_t Target, bool check> struct double_alignment{ typedef double type; };
55 template <std::size_t Target> struct double_alignment<Target, false>{ typedef typename long_double_alignment<Target, boost::alignment_of<long double>::value >= Target>::type type; };
57 #ifndef BOOST_NO_LONG_LONG
58 template <std::size_t Target, bool check> struct long_long_alignment{ typedef boost::long_long_type type; };
59 template <std::size_t Target> struct long_long_alignment<Target, false>{ typedef typename double_alignment<Target, boost::alignment_of<double>::value >= Target>::type type; };
62 template <std::size_t Target, bool check> struct long_alignment{ typedef long type; };
63 #ifndef BOOST_NO_LONG_LONG
64 template <std::size_t Target> struct long_alignment<Target, false>{ typedef typename long_long_alignment<Target, boost::alignment_of<boost::long_long_type>::value >= Target>::type type; };
66 template <std::size_t Target> struct long_alignment<Target, false>{ typedef typename double_alignment<Target, boost::alignment_of<double>::value >= Target>::type type; };
69 template <std::size_t Target, bool check> struct int_alignment{ typedef int type; };
70 template <std::size_t Target> struct int_alignment<Target, false>{ typedef typename long_alignment<Target, boost::alignment_of<long>::value >= Target>::type type; };
72 template <std::size_t Target, bool check> struct short_alignment{ typedef short type; };
73 template <std::size_t Target> struct short_alignment<Target, false>{ typedef typename int_alignment<Target, boost::alignment_of<int>::value >= Target>::type type; };
75 template <std::size_t Target, bool check> struct char_alignment{ typedef char type; };
76 template <std::size_t Target> struct char_alignment<Target, false>{ typedef typename short_alignment<Target, boost::alignment_of<short>::value >= Target>::type type; };
80 template <std::size_t Align>
81 struct type_with_alignment
83 typedef typename boost::detail::char_alignment<Align, boost::alignment_of<char>::value >= Align>::type type;
86 #if (defined(__GNUC__) || (defined (__SUNPRO_CC) && (__SUNPRO_CC >= 0x5130)) || defined(__clang__)) && !defined(BOOST_TT_DISABLE_INTRINSICS)
87 namespace tt_align_ns {
88 struct __attribute__((__aligned__(2))) a2 {};
89 struct __attribute__((__aligned__(4))) a4 {};
90 struct __attribute__((__aligned__(8))) a8 {};
91 struct __attribute__((__aligned__(16))) a16 {};
92 struct __attribute__((__aligned__(32))) a32 {};
93 struct __attribute__((__aligned__(64))) a64 {};
94 struct __attribute__((__aligned__(128))) a128 {};
97 template<> struct type_with_alignment<1> { public: typedef char type; };
98 template<> struct type_with_alignment<2> { public: typedef tt_align_ns::a2 type; };
99 template<> struct type_with_alignment<4> { public: typedef tt_align_ns::a4 type; };
100 template<> struct type_with_alignment<8> { public: typedef tt_align_ns::a8 type; };
101 template<> struct type_with_alignment<16> { public: typedef tt_align_ns::a16 type; };
102 template<> struct type_with_alignment<32> { public: typedef tt_align_ns::a32 type; };
103 template<> struct type_with_alignment<64> { public: typedef tt_align_ns::a64 type; };
104 template<> struct type_with_alignment<128> { public: typedef tt_align_ns::a128 type; };
106 template<> struct is_pod< ::boost::tt_align_ns::a2> : public true_type{};
107 template<> struct is_pod< ::boost::tt_align_ns::a4> : public true_type{};
108 template<> struct is_pod< ::boost::tt_align_ns::a8> : public true_type{};
109 template<> struct is_pod< ::boost::tt_align_ns::a16> : public true_type{};
110 template<> struct is_pod< ::boost::tt_align_ns::a32> : public true_type{};
111 template<> struct is_pod< ::boost::tt_align_ns::a64> : public true_type{};
112 template<> struct is_pod< ::boost::tt_align_ns::a128> : public true_type{};
115 #if (defined(BOOST_MSVC) || (defined(BOOST_INTEL) && defined(_MSC_VER))) && !defined(BOOST_TT_DISABLE_INTRINSICS)
117 // MSVC supports types which have alignments greater than the normal
118 // maximum: these are used for example in the types __m64 and __m128
119 // to provide types with alignment requirements which match the SSE
120 // registers. Therefore we extend type_with_alignment<> to support
121 // such types, however, we have to be careful to use a builtin type
122 // whenever possible otherwise we break previously working code:
123 // see https://lists.boost.org/Archives/boost/2014/03/212391.php
124 // for an example and test case. Thus types like a8 below will
125 // be used *only* if the existing implementation can't provide a type
126 // with suitable alignment. This does mean however, that type_with_alignment<>
127 // may return a type which cannot be passed through a function call
128 // by value (and neither can any type containing such a type like
129 // Boost.Optional). However, this only happens when we have no choice
130 // in the matter because no other "ordinary" type is available.
132 namespace tt_align_ns {
133 struct __declspec(align(8)) a8 {
137 struct __declspec(align(16)) a16 {
141 struct __declspec(align(32)) a32 {
145 struct __declspec(align(64)) a64
150 struct __declspec(align(128)) a128 {
156 template<> struct type_with_alignment<8>
158 typedef boost::conditional<
159 ::boost::alignment_of<boost::detail::max_align>::value < 8,
161 boost::detail::char_alignment<8, false> >::type t1;
163 typedef t1::type type;
165 template<> struct type_with_alignment<16>
167 typedef boost::conditional<
168 ::boost::alignment_of<boost::detail::max_align>::value < 16,
170 boost::detail::char_alignment<16, false> >::type t1;
172 typedef t1::type type;
174 template<> struct type_with_alignment<32>
176 typedef boost::conditional<
177 ::boost::alignment_of<boost::detail::max_align>::value < 32,
179 boost::detail::char_alignment<32, false> >::type t1;
181 typedef t1::type type;
183 template<> struct type_with_alignment<64> {
184 typedef boost::conditional<
185 ::boost::alignment_of<boost::detail::max_align>::value < 64,
187 boost::detail::char_alignment<64, false> >::type t1;
189 typedef t1::type type;
191 template<> struct type_with_alignment<128> {
192 typedef boost::conditional<
193 ::boost::alignment_of<boost::detail::max_align>::value < 128,
195 boost::detail::char_alignment<128, false> >::type t1;
197 typedef t1::type type;
200 template<> struct is_pod< ::boost::tt_align_ns::a8> : public true_type{};
201 template<> struct is_pod< ::boost::tt_align_ns::a16> : public true_type{};
202 template<> struct is_pod< ::boost::tt_align_ns::a32> : public true_type{};
203 template<> struct is_pod< ::boost::tt_align_ns::a64> : public true_type{};
204 template<> struct is_pod< ::boost::tt_align_ns::a128> : public true_type{};
211 // Borland specific version, we have this for two reasons:
212 // 1) The version above doesn't always compile (with the new test cases for example)
213 // 2) Because of Borlands #pragma option we can create types with alignments that are
214 // greater that the largest aligned builtin type.
216 namespace tt_align_ns{
217 #pragma option push -a16
218 struct a2{ short s; };
220 struct a8{ double s; };
221 struct a16{ long double s; };
227 typedef ::boost::tt_align_ns::a16 max_align;
230 //#if ! BOOST_WORKAROUND(BOOST_CODEGEARC, BOOST_TESTED_AT(0x610))
231 template <> struct is_pod< ::boost::tt_align_ns::a2> : public true_type{};
232 template <> struct is_pod< ::boost::tt_align_ns::a4> : public true_type{};
233 template <> struct is_pod< ::boost::tt_align_ns::a8> : public true_type{};
234 template <> struct is_pod< ::boost::tt_align_ns::a16> : public true_type{};
237 template <std::size_t N> struct type_with_alignment
239 // We should never get to here, but if we do use the maximally
241 // BOOST_STATIC_ASSERT(0);
242 typedef tt_align_ns::a16 type;
244 template <> struct type_with_alignment<1>{ typedef char type; };
245 template <> struct type_with_alignment<2>{ typedef tt_align_ns::a2 type; };
246 template <> struct type_with_alignment<4>{ typedef tt_align_ns::a4 type; };
247 template <> struct type_with_alignment<8>{ typedef tt_align_ns::a8 type; };
248 template <> struct type_with_alignment<16>{ typedef tt_align_ns::a16 type; };
255 # pragma warning(pop)
258 #endif // BOOST_TT_TYPE_WITH_ALIGNMENT_INCLUDED