| 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 | |
| 27 | namespace boost { |
| 28 | #ifndef BOOST_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 | |
| 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; }; |
| 53 | |
| 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; }; |
| 56 | |
| 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; }; |
| 60 | #endif |
| 61 | |
| 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; }; |
| 65 | #else |
| 66 | template <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 | |
| 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; }; |
| 71 | |
| 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; }; |
| 74 | |
| 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; }; |
| 77 | |
| 78 | } // namespace detail |
| 79 | |
| 80 | template <std::size_t Align> |
| 81 | struct 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) |
| 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 {}; |
| 95 | } |
| 96 | |
| 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; }; |
| 105 | |
| 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{}; |
| 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 | // |
| 132 | namespace tt_align_ns { |
| 133 | struct __declspec(align(8)) a8 { |
| 134 | char m[8]; |
| 135 | typedef a8 type; |
| 136 | }; |
| 137 | struct __declspec(align(16)) a16 { |
| 138 | char m[16]; |
| 139 | typedef a16 type; |
| 140 | }; |
| 141 | struct __declspec(align(32)) a32 { |
| 142 | char m[32]; |
| 143 | typedef a32 type; |
| 144 | }; |
| 145 | struct __declspec(align(64)) a64 |
| 146 | { |
| 147 | char m[64]; |
| 148 | typedef a64 type; |
| 149 | }; |
| 150 | struct __declspec(align(128)) a128 { |
| 151 | char m[128]; |
| 152 | typedef a128 type; |
| 153 | }; |
| 154 | } |
| 155 | |
| 156 | template<> 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; |
| 162 | public: |
| 163 | typedef t1::type type; |
| 164 | }; |
| 165 | template<> 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; |
| 171 | public: |
| 172 | typedef t1::type type; |
| 173 | }; |
| 174 | template<> 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; |
| 180 | public: |
| 181 | typedef t1::type type; |
| 182 | }; |
| 183 | template<> 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; |
| 188 | public: |
| 189 | typedef t1::type type; |
| 190 | }; |
| 191 | template<> 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; |
| 196 | public: |
| 197 | typedef t1::type type; |
| 198 | }; |
| 199 | |
| 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{}; |
| 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 | |
| 216 | namespace tt_align_ns{ |
| 217 | #pragma option push -a16 |
| 218 | struct a2{ short s; }; |
| 219 | struct a4{ int s; }; |
| 220 | struct a8{ double s; }; |
| 221 | struct a16{ long double s; }; |
| 222 | #pragma option pop |
| 223 | } |
| 224 | |
| 225 | namespace detail { |
| 226 | |
| 227 | typedef ::boost::tt_align_ns::a16 max_align; |
| 228 | |
| 229 | } |
| 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{}; |
| 235 | //#endif |
| 236 | |
| 237 | template <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 | }; |
| 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; }; |
| 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 | |