Vc  1.3.80-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 
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 
40 namespace Vc_VERSIONED_NAMESPACE
41 {
53 template <typename T, typename Abi,
54  typename = enable_if<std::is_floating_point<T>::value &&
55  !detail::is_fixed_size_abi<Abi>::value>>
57 
91 template <typename T, typename Abi,
92  typename = enable_if<std::is_floating_point<T>::value &&
93  !detail::is_fixed_size_abi<Abi>::value>>
95 
103 template <typename T, typename Abi>
104 Vc_INTRINSIC Vc_CONST typename Vector<T, detail::not_fixed_size_abi<Abi>>::MaskType
106 {
107  return x < Vector<T, Abi>::Zero();
108 }
124 template<typename T, typename Abi = VectorAbi::Best<T>> class Vector
125 {
126 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 
180  using reference = Detail::ElementReference<Vector>;
181 
184 
187  static inline Vector Zero();
188 
192  static inline Vector One();
197  static inline Vector IndexesFromZero();
198 
212  static inline Vector Random();
213 
215  template <typename G> static inline Vector generate(G gen);
217 
229  inline Vector() = default;
230 
236  explicit inline Vector(VectorSpecialInitializerZero);
237 
243  explicit inline Vector(VectorSpecialInitializerOne);
244 
250  explicit inline Vector(VectorSpecialInitializerIndexesFromZero);
252 
255 
258  template <typename U>
259  inline Vector(Vector<U, abi> x,
260  enable_if<Traits::is_implicit_cast_allowed<U, T>::value> = nullarg);
261 
262 #if Vc_IS_VERSION_1
263 
273  template <typename U>
274  Vc_DEPRECATED("use simd_cast instead of explicit type casting to convert between "
275  "vector types") inline explicit Vector(
276  Vector<U, abi> x,
277  enable_if<!Traits::is_implicit_cast_allowed<U, T>::value> = nullarg);
278 #endif
279 
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"
300 
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 
359  inline reference operator[](size_t index) noexcept;
368  inline EntryType operator[](size_t index) const noexcept;
370 
373 
378  inline MaskType operator!() const;
379 
387  inline Vector operator~() const;
388 
390  inline Vector operator-() const;
392  inline Vector operator+() const;
394 
405  inline Vector &operator++(); // prefix
407  inline Vector operator++(int); // postfix
408  inline Vector &operator--(); // prefix
409  inline Vector operator--(int); // postfix
411 
412 #define Vc_OP(symbol) \
413  inline Vc_PURE Vector operator symbol(const Vector &x) const;
414 
439  Vc_ALL_ARITHMETICS(Vc_OP);
442 
461  Vc_ALL_BINARY(Vc_OP);
464 
482  Vc_ALL_SHIFTS(Vc_OP);
485 #undef Vc_OP
486 
504 #define Vc_CMP_OP(symbol) inline Vc_PURE MaskType operator symbol(const Vector &x) const;
506  Vc_ALL_COMPARES(Vc_CMP_OP);
507 #undef Vc_CMP_OP
508 
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 
757  Vc_DEPRECATED("use exponent(x) instead") inline Vector exponent() const;
758 
766  Vc_DEPRECATED("use isnegative(x) instead") inline MaskType isNegative() const;
767 
770  static constexpr size_t Size = VectorTraits<T, Abi>::size();
771 
779  template <typename V2> inline V2 staticCast() const;
780 
788  template <typename V2>
789  Vc_DEPRECATED("use reinterpret_components_cast instead") inline V2
790  reinterpretCast() const;
791 
802  Vc_DEPRECATED("use copysign(x, y) instead") inline Vector
803  copySign(Vector reference) const;
805 
806  Vc_FREE_STORE_OPERATORS_ALIGNED(alignof(Vector));
807 
808 private:
809  VectorType d;
810 };
811 
830 template <typename V, typename T, typename Abi>
831 Vc_ALWAYS_INLINE Vc_CONST enable_if<
832  (V::size() == Vector<T, Abi>::size() &&
833  sizeof(typename V::VectorEntryType) ==
834  sizeof(typename Vector<T, Abi>::VectorEntryType) &&
835  sizeof(V) == sizeof(Vector<T, Abi>) && alignof(V) <= alignof(Vector<T, Abi>)),
836  V>
837 reinterpret_components_cast(const Vector<T, Abi> &x)
838 {
839  return reinterpret_cast<const V &>(x);
840 }
841 
842 #define Vc_OP(symbol) \
843  template <typename T, typename Abi> \
844  inline Vector<T, Abi> &operator symbol##=(Vector<T, Abi> &, \
845  const Vector<T, Abi> &x);
846  //Vc_ALL_ARITHMETICS(Vc_OP);
847  //Vc_ALL_BINARY(Vc_OP);
848  //Vc_ALL_SHIFTS(Vc_OP);
849 #undef Vc_OP
850 
851 } // namespace Vc
852 
853 #endif // VC_COMMON_VECTOR_H_
854 
855 // 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: fwddecl.h:53
constexpr VectorSpecialInitializerIndexesFromZero IndexesFromZero
The special object Vc::IndexesFromZero can be used to construct Vector objects initialized to values ...
Definition: types.h:90
Vc::Vector< T > min(const Vc::Vector< T > &x, const Vc::Vector< T > &y)
result_vector_type< L, R > operator-(L &&lhs, R &&rhs)
Applies - component-wise and concurrently.
Definition: simdarray.h:1662
Vc::Vector< T > max(const Vc::Vector< T > &x, const Vc::Vector< T > &y)
Data-parallel arithmetic type with user-defined number of elements.
Definition: fwddecl.h:82
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:105
Abi abi
The ABI tag type of the current template instantiation.
Definition: vector.h:150
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:1216
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:837
result_vector_type< L, R > operator+(L &&lhs, R &&rhs)
Applies + component-wise and concurrently.
Definition: simdarray.h:1662
static constexpr std::size_t size()
Returns N, the number of scalar components in an object of this type.
Definition: simdarray.h:661
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
The main SIMD mask class.
Definition: fwddecl.h:52
constexpr VectorSpecialInitializerZero Zero
The special object Vc::Zero can be used to construct Vector and Mask objects initialized to zero/fals...
Definition: types.h:80
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:1064
constexpr VectorSpecialInitializerOne One
The special object Vc::One can be used to construct Vector and Mask objects initialized to one/true...
Definition: types.h:85
constexpr std::size_t MemoryAlignment
Specifies the most conservative memory alignment necessary for aligned loads and stores of Vector typ...
Definition: vector.h:215
typename VectorTraits< T, Abi >::EntryType EntryType
The type of the entries in the vector.
Definition: vector.h:153