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 © 2018 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_DETAIL_H_
0029 #define VC_COMMON_DETAIL_H_
0030 
0031 #include <vector>
0032 
0033 #include "macros.h"
0034 
0035 namespace Vc_VERSIONED_NAMESPACE
0036 {
0037 namespace Common
0038 {
0039 // convertIndexVector {{{
0040 // if the argument is a Vector<T> already we definitely want to keep it that way
0041 template <typename IV>
0042 Vc_INTRINSIC enable_if<(Traits::is_simd_vector<IV>::value &&
0043                         sizeof(typename IV::EntryType) >= sizeof(int)),
0044                        const IV &>
0045 convertIndexVector(const IV &indexVector)
0046 {
0047     return indexVector;
0048 }
0049 
0050 // but if the scalar (integral) type is smaller than int we convert it up to int. Otherwise it's
0051 // very likely that the calculations we have to perform will overflow.
0052 template <typename IV>
0053 Vc_INTRINSIC enable_if<(Traits::is_simd_vector<IV>::value &&
0054                         sizeof(typename IV::EntryType) < sizeof(int)),
0055                        fixed_size_simd<int, IV::Size>>
0056 convertIndexVector(const IV &indexVector)
0057 {
0058     return static_cast<fixed_size_simd<int, IV::Size>>(indexVector);
0059 }
0060 
0061 // helper for promoting int types to int or higher
0062 template <class T> using promoted_type = decltype(std::declval<T>() + 1);
0063 
0064 // std::array, Vc::array, and C-array are fixed size and can therefore be converted to a
0065 // fixed_size_simd of the same size
0066 template <typename T, std::size_t N>
0067 Vc_INTRINSIC enable_if<std::is_integral<T>::value, fixed_size_simd<promoted_type<T>, N>>
0068 convertIndexVector(const std::array<T, N> &indexVector)
0069 {
0070     return fixed_size_simd<promoted_type<T>, N>{std::addressof(indexVector[0]),
0071                                                 Vc::Unaligned};
0072 }
0073 template <typename T, std::size_t N>
0074 Vc_INTRINSIC enable_if<std::is_integral<T>::value, fixed_size_simd<promoted_type<T>, N>>
0075 convertIndexVector(const Vc::array<T, N> &indexVector)
0076 {
0077     return fixed_size_simd<promoted_type<T>, N>{std::addressof(indexVector[0]),
0078                                                 Vc::Unaligned};
0079 }
0080 template <typename T, std::size_t N>
0081 Vc_INTRINSIC enable_if<std::is_integral<T>::value, fixed_size_simd<promoted_type<T>, N>>
0082 convertIndexVector(const T (&indexVector)[N])
0083 {
0084     return fixed_size_simd<promoted_type<T>, N>{std::addressof(indexVector[0]),
0085                                                 Vc::Unaligned};
0086 }
0087 
0088 // a plain pointer won't work. Because we need some information on the number of values in
0089 // the index argument
0090 #ifndef Vc_MSVC
0091 // MSVC treats the function as usable in SFINAE context if it is deleted. If it's not declared we
0092 // seem to get what we wanted (except for bad diagnostics)
0093 template <class T>
0094 enable_if<std::is_pointer<T>::value, void> convertIndexVector(T indexVector) = delete;
0095 #endif
0096 
0097 // an initializer_list works, but is runtime-sized (before C++14, at least) so we have to
0098 // fall back to std::vector
0099 template <typename T>
0100 Vc_INTRINSIC std::vector<promoted_type<T>> convertIndexVector(
0101     const std::initializer_list<T> &indexVector)
0102 {
0103     return {begin(indexVector), end(indexVector)};
0104 }
0105 
0106 // a std::vector cannot be converted to anything better
0107 template <typename T>
0108 Vc_INTRINSIC
0109     enable_if<(std::is_integral<T>::value && sizeof(T) >= sizeof(int)), std::vector<T>>
0110     convertIndexVector(const std::vector<T> &indexVector)
0111 {
0112     return indexVector;
0113 }
0114 template <typename T>
0115 Vc_INTRINSIC enable_if<(std::is_integral<T>::value && sizeof(T) < sizeof(int)),
0116                        std::vector<promoted_type<T>>>
0117 convertIndexVector(const std::vector<T> &indexVector)
0118 {
0119     return {std::begin(indexVector), std::end(indexVector)};
0120 }
0121 
0122 template <class T,
0123           class = enable_if<
0124               (!std::is_pointer<T>::value && !Traits::is_simd_vector<T>::value &&
0125                !std::is_lvalue_reference<decltype(std::declval<const T &>()[0])>::value)>>
0126 Vc_INTRINSIC const T &convertIndexVector(const T &i)
0127 {
0128     return i;
0129 }
0130 
0131 // }}}
0132 }  // namespace Common
0133 }  // namespace Vc_VERSIONED_NAMESPACE
0134 
0135 #endif  // VC_COMMON_DETAIL_H_
0136 
0137 // vim: foldmethod=marker