Vc  1.0.0-dev
SIMD Vector Classes for C++
vector.h
1 /* This file is part of the Vc library. {{{
2 Copyright © 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_VECTOR_H_
30 #define VC_COMMON_VECTOR_H_
31 
32 #include "types.h"
33 #include "vectorabi.h"
34 #include "vectortraits.h"
35 #include "simdarrayfwd.h"
36 #include "loadstoreflags.h"
37 #include "writemaskedvector.h"
38 
39 namespace Vc_VERSIONED_NAMESPACE
40 {
55 template<typename T, typename Abi = VectorAbi::Best<T>> class Vector
56 {
57 public:
75  static constexpr size_t size() { return VectorTraits<T, Abi>::size(); }
76 
81  static constexpr size_t MemoryAlignment = VectorTraits<T, Abi>::memoryAlignment();
82 
84  using abi = Abi;
85 
87  using EntryType = typename VectorTraits<T, Abi>::EntryType;
90 
91  using VectorEntryType = typename VectorTraits<T, Abi>::VectorEntryType;
95  using VectorType = typename VectorTraits<T, Abi>::VectorType;
99  using vector_type = VectorType;
100 
105 
107  using VectorArgument = Vector;
108 
110  using IndexType = Vc::SimdArray<int, size()>;
113 
116 
119  static inline Vector Zero();
120 
124  static inline Vector One();
125 
129  static inline Vector IndexesFromZero();
130 
144  static inline Vector Random();
145 
147  template <typename G> static inline Vector generate(G gen);
149 
152 
161  inline Vector() = default;
162 
168  explicit inline Vector(VectorSpecialInitializerZero);
169 
175  explicit inline Vector(VectorSpecialInitializerOne);
176 
182  explicit inline Vector(VectorSpecialInitializerIndexesFromZero);
184 
187 
190  template <typename U>
191  inline Vector(Vector<U, abi> x,
192  enable_if<Traits::is_implicit_cast_allowed<U, T>::value> = nullarg);
193 
204  template <typename U>
205  inline explicit Vector(
206  Vector<U, abi> x,
207  enable_if<!Traits::is_implicit_cast_allowed<U, T>::value> = nullarg);
208 
221  inline Vector(EntryType a);
222  template <typename U>
223  inline Vector(U a, enable_if<std::is_same<U, int>::value &&
224  !std::is_same<U, EntryType>::value> = nullarg);
226 
230 #include "../common/loadinterface.h"
232 #include "../common/storeinterface.h"
234 
238  inline void setZero();
239 
247  inline void setZero(MaskType mask);
248 
256  inline void setZeroInverted(MaskType mask);
257 
261  inline void setQnan();
262 
268  inline void setQnan(MaskType mask);
269 
270 #define Vc_CURRENT_CLASS_NAME Vector
271 #include "../common/gatherinterface.h"
272 #include "../common/scatterinterface.h"
273 #undef Vc_CURRENT_CLASS_NAME
274 
277 
289  inline EntryType &operator[](size_t index);
298  inline EntryType operator[](size_t index) const;
300 
303 
308  inline MaskType operator!() const;
309 
317  inline Vector operator~() const;
318 
320  inline Vector operator-() const;
322  inline Vector operator+() const;
324 
335  inline Vector &operator++(); // prefix
337  inline Vector operator++(int); // postfix
338  inline Vector &operator--(); // prefix
339  inline Vector operator--(int); // postfix
341 
342 #define Vc_OP(symbol) \
343  inline Vector &operator symbol##=(const Vector &x); \
344  inline Vc_PURE Vector operator symbol(const Vector &x) const;
345 
370  Vc_ALL_ARITHMETICS(Vc_OP)
373 
392  Vc_ALL_BINARY(Vc_OP)
395 
413  Vc_ALL_SHIFTS(Vc_OP)
416 #undef Vc_OP
417 
435 #define Vc_CMP_OP(symbol) inline Vc_PURE MaskType operator symbol(const Vector &x) const;
437  Vc_ALL_COMPARES(Vc_CMP_OP)
438 #undef Vc_CMP_OP
439 
442  inline MaskType isNegative() const;
443 
445  template <typename V2> inline V2 reinterpretCast() const;
446 
466  inline Common::WriteMaskedVector<Vector, MaskType> operator()(MaskType mask);
467 
482 
485  inline EntryType min() const;
487  inline EntryType max() const;
489  inline EntryType product() const;
491  inline EntryType sum() const;
493  inline Vector partialSum() const;
495  inline EntryType min(MaskType mask) const;
497  inline EntryType max(MaskType mask) const;
499  inline EntryType product(MaskType mask) const;
501  inline EntryType sum(MaskType mask) const;
503 
545 
548  inline Vector shifted(int amount) const;
571  inline Vector shifted(int amount, Vector shiftIn) const;
573  inline Vector rotated(int amount) const;
575  inline Vector reversed() const;
577 
608  inline Vector sorted() const;
609 
638 
641  template <typename F> void callWithValuesSorted(F &&f);
643  template <typename F> inline void call(F &&f) const;
645  template <typename F> inline void call(F &&f, MaskType mask) const;
646 
648  template <typename F> inline Vector apply(F &&f) const;
650  template <typename F> inline Vector apply(F &&f, MaskType mask) const;
651 
653  template <typename IndexT> inline void fill(EntryType(&f)(IndexT));
655  inline void fill(EntryType(&f)());
657 
659  inline Vector exponent() const;
660 
665  inline Vector interleaveLow(Vector x) const;
670  inline Vector interleaveHigh(Vector x) const;
671 
675  inline void assign(const Vector &v, const MaskType &m);
676 
682  inline VectorType &data();
684  inline const VectorType &data() const;
686 
689 
692  static constexpr size_t Size = VectorTraits<T, Abi>::size();
693 
701  template <typename V2> inline V2 staticCast() const;
702 
713  inline Vector copySign(Vector reference) const;
715 private:
716  VectorType d;
717 };
718 
719 } // namespace Vc
720 
721 #endif // VC_COMMON_VECTOR_H_
722 
723 // vim: foldmethod=marker
static constexpr size_t size()
Returns the number of scalar components ( ) in a vector of this type.
Definition: vector.h:75
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
Vc::Vector< T > min(const Vc::Vector< T > &x, const Vc::Vector< T > &y)
Vc::Vector< T > max(const Vc::Vector< T > &x, const Vc::Vector< T > &y)
Abi abi
The ABI tag type of the current template instantiation.
Definition: vector.h:84
IndexType index_type
The type of the vector used for indexes in gather and scatter operations.
Definition: vector.h:112
EntryType value_type
The type of the entries in the vector.
Definition: vector.h:89
void assign(Adapter< S, T, N > &a, size_t i, const S &x)
Assigns one scalar object x to a SIMD slot at offset i in the simdized object a.
Definition: simdize.h:941
The main SIMD mask class.
Definition: mask.h:42
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
Adapter< S, T, N > shifted(const Adapter< S, T, N > &a, int shift)
Returns a new vectorized object where each entry is shifted by shift.
Definition: simdize.h:1002
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
constexpr std::size_t MemoryAlignment
Specifies the most conservative memory alignment necessary for aligned loads and stores of Vector typ...
Definition: vector.h:219
typename VectorTraits< T, Abi >::EntryType EntryType
The type of the entries in the vector.
Definition: vector.h:87
Vc::SimdArray< int, size()> IndexType
The type of the vector used for indexes in gather and scatter operations.
Definition: vector.h:110