dune-common 2.12-git
Loading...
Searching...
No Matches
simd/vc.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_VC_HH
4#define DUNE_COMMON_SIMD_VC_HH
5
11#include <Vc/Vc>
12
13#include <cstddef>
14#include <type_traits>
15#include <utility>
16
19#include <dune/common/simd/defaults.hh> // for anyFalse()
23
163namespace Dune {
164 namespace Simd {
165
166 namespace VcImpl {
167
169 template<class V, class SFINAE = void>
171
172 template<typename T, typename A>
173 struct IsMask<Vc::Mask<T, A> > : std::true_type {};
174
175 template<typename T, std::size_t n, typename V, std::size_t m>
176 struct IsMask<Vc::SimdMaskArray<T, n, V, m> > : std::true_type {};
177
179 template<class V, class SFINAE = void>
180 struct IsVector : IsMask<V> {};
181
182 template<typename T, typename A>
183 struct IsVector<Vc::Vector<T, A> > : std::true_type {};
184
185 template<typename T, std::size_t n, typename V, std::size_t m>
186 struct IsVector<Vc::SimdArray<T, n, V, m> > : std::true_type {};
187
188 template<typename T> struct IsVectorizable : std::false_type {};
189 template<> struct IsVectorizable<double> : std::true_type {};
190 template<> struct IsVectorizable<float> : std::true_type {};
191 template<> struct IsVectorizable<std::int32_t> : std::true_type {};
192 template<> struct IsVectorizable<std::uint32_t> : std::true_type {};
193 template<> struct IsVectorizable<std::int16_t> : std::true_type {};
194 template<> struct IsVectorizable<std::uint16_t> : std::true_type {};
195
197
213 template<class V>
214 class Proxy : public Impl::ValueProxy<V>
215 {
216 using Base = Impl::ValueProxy<V>;
217 public:
218 using value_type = typename Base::value_type;
219
220 using Base::Base;
221 using Base::operator=;
222
223 // binary operators
224 //
225 // Normally, these are provided by the conversion operator in
226 // combination with C++'s builtin binary operators. Other classes
227 // that need to provide the binary operators themselves should either
228 // 1. deduce the "foreign" operand type independently, i.e. use
229 // template<class... Args, class Foreign>
230 // auto operator@(MyClass<Args...>, Foreign);
231 // or
232 // 2. not deduce anything from the foreign argument, i.e.
233 // template<class... Args>
234 // auto operator@(MyClass<Args...>,
235 // typename MyClass<Args...>::value_type);
236 // or
237 // template<class T, class... Args>
238 // struct MyClass {
239 // auto operator@(T);
240 // }
241 // or
242 // template<class T, class... Args>
243 // struct MyClass {
244 // friend auto operator@(MyClass, T);
245 // }
246 //
247 // This allows either for an exact match (in the case of option 1.) or
248 // for conversions to be applied to the foreign argument (options 2.).
249 // In contrast, allowing some of the template parameters being deduced
250 // from the self argument also being deduced from the foreign argument
251 // will likely lead to ambiguous deduction when the foreign argument is
252 // a proxy:
253 // template<class T, class... Args>
254 // auto operator@(MyClass<T, Args...>, T);
255 // One class that suffers from this problem is std::complex.
256 //
257 // Note that option 1. is a bit dangerous, as the foreign argument is
258 // catch-all. This seems tempting in the case of a proxy class, as
259 // the operator could just be forwarded to the proxied object with the
260 // foreign argument unchanged, immediately creating interoperability
261 // with arbitrary foreign classes. However, if the foreign class also
262 // choses option 1., this will result in ambiguous overloads, and there
263 // is no clear guide to decide which class should provide the overload
264 // and which should not.
265 //
266 // Fortunately, deferring to the conversion and the built-in operators
267 // mostly works in the case of this proxy class, because only built-in
268 // types can be proxied anyway. Unfortunately, the Vc vectors and
269 // arrays suffer from a slightly different problem. They chose option
270 // 1., but they can't just accept the argument type they are given,
271 // since they need to somehow implement the operation in terms of
272 // intrinsics. So they check the argument whether it is one of the
273 // expected types, and remove the operator from the overload set if it
274 // isn't via SFINAE. Of course, this proxy class is not one of the
275 // expected types, even though it would convert to them...
276 //
277 // So what we have to do here, unfortunately, is to provide operators
278 // for the Vc types explicitly, and hope that there won't be some Vc
279 // version that gets the operators right, thus creating ambiguous
280 // overloads. Well, if guess it will be #ifdef time if it comes to
281 // that.
282#define DUNE_SIMD_VC_BINARY(OP) \
283 template<class T, class Abi> \
284 friend auto operator OP(const Vc::Vector<T, Abi> &l, Proxy&& r) \
285 -> decltype(l OP std::declval<value_type>()) \
286 { \
287 return l OP value_type(r); \
288 } \
289 template<class T, class Abi> \
290 auto operator OP(const Vc::Vector<T, Abi> &r) && \
291 -> decltype(std::declval<value_type>() OP r) \
292 { \
293 return value_type(*this) OP r; \
294 } \
295 template<class T, std::size_t n, class Vec, std::size_t m> \
296 friend auto \
297 operator OP(const Vc::SimdArray<T, n, Vec, m> &l, Proxy&& r) \
298 -> decltype(l OP std::declval<value_type>()) \
299 { \
300 return l OP value_type(r); \
301 } \
302 template<class T, std::size_t n, class Vec, std::size_t m> \
303 auto operator OP(const Vc::SimdArray<T, n, Vec, m> &r) && \
304 -> decltype(std::declval<value_type>() OP r) \
305 { \
306 return value_type(*this) OP r; \
307 }
308
325#undef DUNE_SIMD_VC_BINARY
326
327 // this is needed to implement broadcast construction from proxy as
328 // the unadorned assignment operator cannot be a non-member
329 template<class T, class Abi,
331 T>::value> >
332 operator Vc::Vector<T, Abi>() &&
333 {
334 return value_type(static_cast<const Proxy &>(*this));
335 }
336 template<class T, std::size_t n, class Vec, std::size_t m,
338 T>::value> >
339 operator Vc::SimdArray<T, n, Vec, m>() &&
340 {
341 return value_type(static_cast<const Proxy &>(*this));
342 }
343
344#define DUNE_SIMD_VC_ASSIGN(OP) \
345 template<class T, class Abi> \
346 friend auto operator OP(Vc::Vector<T, Abi> &l, Proxy&& r) \
347 -> decltype(l OP std::declval<value_type>()) \
348 { \
349 return l OP value_type(r); \
350 }
351
360 // The shift assignment would not be needed for Vc::Vector since it
361 // has overloads for `int` rhs and the proxy can convert to that --
362 // except that there is also overloads for Vector, and because of the
363 // conversion operator needed to support unadorned assignments, the
364 // proxy can convert to that, too.
367#undef DUNE_SIMD_VC_ASSIGN
368 };
369
370 } // namespace VcImpl
371
372 namespace Overloads {
373
380
383 template<class V>
384 struct ScalarType<V, std::enable_if_t<VcImpl::IsVector<V>::value> >
385 {
386 using type = typename V::value_type;
387 };
388
390
397 template<class V>
398 struct RebindType<Simd::Scalar<V>, V,
399 std::enable_if_t<VcImpl::IsVector<V>::value> >
400 {
401 using type = V;
402 };
403
405
411 template<class V>
412 struct RebindType<bool, V, std::enable_if_t<VcImpl::IsVector<V>::value &&
413 !VcImpl::IsMask<V>::value>>
414 {
415 using type = typename V::mask_type;
416 };
417
419
425 template<class M>
426 struct RebindType<Scalar<typename M::Vector>, M,
427 std::enable_if_t<VcImpl::IsMask<M>::value>>
428 {
429 using type = typename M::Vector;
430 };
431
433
442 template<class S, class M>
443 struct RebindType<S, M,
444 std::enable_if_t<
445 VcImpl::IsMask<M>::value &&
446 VcImpl::IsVectorizable<S>::value &&
447 !std::is_same<S, Scalar<typename M::Vector> >::value
448 > >
449 {
450 using type = Vc::fixed_size_simd<S, int(Simd::lanes<M>())>;
451 };
452
454
463 template<class S, class V>
464 struct RebindType<S, V,
465 std::enable_if_t<VcImpl::IsVector<V>::value &&
466 !VcImpl::IsMask<V>::value &&
467 VcImpl::IsVectorizable<S>::value &&
468 !std::is_same<S, Scalar<V> >::value> >
469 {
470 using type = Vc::fixed_size_simd<S, int(Simd::lanes<V>())>;
471 };
472
474
481 template<class S, class V>
482 struct RebindType<S, V,
483 std::enable_if_t<VcImpl::IsVector<V>::value &&
484 !VcImpl::IsVectorizable<S>::value &&
485 !std::is_same<S, bool>::value &&
486 !std::is_same<S, Scalar<V> >::value> >
487 {
489 };
490
492
495 template<class V>
496 struct LaneCount<V, std::enable_if_t<VcImpl::IsVector<V>::value> >
497 : public index_constant<V::size()>
498 { };
499
501 template<class V>
503 std::size_t l, V &v)
504 {
505 return { l, v };
506 }
507
509 template<class V>
511 std::size_t l, const V &v)
512 {
513 return v[l];
514 }
515
517 /*
518 * The hack with the SFINAE is necessary, because if I use just
519 * Scalar<V> as the return type, the compiler still errors out if V is
520 * an lvalue-reference T&. You'd think he'd notice that he can't
521 * instantiate this declaration for this template parameter, and would
522 * simply remove it from the overload set, but no...
523 */
524 template<class V,
527 std::size_t l, V &&v)
528 {
529 return std::forward<V>(v)[l];
530 }
531
533 template<class V>
536 const Mask<V> &mask, const V &ifTrue, const V &ifFalse)
537 {
538 return Vc::iif(mask, ifTrue, ifFalse);
539 }
540
542 /*
543 * Kludge because iif seems to be unimplemented for masks
544 */
545 template<class V>
547 const V &mask, const V &ifTrue, const V &ifFalse)
548 {
549 return (mask && ifTrue) || (!mask && ifFalse);
550 }
551
553 template<class V>
556 const V &v1, const V &v2)
557 {
558 return Simd::cond(v1 < v2, v2, v1);
559 }
560
562 template<class M>
564 const M &m1, const M &m2)
565 {
566 // for SimdMaskArray, Vc returns the derived fixed_size_simd_mask
567 return m1 || m2;
568 }
569
571 template<class V>
574 const V &v1, const V &v2)
575 {
576 return Simd::cond(v1 < v2, v1, v2);
577 }
578
580 template<class M>
582 const M &m1, const M &m2)
583 {
584 // for SimdMaskArray, Vc returns the derived fixed_size_simd_mask
585 return m1 && m2;
586 }
587
589 template<class M>
591 {
592 return Vc::any_of(mask);
593 }
594
596 template<class M>
598 {
599 return Vc::all_of(mask);
600 }
601
602 // nothing like anyFalse() in Vc, so let defaults.hh handle it
603
605 template<class M>
607 {
608 return Vc::none_of(mask);
609 }
610
612 template<class V>
615 const V &v)
616 {
617 return v.max();
618 }
619
621 template<class M>
623 {
624 return Vc::any_of(mask);
625 }
626
628 template<class V>
631 const V &v)
632 {
633 return v.min();
634 }
635
637 template<class M>
639 {
640 return !Vc::any_of(!mask);
641 }
642
644 template<class S1, class V2>
645 auto maskAnd(ADLTag<5, std::is_same<Mask<S1>, bool>::value &&
647 const S1 &s1, const V2 &v2)
648 {
649 return Simd::Mask<V2>(Simd::mask(s1)) && Simd::mask(v2);
650 }
651
653 template<class V1, class S2>
655 std::is_same<Mask<S2>, bool>::value>,
656 const V1 &v1, const S2 &s2)
657 {
658 return Simd::mask(v1) && Simd::Mask<V1>(Simd::mask(s2));
659 }
660
662 template<class S1, class V2>
663 auto maskOr(ADLTag<5, std::is_same<Mask<S1>, bool>::value &&
665 const S1 &s1, const V2 &v2)
666 {
667 return Simd::Mask<V2>(Simd::mask(s1)) || Simd::mask(v2);
668 }
669
671 template<class V1, class S2>
673 std::is_same<Mask<S2>, bool>::value>,
674 const V1 &v1, const S2 &s2)
675 {
676 return Simd::mask(v1) || Simd::Mask<V1>(Simd::mask(s2));
677 }
678
680
681 } // namespace Overloads
682
683 } // namespace Simd
684
685 /*
686 * Specialize IsNumber for Vc::SimdArray and Vc::Vector to be able to use
687 * it as a scalar in DenseMatrix etc.
688 */
689 template <typename T, std::size_t N, class V, size_t Wt>
690 struct IsNumber<Vc::SimdArray<T, N, V, Wt>>
691 : public std::integral_constant<bool, IsNumber<T>::value> {
692 };
693
694 template <typename T, typename Abi>
695 struct IsNumber<Vc::Vector<T, Abi>>
696 : public std::integral_constant<bool, IsNumber<T>::value> {
697 };
698
700 template<class V>
701 struct AutonomousValueType<Simd::VcImpl::Proxy<V> > :
702 AutonomousValueType<typename Simd::VcImpl::Proxy<V>::value_type> {};
703
704} // namespace Dune
705
706namespace Vc_VERSIONED_NAMESPACE {
707
709
726 template<class T, int N>
727 fixed_size_simd<T, N> max(const fixed_size_simd<T, N> &a,
728 const fixed_size_simd<T, N> &b)
729 {
730 using A = SimdArray<T, std::size_t(N)>;
731 return max(static_cast<const A &>(a), static_cast<const A &>(b));
732 }
733
735
740 template<class T, int N>
741 fixed_size_simd<T, N> min(const fixed_size_simd<T, N> &a,
742 const fixed_size_simd<T, N> &b)
743 {
744 using A = SimdArray<T, std::size_t(N)>;
745 return min(static_cast<const A &>(a), static_cast<const A &>(b));
746 }
747
748} // namespace Vc_VERSIONED_NAMESPACE
749
750#endif // DUNE_COMMON_SIMD_VC_HH
A proxy for lanes of SIMD types with operator[]
Basic definitions for SIMD Implementations.
Default implementations for SIMD Implementations.
Traits for type conversions and type information.
V cond(M &&mask, const V &ifTrue, const V &ifFalse)
Like the ?: operator.
Definition simd/interface.hh:393
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:15
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
Definition simd/vc.hh:706
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
specialized to true for Vc mask types
Definition simd/vc.hh:170
specialized to true for Vc vector and mask types
Definition simd/vc.hh:180
Definition simd/vc.hh:188
A reference-like proxy for elements of Vc vectors and masks.
Definition simd/vc.hh:215
DUNE_SIMD_VC_BINARY * DUNE_SIMD_VC_BINARY(/);DUNE_SIMD_VC_BINARY(%
DUNE_SIMD_VC_ASSIGN * DUNE_SIMD_VC_ASSIGN(/=);DUNE_SIMD_VC_ASSIGN(%=
DUNE_SIMD_VC_BINARY & DUNE_SIMD_VC_BINARY(^);DUNE_SIMD_VC_BINARY(|
typename Base::value_type value_type
Definition simd/vc.hh:218
DUNE_SIMD_VC_ASSIGN & DUNE_SIMD_VC_ASSIGN(^=);DUNE_SIMD_VC_ASSIGN(|=
Type free of internal references that T can be converted to.
Definition typetraits.hh:509
#define DUNE_SIMD_VC_ASSIGN(OP)
Definition simd/vc.hh:344