Vc  1.3.80-dev
SIMD Vector Classes for C++
algorithms.h
1 /* This file is part of the Vc library. {{{
2 Copyright © 2013-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_ALGORITHMS_H_
29 #define VC_COMMON_ALGORITHMS_H_
30 
31 #include "simdize.h"
32 
33 namespace Vc_VERSIONED_NAMESPACE
34 {
44 template<typename Mask> constexpr bool all_of(const Mask &m) { return m.isFull(); }
48 constexpr bool all_of(bool b) { return b; }
49 
53 template<typename Mask> constexpr bool any_of(const Mask &m) { return m.isNotEmpty(); }
57 constexpr bool any_of(bool b) { return b; }
58 
62 template<typename Mask> constexpr bool none_of(const Mask &m) { return m.isEmpty(); }
66 constexpr bool none_of(bool b) { return !b; }
67 
72 template<typename Mask> constexpr bool some_of(const Mask &m) { return m.isMix(); }
76 constexpr bool some_of(bool) { return false; }
78 
79 #ifdef DOXYGEN
80 
107 template <class InputIt, class UnaryFunction>
108 UnaryFunction simd_for_each(InputIt first, InputIt last, UnaryFunction f);
109 #else
110 template <class InputIt, class UnaryFunction,
111  class ValueType = typename std::iterator_traits<InputIt>::value_type>
112 inline enable_if<
113  Traits::is_functor_argument_immutable<UnaryFunction, simdize<ValueType>>::value,
114  UnaryFunction>
115 simd_for_each(InputIt first, InputIt last, UnaryFunction f)
116 {
117  typedef simdize<ValueType> V;
118  typedef simdize<ValueType, 1> V1;
119  const auto lastV = last - V::Size + 1;
120  for (; first < lastV; first += V::Size) {
121  V tmp;
122  load_interleaved(tmp, std::addressof(*first));
123  f(tmp);
124  }
125  for (; first != last; ++first) {
126  V1 tmp;
127  load_interleaved(tmp, std::addressof(*first));
128  f(tmp);
129  }
130  return std::move(f);
131 }
132 
133 template <typename InputIt, typename UnaryFunction,
134  class ValueType = typename std::iterator_traits<InputIt>::value_type>
135 inline enable_if<
136  !Traits::is_functor_argument_immutable<UnaryFunction, simdize<ValueType>>::value,
137  UnaryFunction>
138 simd_for_each(InputIt first, InputIt last, UnaryFunction f)
139 {
140  typedef simdize<ValueType> V;
141  typedef simdize<ValueType, 1> V1;
142  const auto lastV = last - V::size() + 1;
143  for (; first < lastV; first += V::size()) {
144  V tmp;
145  load_interleaved(tmp, std::addressof(*first));
146  f(tmp);
147  store_interleaved(tmp, std::addressof(*first));
148  }
149  for (; first != last; ++first) {
150  V1 tmp;
151  load_interleaved(tmp, std::addressof(*first));
152  f(tmp);
153  store_interleaved(tmp, std::addressof(*first));
154  }
155  return std::move(f);
156 }
157 #endif
158 
160 template <typename InputIt, typename UnaryFunction,
161  class ValueType = typename std::iterator_traits<InputIt>::value_type>
162 inline enable_if<
163  Traits::is_functor_argument_immutable<UnaryFunction, simdize<ValueType>>::value,
164  UnaryFunction>
165 simd_for_each_n(InputIt first, std::size_t count, UnaryFunction f)
166 {
167  typename std::make_signed<size_t>::type len = count;
168  typedef simdize<ValueType> V;
169  typedef simdize<ValueType, 1> V1;
170  for (; len >= int(V::size()); len -= V::Size, first += V::Size) {
171  V tmp;
172  load_interleaved(tmp, std::addressof(*first));
173  f(tmp);
174  }
175  for (; len != 0; --len, ++first) {
176  V1 tmp;
177  load_interleaved(tmp, std::addressof(*first));
178  f(tmp);
179  }
180  return std::move(f);
181 }
182 
183 template <typename InputIt, typename UnaryFunction,
184  class ValueType = typename std::iterator_traits<InputIt>::value_type>
185 inline enable_if<
186  !Traits::is_functor_argument_immutable<UnaryFunction, simdize<ValueType>>::value,
187  UnaryFunction>
188 simd_for_each_n(InputIt first, std::size_t count, UnaryFunction f)
189 {
190  typename std::make_signed<size_t>::type len = count;
191  typedef simdize<ValueType> V;
192  typedef simdize<ValueType, 1> V1;
193  for (; len >= int(V::size()); len -= V::Size, first += V::Size) {
194  V tmp;
195  load_interleaved(tmp, std::addressof(*first));
196  f(tmp);
197  store_interleaved(tmp, std::addressof(*first));
198  }
199  for (; len != 0; --len, ++first) {
200  V1 tmp;
201  load_interleaved(tmp, std::addressof(*first));
202  f(tmp);
203  store_interleaved(tmp, std::addressof(*first));
204  }
205  return std::move(f);
206 }
207 
208 } // namespace Vc
209 
210 #endif // VC_COMMON_ALGORITHMS_H_
constexpr bool any_of(bool b)
Returns b.
Definition: algorithms.h:57
SimdizeDetail::simdize< T, N, MT > simdize
Definition: simdize.h:1878
UnaryFunction simd_for_each(InputIt first, InputIt last, UnaryFunction f)
Vc variant of the std::for_each algorithm.
bool isFull() const
Returns a logical AND of all components.
constexpr bool none_of(bool b)
Returns !b.
Definition: algorithms.h:66
constexpr bool all_of(bool b)
Returns b.
Definition: algorithms.h:48
constexpr bool some_of(bool)
Returns false.
Definition: algorithms.h:76
The main SIMD mask class.
Definition: fwddecl.h:52
bool isNotEmpty() const
Returns a logical OR of all components.
bool isEmpty() const
Returns true if components are false, false otherwise.
bool isMix() const
Returns !isFull() && !isEmpty().