Vc  1.3.2-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 "macros.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 template <typename InputIt, typename UnaryFunction>
80 inline enable_if<
81  std::is_arithmetic<typename std::iterator_traits<InputIt>::value_type>::value &&
82  Traits::is_functor_argument_immutable<
83  UnaryFunction,
84  Vector<typename std::iterator_traits<InputIt>::value_type>>::value,
85  UnaryFunction>
86 simd_for_each(InputIt first, InputIt last, UnaryFunction f)
87 {
88  typedef Vector<typename std::iterator_traits<InputIt>::value_type> V;
89  typedef Scalar::Vector<typename std::iterator_traits<InputIt>::value_type> V1;
90  for (; reinterpret_cast<std::uintptr_t>(std::addressof(*first)) &
91  (V::MemoryAlignment - 1) &&
92  first != last;
93  ++first) {
94  f(V1(std::addressof(*first), Vc::Aligned));
95  }
96  const auto lastV = last - V::Size + 1;
97  for (; first < lastV; first += V::Size) {
98  f(V(std::addressof(*first), Vc::Aligned));
99  }
100  for (; first != last; ++first) {
101  f(V1(std::addressof(*first), Vc::Aligned));
102  }
103  return std::move(f);
104 }
105 
106 template <typename InputIt, typename UnaryFunction>
107 inline enable_if<
108  std::is_arithmetic<typename std::iterator_traits<InputIt>::value_type>::value &&
109  !Traits::is_functor_argument_immutable<
110  UnaryFunction,
111  Vector<typename std::iterator_traits<InputIt>::value_type>>::value,
112  UnaryFunction>
113 simd_for_each(InputIt first, InputIt last, UnaryFunction f)
114 {
115  typedef Vector<typename std::iterator_traits<InputIt>::value_type> V;
116  typedef Scalar::Vector<typename std::iterator_traits<InputIt>::value_type> V1;
117  for (; reinterpret_cast<std::uintptr_t>(std::addressof(*first)) &
118  (V::MemoryAlignment - 1) &&
119  first != last;
120  ++first) {
121  V1 tmp(std::addressof(*first), Vc::Aligned);
122  f(tmp);
123  tmp.store(std::addressof(*first), Vc::Aligned);
124  }
125  const auto lastV = last - V::Size + 1;
126  for (; first < lastV; first += V::Size) {
127  V tmp(std::addressof(*first), Vc::Aligned);
128  f(tmp);
129  tmp.store(std::addressof(*first), Vc::Aligned);
130  }
131  for (; first != last; ++first) {
132  V1 tmp(std::addressof(*first), Vc::Aligned);
133  f(tmp);
134  tmp.store(std::addressof(*first), Vc::Aligned);
135  }
136  return std::move(f);
137 }
138 
139 template <typename InputIt, typename UnaryFunction>
140 inline enable_if<
141  !std::is_arithmetic<typename std::iterator_traits<InputIt>::value_type>::value,
142  UnaryFunction>
143 simd_for_each(InputIt first, InputIt last, UnaryFunction f)
144 {
145  return std::for_each(first, last, std::move(f));
146 }
147 
149 template <typename InputIt, typename UnaryFunction>
150 inline enable_if<
151  std::is_arithmetic<typename std::iterator_traits<InputIt>::value_type>::value &&
152  Traits::is_functor_argument_immutable<
153  UnaryFunction,
154  Vector<typename std::iterator_traits<InputIt>::value_type>>::value,
155  UnaryFunction>
156 simd_for_each_n(InputIt first, std::size_t count, UnaryFunction f)
157 {
158  typename std::make_signed<size_t>::type len = count;
159  typedef Vector<typename std::iterator_traits<InputIt>::value_type> V;
160  typedef Scalar::Vector<typename std::iterator_traits<InputIt>::value_type> V1;
161  for (; reinterpret_cast<std::uintptr_t>(std::addressof(*first)) &
162  (V::MemoryAlignment - 1) &&
163  len != 0;
164  --len, ++first) {
165  f(V1(std::addressof(*first), Vc::Aligned));
166  }
167  for (; len >= int(V::Size); len -= V::Size, first += V::Size) {
168  f(V(std::addressof(*first), Vc::Aligned));
169  }
170  for (; len != 0; --len, ++first) {
171  f(V1(std::addressof(*first), Vc::Aligned));
172  }
173  return std::move(f);
174 }
175 
176 template <typename InputIt, typename UnaryFunction>
177 inline enable_if<
178  std::is_arithmetic<typename std::iterator_traits<InputIt>::value_type>::value &&
179  !Traits::is_functor_argument_immutable<
180  UnaryFunction,
181  Vector<typename std::iterator_traits<InputIt>::value_type>>::value,
182  UnaryFunction>
183 simd_for_each_n(InputIt first, std::size_t count, UnaryFunction f)
184 {
185  typename std::make_signed<size_t>::type len = count;
186  typedef Vector<typename std::iterator_traits<InputIt>::value_type> V;
187  typedef Scalar::Vector<typename std::iterator_traits<InputIt>::value_type> V1;
188  for (; reinterpret_cast<std::uintptr_t>(std::addressof(*first)) &
189  (V::MemoryAlignment - 1) &&
190  len != 0;
191  --len, ++first) {
192  V1 tmp(std::addressof(*first), Vc::Aligned);
193  f(tmp);
194  tmp.store(std::addressof(*first), Vc::Aligned);
195  }
196  for (; len >= int(V::Size); len -= V::Size, first += V::Size) {
197  V tmp(std::addressof(*first), Vc::Aligned);
198  f(tmp);
199  tmp.store(std::addressof(*first), Vc::Aligned);
200  }
201  for (; len != 0; --len, ++first) {
202  V1 tmp(std::addressof(*first), Vc::Aligned);
203  f(tmp);
204  tmp.store(std::addressof(*first), Vc::Aligned);
205  }
206  return std::move(f);
207 }
208 
209 #ifdef Vc_CXX17
210 template <typename InputIt, typename UnaryFunction>
211 inline enable_if<
212  !std::is_arithmetic<typename std::iterator_traits<InputIt>::value_type>::value,
213  UnaryFunction>
214 simd_for_each_n(InputIt first, std::size_t count, UnaryFunction f)
215 {
216  return std::for_each_n(first, count, std::move(f));
217 }
218 #endif
219 
220 } // namespace Vc
221 
222 #endif // VC_COMMON_ALGORITHMS_H_
constexpr bool any_of(bool b)
Returns b.
Definition: algorithms.h:57
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 AlignedTag Aligned
Use this object for a flags parameter to request aligned loads and stores.
constexpr bool some_of(bool)
Returns false.
Definition: algorithms.h:76
The main SIMD mask class.
Definition: mask.h:41
bool isNotEmpty() const
Returns a logical OR of all components.
constexpr std::size_t MemoryAlignment
Specifies the most conservative memory alignment necessary for aligned loads and stores of Vector typ...
Definition: vector.h:218
bool isEmpty() const
Returns true if components are false, false otherwise.
bool isMix() const
Returns !isFull() && !isEmpty().