dune-common 2.12-git
Loading...
Searching...
No Matches
stdsimd.hh
Go to the documentation of this file.
1// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
2// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
3#ifndef DUNE_COMMON_SIMD_STDSIMD_HH
4#define DUNE_COMMON_SIMD_STDSIMD_HH
5
11#include <dune-common-config.hh> // DUNE_HAVE_CXX_EXPERIMENTAL_SIMD
12
13#if DUNE_HAVE_CXX_EXPERIMENTAL_SIMD
14
15#include <cstddef>
16#include <type_traits>
17#include <utility>
18
19#include <experimental/simd>
20
23#include <dune/common/simd/defaults.hh> // for anyFalse()
27
76namespace Dune {
77 namespace Simd {
78
79 namespace StdSimdImpl {
80
81 namespace stdx = std::experimental;
82
84 template<class V>
85 struct IsMask : std::bool_constant<stdx::is_simd_mask_v<V> > {};
86
88 template<class V>
89 struct IsVector
90 : std::bool_constant<stdx::is_simd_v<V> || stdx::is_simd_mask_v<V> >
91 {};
92
94 template<class T>
96 : std::bool_constant<std::is_arithmetic<T>::value &&
97 !std::is_same<T, bool>::value> {};
98
100 template<class V, class = void>
101 struct VectorType { using type = V; };
102
103 template<class M>
104 struct VectorType<M, std::enable_if_t<IsMask<M>::value> >
105 {
106 using type = typename M::simd_type;
107 };
108
110
132 template<class V>
133 class Proxy : public Impl::ValueProxy<V>
134 {
135 using Base = Impl::ValueProxy<V>;
136 public:
137 using value_type = typename Base::value_type;
138
139 using Base::Base;
140 using Base::operator=;
141
142#define DUNE_SIMD_STDSIMD_BINARY(OP) \
143 template<class T, class Abi> \
144 friend auto operator OP(const stdx::simd<T, Abi> &l, Proxy&& r) \
145 -> decltype(l OP std::declval<value_type>()) \
146 { \
147 return l OP value_type(r); \
148 } \
149 template<class T, class Abi> \
150 auto operator OP(const stdx::simd<T, Abi> &r) && \
151 -> decltype(std::declval<value_type>() OP r) \
152 { \
153 return value_type(*this) OP r; \
154 }
155
172#undef DUNE_SIMD_STDSIMD_BINARY
173
174#define DUNE_SIMD_STDSIMD_ASSIGN(OP) \
175 template<class T, class Abi> \
176 friend auto operator OP(stdx::simd<T, Abi> &l, Proxy&& r) \
177 -> decltype(l OP std::declval<value_type>()) \
178 { \
179 return l OP value_type(r); \
180 }
181
192#undef DUNE_SIMD_STDSIMD_ASSIGN
193 };
194
195 } // namespace StdSimdImpl
196
197 namespace Overloads {
198
205
208 template<class V>
209 struct ScalarType<V,
210 std::enable_if_t<StdSimdImpl::IsVector<V>::value> >
211 {
212 using type = typename V::value_type;
213 };
214
216
223 template<class V>
224 struct RebindType<Simd::Scalar<V>, V,
225 std::enable_if_t<StdSimdImpl::IsVector<V>::value> >
226 {
227 using type = V;
228 };
229
231
237 template<class V>
238 struct RebindType<bool, V,
239 std::enable_if_t<StdSimdImpl::IsVector<V>::value &&
240 !StdSimdImpl::IsMask<V>::value> >
241 {
242 using type = typename V::mask_type;
243 };
244
246
252 template<class M>
253 struct RebindType<Scalar<typename M::simd_type>, M,
254 std::enable_if_t<StdSimdImpl::IsMask<M>::value> >
255 {
256 using type = typename M::simd_type;
257 };
258
260
270 template<class S, class V>
271 struct RebindType<S, V,
272 std::enable_if_t<
273 StdSimdImpl::IsVector<V>::value &&
274 StdSimdImpl::IsVectorizable<S>::value &&
275 !std::is_same<
276 S, Scalar<typename StdSimdImpl::VectorType<V>::type>
277 >::value> >
278 {
281 };
282
284
291 template<class S, class V>
292 struct RebindType<S, V,
293 std::enable_if_t<
294 StdSimdImpl::IsVector<V>::value &&
295 !StdSimdImpl::IsVectorizable<S>::value &&
296 !std::is_same<S, bool>::value &&
297 !std::is_same<S, Scalar<V> >::value> >
298 {
300 };
301
303
306 template<class V>
307 struct LaneCount<V,
308 std::enable_if_t<StdSimdImpl::IsVector<V>::value> >
309 : public index_constant<V::size()>
310 { };
311
313 template<class V>
315 std::size_t l, V &v)
316 {
317 return { l, v };
318 }
319
321 template<class V>
323 std::size_t l, const V &v)
324 {
325 return v[l];
326 }
327
329 template<class V,
332 std::size_t l, V &&v)
333 {
334 return std::as_const(v)[l];
335 }
336
338 template<class V>
340 const Mask<V> &mask, const V &ifTrue, const V &ifFalse)
341 {
342 V result = ifFalse;
343 std::experimental::where(mask, result) = ifTrue;
344 return result;
345 }
346
348 template<class V>
351 const V &v1, const V &v2)
352 {
353 return std::experimental::max(v1, v2);
354 }
355
357 template<class M>
359 const M &m1, const M &m2)
360 {
361 return m1 || m2;
362 }
363
365 template<class V>
368 const V &v1, const V &v2)
369 {
370 return std::experimental::min(v1, v2);
371 }
372
374 template<class M>
376 const M &m1, const M &m2)
377 {
378 return m1 && m2;
379 }
380
382 template<class M>
384 {
385 return std::experimental::any_of(mask);
386 }
387
389 template<class M>
391 {
392 return std::experimental::all_of(mask);
393 }
394
395 // anyFalse() is handled by defaults.hh
396
398 template<class M>
400 {
401 return std::experimental::none_of(mask);
402 }
403
405 template<class V>
408 const V &v)
409 {
410 return std::experimental::hmax(v);
411 }
412
414 template<class M>
416 {
417 return std::experimental::any_of(mask);
418 }
419
421 template<class V>
424 const V &v)
425 {
426 return std::experimental::hmin(v);
427 }
428
430 template<class M>
432 {
433 return std::experimental::all_of(mask);
434 }
435
437 template<class S1, class V2>
438 auto maskAnd(ADLTag<5, std::is_same<Mask<S1>, bool>::value &&
440 const S1 &s1, const V2 &v2)
441 {
442 return Simd::Mask<V2>(Simd::mask(s1)) && Simd::mask(v2);
443 }
444
446 template<class V1, class S2>
448 std::is_same<Mask<S2>, bool>::value>,
449 const V1 &v1, const S2 &s2)
450 {
451 return Simd::mask(v1) && Simd::Mask<V1>(Simd::mask(s2));
452 }
453
455 template<class S1, class V2>
456 auto maskOr(ADLTag<5, std::is_same<Mask<S1>, bool>::value &&
458 const S1 &s1, const V2 &v2)
459 {
460 return Simd::Mask<V2>(Simd::mask(s1)) || Simd::mask(v2);
461 }
462
464 template<class V1, class S2>
466 std::is_same<Mask<S2>, bool>::value>,
467 const V1 &v1, const S2 &s2)
468 {
469 return Simd::mask(v1) || Simd::Mask<V1>(Simd::mask(s2));
470 }
471
473
474 } // namespace Overloads
475
476 } // namespace Simd
477
478 /*
479 * Specialize IsNumber for std::experimental::simd to be able to use it as a
480 * scalar in DenseMatrix etc.
481 */
482 template<class T, class Abi>
483 struct IsNumber<std::experimental::simd<T, Abi> >
484 : public std::integral_constant<bool, IsNumber<T>::value> {
485 };
486
488 template<class V>
489 struct AutonomousValueType<Simd::StdSimdImpl::Proxy<V> > :
490 AutonomousValueType<typename Simd::StdSimdImpl::Proxy<V>::value_type> {};
491
492} // namespace Dune
493
494#endif // DUNE_HAVE_CXX_EXPERIMENTAL_SIMD
495
496#endif // DUNE_COMMON_SIMD_STDSIMD_HH
A proxy for lanes of SIMD types with operator[]
#define DUNE_SIMD_STDSIMD_ASSIGN(OP)
Definition stdsimd.hh:174
Basic definitions for SIMD Implementations.
Default implementations for SIMD Implementations.
Traits for type conversions and type information.
auto mask(const V &v)
Convert to mask, analogue of bool(s) for scalars.
Definition simd/interface.hh:502
Rebind< bool, V > Mask
Mask type type of some SIMD type.
Definition simd/interface.hh:289
typename Overloads::ScalarType< std::decay_t< V > >::type Scalar
Element type of some SIMD type.
Definition simd/interface.hh:235
Mask< V > mask(ADLTag< 0, std::is_same< V, Mask< V > >::value >, const V &v)
implements Simd::mask()
Definition defaults.hh:153
bool allFalse(ADLTag< 0 >, const Mask &mask)
implements Simd::allFalse()
Definition defaults.hh:124
bool allTrue(ADLTag< 0 >, const Mask &mask)
implements Simd::allTrue()
Definition defaults.hh:104
auto maskAnd(ADLTag< 0 >, const V1 &v1, const V2 &v2)
implements Simd::maskAnd()
Definition defaults.hh:177
auto maskOr(ADLTag< 0 >, const V1 &v1, const V2 &v2)
implements Simd::maskOr()
Definition defaults.hh:170
auto min(ADLTag< 0 >, const V &v1, const V &v2)
implements binary Simd::min()
Definition defaults.hh:89
auto max(ADLTag< 0 >, const V &v1, const V &v2)
implements binary Simd::max()
Definition defaults.hh:81
STL namespace.
Dune namespace
Definition alignedallocator.hh:16
bool anyTrue(ADLTag< 5 >, const AlignedNumber< bool, align > &mask)
Definition debugalign.hh:556
const AlignedNumber< T, align > & cond(ADLTag< 5 >, AlignedNumber< bool, align > mask, const AlignedNumber< T, align > &ifTrue, const AlignedNumber< T, align > &ifFalse)
Definition debugalign.hh:548
T & lane(ADLTag< 5 >, std::size_t l, AlignedNumber< T, align > &v)
Definition debugalign.hh:533
Whether this type acts as a scalar in the context of (hierarchically blocked) containers.
Definition typetraits.hh:194
Tag used to force late-binding lookup in Dune::Simd::Overloads.
Definition base.hh:182
should have a member type type
Definition standard.hh:60
should have a member type type
Definition standard.hh:67
should be derived from a Dune::index_constant
Definition standard.hh:74
Definition loop.hh:65
true for std::experimental::simd_mask types
Definition stdsimd.hh:85
true for std::experimental::simd and simd_mask types
Definition stdsimd.hh:91
true for the element types of std::experimental::simd
Definition stdsimd.hh:97
the vector type of a vector or mask type
Definition stdsimd.hh:101
V type
Definition stdsimd.hh:101
A reference-like proxy for lanes of std::experimental::simd types.
Definition stdsimd.hh:134
DUNE_SIMD_STDSIMD_BINARY & DUNE_SIMD_STDSIMD_BINARY(^);DUNE_SIMD_STDSIMD_BINARY(|
DUNE_SIMD_STDSIMD_ASSIGN & DUNE_SIMD_STDSIMD_ASSIGN(^=);DUNE_SIMD_STDSIMD_ASSIGN(|=
typename Base::value_type value_type
Definition stdsimd.hh:137
DUNE_SIMD_STDSIMD_ASSIGN * DUNE_SIMD_STDSIMD_ASSIGN(/=);DUNE_SIMD_STDSIMD_ASSIGN(%=
DUNE_SIMD_STDSIMD_BINARY * DUNE_SIMD_STDSIMD_BINARY(/);DUNE_SIMD_STDSIMD_BINARY(%
Type free of internal references that T can be converted to.
Definition typetraits.hh:509
T as_const(T... args)