Vc  1.0.0-dev
SIMD Vector Classes for C++
types.h
1 /* This file is part of the Vc library. {{{
2 Copyright © 2012-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_TYPES_H_
30 #define VC_COMMON_TYPES_H_
31 
32 #ifdef Vc_CHECK_ALIGNMENT
33 #include <cstdlib>
34 #include <cstdio>
35 #endif
36 
37 #include <Vc/global.h>
38 #include "../traits/type_traits.h"
39 #include "permutation.h"
40 #include "vectorabi.h"
41 
42 namespace Vc_VERSIONED_NAMESPACE
43 {
44 template<typename T, typename Abi> class Mask;
45 template<typename T, typename Abi> class Vector;
46 
49 
51 using std::size_t;
52 
54 using llong = long long;
56 using ullong = unsigned long long;
58 using ulong = unsigned long;
60 using uint = unsigned int;
62 using ushort = unsigned short;
64 using uchar = unsigned char;
66 using schar = signed char;
67 
71 struct VectorSpecialInitializerZero {};
75 struct VectorSpecialInitializerOne {};
79 struct VectorSpecialInitializerIndexesFromZero {};
80 
85 constexpr VectorSpecialInitializerZero Zero = {};
90 constexpr VectorSpecialInitializerOne One = {};
95 constexpr VectorSpecialInitializerIndexesFromZero IndexesFromZero = {};
97 
98 namespace Detail
99 {
100 template<typename T> struct MayAliasImpl { typedef T type Vc_MAY_ALIAS; };
101 //template<size_t Bytes> struct MayAlias<MaskBool<Bytes>> { typedef MaskBool<Bytes> type; };
102 } // namespace Detail
109 #ifdef Vc_ICC
110 template <typename T> using MayAlias [[gnu::may_alias]] = T;
111 #else
112 template <typename T> using MayAlias = typename Detail::MayAliasImpl<T>::type;
113 #endif
114 
121 enum class Operator : char {
122  Assign,
123  Multiply,
124  MultiplyAssign,
125  Divide,
126  DivideAssign,
127  Remainder,
128  RemainderAssign,
129  Plus,
130  PlusAssign,
131  Minus,
132  MinusAssign,
133  RightShift,
134  RightShiftAssign,
135  LeftShift,
136  LeftShiftAssign,
137  And,
138  AndAssign,
139  Xor,
140  XorAssign,
141  Or,
142  OrAssign,
143  PreIncrement,
144  PostIncrement,
145  PreDecrement,
146  PostDecrement,
147  LogicalAnd,
148  LogicalOr,
149  Comma,
150  UnaryPlus,
151  UnaryMinus,
152  UnaryNot,
153  UnaryOnesComplement,
154  CompareEqual,
155  CompareNotEqual,
156  CompareLess,
157  CompareGreater,
158  CompareLessEqual,
159  CompareGreaterEqual
160 };
161 
162 // forward declaration for Vc::array in <Vc/array>
163 template <typename T, std::size_t N> struct array;
164 
165 /* TODO: add type for half-float, something along these lines:
166 class half_float
167 {
168  uint16_t data;
169 public:
170  constexpr half_float() : data(0) {}
171  constexpr half_float(const half_float &) = default;
172  constexpr half_float(half_float &&) = default;
173  constexpr half_float &operator=(const half_float &) = default;
174 
175  constexpr explicit half_float(float);
176  constexpr explicit half_float(double);
177  constexpr explicit half_float(int);
178  constexpr explicit half_float(unsigned int);
179 
180  explicit operator float () const;
181  explicit operator double () const;
182  explicit operator int () const;
183  explicit operator unsigned int() const;
184 
185  bool operator==(half_float rhs) const;
186  bool operator!=(half_float rhs) const;
187  bool operator>=(half_float rhs) const;
188  bool operator<=(half_float rhs) const;
189  bool operator> (half_float rhs) const;
190  bool operator< (half_float rhs) const;
191 
192  half_float operator+(half_float rhs) const;
193  half_float operator-(half_float rhs) const;
194  half_float operator*(half_float rhs) const;
195  half_float operator/(half_float rhs) const;
196 };
197 */
198 
199 // TODO: the following doesn't really belong into the toplevel Vc namespace.
200 #ifndef Vc_CHECK_ALIGNMENT
201 template<typename _T> static Vc_ALWAYS_INLINE void assertCorrectAlignment(const _T *){}
202 #else
203 template<typename _T> static Vc_ALWAYS_INLINE void assertCorrectAlignment(const _T *ptr)
204 {
205  const size_t s = alignof(_T);
206  if((reinterpret_cast<size_t>(ptr) & ((s ^ (s & (s - 1))) - 1)) != 0) {
207  fprintf(stderr, "A vector with incorrect alignment has just been created. Look at the stacktrace to find the guilty object.\n");
208  abort();
209  }
210 }
211 #endif
212 
213 namespace Common
214 {
221 template<size_t StructSize> class SuccessiveEntries
222 {
223  std::size_t m_first;
224 public:
225  typedef SuccessiveEntries AsArg;
226  constexpr SuccessiveEntries(size_t first) : m_first(first) {}
227  constexpr Vc_PURE size_t operator[](size_t offset) const { return m_first + offset * StructSize; }
228  constexpr Vc_PURE size_t data() const { return m_first; }
229  constexpr Vc_PURE SuccessiveEntries operator+(const SuccessiveEntries &rhs) const { return SuccessiveEntries(m_first + rhs.m_first); }
230  constexpr Vc_PURE SuccessiveEntries operator*(const SuccessiveEntries &rhs) const { return SuccessiveEntries(m_first * rhs.m_first); }
231  constexpr Vc_PURE SuccessiveEntries operator<<(std::size_t x) const { return {m_first << x}; }
232 
233  friend SuccessiveEntries &internal_data(SuccessiveEntries &x) { return x; }
234  friend const SuccessiveEntries &internal_data(const SuccessiveEntries &x)
235  {
236  return x;
237  }
238 };
239 
240 // declaration for functions in common/malloc.h
241 template <std::size_t alignment>
242 Vc_INTRINSIC_L void *aligned_malloc(std::size_t n) Vc_INTRINSIC_R;
243 Vc_ALWAYS_INLINE_L void free(void *p) Vc_ALWAYS_INLINE_R;
244 
248 template <typename T, typename U>
249 using enable_if_mask_converts_implicitly =
250  enable_if<(Traits::is_simd_mask<U>::value && !Traits::isSimdMaskArray<U>::value &&
251  Traits::is_implicit_cast_allowed_mask<
252  Traits::entry_type_of<typename Traits::decay<U>::Vector>, T>::value)>;
256 template <typename T, typename U>
257 using enable_if_mask_converts_explicitly = enable_if<(
258  Traits::isSimdMaskArray<U>::value ||
259  (Traits::is_simd_mask<U>::value &&
260  !Traits::is_implicit_cast_allowed_mask<
261  Traits::entry_type_of<typename Traits::decay<U>::Vector>, T>::value))>;
262 
266 template <typename T> using WidthT = std::integral_constant<std::size_t, sizeof(T)>;
267 
268 // forward declaration of MaskBool in common/maskentry.h
269 template <std::size_t Bytes> class MaskBool;
270 
271 // forward declaration of SubscriptOperation in common/subscript.h
272 template <typename T, typename IndexVector, typename Scale, bool>
273 class SubscriptOperation;
274 
282 template <typename T, typename IndexVector> struct GatherArguments
283 {
284  const IndexVector indexes;
285  const T *const address;
286 };
287 
295 template <typename T, typename IndexVector> struct ScatterArguments
296 {
297  const IndexVector indexes;
298  T *const address;
299 };
300 
304 template <typename I, I Begin, I End, typename F>
305 Vc_INTRINSIC enable_if<(Begin >= End), void> unrolled_loop(F &&)
306 {
307 }
308 
314 template <typename I, I Begin, I End, typename F>
315 Vc_INTRINSIC Vc_FLATTEN enable_if<(Begin < End), void> unrolled_loop(F &&f)
316 {
317  f(Begin);
318  unrolled_loop<I, Begin + 1, End>(f);
319 }
320 
325 template <std::size_t Size, typename F> Vc_INTRINSIC void for_all_vector_entries(F &&f)
326 {
327  unrolled_loop<std::size_t, 0u, Size>(std::forward<F>(f));
328 }
329 
330 } // namespace Common
331 } // namespace Vc
332 
333 #include "vector.h"
334 #include "mask.h"
335 #include "memoryfwd.h"
336 
337 #endif // VC_COMMON_TYPES_H_
338 
339 // vim: foldmethod=marker
signed char schar
signed char shorthand
Definition: types.h:66
void free(T *p)
Frees memory that was allocated with Vc::malloc.
Definition: memory.h:103
unsigned char uchar
unsigned char shorthand
Definition: types.h:64
unsigned int uint
unsigned int shorthand
Definition: types.h:60
unsigned long long ullong
unsigned long long shorthand
Definition: types.h:56
The main vector class for expressing data parallelism.
Definition: types.h:45
constexpr VectorSpecialInitializerIndexesFromZero IndexesFromZero
The special object Vc::IndexesFromZero can be used to construct Vector objects initialized to values ...
Definition: types.h:95
std::ostream & operator<<(std::ostream &out, const Vc::Vector< T, Abi > &v)
Prints the contents of a vector into a stream object.
Definition: IO:118
long long llong
long long shorthand
Definition: types.h:54
unsigned long ulong
unsigned long shorthand
Definition: types.h:58
unsigned short ushort
unsigned short shorthand
Definition: types.h:62
constexpr VectorSpecialInitializerZero Zero
The special object Vc::Zero can be used to construct Vector and Mask objects initialized to zero/fals...
Definition: types.h:85
constexpr VectorSpecialInitializerOne One
The special object Vc::One can be used to construct Vector and Mask objects initialized to one/true...
Definition: types.h:90