Vc  1.0.0-dev
SIMD Vector Classes for C++
simdarrayfwd.h
1 /* This file is part of the Vc library. {{{
2 Copyright © 2014-2015 Matthias Kretz <kretz@kde.org>
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7  * Redistributions of source code must retain the above copyright
8  notice, this list of conditions and the following disclaimer.
9  * Redistributions in binary form must reproduce the above copyright
10  notice, this list of conditions and the following disclaimer in the
11  documentation and/or other materials provided with the distribution.
12  * Neither the names of contributing organizations nor the
13  names of its contributors may be used to endorse or promote products
14  derived from this software without specific prior written permission.
15 
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 
27 }}}*/
28 
29 #ifndef VC_COMMON_SIMDARRAYFWD_H_
30 #define VC_COMMON_SIMDARRAYFWD_H_
31 
32 #include "../scalar/types.h"
33 #include "../sse/types.h"
34 #include "../avx/types.h"
35 #include "../mic/types.h"
36 
37 #include "utility.h"
38 #include "macros.h"
39 
40 namespace Vc_VERSIONED_NAMESPACE
41 {
42 namespace Common
43 {
46 /*select_best_vector_type{{{*/
51 template<std::size_t N, typename... Typelist> struct select_best_vector_type_impl;
52 
53 template<std::size_t N, typename T> struct select_best_vector_type_impl<N, T>
54 {
55  using type = T;
56 };
57 template<std::size_t N, typename T, typename... Typelist> struct select_best_vector_type_impl<N, T, Typelist...>
58 {
59  using type = typename std::conditional<
60  (N < T::Size), typename select_best_vector_type_impl<N, Typelist...>::type,
61  T>::type;
62 };
63 template <typename T, std::size_t N>
64 using select_best_vector_type =
65  typename select_best_vector_type_impl<N,
66 #ifdef Vc_IMPL_AVX2
67  Vc::AVX2::Vector<T>,
68  Vc::SSE::Vector<T>,
69  Vc::Scalar::Vector<T>
70 #elif defined(Vc_IMPL_AVX)
71  Vc::AVX::Vector<T>,
72  Vc::SSE::Vector<T>,
73  Vc::Scalar::Vector<T>
74 #elif defined(Vc_IMPL_Scalar)
75  Vc::Scalar::Vector<T>
76 #elif defined(Vc_IMPL_SSE)
77  Vc::SSE::Vector<T>,
78  Vc::Scalar::Vector<T>
79 #elif defined(Vc_IMPL_MIC)
80  Vc::MIC::Vector<T>,
81  Vc::Scalar::Vector<T>
82 #endif
83  >::type;
84 //}}}
86 } // namespace Common
87 
88 // === having SimdArray<T, N> in the Vc namespace leads to a ABI bug ===
89 //
90 // SimdArray<double, 4> can be { double[4] }, { __m128d[2] }, or { __m256d } even though the type
91 // is the same.
92 // The question is, what should SimdArray focus on?
93 // a) A type that makes interfacing between different implementations possible?
94 // b) Or a type that makes fixed size SIMD easier and efficient?
95 //
96 // a) can be achieved by using a union with T[N] as one member. But this may have more serious
97 // performance implications than only less efficient parameter passing (because compilers have a
98 // much harder time wrt. aliasing issues). Also alignment would need to be set to the sizeof in
99 // order to be compatible with targets with larger alignment requirements.
100 // But, the in-memory representation of masks is not portable. Thus, at the latest with AVX-512,
101 // there would be a problem with requiring SimdMaskArray<T, N> to be an ABI compatible type.
102 // AVX-512 uses one bit per boolean, whereas SSE/AVX use sizeof(T) Bytes per boolean. Conversion
103 // between the two representations is not a trivial operation. Therefore choosing one or the other
104 // representation will have a considerable impact for the targets that do not use this
105 // representation. Since the future probably belongs to one bit per boolean representation, I would
106 // go with that choice.
107 //
108 // b) requires that SimdArray<T, N> != SimdArray<T, N> if
109 // SimdArray<T, N>::vector_type != SimdArray<T, N>::vector_type
110 //
111 // Therefore use SimdArray<T, N, V>, where V follows from the above.
112 template <typename T, std::size_t N,
113  typename VectorType = Common::select_best_vector_type<T, N>,
114  std::size_t VectorSize =
115  VectorType::size() // this last parameter is only used for
116  // specialization of N == VectorSize
117  >
118 class SimdArray;
119 
120 template <typename T, std::size_t N,
121  typename VectorType = Common::select_best_vector_type<T, N>,
122  std::size_t VectorSize =
123  VectorType::size() // this last parameter is only used for
124  // specialization of N == VectorSize
125  >
126 class SimdMaskArray;
127 
132 template <typename T, std::size_t N> struct SimdArrayTraits {
133  static constexpr std::size_t N0 = Common::left_size(N);
134  static constexpr std::size_t N1 = Common::right_size(N);
135 
136  using storage_type0 = SimdArray<T, N0>;
137  using storage_type1 = SimdArray<T, N1>;
138 };
139 
140 template <typename T, std::size_t N, typename VectorType, std::size_t VectorSize>
141 Vc_INTRINSIC_L typename SimdArrayTraits<T, N>::storage_type0 &internal_data0(
142  SimdArray<T, N, VectorType, VectorSize> &x) Vc_INTRINSIC_R;
143 template <typename T, std::size_t N, typename VectorType, std::size_t VectorSize>
144 Vc_INTRINSIC_L typename SimdArrayTraits<T, N>::storage_type1 &internal_data1(
145  SimdArray<T, N, VectorType, VectorSize> &x) Vc_INTRINSIC_R;
146 template <typename T, std::size_t N, typename VectorType, std::size_t VectorSize>
147 Vc_INTRINSIC_L const typename SimdArrayTraits<T, N>::storage_type0 &internal_data0(
148  const SimdArray<T, N, VectorType, VectorSize> &x) Vc_INTRINSIC_R;
149 template <typename T, std::size_t N, typename VectorType, std::size_t VectorSize>
150 Vc_INTRINSIC_L const typename SimdArrayTraits<T, N>::storage_type1 &internal_data1(
151  const SimdArray<T, N, VectorType, VectorSize> &x) Vc_INTRINSIC_R;
152 
153 template <typename T, std::size_t N, typename V>
154 Vc_INTRINSIC_L V &internal_data(SimdArray<T, N, V, N> &x) Vc_INTRINSIC_R;
155 template <typename T, std::size_t N, typename V>
156 Vc_INTRINSIC_L const V &internal_data(const SimdArray<T, N, V, N> &x) Vc_INTRINSIC_R;
157 
158 namespace Traits
159 {
160 template <typename T, std::size_t N, typename V> struct is_atomic_simdarray_internal<SimdArray<T, N, V, N>> : public std::true_type {};
161 template <typename T, std::size_t N, typename V> struct is_atomic_simd_mask_array_internal<SimdMaskArray<T, N, V, N>> : public std::true_type {};
162 
163 template <typename T, std::size_t N, typename VectorType, std::size_t M> struct is_simdarray_internal<SimdArray<T, N, VectorType, M>> : public std::true_type {};
164 template <typename T, std::size_t N, typename VectorType, std::size_t M> struct is_simd_mask_array_internal<SimdMaskArray<T, N, VectorType, M>> : public std::true_type {};
165 template <typename T, std::size_t N, typename V, std::size_t M> struct is_integral_internal <SimdArray<T, N, V, M>, false> : public std::is_integral<T> {};
166 template <typename T, std::size_t N, typename V, std::size_t M> struct is_floating_point_internal<SimdArray<T, N, V, M>, false> : public std::is_floating_point<T> {};
167 template <typename T, std::size_t N, typename V, std::size_t M> struct is_signed_internal <SimdArray<T, N, V, M>, false> : public std::is_signed<T> {};
168 template <typename T, std::size_t N, typename V, std::size_t M> struct is_unsigned_internal <SimdArray<T, N, V, M>, false> : public std::is_unsigned<T> {};
169 
170 template<typename T, std::size_t N> struct has_no_allocated_data_impl<Vc::SimdArray<T, N>> : public std::true_type {};
171 } // namespace Traits
172 
173 } // namespace Vc
174 
175 #endif // VC_COMMON_SIMDARRAYFWD_H_
176 
177 // vim: foldmethod=marker
Vector Classes Namespace.
Definition: cpuid.h:33