Vc 1.4.5
SIMD Vector Classes for C++
 
Loading...
Searching...
No Matches
detail.h
1/* This file is part of the Vc library. {{{
2Copyright © 2018 Matthias Kretz <kretz@kde.org>
3
4Redistribution and use in source and binary forms, with or without
5modification, 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
15THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
19DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22ON 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
24SOFTWARE, 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
35namespace Vc_VERSIONED_NAMESPACE
36{
37namespace Common
38{
39// convertIndexVector {{{
40// if the argument is a Vector<T> already we definitely want to keep it that way
41template <typename IV>
42Vc_INTRINSIC enable_if<(Traits::is_simd_vector<IV>::value &&
43 sizeof(typename IV::EntryType) >= sizeof(int)),
44 const IV &>
45convertIndexVector(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.
52template <typename IV>
53Vc_INTRINSIC enable_if<(Traits::is_simd_vector<IV>::value &&
54 sizeof(typename IV::EntryType) < sizeof(int)),
55 fixed_size_simd<int, IV::Size>>
56convertIndexVector(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
62template <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
66template <typename T, std::size_t N>
67Vc_INTRINSIC enable_if<std::is_integral<T>::value, fixed_size_simd<promoted_type<T>, N>>
68convertIndexVector(const std::array<T, N> &indexVector)
69{
70 return fixed_size_simd<promoted_type<T>, N>{std::addressof(indexVector[0]),
72}
73template <typename T, std::size_t N>
74Vc_INTRINSIC enable_if<std::is_integral<T>::value, fixed_size_simd<promoted_type<T>, N>>
75convertIndexVector(const Vc::array<T, N> &indexVector)
76{
77 return fixed_size_simd<promoted_type<T>, N>{std::addressof(indexVector[0]),
79}
80template <typename T, std::size_t N>
81Vc_INTRINSIC enable_if<std::is_integral<T>::value, fixed_size_simd<promoted_type<T>, N>>
82convertIndexVector(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)
93template <class T>
94enable_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
99template <typename T>
100Vc_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
107template <typename T>
108Vc_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}
114template <typename T>
115Vc_INTRINSIC enable_if<(std::is_integral<T>::value && sizeof(T) < sizeof(int)),
116 std::vector<promoted_type<T>>>
117convertIndexVector(const std::vector<T> &indexVector)
118{
119 return {std::begin(indexVector), std::end(indexVector)};
120}
121
122template <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)>>
126Vc_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
constexpr UnalignedTag Unaligned
Use this object for a flags parameter to request unaligned loads and stores.
This is std::array with additional subscript operators supporting gather and scatter operations.
Definition types.h:188