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>
38 #ifndef BOOST_NO_LONG_LONG
39 boost::long_long_type ll;
41 #ifdef BOOST_HAS_INT128
42 boost::int128_type i128;
47 #ifdef BOOST_HAS_FLOAT128
52 template <std::size_t Target, bool check> struct long_double_alignment{ typedef long double type; };
53 template <std::size_t Target> struct long_double_alignment<Target, false>{ typedef boost::detail::max_align type; };
55 template <std::size_t Target, bool check> struct double_alignment{ typedef double type; };
56 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; };
58 #ifndef BOOST_NO_LONG_LONG
59 template <std::size_t Target, bool check> struct long_long_alignment{ typedef boost::long_long_type type; };
60 template <std::size_t Target> struct long_long_alignment<Target, false>{ typedef typename double_alignment<Target, boost::alignment_of<double>::value >= Target>::type type; };
63 template <std::size_t Target, bool check> struct long_alignment{ typedef long type; };
64 #ifndef BOOST_NO_LONG_LONG
65 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; };
67 template <std::size_t Target> struct long_alignment<Target, false>{ typedef typename double_alignment<Target, boost::alignment_of<double>::value >= Target>::type type; };
70 template <std::size_t Target, bool check> struct int_alignment{ typedef int type; };
71 template <std::size_t Target> struct int_alignment<Target, false>{ typedef typename long_alignment<Target, boost::alignment_of<long>::value >= Target>::type type; };
73 template <std::size_t Target, bool check> struct short_alignment{ typedef short type; };
74 template <std::size_t Target> struct short_alignment<Target, false>{ typedef typename int_alignment<Target, boost::alignment_of<int>::value >= Target>::type type; };
76 template <std::size_t Target, bool check> struct char_alignment{ typedef char type; };
77 template <std::size_t Target> struct char_alignment<Target, false>{ typedef typename short_alignment<Target, boost::alignment_of<short>::value >= Target>::type type; };
81 template <std::size_t Align>
82 struct type_with_alignment
84 typedef typename boost::detail::char_alignment<Align, boost::alignment_of<char>::value >= Align>::type type;
87 #if (defined(__GNUC__) || (defined (__SUNPRO_CC) && (__SUNPRO_CC >= 0x5130)) || defined(__clang__)) && !defined(BOOST_TT_DISABLE_INTRINSICS)
88 namespace tt_align_ns {
89 struct __attribute__((__aligned__(2))) a2 {};
90 struct __attribute__((__aligned__(4))) a4 {};
91 struct __attribute__((__aligned__(8))) a8 {};
92 struct __attribute__((__aligned__(16))) a16 {};
93 struct __attribute__((__aligned__(32))) a32 {};
94 struct __attribute__((__aligned__(64))) a64 {};
95 struct __attribute__((__aligned__(128))) a128 {};
98 template<> struct type_with_alignment<1> { public: typedef char type; };
99 template<> struct type_with_alignment<2> { public: typedef tt_align_ns::a2 type; };
100 template<> struct type_with_alignment<4> { public: typedef tt_align_ns::a4 type; };
101 template<> struct type_with_alignment<8> { public: typedef tt_align_ns::a8 type; };
102 template<> struct type_with_alignment<16> { public: typedef tt_align_ns::a16 type; };
103 template<> struct type_with_alignment<32> { public: typedef tt_align_ns::a32 type; };
104 template<> struct type_with_alignment<64> { public: typedef tt_align_ns::a64 type; };
105 template<> struct type_with_alignment<128> { public: typedef tt_align_ns::a128 type; };
107 template<> struct is_pod< ::boost::tt_align_ns::a2> : public true_type{};
108 template<> struct is_pod< ::boost::tt_align_ns::a4> : public true_type{};
109 template<> struct is_pod< ::boost::tt_align_ns::a8> : public true_type{};
110 template<> struct is_pod< ::boost::tt_align_ns::a16> : public true_type{};
111 template<> struct is_pod< ::boost::tt_align_ns::a32> : public true_type{};
112 template<> struct is_pod< ::boost::tt_align_ns::a64> : public true_type{};
113 template<> struct is_pod< ::boost::tt_align_ns::a128> : public true_type{};
116 #if (defined(BOOST_MSVC) || (defined(BOOST_INTEL) && defined(_MSC_VER))) && !defined(BOOST_TT_DISABLE_INTRINSICS)
118 // MSVC supports types which have alignments greater than the normal
119 // maximum: these are used for example in the types __m64 and __m128
120 // to provide types with alignment requirements which match the SSE
121 // registers. Therefore we extend type_with_alignment<> to support
122 // such types, however, we have to be careful to use a builtin type
123 // whenever possible otherwise we break previously working code:
124 // see http://article.gmane.org/gmane.comp.lib.boost.devel/173011
125 // for an example and test case. Thus types like a8 below will
126 // be used *only* if the existing implementation can't provide a type
127 // with suitable alignment. This does mean however, that type_with_alignment<>
128 // may return a type which cannot be passed through a function call
129 // by value (and neither can any type containing such a type like
130 // Boost.Optional). However, this only happens when we have no choice
131 // in the matter because no other "ordinary" type is available.
133 namespace tt_align_ns {
134 struct __declspec(align(8)) a8 {
138 struct __declspec(align(16)) a16 {
142 struct __declspec(align(32)) a32 {
146 struct __declspec(align(64)) a64
151 struct __declspec(align(128)) a128 {
157 template<> struct type_with_alignment<8>
159 typedef boost::conditional<
160 ::boost::alignment_of<boost::detail::max_align>::value < 8,
162 boost::detail::char_alignment<8, false> >::type t1;
164 typedef t1::type type;
166 template<> struct type_with_alignment<16>
168 typedef boost::conditional<
169 ::boost::alignment_of<boost::detail::max_align>::value < 16,
171 boost::detail::char_alignment<16, false> >::type t1;
173 typedef t1::type type;
175 template<> struct type_with_alignment<32>
177 typedef boost::conditional<
178 ::boost::alignment_of<boost::detail::max_align>::value < 32,
180 boost::detail::char_alignment<32, false> >::type t1;
182 typedef t1::type type;
184 template<> struct type_with_alignment<64> {
185 typedef boost::conditional<
186 ::boost::alignment_of<boost::detail::max_align>::value < 64,
188 boost::detail::char_alignment<64, false> >::type t1;
190 typedef t1::type type;
192 template<> struct type_with_alignment<128> {
193 typedef boost::conditional<
194 ::boost::alignment_of<boost::detail::max_align>::value < 128,
196 boost::detail::char_alignment<128, false> >::type t1;
198 typedef t1::type type;
201 template<> struct is_pod< ::boost::tt_align_ns::a8> : public true_type{};
202 template<> struct is_pod< ::boost::tt_align_ns::a16> : public true_type{};
203 template<> struct is_pod< ::boost::tt_align_ns::a32> : public true_type{};
204 template<> struct is_pod< ::boost::tt_align_ns::a64> : public true_type{};
205 template<> struct is_pod< ::boost::tt_align_ns::a128> : public true_type{};
212 // Borland specific version, we have this for two reasons:
213 // 1) The version above doesn't always compile (with the new test cases for example)
214 // 2) Because of Borlands #pragma option we can create types with alignments that are
215 // greater that the largest aligned builtin type.
217 namespace tt_align_ns{
218 #pragma option push -a16
219 struct a2{ short s; };
221 struct a8{ double s; };
222 struct a16{ long double s; };
228 typedef ::boost::tt_align_ns::a16 max_align;
231 //#if ! BOOST_WORKAROUND(__CODEGEARC__, BOOST_TESTED_AT(0x610))
232 template <> struct is_pod< ::boost::tt_align_ns::a2> : public true_type{};
233 template <> struct is_pod< ::boost::tt_align_ns::a4> : public true_type{};
234 template <> struct is_pod< ::boost::tt_align_ns::a8> : public true_type{};
235 template <> struct is_pod< ::boost::tt_align_ns::a16> : public true_type{};
238 template <std::size_t N> struct type_with_alignment
240 // We should never get to here, but if we do use the maximally
242 // BOOST_STATIC_ASSERT(0);
243 typedef tt_align_ns::a16 type;
245 template <> struct type_with_alignment<1>{ typedef char type; };
246 template <> struct type_with_alignment<2>{ typedef tt_align_ns::a2 type; };
247 template <> struct type_with_alignment<4>{ typedef tt_align_ns::a4 type; };
248 template <> struct type_with_alignment<8>{ typedef tt_align_ns::a8 type; };
249 template <> struct type_with_alignment<16>{ typedef tt_align_ns::a16 type; };
256 # pragma warning(pop)
259 #endif // BOOST_TT_TYPE_WITH_ALIGNMENT_INCLUDED