Vc  1.4.2
SIMD Vector Classes for C++
detail.h
1 /* This file is part of the Vc library. {{{
2 Copyright © 2018 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_DETAIL_H_
29 #define VC_COMMON_DETAIL_H_
30 
31 #include <vector>
32 
33 #include "macros.h"
34 
35 namespace Vc_VERSIONED_NAMESPACE
36 {
37 namespace Common
38 {
39 // convertIndexVector {{{
40 // if the argument is a Vector<T> already we definitely want to keep it that way
41 template <typename IV>
42 Vc_INTRINSIC enable_if<(Traits::is_simd_vector<IV>::value &&
43  sizeof(typename IV::EntryType) >= sizeof(int)),
44  const IV &>
45 convertIndexVector(const IV &indexVector)
46 {
47  return indexVector;
48 }
49 
50 // but if the scalar (integral) type is smaller than int we convert it up to int. Otherwise it's
51 // very likely that the calculations we have to perform will overflow.
52 template <typename IV>
53 Vc_INTRINSIC enable_if<(Traits::is_simd_vector<IV>::value &&
54  sizeof(typename IV::EntryType) < sizeof(int)),
55  fixed_size_simd<int, IV::Size>>
56 convertIndexVector(const IV &indexVector)
57 {
58  return static_cast<fixed_size_simd<int, IV::Size>>(indexVector);
59 }
60 
61 // helper for promoting int types to int or higher
62 template <class T> using promoted_type = decltype(std::declval<T>() + 1);
63 
64 // std::array, Vc::array, and C-array are fixed size and can therefore be converted to a
65 // fixed_size_simd of the same size
66 template <typename T, std::size_t N>
67 Vc_INTRINSIC enable_if<std::is_integral<T>::value, fixed_size_simd<promoted_type<T>, N>>
68 convertIndexVector(const std::array<T, N> &indexVector)
69 {
70  return fixed_size_simd<promoted_type<T>, N>{std::addressof(indexVector[0]),
72 }
73 template <typename T, std::size_t N>
74 Vc_INTRINSIC enable_if<std::is_integral<T>::value, fixed_size_simd<promoted_type<T>, N>>
75 convertIndexVector(const Vc::array<T, N> &indexVector)
76 {
77  return fixed_size_simd<promoted_type<T>, N>{std::addressof(indexVector[0]),
79 }
80 template <typename T, std::size_t N>
81 Vc_INTRINSIC enable_if<std::is_integral<T>::value, fixed_size_simd<promoted_type<T>, N>>
82 convertIndexVector(const T (&indexVector)[N])
83 {
84  return fixed_size_simd<promoted_type<T>, N>{std::addressof(indexVector[0]),
86 }
87 
88 // a plain pointer won't work. Because we need some information on the number of values in
89 // the index argument
90 #ifndef Vc_MSVC
91 // MSVC treats the function as usable in SFINAE context if it is deleted. If it's not declared we
92 // seem to get what we wanted (except for bad diagnostics)
93 template <class T>
94 enable_if<std::is_pointer<T>::value, void> convertIndexVector(T indexVector) = delete;
95 #endif
96 
97 // an initializer_list works, but is runtime-sized (before C++14, at least) so we have to
98 // fall back to std::vector
99 template <typename T>
100 Vc_INTRINSIC std::vector<promoted_type<T>> convertIndexVector(
101  const std::initializer_list<T> &indexVector)
102 {
103  return {begin(indexVector), end(indexVector)};
104 }
105 
106 // a std::vector cannot be converted to anything better
107 template <typename T>
108 Vc_INTRINSIC
109  enable_if<(std::is_integral<T>::value && sizeof(T) >= sizeof(int)), std::vector<T>>
110  convertIndexVector(const std::vector<T> &indexVector)
111 {
112  return indexVector;
113 }
114 template <typename T>
115 Vc_INTRINSIC enable_if<(std::is_integral<T>::value && sizeof(T) < sizeof(int)),
116  std::vector<promoted_type<T>>>
117 convertIndexVector(const std::vector<T> &indexVector)
118 {
119  return {std::begin(indexVector), std::end(indexVector)};
120 }
121 
122 template <class T,
123  class = enable_if<
124  (!std::is_pointer<T>::value && !Traits::is_simd_vector<T>::value &&
125  !std::is_lvalue_reference<decltype(std::declval<const T &>()[0])>::value)>>
126 Vc_INTRINSIC const T &convertIndexVector(const T &i)
127 {
128  return i;
129 }
130 
131 // }}}
132 } // namespace Common
133 } // namespace Vc_VERSIONED_NAMESPACE
134 
135 #endif // VC_COMMON_DETAIL_H_
136 
137 // vim: foldmethod=marker
Vc::Unaligned
constexpr UnalignedTag Unaligned
Use this object for a flags parameter to request unaligned loads and stores.
Definition: loadstoreflags.h:191
Vc::array
Definition: array:86
Vc::vector
Common::AdaptSubscriptOperator< std::vector< T, Allocator > > vector
Definition: vector:55