Vc  1.3.2-dev
SIMD Vector Classes for C++
writemaskedvector.h
1 /* This file is part of the Vc library. {{{
2 Copyright © 2014-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_WRITEMASKEDVECTOR_H_
29 #define VC_COMMON_WRITEMASKEDVECTOR_H_
30 
31 #include <utility>
32 #include "macros.h"
33 
34 namespace Vc_VERSIONED_NAMESPACE
35 {
36 namespace Common
37 {
38 
39 template <typename V, typename M = typename V::Mask> class WriteMaskedVector
40 {
41  static_assert(
42  V::Size == M::Size,
43  "incorrect use of Vc::Common::WriteMaskedVector<V, M>. V and M must have the same «Size».");
44 
45 public:
46  typedef M Mask;
47  static constexpr size_t Size = V::Size;
48 
49  Vc_FREE_STORE_OPERATORS_ALIGNED(alignof(Mask));
50 
51  // implicit (allows {vec, mask} in places where WriteMaskedVector is expected)
52  Vc_INTRINSIC WriteMaskedVector(V &v, const Mask &k) : mask(k), vec(v)
53  {
54  }
55 
56  // prefix
57  Vc_INTRINSIC V &operator++()
58  {
59  V one = V::One();
60  one.setZeroInverted(mask);
61  return vec += one;
62  }
63  Vc_INTRINSIC V &operator--()
64  {
65  V one = V::One();
66  one.setZeroInverted(mask);
67  return vec -= one;
68  }
69 
70  // postfix
71  Vc_INTRINSIC V operator++(int)
72  {
73  V ret(vec);
74  operator++();
75  return ret;
76  }
77  Vc_INTRINSIC V operator--(int)
78  {
79  V ret(vec);
80  operator--();
81  return ret;
82  }
83 
84 #define Vc_OPERATOR_(op) \
85  template <typename U> Vc_ALWAYS_INLINE void operator op##=(U &&x) \
86  { \
87  operator=(static_cast<V>(vec op std::forward<U>(x))); \
88  }
89  Vc_ALL_BINARY(Vc_OPERATOR_);
90  Vc_ALL_ARITHMETICS(Vc_OPERATOR_);
91  Vc_ALL_SHIFTS(Vc_OPERATOR_);
92 #undef Vc_OPERATOR_
93 
94  Vc_ALWAYS_INLINE void operator=(const V &x)
95  {
96  vec.assign(x, mask);
97  }
98 
99  template <typename T, typename I, typename S>
100  Vc_ALWAYS_INLINE void operator=(SubscriptOperation<T, I, S, true> &&x)
101  {
102  vec.gather(x.gatherArguments(), mask);
103  }
104 
105  template <typename F> Vc_INTRINSIC void call(const F &f) const
106  {
107  return vec.call(f, mask);
108  }
109  template <typename F> Vc_INTRINSIC V apply(const F &f) const
110  {
111  return vec.apply(f, mask);
112  }
113  template <typename F> Vc_INTRINSIC void call(F &&f) const
114  {
115  return vec.call(std::forward<F>(f), mask);
116  }
117  template <typename F> Vc_INTRINSIC V apply(F &&f) const
118  {
119  return vec.apply(std::forward<F>(f), mask);
120  }
121 
122 private:
123 #ifdef Vc_ICC
124  // If ICC gets a by-value copy of Mask here, it'll generate a lot of superfluous
125  // stack-register copies.
126  const Mask &mask;
127 #else
128  // If Clang gets a const-ref Mask here, it'll miscompile some of the masked assignment
129  // statements.
130  const Mask mask;
131 #endif
132  V &vec;
133 };
134 } // namespace Common
135 } // namespace Vc
136 
137 #endif // VC_COMMON_WRITEMASKEDVECTOR_H_
constexpr VectorSpecialInitializerOne One
The special object Vc::One can be used to construct Vector and Mask objects initialized to one/true...
Definition: types.h:89