Vc 1.4.1
SIMD Vector Classes for C++
vector.h
1/* This file is part of the Vc library. {{{
2Copyright © 2015 Matthias Kretz <kretz@kde.org>
3
4Redistribution and use in source and binary forms, with or without
5modification, 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
15THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
19DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22ON 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
24SOFTWARE, 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
41namespace Vc_VERSIONED_NAMESPACE
42{
54template <typename T, typename Abi,
55 typename = enable_if<std::is_floating_point<T>::value &&
56 !detail::is_fixed_size_abi<Abi>::value>>
58
92template <typename T, typename Abi,
93 typename = enable_if<std::is_floating_point<T>::value &&
94 !detail::is_fixed_size_abi<Abi>::value>>
96
104template <typename T, typename Abi>
105Vc_INTRINSIC Vc_CONST typename Vector<T, detail::not_fixed_size_abi<Abi>>::MaskType
107{
109}
110
125template<typename T, typename Abi = VectorAbi::Best<T>> class Vector
126{
127public:
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);
293
298#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
407 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;
441 Vc_ALL_ARITHMETICS(Vc_OP);
443
463 Vc_ALL_BINARY(Vc_OP);
465
484 Vc_ALL_SHIFTS(Vc_OP);
486#undef Vc_OP
487
506#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
510
530 inline Common::WriteMaskedVector<Vector, MaskType> operator()(MaskType mask);
531
547
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
610
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
703
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
744 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
809private:
810 VectorType d;
811};
812
831template <typename V, typename T, typename Abi>
832Vc_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
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.
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.
Common::WriteMaskedVector< Vector, MaskType > operator()(MaskType mask)
Writemask the vector before an assignment.
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, Abi > exponent(Vector< T, Abi > x)
Extracts the exponent of each floating-point vector component.
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
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
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:838
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