Vc  1.4.2
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>>
57 inline Vector<T, Abi> copysign(Vector<T, Abi> magnitude, Vector<T, Abi> sign);
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>>
95 inline Vector<T, Abi> exponent(Vector<T, Abi> x);
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>
260  inline Vector(Vector<U, abi> x,
261  enable_if<Traits::is_implicit_cast_allowed<U, T>::value> = nullarg);
262 
263 #if Vc_IS_VERSION_1
264 
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);
293 
297 #include "../common/loadinterface.h"
299 #include "../common/storeinterface.h"
301 
305  inline void setZero();
306 
314  inline void setZero(MaskType mask);
315 
323  inline void setZeroInverted(MaskType mask);
324 
328  inline void setQnan();
329 
335  inline void setQnan(MaskType mask);
336 
337 #define Vc_CURRENT_CLASS_NAME Vector
338 #include "../common/gatherinterface.h"
339 #include "../common/scatterinterface.h"
340 #undef Vc_CURRENT_CLASS_NAME
341 
344 
360  inline reference operator[](size_t index) noexcept;
369  inline EntryType operator[](size_t index) const noexcept;
371 
374 
379  inline MaskType operator!() const;
380 
388  inline Vector operator~() const;
389 
391  inline Vector operator-() const;
393  inline Vector operator+() const;
395 
406  inline Vector &operator++(); // prefix
408  inline Vector operator++(int); // postfix
409  inline Vector &operator--(); // prefix
410  inline Vector operator--(int); // postfix
412 
413 #define Vc_OP(symbol) \
414  inline Vc_PURE Vector operator symbol(const Vector &x) const;
415 
440  Vc_ALL_ARITHMETICS(Vc_OP);
443 
462  Vc_ALL_BINARY(Vc_OP);
465 
483  Vc_ALL_SHIFTS(Vc_OP);
486 #undef Vc_OP
487 
505 #define Vc_CMP_OP(symbol) inline Vc_PURE MaskType operator symbol(const Vector &x) const;
507  Vc_ALL_COMPARES(Vc_CMP_OP);
508 #undef Vc_CMP_OP
509 
530  inline Common::WriteMaskedVector<Vector, MaskType> operator()(MaskType mask);
531 
546 
549  inline EntryType min() const;
551  inline EntryType max() const;
553  inline EntryType product() const;
555  inline EntryType sum() const;
557  inline Vector partialSum() const;
559  inline EntryType min(MaskType mask) const;
561  inline EntryType max(MaskType mask) const;
563  inline EntryType product(MaskType mask) const;
565  inline EntryType sum(MaskType mask) const;
567 
609 
612  inline Vector shifted(int amount) const;
635  inline Vector shifted(int amount, Vector shiftIn) const;
637  inline Vector rotated(int amount) const;
639  inline Vector reversed() const;
641 
672  inline Vector sorted() const;
673 
702 
705  template <typename F> void callWithValuesSorted(F &&f);
707  template <typename F> inline void call(F &&f) const;
709  template <typename F> inline void call(F &&f, MaskType mask) const;
710 
712  template <typename F> inline Vector apply(F &&f) const;
714  template <typename F> inline Vector apply(F &&f, MaskType mask) const;
715 
717  template <typename IndexT> inline void fill(EntryType(&f)(IndexT));
719  inline void fill(EntryType(&f)());
721 
726  inline Vector interleaveLow(Vector x) const;
731  inline Vector interleaveHigh(Vector x) const;
732 
736  inline void assign(const Vector &v, const MaskType &m);
737 
743  inline VectorType &data();
745  inline const VectorType &data() const;
747 
750 
758  Vc_DEPRECATED("use exponent(x) instead") inline Vector exponent() const;
759 
767  Vc_DEPRECATED("use isnegative(x) instead") inline MaskType isNegative() const;
768 
771  static constexpr size_t Size = VectorTraits<T, Abi>::size();
772 
780  template <typename V2> inline V2 staticCast() const;
781 
789  template <typename V2>
790  Vc_DEPRECATED("use reinterpret_components_cast instead") inline V2
791  reinterpretCast() const;
792 
803  Vc_DEPRECATED("use copysign(x, y) instead") inline Vector
804  copySign(Vector reference) const;
806 
807  Vc_FREE_STORE_OPERATORS_ALIGNED(alignof(Vector));
808 
809 private:
810  VectorType d;
811 };
812 
831 template <typename V, typename T, typename Abi>
832 Vc_ALWAYS_INLINE Vc_CONST enable_if<
833  (V::size() == Vector<T, Abi>::size() &&
834  sizeof(typename V::VectorEntryType) ==
835  sizeof(typename Vector<T, Abi>::VectorEntryType) &&
836  sizeof(V) == sizeof(Vector<T, Abi>) && alignof(V) <= alignof(Vector<T, Abi>)),
837  V>
839 {
840  return reinterpret_cast<const V &>(x);
841 }
842 
843 #define Vc_OP(symbol) \
844  template <typename T, typename Abi> \
845  inline Vector<T, Abi> &operator symbol##=(Vector<T, Abi> &, \
846  const Vector<T, Abi> &x);
847  //Vc_ALL_ARITHMETICS(Vc_OP);
848  //Vc_ALL_BINARY(Vc_OP);
849  //Vc_ALL_SHIFTS(Vc_OP);
850 #undef Vc_OP
851 
852 } // namespace Vc
853 
854 #endif // VC_COMMON_VECTOR_H_
855 
856 // vim: foldmethod=marker
Vc::Vector::size
static constexpr size_t size()
Returns the number of scalar components ( ) in a vector of this type.
Definition: vector.h:142
Vc::operator-
Vc_INTRINSIC result_vector_type< L, R > operator-(L &&lhs, R &&rhs)
Applies - component-wise and concurrently.
Definition: simdarray.h:1722
Vc::Vector< T, Abi >::abi
Abi abi
The ABI tag type of the current template instantiation.
Definition: vector.h:151
Vc::MemoryAlignment
constexpr std::size_t MemoryAlignment
Definition: vector.h:215
Vc::Zero
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
Vc::Vector< T, Abi >
Vc::reinterpret_components_cast
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)
Definition: vector.h:838
Vc::isnegative
Vector< T, detail::not_fixed_size_abi< Abi > >::MaskType isnegative(Vector< T, Abi > x)
Definition: vector.h:106
Vc::exponent
Vector< T, Abi > exponent(Vector< T, Abi > x)
Vc::assign
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
Vc::max
fixed_size_simd< T, N > max(const SimdArray< T, N, V, M > &x, const SimdArray< T, N, V, M > &y)
Applies the std:: max function component-wise and concurrently.
Definition: simdarray.h:1855
Vc::Vector< T, Abi >::value_type
EntryType value_type
The type of the entries in the vector.
Definition: vector.h:156
Vc::One
constexpr VectorSpecialInitializerOne One
The special object Vc::One can be used to construct Vector and Mask objects initialized to one/true.
Definition: types.h:86
Vc::Mask< T, Abi >
Vc::SimdizeDetail::shifted
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:1069
Vc::operator+
result_vector_type< L, R > operator+(L &&lhs, R &&rhs)
Applies + component-wise and concurrently.
Definition: simdarray.h:1722
Vc::IndexesFromZero
constexpr VectorSpecialInitializerIndexesFromZero IndexesFromZero
The special object Vc::IndexesFromZero can be used to construct Vector objects initialized to values ...
Definition: types.h:91
Vc::Vector< T, Abi >::EntryType
typename VectorTraits< T, Abi >::EntryType EntryType
The type of the entries in the vector.
Definition: vector.h:154
Vc::copysign
Vector< T, Abi > copysign(Vector< T, Abi > magnitude, Vector< T, Abi > sign)
Vc::min
fixed_size_simd< T, N > min(const SimdArray< T, N, V, M > &x, const SimdArray< T, N, V, M > &y)
Applies the std:: min function component-wise and concurrently.
Definition: simdarray.h:1854