Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 10:25:45

0001 /*  This file is part of the Vc library. {{{
0002 Copyright © 2013-2015 Matthias Kretz <kretz@kde.org>
0003 
0004 Redistribution and use in source and binary forms, with or without
0005 modification, are permitted provided that the following conditions are met:
0006     * Redistributions of source code must retain the above copyright
0007       notice, this list of conditions and the following disclaimer.
0008     * Redistributions in binary form must reproduce the above copyright
0009       notice, this list of conditions and the following disclaimer in the
0010       documentation and/or other materials provided with the distribution.
0011     * Neither the names of contributing organizations nor the
0012       names of its contributors may be used to endorse or promote products
0013       derived from this software without specific prior written permission.
0014 
0015 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
0016 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
0017 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
0018 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
0019 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
0020 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
0021 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
0022 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0023 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
0024 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0025 
0026 }}}*/
0027 
0028 #ifndef VC_COMMON_ALGORITHMS_H_
0029 #define VC_COMMON_ALGORITHMS_H_
0030 
0031 #include "simdize.h"
0032 
0033 namespace Vc_VERSIONED_NAMESPACE
0034 {
0035 #ifdef DOXYGEN
0036 /**
0037  * \ingroup Utilities
0038  * \headerfile algorithms.h <Vc/Vc>
0039  *
0040  * Vc variant of the `std::for_each` algorithm.
0041  *
0042  * This algorithm calls \p f with one argument of type
0043  * `Vc::Vector<` *iterator value type* `, ` *unspecified* `>` as often as is needed to
0044  * iterate over the complete range from \p first to \p last.
0045  * It will try to use the best vector size (VectorAbi) to work on the largest chunks
0046  * possible.
0047  * To support aligned loads (and stores) and to support arbitrary range distances, the
0048  * algorithm may require the use of `Vc::VectorAbi` types that work on fewer elements in
0049  * parallel.
0050  *
0051  * The following example requires C++14 for generic lambdas. If you don't have generic
0052  * lambdas available you can use a "classic" functor type with a templated call operator
0053  * instead.
0054  *
0055  * \code
0056  * void scale(std::vector<double> &data, double factor) {
0057  *   Vc::simd_for_each(data.begin(), data.end(), [&](auto v) {
0058  *      v *= factor;
0059  *   });
0060  * }
0061  * \endcode
0062  */
0063 template <class InputIt, class UnaryFunction>
0064 UnaryFunction simd_for_each(InputIt first, InputIt last, UnaryFunction f);
0065 #else
0066 template <class InputIt, class UnaryFunction,
0067           class ValueType = typename std::iterator_traits<InputIt>::value_type>
0068 inline enable_if<
0069     Traits::is_functor_argument_immutable<UnaryFunction, simdize<ValueType>>::value,
0070     UnaryFunction>
0071 simd_for_each(InputIt first, InputIt last, UnaryFunction f)
0072 {
0073     typedef simdize<ValueType> V;
0074     typedef simdize<ValueType, 1> V1;
0075     const auto lastV = last - V::Size + 1;
0076     for (; first < lastV; first += V::Size) {
0077         V tmp;
0078         load_interleaved(tmp, std::addressof(*first));
0079         f(tmp);
0080     }
0081     for (; first != last; ++first) {
0082         V1 tmp;
0083         load_interleaved(tmp, std::addressof(*first));
0084         f(tmp);
0085     }
0086     return f;
0087 }
0088 
0089 template <typename InputIt, typename UnaryFunction,
0090           class ValueType = typename std::iterator_traits<InputIt>::value_type>
0091 inline enable_if<
0092         !Traits::is_functor_argument_immutable<UnaryFunction, simdize<ValueType>>::value,
0093     UnaryFunction>
0094 simd_for_each(InputIt first, InputIt last, UnaryFunction f)
0095 {
0096     typedef simdize<ValueType> V;
0097     typedef simdize<ValueType, 1> V1;
0098     const auto lastV = last - V::size() + 1;
0099     for (; first < lastV; first += V::size()) {
0100         V tmp;
0101         load_interleaved(tmp, std::addressof(*first));
0102         f(tmp);
0103         store_interleaved(tmp, std::addressof(*first));
0104     }
0105     for (; first != last; ++first) {
0106         V1 tmp;
0107         load_interleaved(tmp, std::addressof(*first));
0108         f(tmp);
0109         store_interleaved(tmp, std::addressof(*first));
0110     }
0111     return f;
0112 }
0113 #endif
0114 
0115 ///////////////////////////////////////////////////////////////////////////////
0116 template <typename InputIt, typename UnaryFunction,
0117           class ValueType = typename std::iterator_traits<InputIt>::value_type>
0118 inline enable_if<
0119     Traits::is_functor_argument_immutable<UnaryFunction, simdize<ValueType>>::value,
0120     UnaryFunction>
0121 simd_for_each_n(InputIt first, std::size_t count, UnaryFunction f)
0122 {
0123     typename std::make_signed<size_t>::type len = count;
0124     typedef simdize<ValueType> V;
0125     typedef simdize<ValueType, 1> V1;
0126     for (; len >= int(V::size()); len -= V::Size, first += V::Size) {
0127         V tmp;
0128         load_interleaved(tmp, std::addressof(*first));
0129         f(tmp);
0130     }
0131     for (; len != 0; --len, ++first) {
0132         V1 tmp;
0133         load_interleaved(tmp, std::addressof(*first));
0134         f(tmp);
0135     }
0136     return f;
0137 }
0138 
0139 template <typename InputIt, typename UnaryFunction,
0140           class ValueType = typename std::iterator_traits<InputIt>::value_type>
0141 inline enable_if<
0142     !Traits::is_functor_argument_immutable<UnaryFunction, simdize<ValueType>>::value,
0143     UnaryFunction>
0144 simd_for_each_n(InputIt first, std::size_t count, UnaryFunction f)
0145 {
0146     typename std::make_signed<size_t>::type len = count;
0147     typedef simdize<ValueType> V;
0148     typedef simdize<ValueType, 1> V1;
0149     for (; len >= int(V::size()); len -= V::Size, first += V::Size) {
0150         V tmp;
0151         load_interleaved(tmp, std::addressof(*first));
0152         f(tmp);
0153         store_interleaved(tmp, std::addressof(*first));
0154     }
0155     for (; len != 0; --len, ++first) {
0156         V1 tmp;
0157         load_interleaved(tmp, std::addressof(*first));
0158         f(tmp);
0159         store_interleaved(tmp, std::addressof(*first));
0160     }
0161     return f;
0162 }
0163 
0164 }  // namespace Vc
0165 
0166 #endif // VC_COMMON_ALGORITHMS_H_