Vc  1.4.3
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 
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are met:
6  * Redistributions of source code must retain the above copyright
7  notice, this list of conditions and the following disclaimer.
8  * Redistributions in binary form must reproduce the above copyright
9  notice, this list of conditions and the following disclaimer in the
10  documentation and/or other materials provided with the distribution.
11  * Neither the names of contributing organizations nor the
12  names of its contributors may be used to endorse or promote products
13  derived from this software without specific prior written permission.
14 
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
19 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 }}}*/
27 
28 #ifndef VC_COMMON_VECTOR_H_
29 #define VC_COMMON_VECTOR_H_
30 
31 #include <ratio>
32 #include "elementreference.h"
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 #include "detail.h"
40 
41 namespace Vc_VERSIONED_NAMESPACE
42 {
54 template <typename T, typename Abi,
55  typename = enable_if<std::is_floating_point<T>::value &&
56  !detail::is_fixed_size_abi<Abi>::value>>
58 
92 template <typename T, typename Abi,
93  typename = enable_if<std::is_floating_point<T>::value &&
94  !detail::is_fixed_size_abi<Abi>::value>>
96 
104 template <typename T, typename Abi>
105 Vc_INTRINSIC Vc_CONST typename Vector<T, detail::not_fixed_size_abi<Abi>>::MaskType
107 {
108  return x < Vector<T, Abi>::Zero();
109 }
110 
125 template<typename T, typename Abi = VectorAbi::Best<T>> class Vector
126 {
127 public:
142  static constexpr size_t size() { return VectorTraits<T, Abi>::size(); }
143 
148  static constexpr size_t MemoryAlignment = VectorTraits<T, Abi>::memoryAlignment();
149 
151  using abi = Abi;
152 
154  using EntryType = typename VectorTraits<T, Abi>::EntryType;
157 
158  using VectorEntryType = typename VectorTraits<T, Abi>::VectorEntryType;
162  using VectorType = typename VectorTraits<T, Abi>::VectorType;
166  using vector_type = VectorType;
167 
172 
173  using MaskArgument = MaskType;
174  using VectorArgument = Vector;
175 
180 
181  using reference = Detail::ElementReference<Vector>;
182 
185 
188  static inline Vector Zero();
189 
193  static inline Vector One();
194 
198  static inline Vector IndexesFromZero();
199 
213  static inline Vector Random();
214 
216  template <typename G> static inline Vector generate(G gen);
218 
221 
230  inline Vector() = default;
231 
237  explicit inline Vector(VectorSpecialInitializerZero);
238 
244  explicit inline Vector(VectorSpecialInitializerOne);
245 
251  explicit inline Vector(VectorSpecialInitializerIndexesFromZero);
253 
256 
259  template <typename U>
261  enable_if<Traits::is_implicit_cast_allowed<U, T>::value> = nullarg);
262 
263 #if Vc_IS_VERSION_1
274  template <typename U>
275  Vc_DEPRECATED("use simd_cast instead of explicit type casting to convert between "
276  "vector types") inline explicit Vector(
277  Vector<U, abi> x,
278  enable_if<!Traits::is_implicit_cast_allowed<U, T>::value> = nullarg);
279 #endif
280 
288  inline Vector(EntryType a);
289  template <typename U>
290  inline Vector(U a, enable_if<std::is_same<U, int>::value &&
291  !std::is_same<U, EntryType>::value> = nullarg);
292  inline explicit Vector(reference a);
294 
299 #include "../common/loadinterface.h"
300 #include "../common/storeinterface.h"
302 
306  inline void setZero();
307 
315  inline void setZero(MaskType mask);
316 
324  inline void setZeroInverted(MaskType mask);
325 
329  inline void setQnan();
330 
336  inline void setQnan(MaskType mask);
337 
338 #define Vc_CURRENT_CLASS_NAME Vector
339 #include "../common/gatherinterface.h"
340 #include "../common/scatterinterface.h"
341 #undef Vc_CURRENT_CLASS_NAME
342 
345 
361  inline reference operator[](size_t index) noexcept;
370  inline EntryType operator[](size_t index) const noexcept;
372 
375 
380  inline MaskType operator!() const;
381 
389  inline Vector operator~() const;
390 
392  inline Vector operator-() const;
394  inline Vector operator+() const;
396 
408  inline Vector &operator++(); // prefix
409  inline Vector operator++(int); // postfix
410  inline Vector &operator--(); // prefix
411  inline Vector operator--(int); // postfix
413 
414 #define Vc_OP(symbol) \
415  inline Vc_PURE Vector operator symbol(const Vector &x) const;
442  Vc_ALL_ARITHMETICS(Vc_OP);
444 
464  Vc_ALL_BINARY(Vc_OP);
466 
485  Vc_ALL_SHIFTS(Vc_OP);
487 #undef Vc_OP
488 
507 #define Vc_CMP_OP(symbol) inline Vc_PURE MaskType operator symbol(const Vector &x) const;
508  Vc_ALL_COMPARES(Vc_CMP_OP);
509 #undef Vc_CMP_OP
511 
531  inline Common::WriteMaskedVector<Vector, MaskType> operator()(MaskType mask);
532 
548 
550  inline EntryType min() const;
552  inline EntryType max() const;
554  inline EntryType product() const;
556  inline EntryType sum() const;
558  inline Vector partialSum() const;
560  inline EntryType min(MaskType mask) const;
562  inline EntryType max(MaskType mask) const;
564  inline EntryType product(MaskType mask) const;
566  inline EntryType sum(MaskType mask) const;
568 
611 
613  inline Vector shifted(int amount) const;
636  inline Vector shifted(int amount, Vector shiftIn) const;
638  inline Vector rotated(int amount) const;
640  inline Vector reversed() const;
642 
673  inline Vector sorted() const;
674 
704 
706  template <typename F> void callWithValuesSorted(F &&f);
708  template <typename F> inline void call(F &&f) const;
710  template <typename F> inline void call(F &&f, MaskType mask) const;
711 
713  template <typename F> inline Vector apply(F &&f) const;
715  template <typename F> inline Vector apply(F &&f, MaskType mask) const;
716 
718  template <typename IndexT> inline void fill(EntryType(&f)(IndexT));
720  inline void fill(EntryType(&f)());
722 
727  inline Vector interleaveLow(Vector x) const;
732  inline Vector interleaveHigh(Vector x) const;
733 
737  inline void assign(const Vector &v, const MaskType &m);
738 
745  inline VectorType &data();
746  inline const VectorType &data() const;
748 
751 
759  Vc_DEPRECATED("use exponent(x) instead") inline Vector exponent() const;
760 
768  Vc_DEPRECATED("use isnegative(x) instead") inline MaskType isNegative() const;
769 
772  static constexpr size_t Size = VectorTraits<T, Abi>::size();
773 
781  template <typename V2> inline V2 staticCast() const;
782 
790  template <typename V2>
791  Vc_DEPRECATED("use reinterpret_components_cast instead") inline V2
792  reinterpretCast() const;
793 
804  Vc_DEPRECATED("use copysign(x, y) instead") inline Vector
805  copySign(Vector reference) const;
807 
808  Vc_FREE_STORE_OPERATORS_ALIGNED(alignof(Vector));
809 
810 private:
811  VectorType d;
812 };
813 
832 template <typename V, typename T, typename Abi>
833 Vc_ALWAYS_INLINE Vc_CONST enable_if<
834  (V::size() == Vector<T, Abi>::size() &&
835  sizeof(typename V::VectorEntryType) ==
836  sizeof(typename Vector<T, Abi>::VectorEntryType) &&
837  sizeof(V) == sizeof(Vector<T, Abi>) && alignof(V) <= alignof(Vector<T, Abi>)),
838  V>
840 {
841  return reinterpret_cast<const V &>(x);
842 }
843 
844 #define Vc_OP(symbol) \
845  template <typename T, typename Abi> \
846  inline Vector<T, Abi> &operator symbol##=(Vector<T, Abi> &, \
847  const Vector<T, Abi> &x);
848  //Vc_ALL_ARITHMETICS(Vc_OP);
849  //Vc_ALL_BINARY(Vc_OP);
850  //Vc_ALL_SHIFTS(Vc_OP);
851 #undef Vc_OP
852 
853 } // namespace Vc
854 
855 #endif // VC_COMMON_VECTOR_H_
856 
857 // vim: foldmethod=marker
The main SIMD mask class.
Definition: mask.h:42
The main vector class for expressing data parallelism.
Definition: vector.h:126
void setZeroInverted(MaskType mask)
Set all entries to zero where the mask is not set.
static constexpr size_t size()
Returns the number of scalar components ( ) in a vector of this type.
Definition: vector.h:142
Vector operator~() const
Inverts all bits.
EntryType sum(MaskType mask) const
Returns the sum of the vector components selected by mask.
Vector apply(F &&f, MaskType mask) const
As above, but skip the entries where mask is not set.
void fill(EntryType(&f)())
Fill the vector with the values [f(), f(), f(), ...].
EntryType operator[](size_t index) const noexcept
This operator can be used to read scalar entries of the vector.
void setQnan(MaskType mask)
Set all entries to the bit representation of a QNaN where the mask is set.
void setQnan()
Set all entries to the bit representation of a QNaN.
void call(F &&f, MaskType mask) const
As above, but skip the entries where mask is not set.
void setZero()
Set all entries to zero.
static Vector IndexesFromZero()
Returns a vector with the entries initialized to 0, 1, 2, 3, 4, 5, ...
static Vector One()
Returns a vector with the entries initialized to one.
Vector(VectorSpecialInitializerZero)
Construct a vector with the entries initialized to zero.
EntryType sum() const
Returns the sum of all entries in the vector.
Vector operator+() const
Returns a copy of the vector object.
Vector operator-() const
Returns a new vector object with all entries negated.
Vector shifted(int amount) const
Shift vector entries to the left by amount; shifting in zeros.
EntryType product() const
Returns the product of all entries in the vector.
Common::WriteMaskedVector< Vector, MaskType > operator()(MaskType mask)
Writemask the vector before an assignment.
void call(F &&f) const
Call f with the scalar entries of the vector.
void callWithValuesSorted(F &&f)
Call f sequentially, starting with the minimum up to the maximum value.
Vector(EntryType a)
Broadcast Constructor.
void setZero(MaskType mask)
Set all entries to zero where the mask is set.
Vector apply(F &&f) const
Call f on every entry of the vector and return the results as a new vector.
void fill(EntryType(&f)(IndexT))
Fill the vector with the values [f(0), f(1), f(2), ...].
EntryType max() const
Returns the largest entry in the vector.
Vector(VectorSpecialInitializerIndexesFromZero)
Construct a vector with the entries initialized to 0, 1, 2, 3, 4, 5, ...
Vector rotated(int amount) const
Rotate vector entries to the left by amount.
static Vector generate(G gen)
Generate a vector object from return values of gen (static variant of fill).
static Vector Random()
Returns a vector with pseudo-random entries.
Vector(Vector< U, abi > x, enable_if< Traits::is_implicit_cast_allowed< U, T >::value >=nullarg)
Implict conversion from compatible Vector<U, Abi> types.
EntryType min() const
Returns the smallest entry in the vector.
Vector()=default
Construct a zero-initialized vector object.
Vector sorted() const
Return a sorted copy of the vector.
Abi abi
The ABI tag type of the current template instantiation.
Definition: vector.h:151
Vector reversed() const
Returns a vector with all components reversed.
Vector(VectorSpecialInitializerOne)
Construct a vector with the entries initialized to one.
Vector partialSum() const
Returns a vector containing the sum of all entries with smaller index.
reference operator[](size_t index) noexcept
This operator can be used to modify scalar entries of the vector.
EntryType min(MaskType mask) const
Returns the smallest entry of the vector components selected by mask.
EntryType product(MaskType mask) const
Returns the product of the vector components selected by mask.
typename VectorTraits< T, Abi >::EntryType EntryType
The type of the entries in the vector.
Definition: vector.h:154
EntryType value_type
The type of the entries in the vector.
Definition: vector.h:156
MaskType operator!() const
Determine where the vector is null.
EntryType max(MaskType mask) const
Returns the largest entry of the vector components selected by mask.
static Vector Zero()
Returns a vector with the entries initialized to zero.
Vector shifted(int amount, Vector shiftIn) const
Shift vector entries to the left by amount; shifting in values from shiftIn (instead of zeros).
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.
Vector< T, detail::not_fixed_size_abi< Abi > >::MaskType isnegative(Vector< T, Abi > x)
Returns for each vector component whether it stores a negative value.
Definition: vector.h:106
Vector< T, Abi > exponent(Vector< T, Abi > x)
Extracts the exponent of each floating-point vector component.
enable_if<(V::size()==Vector< T, Abi >::size() &&sizeof(typename V::VectorEntryType)==sizeof(typename Vector< T, Abi >::VectorEntryType) &&sizeof(V)==sizeof(Vector< T, Abi >) &&alignof(V)<=alignof(Vector< T, Abi >)), V > reinterpret_components_cast(const Vector< T, Abi > &x)
Constructs a new Vector object of type V from the Vector x, reinterpreting the bits of x for the new ...
Definition: vector.h:839
constexpr VectorSpecialInitializerZero Zero
The special object Vc::Zero can be used to construct Vector and Mask objects initialized to zero/fals...
Definition: types.h:81
constexpr std::size_t MemoryAlignment
Specifies the most conservative memory alignment necessary for aligned loads and stores of Vector typ...
Definition: vector.h:215
void assign(SimdizeDetail::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:1221