File indexing completed on 2025-01-30 10:25:46
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028 #ifndef VC_COMMON_INTERLEAVEDMEMORY_H_
0029 #define VC_COMMON_INTERLEAVEDMEMORY_H_
0030
0031 #include "macros.h"
0032
0033 namespace Vc_VERSIONED_NAMESPACE
0034 {
0035 namespace Common
0036 {
0037
0038
0039
0040 template<typename V, typename I, bool Readonly> struct InterleavedMemoryAccessBase
0041 {
0042
0043
0044
0045 typedef typename std::conditional<
0046 Readonly, typename std::add_const<typename V::EntryType>::type,
0047 typename V::EntryType>::type T;
0048 typedef typename V::AsArg VArg;
0049 typedef T Ta Vc_MAY_ALIAS;
0050 const I m_indexes;
0051 Ta *const m_data;
0052
0053 Vc_ALWAYS_INLINE InterleavedMemoryAccessBase(typename I::AsArg indexes, Ta *data)
0054 : m_indexes(indexes), m_data(data)
0055 {
0056 }
0057
0058
0059 template <typename... Vs> Vc_INTRINSIC void deinterleave(Vs &&... vs) const
0060 {
0061 Impl::deinterleave(m_data, m_indexes, std::forward<Vs>(vs)...);
0062 }
0063
0064 protected:
0065 using Impl = Vc::Detail::InterleaveImpl<V, V::Size, sizeof(V)>;
0066
0067 template <typename T, std::size_t... Indexes>
0068 Vc_INTRINSIC void callInterleave(T &&a, index_sequence<Indexes...>)
0069 {
0070 Impl::interleave(m_data, m_indexes, a[Indexes]...);
0071 }
0072 };
0073
0074
0075
0076
0077
0078 template <size_t StructSize, typename V, typename I = typename V::IndexType,
0079 bool Readonly>
0080 struct InterleavedMemoryReadAccess : public InterleavedMemoryAccessBase<V, I, Readonly>
0081 {
0082 typedef InterleavedMemoryAccessBase<V, I, Readonly> Base;
0083 typedef typename Base::Ta Ta;
0084
0085 Vc_ALWAYS_INLINE InterleavedMemoryReadAccess(Ta *data, typename I::AsArg indexes)
0086 : Base(StructSize == 1u
0087 ? indexes
0088 : StructSize == 2u
0089 ? indexes << 1
0090 : StructSize == 4u
0091 ? indexes << 2
0092 : StructSize == 8u
0093 ? indexes << 3
0094 : StructSize == 16u ? indexes << 4
0095 : indexes * I(int(StructSize)),
0096 data)
0097 {
0098 }
0099
0100 template <typename T, std::size_t... Indexes>
0101 Vc_ALWAYS_INLINE T deinterleave_unpack(index_sequence<Indexes...>) const
0102 {
0103 T r;
0104 Base::Impl::deinterleave(this->m_data, this->m_indexes, std::get<Indexes>(r)...);
0105 return r;
0106 }
0107
0108 template <typename T,
0109 typename = enable_if<(std::is_default_constructible<T>::value &&
0110 std::is_same<V, Traits::decay<decltype(std::get<0>(
0111 std::declval<T &>()))>>::value)>>
0112 Vc_ALWAYS_INLINE operator T() const
0113 {
0114 return deinterleave_unpack<T>(make_index_sequence<std::tuple_size<T>::value>());
0115 }
0116 };
0117
0118
0119 template<typename I> struct CheckIndexesUnique
0120 {
0121 #ifdef NDEBUG
0122 static Vc_INTRINSIC void test(const I &) {}
0123 #else
0124 static void test(const I &indexes)
0125 {
0126 const I test = indexes.sorted();
0127 Vc_ASSERT(I::Size == 1 || (test == test.rotated(1)).isEmpty())
0128 }
0129 #endif
0130 };
0131
0132 template<size_t S> struct CheckIndexesUnique<SuccessiveEntries<S> >
0133 {
0134 static Vc_INTRINSIC void test(const SuccessiveEntries<S> &) {}
0135 };
0136
0137
0138
0139
0140 template <size_t StructSize, typename V, typename I = typename V::IndexType>
0141 struct InterleavedMemoryAccess : public InterleavedMemoryReadAccess<StructSize, V, I, false>
0142 {
0143 typedef InterleavedMemoryAccessBase<V, I, false> Base;
0144 typedef typename Base::Ta Ta;
0145
0146 Vc_ALWAYS_INLINE InterleavedMemoryAccess(Ta *data, typename I::AsArg indexes)
0147 : InterleavedMemoryReadAccess<StructSize, V, I, false>(data, indexes)
0148 {
0149 CheckIndexesUnique<I>::test(indexes);
0150 }
0151
0152 template <int N> Vc_ALWAYS_INLINE void operator=(VectorReferenceArray<N, V> &&rhs)
0153 {
0154 static_assert(N <= StructSize,
0155 "You_are_trying_to_scatter_more_data_into_the_struct_than_it_has");
0156 this->callInterleave(std::move(rhs), make_index_sequence<N>());
0157 }
0158 template <int N> Vc_ALWAYS_INLINE void operator=(VectorReferenceArray<N, const V> &&rhs)
0159 {
0160 static_assert(N <= StructSize,
0161 "You_are_trying_to_scatter_more_data_into_the_struct_than_it_has");
0162 this->callInterleave(std::move(rhs), make_index_sequence<N>());
0163 }
0164 };
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177 template<typename S, typename V> class InterleavedMemoryWrapper
0178 {
0179 typedef typename std::conditional<std::is_const<S>::value,
0180 const typename V::EntryType,
0181 typename V::EntryType>::type T;
0182 typedef typename V::IndexType I;
0183 typedef typename V::AsArg VArg;
0184 typedef const I &IndexType;
0185 static constexpr std::size_t StructSize = sizeof(S) / sizeof(T);
0186 using ReadAccess = InterleavedMemoryReadAccess<StructSize, V>;
0187 using Access =
0188 typename std::conditional<std::is_const<T>::value, ReadAccess,
0189 InterleavedMemoryAccess<StructSize, V>>::type;
0190 using ReadSuccessiveEntries =
0191 InterleavedMemoryReadAccess<StructSize, V, SuccessiveEntries<StructSize>>;
0192 using AccessSuccessiveEntries = typename std::conditional<
0193 std::is_const<T>::value, ReadSuccessiveEntries,
0194 InterleavedMemoryAccess<StructSize, V, SuccessiveEntries<StructSize>>>::type;
0195 typedef T Ta Vc_MAY_ALIAS;
0196 Ta *const m_data;
0197
0198 static_assert(StructSize * sizeof(T) == sizeof(S),
0199 "InterleavedMemoryAccess_does_not_support_packed_structs");
0200
0201 public:
0202
0203
0204
0205
0206
0207 Vc_ALWAYS_INLINE InterleavedMemoryWrapper(S *s)
0208 : m_data(reinterpret_cast<Ta *>(s))
0209 {
0210 }
0211
0212
0213
0214
0215
0216
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243
0244
0245
0246
0247
0248
0249
0250
0251
0252
0253
0254
0255
0256
0257
0258
0259
0260
0261
0262
0263
0264 template <typename IT>
0265 Vc_ALWAYS_INLINE enable_if<!std::is_convertible<IT, size_t>::value &&
0266 std::is_convertible<IT, IndexType>::value &&
0267 !std::is_const<S>::value,
0268 Access>
0269 operator[](IT indexes)
0270 {
0271 return Access(m_data, indexes);
0272 }
0273
0274
0275 Vc_ALWAYS_INLINE ReadAccess operator[](IndexType indexes) const
0276 {
0277 return ReadAccess(m_data, indexes);
0278 }
0279
0280
0281 Vc_ALWAYS_INLINE ReadAccess gather(IndexType indexes) const { return operator[](indexes); }
0282
0283
0284
0285
0286
0287
0288
0289
0290
0291
0292
0293
0294
0295
0296
0297
0298
0299
0300
0301
0302
0303
0304
0305
0306
0307
0308
0309
0310
0311
0312
0313
0314
0315
0316 Vc_ALWAYS_INLINE ReadSuccessiveEntries operator[](size_t first) const
0317 {
0318 return ReadSuccessiveEntries(m_data, first);
0319 }
0320
0321 Vc_ALWAYS_INLINE AccessSuccessiveEntries operator[](size_t first)
0322 {
0323 return AccessSuccessiveEntries(m_data, first);
0324 }
0325
0326
0327 };
0328 }
0329
0330 using Common::InterleavedMemoryWrapper;
0331
0332
0333
0334
0335
0336
0337
0338
0339
0340
0341
0342
0343
0344 template <typename V, typename S>
0345 inline Common::InterleavedMemoryWrapper<S, V> make_interleave_wrapper(S *s)
0346 {
0347 return Common::InterleavedMemoryWrapper<S, V>(s);
0348 }
0349 }
0350
0351 #endif