Vc 1.4.5
SIMD Vector Classes for C++
 
Loading...
Searching...
No Matches
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
50
50 *
51 * \return a value where the sign of the value equals the sign of \p sign. I.e.
52 * `sign(copysign(v, r)) == sign(r)`.
53 */
54template <typename T, typename Abi,
55 typename = enable_if<std::is_floating_point<T>::value &&
56 !detail::is_fixed_size_abi<Abi>::value>>
58
67 * returned value is a fast approximation to the logarithm of base 2. The absolute error of that
68 * approximation is between [0, 1[.
69 *
70 * Examples:
71\verbatim
72 value | exponent | log2
73=======|==========|=======
74 1.0 | 0 | 0
75 2.0 | 1 | 1
76 3.0 | 1 | 1.585
77 3.9 | 1 | 1.963
78 4.0 | 2 | 2
79 4.1 | 2 | 2.036
80\endverbatim
81 *
82 * \warning This function assumes a positive value (non-zero). If the value is negative the sign bit will
83 * modify the returned value. An input value of zero will return the bias of the floating-point
84 * representation. If you compile with Vc runtime checks, the function will assert
85 * values greater than or equal to zero.
86 *
87 * You may use abs to apply this function to negative values:
88 * \code
89 * exponent(abs(v))
90 * \endcode
91 */
92template <typename T, typename Abi,
93 typename = enable_if<std::is_floating_point<T>::value &&
94 !detail::is_fixed_size_abi<Abi>::value>>
99 * Returns for each vector component whether it stores a negative value.
100 *
101 * \param x The vector of values to check for the sign.
102 * \returns a mask which is \c true only in those components that are negative in \p x.
103 */
104template <typename T, typename Abi>
105Vc_INTRINSIC Vc_CONST typename Vector<T, detail::not_fixed_size_abi<Abi>>::MaskType
107{
108 return x < Vector<T, Abi>::Zero();
110
117 * are specializations of this class.
118 * For most cases there are no API differences for the specializations.
119 * Make use of Vector<T> for generic programming, otherwise you might prefer to use
120 * the \p *_v aliases.
122 * \see Vc::float_v, Vc::double_v, Vc::int_v, Vc::uint_v, Vc::short_v, Vc::ushort_v
123 * \see Mask
124 */
125template<typename T, typename Abi = VectorAbi::Best<T>> class Vector
126{
127public:
139 * \returns The number of components (i.e. \VSize{T}) objects of this vector type
140 * store and manipulate.
141 */
142 static constexpr size_t size() { return VectorTraits<T, Abi>::size(); }
145 * Specifies the alignment requirement for aligned load and store calls for objects of
146 * this vector type.
147 */
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;
160 * This type reveals the implementation-specific type used for the data member.
161 */
162 using VectorType = typename VectorTraits<T, Abi>::VectorType;
166 using vector_type = VectorType;
167
170 /// \copydoc MaskType
172
173 using MaskArgument = MaskType;
174 using VectorArgument = Vector;
175
180
181 using reference = Detail::ElementReference<Vector>;
182
185
188 static inline Vector Zero();
193 static inline Vector One();
194
198 static inline Vector IndexesFromZero();
199
203
213 static inline Vector Random();
214
216 template <typename G> static inline Vector generate(G gen);
218
220 ///@{
221
227
230 inline Vector() = default;
231
237 explicit inline Vector(VectorSpecialInitializerZero);
238
244 explicit inline Vector(VectorSpecialInitializerOne);
251 explicit inline Vector(VectorSpecialInitializerIndexesFromZero);
253
254
256
259 template <typename U>
261 enable_if<Traits::is_implicit_cast_allowed<U, T>::value> = nullarg);
262
263#if Vc_IS_VERSION_1
268 * x contains more entries than the new object the high components will be
269 * ignored. If \p x contains fewer entries than the new object the high
270 * components of the new object will be zero-initialized. Type conversion is
271 * done according to the standard conversion rules for the underlying
272 * fundamental arithmetic types.
273 */
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"
301
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
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
810private:
811 VectorType d;
812};
813
832template <typename V, typename T, typename Abi>
833Vc_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
Vc::fixed_size_simd< int, VectorTraits< T, Abi >::size()> IndexType
Definition vector.h:177
void setZeroInverted(MaskType mask)
static constexpr size_t size()
Definition vector.h:142
Vector operator~() const
EntryType sum(MaskType mask) const
Vector copySign(Vector reference) const
Vector apply(F &&f, MaskType mask) const
void fill(EntryType(&f)())
EntryType operator[](size_t index) const noexcept
void setQnan(MaskType mask)
void call(F &&f, MaskType mask) const
static Vector IndexesFromZero()
Vector(VectorSpecialInitializerZero)
EntryType sum() const
Vector operator+() const
Vector operator-() const
Vector shifted(int amount) const
EntryType product() const
void call(F &&f) const
void callWithValuesSorted(F &&f)
Vector(EntryType a)
void setZero(MaskType mask)
Vector apply(F &&f) const
void fill(EntryType(&f)(IndexT))
EntryType max() const
Vector(VectorSpecialInitializerIndexesFromZero)
Vector rotated(int amount) const
static Vector generate(G gen)
static Vector Random()
Vector(Vector< U, abi > x, enable_if< Traits::is_implicit_cast_allowed< U, T >::value >=nullarg)
EntryType min() const
Vector sorted() const
Common::WriteMaskedVector< Vector, MaskType > operator()(MaskType mask)
Vector reversed() const
Vector(VectorSpecialInitializerOne)
Vector partialSum() const
reference operator[](size_t index) noexcept
EntryType min(MaskType mask) const
EntryType product(MaskType mask) const
typename VectorTraits< T, Abi >::EntryType EntryType
Definition vector.h:154
MaskType operator!() const
EntryType max(MaskType mask) const
static Vector Zero()
Vector shifted(int amount, Vector shiftIn) const
fixed_size_simd< T, N > copysign(const SimdArray< T, N, V, M > &x, const SimdArray< T, N, V, M > &y)
Applies the std::copysign function component-wise and concurrently.
Definition simdarray.h:1811
fixed_size_simd_mask< T, N > isnegative(const SimdArray< T, N, V, M > &x)
Applies the std::isnegative function component-wise and concurrently.
Definition simdarray.h:1826
fixed_size_simd< T, N > exponent(const SimdArray< T, N, V, M > &x)
Applies the std::exponent function component-wise and concurrently.
Definition simdarray.h:1814
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