Vc  1.1.0
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 <ratio>
33 #include "types.h"
34 #include "vectorabi.h"
35 #include "vectortraits.h"
36 #include "simdarrayfwd.h"
37 #include "loadstoreflags.h"
38 #include "writemaskedvector.h"
39 
40 namespace Vc_VERSIONED_NAMESPACE
41 {
53 template <typename T, typename Abi,
54  typename = enable_if<std::is_floating_point<T>::value>>
56 
90 template <typename T, typename Abi,
91  typename = enable_if<std::is_floating_point<T>::value>>
93 
101 template <typename T, typename Abi>
102 Vc_INTRINSIC Vc_CONST typename Vector<T, Abi>::MaskType isnegative(Vector<T, Abi> x)
103 {
104  return x < Vector<T, Abi>::Zero();
105 }
106 
121 template<typename T, typename Abi = VectorAbi::Best<T>> class Vector
122 {
123 public:
141  static constexpr size_t size() { return VectorTraits<T, Abi>::size(); }
142 
147  static constexpr size_t MemoryAlignment = VectorTraits<T, Abi>::memoryAlignment();
148 
150  using abi = Abi;
151 
153  using EntryType = typename VectorTraits<T, Abi>::EntryType;
156 
157  using VectorEntryType = typename VectorTraits<T, Abi>::VectorEntryType;
161  using VectorType = typename VectorTraits<T, Abi>::VectorType;
165  using vector_type = VectorType;
166 
171 
172  using MaskArgument = MaskType;
173  using VectorArgument = Vector;
174 
179 
182 
185  static inline Vector Zero();
186 
190  static inline Vector One();
191 
195  static inline Vector IndexesFromZero();
196 
210  static inline Vector Random();
211 
213  template <typename G> static inline Vector generate(G gen);
215 
218 
227  inline Vector() = default;
228 
234  explicit inline Vector(VectorSpecialInitializerZero);
235 
241  explicit inline Vector(VectorSpecialInitializerOne);
242 
248  explicit inline Vector(VectorSpecialInitializerIndexesFromZero);
250 
253 
256  template <typename U>
257  inline Vector(Vector<U, abi> x,
258  enable_if<Traits::is_implicit_cast_allowed<U, T>::value> = nullarg);
259 
270  template <typename U>
271  inline explicit Vector(
272  Vector<U, abi> x,
273  enable_if<!Traits::is_implicit_cast_allowed<U, T>::value> = nullarg);
274 
287  inline Vector(EntryType a);
288  template <typename U>
289  inline Vector(U a, enable_if<std::is_same<U, int>::value &&
290  !std::is_same<U, EntryType>::value> = nullarg);
292 
296 #include "../common/loadinterface.h"
298 #include "../common/storeinterface.h"
304  inline void setZero();
305 
313  inline void setZero(MaskType mask);
314 
322  inline void setZeroInverted(MaskType mask);
323 
327  inline void setQnan();
328 
334  inline void setQnan(MaskType mask);
335 
336 #define Vc_CURRENT_CLASS_NAME Vector
337 #include "../common/gatherinterface.h"
338 #include "../common/scatterinterface.h"
339 #undef Vc_CURRENT_CLASS_NAME
340 
343 
355  inline EntryType &operator[](size_t index);
364  inline EntryType operator[](size_t index) const;
366 
369 
374  inline MaskType operator!() const;
375 
383  inline Vector operator~() const;
384 
386  inline Vector operator-() const;
388  inline Vector operator+() const;
390 
401  inline Vector &operator++(); // prefix
403  inline Vector operator++(int); // postfix
404  inline Vector &operator--(); // prefix
405  inline Vector operator--(int); // postfix
407 
408 #define Vc_OP(symbol) \
409  inline Vector &operator symbol##=(const Vector &x); \
410  inline Vc_PURE Vector operator symbol(const Vector &x) const;
411 
436  Vc_ALL_ARITHMETICS(Vc_OP)
439 
458  Vc_ALL_BINARY(Vc_OP)
461 
479  Vc_ALL_SHIFTS(Vc_OP)
482 #undef Vc_OP
483 
501 #define Vc_CMP_OP(symbol) inline Vc_PURE MaskType operator symbol(const Vector &x) const;
503  Vc_ALL_COMPARES(Vc_CMP_OP)
504 #undef Vc_CMP_OP
505 
508  template <typename V2> inline V2 reinterpretCast() const;
509 
529  inline Common::WriteMaskedVector<Vector, MaskType> operator()(MaskType mask);
530 
545 
548  inline EntryType min() const;
550  inline EntryType max() const;
552  inline EntryType product() const;
554  inline EntryType sum() const;
556  inline Vector partialSum() const;
558  inline EntryType min(MaskType mask) const;
560  inline EntryType max(MaskType mask) const;
562  inline EntryType product(MaskType mask) const;
564  inline EntryType sum(MaskType mask) const;
566 
608 
611  inline Vector shifted(int amount) const;
634  inline Vector shifted(int amount, Vector shiftIn) const;
636  inline Vector rotated(int amount) const;
638  inline Vector reversed() const;
640 
671  inline Vector sorted() const;
672 
701 
704  template <typename F> void callWithValuesSorted(F &&f);
706  template <typename F> inline void call(F &&f) const;
708  template <typename F> inline void call(F &&f, MaskType mask) const;
709 
711  template <typename F> inline Vector apply(F &&f) const;
713  template <typename F> inline Vector apply(F &&f, MaskType mask) const;
714 
716  template <typename IndexT> inline void fill(EntryType(&f)(IndexT));
718  inline void fill(EntryType(&f)());
720 
725  inline Vector interleaveLow(Vector x) const;
730  inline Vector interleaveHigh(Vector x) const;
731 
735  inline void assign(const Vector &v, const MaskType &m);
736 
742  inline VectorType &data();
744  inline const VectorType &data() const;
746 
749 
751  inline Vc_DEPRECATED("use exponent(x) instead") Vector exponent() const;
752 
754  inline Vc_DEPRECATED("use isnegative(x) instead") MaskType isNegative() const;
755 
758  static constexpr size_t Size = VectorTraits<T, Abi>::size();
759 
767  template <typename V2> inline V2 staticCast() const;
768 
779  inline Vc_DEPRECATED("use copysign(x, y) instead") Vector
780  copySign(Vector reference) const;
782 private:
783  VectorType d;
784 };
785 
786 } // namespace Vc
787 
788 #endif // VC_COMMON_VECTOR_H_
789 
790 // vim: foldmethod=marker
static constexpr size_t size()
Returns the number of scalar components ( ) in a vector of this type.
Definition: vector.h:141
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)
Data-parallel type with (somewhat) arbitrary number of components.
Definition: simdarray.h:475
Abi abi
The ABI tag type of the current template instantiation.
Definition: vector.h:150
Vector< T, Abi > exponent(Vector< T, Abi > x)
Extracts the exponent of each floating-point vector component.
Vector< T, Abi > copysign(Vector< T, Abi > magnitude, Vector< T, Abi > sign)
Copies the sign(s) of sign to the value(s) in magnitude and returns the resulting vector...
EntryType value_type
The type of the entries in the vector.
Definition: vector.h:155
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:938
Vector< T, Abi >::MaskType isnegative(Vector< T, Abi > x)
Returns for each vector component whether it stores a negative value.
Definition: vector.h:102
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:999
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:153