Vc  1.3.2-dev
SIMD Vector Classes for C++
vectortuple.h
1 /* This file is part of the Vc library. {{{
2 Copyright © 2012-2015 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_VECTORTUPLE_H_
29 #define VC_COMMON_VECTORTUPLE_H_
30 
31 #include "transpose.h"
32 #include "macros.h"
33 
34 namespace Vc_VERSIONED_NAMESPACE
35 {
36 namespace Common
37 {
38 
39 template<size_t StructSize, typename V, typename I, bool Readonly = true> struct InterleavedMemoryReadAccess;
40 
41 template <int Length, typename V> class VectorReferenceArray
42 {
43  typedef typename V::EntryType T;
44  typedef V &Vc_RESTRICT Reference;
45  std::array<V * Vc_RESTRICT, Length> r;
46 
47  typedef make_index_sequence<Length> IndexSequence;
48 
49  template <typename VV, std::size_t... Indexes>
50  constexpr VectorReferenceArray<Length + 1, VV> appendOneReference(
51  VV &a, index_sequence<Indexes...>) const
52  {
53  return {*r[Indexes]..., a};
54  }
55 
56  template <typename A, std::size_t... Indexes>
57  Vc_INTRINSIC void callDeinterleave(const A &access, index_sequence<Indexes...>) const
58  {
59  access.deinterleave(*r[Indexes]...);
60  }
61 
62 public:
63  template <typename... Us, typename = enable_if<(sizeof...(Us) == Length)>>
64  constexpr VectorReferenceArray(Us &&... args)
65  : r{{std::addressof(std::forward<Us>(args))...}}
66  {
67  }
68 
69  template <typename VV, typename = enable_if<!std::is_const<V>::value &&
70  std::is_same<VV, V>::value>>
71  Vc_DEPRECATED("build the tuple with Vc::tie instead")
72  constexpr VectorReferenceArray<Length + 1, V> operator, (VV & a) const
73  {
74  return appendOneReference(a, IndexSequence());
75  }
76 
77  Vc_DEPRECATED("build the tuple with Vc::tie instead")
78  constexpr VectorReferenceArray<Length + 1, const V> operator, (const V &a) const
79  {
80  return appendOneReference(a, IndexSequence());
81  }
82 
83  template <size_t StructSize, typename I, bool RO>
84  Vc_ALWAYS_INLINE enable_if<(Length <= StructSize), void> operator=(
85  const InterleavedMemoryReadAccess<StructSize, V, I, RO> &access)
86  {
87  callDeinterleave(access, IndexSequence());
88  }
89 
90  template <size_t StructSize, typename I, bool RO>
91  enable_if<(Length > StructSize), void> operator=(
92  const InterleavedMemoryReadAccess<StructSize, V, I, RO> &access) =
93  delete; //("You are trying to extract more data from the struct than it has");
94 
95  template <typename... Inputs> void operator=(TransposeProxy<Inputs...> &&proxy)
96  {
97  transpose_impl(TransposeTag<Length, sizeof...(Inputs)>(), &r[0], proxy);
98  }
99 
100  template <typename T, typename IndexVector, typename Scale, bool Flag>
101  void operator=(const SubscriptOperation<T, IndexVector, Scale, Flag> &sub)
102  {
103  const auto &args = sub.gatherArguments();
104  //const IndexVector args.indexes;
105  //const T *const args.address;
106  Common::InterleavedMemoryReadAccess<1, V, Traits::decay<decltype(args.indexes)>>
107  deinterleaver(args.address, args.indexes);
108  callDeinterleave(deinterleaver, IndexSequence());
109  }
110 
111  Vc_ALWAYS_INLINE Reference operator[](std::size_t i) { return *r[i]; }
112 };
113 
114 } // namespace Common
115 
116 template <typename T, typename Abi>
117 Vc_DEPRECATED("build the tuple with Vc::tie instead")
118 constexpr Common::VectorReferenceArray<2, Vc::Vector<T, Abi>>
119 operator,(Vc::Vector<T, Abi> &a, Vc::Vector<T, Abi> &b)
120 {
121  return {a, b};
122 }
123 
124 template <typename T, typename Abi>
125 Vc_DEPRECATED("build the tuple with Vc::tie instead")
126 constexpr Common::VectorReferenceArray<2, const Vc::Vector<T, Abi>>
127 operator,(const Vc::Vector<T, Abi> &a, const Vc::Vector<T, Abi> &b)
128 {
129  return {a, b};
130 }
131 
132 template <typename V, typename... Vs>
133 constexpr Common::VectorReferenceArray<sizeof...(Vs) + 1,
134  typename std::remove_reference<V>::type>
135  tie(V &&a, Vs &&... b)
136 {
137  return {std::forward<V>(a), std::forward<Vs>(b)...};
138 }
139 
140 } // namespace Vc
141 
142 #endif // VC_COMMON_VECTORTUPLE_H_
Vector Classes Namespace.
Definition: cpuid.h:32