Vc  1.1.0
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 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_ALGORITHMS_H_
30 #define VC_COMMON_ALGORITHMS_H_
31 
32 #include "macros.h"
33 
34 namespace Vc_VERSIONED_NAMESPACE
35 {
36 
46 template<typename Mask> constexpr bool all_of(const Mask &m) { return m.isFull(); }
50 constexpr bool all_of(bool b) { return b; }
51 
55 template<typename Mask> constexpr bool any_of(const Mask &m) { return m.isNotEmpty(); }
59 constexpr bool any_of(bool b) { return b; }
60 
64 template<typename Mask> constexpr bool none_of(const Mask &m) { return m.isEmpty(); }
68 constexpr bool none_of(bool b) { return !b; }
69 
74 template<typename Mask> constexpr bool some_of(const Mask &m) { return m.isMix(); }
78 constexpr bool some_of(bool) { return false; }
80 
81 template <typename InputIt, typename UnaryFunction>
82 inline enable_if<std::is_arithmetic<typename InputIt::value_type>::value &&
83  Traits::is_functor_argument_immutable<
84  UnaryFunction, Vector<typename InputIt::value_type>>::value,
85  UnaryFunction>
86 simd_for_each(InputIt first, InputIt last, UnaryFunction f)
87 {
88  typedef Vector<typename InputIt::value_type> V;
89  typedef Scalar::Vector<typename 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<std::is_arithmetic<typename InputIt::value_type>::value &&
108  !Traits::is_functor_argument_immutable<
109  UnaryFunction, Vector<typename InputIt::value_type>>::value,
110  UnaryFunction>
111 simd_for_each(InputIt first, InputIt last, UnaryFunction f)
112 {
113  typedef Vector<typename InputIt::value_type> V;
114  typedef Scalar::Vector<typename InputIt::value_type> V1;
115  for (; reinterpret_cast<std::uintptr_t>(std::addressof(*first)) &
116  (V::MemoryAlignment - 1) &&
117  first != last;
118  ++first) {
119  V1 tmp(std::addressof(*first), Vc::Aligned);
120  f(tmp);
121  tmp.store(std::addressof(*first), Vc::Aligned);
122  }
123  const auto lastV = last - (V::Size + 1);
124  for (; first < lastV; first += V::Size) {
125  V tmp(std::addressof(*first), Vc::Aligned);
126  f(tmp);
127  tmp.store(std::addressof(*first), Vc::Aligned);
128  }
129  for (; first != last; ++first) {
130  V1 tmp(std::addressof(*first), Vc::Aligned);
131  f(tmp);
132  tmp.store(std::addressof(*first), Vc::Aligned);
133  }
134  return std::move(f);
135 }
136 
137 template <typename InputIt, typename UnaryFunction>
138 inline enable_if<!std::is_arithmetic<typename InputIt::value_type>::value, UnaryFunction>
139 simd_for_each(InputIt first, InputIt last, UnaryFunction f)
140 {
141  return std::for_each(first, last, std::move(f));
142 }
143 
144 } // namespace Vc
145 
146 #endif // VC_COMMON_ALGORITHMS_H_
constexpr bool any_of(bool b)
Returns b.
Definition: algorithms.h:59
bool isFull() const
Returns a logical AND of all components.
constexpr bool none_of(bool b)
Returns !b.
Definition: algorithms.h:68
constexpr bool all_of(bool b)
Returns b.
Definition: algorithms.h:50
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:78
The main SIMD mask class.
Definition: mask.h:42
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:219
bool isEmpty() const
Returns true if components are false, false otherwise.
bool isMix() const
Returns !isFull() && !isEmpty().