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).
5//
6// See http://www.boost.org/libs/type_traits for most recent version including documentation.
7
8#ifndef BOOST_TT_TYPE_WITH_ALIGNMENT_INCLUDED
9#define BOOST_TT_TYPE_WITH_ALIGNMENT_INCLUDED
10
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>
17
18#ifdef BOOST_MSVC
19# pragma warning(push)
20# pragma warning(disable: 4121) // alignment is sensitive to packing
21#endif
22
23#ifdef _MSC_VER
24#include <boost/type_traits/conditional.hpp>
25#endif
26
27namespace boost {
28#ifndef __BORLANDC__
29 namespace detail{
30
31 union max_align
32 {
33 char c;
34 short s;
35 int i;
36 long l;
37#ifndef BOOST_NO_LONG_LONG
38 boost::long_long_type ll;
39#endif
40#ifdef BOOST_HAS_INT128
41 boost::int128_type i128;
42#endif
43 float f;
44 double d;
45 long double ld;
46#ifdef BOOST_HAS_FLOAT128
47 __float128 f128;
48#endif
49 };
50
51template <std::size_t Target, bool check> struct long_double_alignment{ typedef long double type; };
52template <std::size_t Target> struct long_double_alignment<Target, false>{ typedef boost::detail::max_align type; };
53
54template <std::size_t Target, bool check> struct double_alignment{ typedef double type; };
55template <std::size_t Target> struct double_alignment<Target, false>{ typedef typename long_double_alignment<Target, boost::alignment_of<long double>::value >= Target>::type type; };
56
57#ifndef BOOST_NO_LONG_LONG
58template <std::size_t Target, bool check> struct long_long_alignment{ typedef boost::long_long_type type; };
59template <std::size_t Target> struct long_long_alignment<Target, false>{ typedef typename double_alignment<Target, boost::alignment_of<double>::value >= Target>::type type; };
60#endif
61
62template <std::size_t Target, bool check> struct long_alignment{ typedef long type; };
63#ifndef BOOST_NO_LONG_LONG
64template <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; };
65#else
66template <std::size_t Target> struct long_alignment<Target, false>{ typedef typename double_alignment<Target, boost::alignment_of<double>::value >= Target>::type type; };
67#endif
68
69template <std::size_t Target, bool check> struct int_alignment{ typedef int type; };
70template <std::size_t Target> struct int_alignment<Target, false>{ typedef typename long_alignment<Target, boost::alignment_of<long>::value >= Target>::type type; };
71
72template <std::size_t Target, bool check> struct short_alignment{ typedef short type; };
73template <std::size_t Target> struct short_alignment<Target, false>{ typedef typename int_alignment<Target, boost::alignment_of<int>::value >= Target>::type type; };
74
75template <std::size_t Target, bool check> struct char_alignment{ typedef char type; };
76template <std::size_t Target> struct char_alignment<Target, false>{ typedef typename short_alignment<Target, boost::alignment_of<short>::value >= Target>::type type; };
77
78} // namespace detail
79
80template <std::size_t Align>
81struct type_with_alignment
82{
83 typedef typename boost::detail::char_alignment<Align, boost::alignment_of<char>::value >= Align>::type type;
84};
85
86#if (defined(__GNUC__) || (defined (__SUNPRO_CC) && (__SUNPRO_CC >= 0x5130)) || defined(__clang__)) && !defined(BOOST_TT_DISABLE_INTRINSICS)
87namespace tt_align_ns {
88struct __attribute__((__aligned__(2))) a2 {};
89struct __attribute__((__aligned__(4))) a4 {};
90struct __attribute__((__aligned__(8))) a8 {};
91struct __attribute__((__aligned__(16))) a16 {};
92struct __attribute__((__aligned__(32))) a32 {};
93struct __attribute__((__aligned__(64))) a64 {};
94struct __attribute__((__aligned__(128))) a128 {};
95}
96
97template<> struct type_with_alignment<1> { public: typedef char type; };
98template<> struct type_with_alignment<2> { public: typedef tt_align_ns::a2 type; };
99template<> struct type_with_alignment<4> { public: typedef tt_align_ns::a4 type; };
100template<> struct type_with_alignment<8> { public: typedef tt_align_ns::a8 type; };
101template<> struct type_with_alignment<16> { public: typedef tt_align_ns::a16 type; };
102template<> struct type_with_alignment<32> { public: typedef tt_align_ns::a32 type; };
103template<> struct type_with_alignment<64> { public: typedef tt_align_ns::a64 type; };
104template<> struct type_with_alignment<128> { public: typedef tt_align_ns::a128 type; };
105
106template<> struct is_pod< ::boost::tt_align_ns::a2> : public true_type{};
107template<> struct is_pod< ::boost::tt_align_ns::a4> : public true_type{};
108template<> struct is_pod< ::boost::tt_align_ns::a8> : public true_type{};
109template<> struct is_pod< ::boost::tt_align_ns::a16> : public true_type{};
110template<> struct is_pod< ::boost::tt_align_ns::a32> : public true_type{};
111template<> struct is_pod< ::boost::tt_align_ns::a64> : public true_type{};
112template<> struct is_pod< ::boost::tt_align_ns::a128> : public true_type{};
113
114#endif
115#if (defined(BOOST_MSVC) || (defined(BOOST_INTEL) && defined(_MSC_VER))) && !defined(BOOST_TT_DISABLE_INTRINSICS)
116//
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 http://article.gmane.org/gmane.comp.lib.boost.devel/173011
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.
131//
132namespace tt_align_ns {
133struct __declspec(align(8)) a8 {
134 char m[8];
135 typedef a8 type;
136};
137struct __declspec(align(16)) a16 {
138 char m[16];
139 typedef a16 type;
140};
141struct __declspec(align(32)) a32 {
142 char m[32];
143 typedef a32 type;
144};
145struct __declspec(align(64)) a64
146{
147 char m[64];
148 typedef a64 type;
149};
150struct __declspec(align(128)) a128 {
151 char m[128];
152 typedef a128 type;
153};
154}
155
156template<> struct type_with_alignment<8>
157{
158 typedef boost::conditional<
159 ::boost::alignment_of<boost::detail::max_align>::value < 8,
160 tt_align_ns::a8,
161 boost::detail::char_alignment<8, false> >::type t1;
162public:
163 typedef t1::type type;
164};
165template<> struct type_with_alignment<16>
166{
167 typedef boost::conditional<
168 ::boost::alignment_of<boost::detail::max_align>::value < 16,
169 tt_align_ns::a16,
170 boost::detail::char_alignment<16, false> >::type t1;
171public:
172 typedef t1::type type;
173};
174template<> struct type_with_alignment<32>
175{
176 typedef boost::conditional<
177 ::boost::alignment_of<boost::detail::max_align>::value < 32,
178 tt_align_ns::a32,
179 boost::detail::char_alignment<32, false> >::type t1;
180public:
181 typedef t1::type type;
182};
183template<> struct type_with_alignment<64> {
184 typedef boost::conditional<
185 ::boost::alignment_of<boost::detail::max_align>::value < 64,
186 tt_align_ns::a64,
187 boost::detail::char_alignment<64, false> >::type t1;
188public:
189 typedef t1::type type;
190};
191template<> struct type_with_alignment<128> {
192 typedef boost::conditional<
193 ::boost::alignment_of<boost::detail::max_align>::value < 128,
194 tt_align_ns::a128,
195 boost::detail::char_alignment<128, false> >::type t1;
196public:
197 typedef t1::type type;
198};
199
200template<> struct is_pod< ::boost::tt_align_ns::a8> : public true_type{};
201template<> struct is_pod< ::boost::tt_align_ns::a16> : public true_type{};
202template<> struct is_pod< ::boost::tt_align_ns::a32> : public true_type{};
203template<> struct is_pod< ::boost::tt_align_ns::a64> : public true_type{};
204template<> struct is_pod< ::boost::tt_align_ns::a128> : public true_type{};
205
206#endif
207
208#else
209
210//
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.
215
216namespace tt_align_ns{
217#pragma option push -a16
218struct a2{ short s; };
219struct a4{ int s; };
220struct a8{ double s; };
221struct a16{ long double s; };
222#pragma option pop
223}
224
225namespace detail {
226
227typedef ::boost::tt_align_ns::a16 max_align;
228
229}
230//#if ! BOOST_WORKAROUND(__CODEGEARC__, BOOST_TESTED_AT(0x610))
231template <> struct is_pod< ::boost::tt_align_ns::a2> : public true_type{};
232template <> struct is_pod< ::boost::tt_align_ns::a4> : public true_type{};
233template <> struct is_pod< ::boost::tt_align_ns::a8> : public true_type{};
234template <> struct is_pod< ::boost::tt_align_ns::a16> : public true_type{};
235//#endif
236
237template <std::size_t N> struct type_with_alignment
238{
239 // We should never get to here, but if we do use the maximally
240 // aligned type:
241 // BOOST_STATIC_ASSERT(0);
242 typedef tt_align_ns::a16 type;
243};
244template <> struct type_with_alignment<1>{ typedef char type; };
245template <> struct type_with_alignment<2>{ typedef tt_align_ns::a2 type; };
246template <> struct type_with_alignment<4>{ typedef tt_align_ns::a4 type; };
247template <> struct type_with_alignment<8>{ typedef tt_align_ns::a8 type; };
248template <> struct type_with_alignment<16>{ typedef tt_align_ns::a16 type; };
249
250#endif
251
252} // namespace boost
253
254#ifdef BOOST_MSVC
255# pragma warning(pop)
256#endif
257
258#endif // BOOST_TT_TYPE_WITH_ALIGNMENT_INCLUDED
259
260
261