Vc  1.0.0-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 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7  * Redistributions of source code must retain the above copyright
8  notice, this list of conditions and the following disclaimer.
9  * Redistributions in binary form must reproduce the above copyright
10  notice, this list of conditions and the following disclaimer in the
11  documentation and/or other materials provided with the distribution.
12  * Neither the names of contributing organizations nor the
13  names of its contributors may be used to endorse or promote products
14  derived from this software without specific prior written permission.
15 
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 
27 }}}*/
28 
29 #ifndef VC_COMMON_WRITEMASKEDVECTOR_H_
30 #define VC_COMMON_WRITEMASKEDVECTOR_H_
31 
32 #include <utility>
33 #include "macros.h"
34 
35 namespace Vc_VERSIONED_NAMESPACE
36 {
37 namespace Common
38 {
39 
40 template <typename V, typename M = typename V::Mask> class WriteMaskedVector
41 {
42  static_assert(
43  V::Size == M::Size,
44  "incorrect use of Vc::Common::WriteMaskedVector<V, M>. V and M must have the same «Size».");
45 
46 public:
47  typedef M Mask;
48  static constexpr size_t Size = V::Size;
49 
50  Vc_FREE_STORE_OPERATORS_ALIGNED(alignof(Mask))
51 
52  // implicit (allows {vec, mask} in places where WriteMaskedVector is expected)
53  Vc_INTRINSIC WriteMaskedVector(V *v, const Mask &k) : mask(k), vec(v)
54  {
55  }
56 
57  // prefix
58  Vc_INTRINSIC V &operator++()
59  {
60  V one = V::One();
61  one.setZeroInverted(mask);
62  return *vec += one;
63  }
64  Vc_INTRINSIC V &operator--()
65  {
66  V one = V::One();
67  one.setZeroInverted(mask);
68  return *vec -= one;
69  }
70 
71  // postfix
72  Vc_INTRINSIC V operator++(int)
73  {
74  V ret(*vec);
75  operator++();
76  return ret;
77  }
78  Vc_INTRINSIC V operator--(int)
79  {
80  V ret(*vec);
81  operator--();
82  return ret;
83  }
84 
85 #define Vc_OPERATOR__(op) \
86  template <typename U> Vc_ALWAYS_INLINE void operator op##=(U &&x) \
87  { \
88  operator=(static_cast<V>(*vec op std::forward<U>(x))); \
89  }
90  Vc_ALL_BINARY(Vc_OPERATOR__)
91  Vc_ALL_ARITHMETICS(Vc_OPERATOR__)
92  Vc_ALL_SHIFTS(Vc_OPERATOR__)
93 #undef Vc_OPERATOR__
94 
95  Vc_ALWAYS_INLINE void operator=(const V &x)
96  {
97  vec->assign(x, mask);
98  }
99 
100  template <typename T, typename I, typename S>
101  Vc_ALWAYS_INLINE void operator=(SubscriptOperation<T, I, S, true> &&x)
102  {
103  vec->gather(x.gatherArguments(), mask);
104  }
105 
106  template <typename F> Vc_INTRINSIC void call(const F &f) const
107  {
108  return vec->call(f, mask);
109  }
110  template <typename F> Vc_INTRINSIC V apply(const F &f) const
111  {
112  return vec->apply(f, mask);
113  }
114  template <typename F> Vc_INTRINSIC void call(F &&f) const
115  {
116  return vec->call(std::forward<F>(f), mask);
117  }
118  template <typename F> Vc_INTRINSIC V apply(F &&f) const
119  {
120  return vec->apply(std::forward<F>(f), mask);
121  }
122 
123 private:
124  Mask mask;
125  V *const vec;
126 };
127 } // namespace Common
128 } // namespace Vc
129 
130 #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:90