3 #ifndef DUNE_COMMON_SIMD_TEST_HH 4 #define DUNE_COMMON_SIMD_TEST_HH 17 #include <type_traits> 20 #include <unordered_set> 38 template<
class T,
class SFINAE =
void>
39 struct LessThenComparable : std::false_type {};
41 struct LessThenComparable<T,
std::
void_t<decltype(std::declval<T>()
42 < std::declval<T>())> > :
46 template<
class Dst,
class Src>
47 struct CopyConstHelper
51 template<
class Dst,
class Src>
52 struct CopyConstHelper<Dst, const Src>
54 using type = std::add_const_t<Dst>;
57 template<
class Dst,
class Src>
58 struct CopyVolatileHelper
62 template<
class Dst,
class Src>
63 struct CopyVolatileHelper<Dst, volatile Src>
65 using type = std::add_volatile_t<Dst>;
68 template<
class Dst,
class Src>
69 struct CopyReferenceHelper
73 template<
class Dst,
class Src>
74 struct CopyReferenceHelper<Dst, Src&>
76 using type = std::add_lvalue_reference_t<Dst>;
79 template<
class Dst,
class Src>
80 struct CopyReferenceHelper<Dst, Src&&>
82 using type = std::add_rvalue_reference_t<Dst>;
85 template<
class Dst,
class Src>
86 using CopyRefQual =
typename CopyReferenceHelper<
87 typename CopyVolatileHelper<
88 typename CopyConstHelper<
90 std::remove_reference_t<Src>
92 std::remove_reference_t<Src>
97 template<
class Mark,
class Types,
99 std::make_index_sequence<TypeListSize<Types>::value - 1> >
101 template<
class Mark,
class Types, std::size_t... I>
102 struct RemoveEnd<Mark, Types,
std::index_sequence<I...>>
104 using Back = TypeListEntry_t<TypeListSize<Types>::value - 1, Types>;
105 static_assert(std::is_same<Mark, Back>::value,
106 "TypeList not terminated by proper EndMark");
107 using type = TypeList<TypeListEntry_t<I, Types>...>;
110 template<
class T,
class List,
class =
void>
114 struct TypeInList<T,
TypeList<> > : std::false_type {};
116 template<
class T,
class... Rest>
117 struct TypeInList<T,
TypeList<T, Rest...> > : std::true_type {};
119 template<
class T,
class Head,
class... Rest>
120 struct TypeInList<T,
TypeList<Head, Rest...>,
121 std::enable_if_t<!std::is_same<T, Head>::value> > :
122 TypeInList<T, TypeList<Rest...> >::type
126 struct IsLoop : std::false_type {};
127 template<
class T, std::
size_t S>
128 struct IsLoop<LoopSIMD<T, S> > : std::true_type {};
137 constexpr
bool debugTypes(std::true_type) {
return true; }
138 template<
class... Types>
140 constexpr
bool debugTypes(std::false_type) {
return false; }
156 template<
class... Types>
162 using IsLoop =
typename Impl::IsLoop<T>::type;
166 std::ostream &log_ = std::cerr;
169 std::unordered_set<std::type_index> seen_;
176 void complain(
const char *file,
int line,
const char *func,
179 void complain(
const char *file,
int line,
const char *func,
180 const std::string &opname,
const char *expr);
186 #define DUNE_SIMD_CHECK(expr) \ 187 ((expr) ? void() : complain(__FILE__, __LINE__, __func__, #expr)) 191 #define DUNE_SIMD_CHECK_OP(expr) \ 192 ((expr) ? void() : complain(__FILE__, __LINE__, __func__, \ 193 DUNE_SIMD_OPNAME, #expr)) 197 static std::decay_t<T> prvalue(T &&t)
199 return std::forward<T>(t);
204 static bool is42(
const V &v)
208 for(std::size_t l = 0; l <
lanes(v); ++l)
210 good &= (
lane(l, v) == Scalar<V>(42));
224 for(std::size_t l = 0; l <
lanes(vec); ++l)
225 lane(l, vec) = l + 1;
231 static bool is123(
const V &v)
235 for(std::size_t l = 0; l <
lanes(v); ++l)
237 good &= (
lane(l, v) == Scalar<V>(l+1));
243 static V leftVector()
248 for(std::size_t l = 0; l <
lanes(res); ++l)
249 lane(l, res) = Scalar<V>(l+1);
254 static V rightVector()
259 for(std::size_t l = 0; l <
lanes(res); ++l)
262 lane(l, res) = Scalar<V>((l)%7+1);
267 static T leftScalar()
273 static T rightScalar()
280 template<
class Dst,
class Src>
281 using CopyRefQual = Impl::CopyRefQual<Dst, Src>;
288 template<
class Op,
class... Vectors>
290 decltype(std::declval<Op>().
291 scalar(std::declval<CopyRefQual<Scalar<Vectors>,
305 static_assert(std::is_same<T, std::decay_t<T> >::value,
"Scalar types " 306 "must not be references, and must not include " 308 [[maybe_unused]] T a{};
312 [[deprecated(
"Warning: please include bool in the Rebinds for " 313 "simd type V, as Masks are not checked otherwise.")]]
314 void warnMissingMaskRebind(std::true_type) {}
316 void warnMissingMaskRebind(std::false_type) {}
318 template<
class V,
class Rebinds,
template<
class>
class RebindPrune,
319 template<
class>
class RebindAccept,
class Recurse>
320 void checkRebindOf(Recurse recurse)
323 using T =
typename decltype(target)::type;
326 using W = Rebind<T, V>;
327 log_ <<
"Type " << className<V>() <<
" rebound to " 328 << className<T>() <<
" is " << className<W>() << std::endl;
330 static_assert(std::is_same<W, std::decay_t<W> >::value,
"Rebound " 331 "types must not be references, and must not include " 333 static_assert(lanes<V>() == lanes<W>(),
"Rebound types must have " 334 "the same number of lanes as the original vector " 336 static_assert(std::is_same<T, Scalar<W> >::value,
"Rebound types " 337 "must have the bound-to scalar type");
339 if constexpr (RebindPrune<W>{}) {
340 log_ <<
"Pruning check of Simd type " << className<W>()
344 using Impl::debugTypes;
345 static_assert(debugTypes<T, V, W>(RebindAccept<W>{}),
346 "Rebind<T, V> is W, but that is not accepted " 348 recurse(MetaType<W>{});
352 static_assert(std::is_same<
Rebind<Scalar<V>, V>, V>::value,
"A type " 353 "rebound to its own scalar type must be the same type " 354 "as the original type");
355 static_assert(std::is_same<Rebind<bool, V>, Mask<V> >::value,
"A type " 356 "rebound to bool must be the mask type for that type");
358 constexpr
bool hasBool = Impl::TypeInList<bool, Rebinds>::value;
359 warnMissingMaskRebind<V>(std::bool_constant<!hasBool>{});
371 static_assert(std::is_same<std::size_t, decltype(lanes<V>())>::value,
372 "return type of lanes<V>() should be std::size_t");
373 static_assert(std::is_same<std::size_t, decltype(
lanes(V{}))>::value,
374 "return type of lanes(V{}) should be std::size_t");
377 [[maybe_unused]] constexpr
auto size = lanes<V>();
383 void checkDefaultConstruct()
385 { [[maybe_unused]] V vec; }
386 { [[maybe_unused]] V vec{}; }
387 { [[maybe_unused]] V vec = {}; }
397 for(std::size_t l = 0; l <
lanes(vec); ++l)
398 lane(l, vec) = l + 1;
399 for(std::size_t l = 0; l <
lanes(vec); ++l)
401 using MLRes = decltype(
lane(0, vec));
402 static_assert(std::is_same<MLRes, Scalar<V>&>::value ||
403 std::is_same<MLRes, std::decay_t<MLRes> >::value,
404 "Result of lane() on a mutable lvalue vector must " 405 "either be a mutable reference to a scalar of that " 406 "vector or a proxy object (which itself may not be a " 407 "reference nor const).");
411 for(std::size_t l = 0; l <
lanes(vec); ++l)
413 using CLRes = decltype(
lane(0, vec2));
414 static_assert(std::is_same<CLRes,
const Scalar<V>&>::value ||
415 std::is_same<CLRes, std::decay_t<CLRes> >::value,
416 "Result of lane() on a const lvalue vector must " 417 "either be a const lvalue reference to a scalar of that " 418 "vector or a proxy object (which itself may not be a " 419 "reference nor const).");
420 static_assert(!std::is_assignable<CLRes, Scalar<V> >::value,
421 "Result of lane() on a const lvalue vector must not be " 422 "assignable from a scalar.");
425 for(std::size_t l = 0; l <
lanes(vec); ++l)
427 using RRes = decltype(
lane(0, prvalue(vec)));
436 static_assert(std::is_same<RRes, Scalar<V> >::value ||
437 std::is_same<RRes, Scalar<V>&&>::value,
438 "Result of lane() on a rvalue vector V must be " 439 "Scalar<V> or Scalar<V>&&.");
450 void checkCopyMoveConstruct()
459 { V ref(make123<V>()); V vec (ref);
461 { V ref(make123<V>()); V vec = ref ;
463 { V ref(make123<V>()); V vec {ref};
465 { V ref(make123<V>()); V vec = {ref};
467 {
const V ref(make123<V>()); V vec (ref);
469 {
const V ref(make123<V>()); V vec = ref ;
471 {
const V ref(make123<V>()); V vec {ref};
473 {
const V ref(make123<V>()); V vec = {ref};
477 { V ref(make123<V>()); V vec (std::move(ref));
479 { V ref(make123<V>()); V vec = std::move(ref) ;
481 { V ref(make123<V>()); V vec {std::move(ref)};
483 { V ref(make123<V>()); V vec = {std::move(ref)};
488 void checkBroadcastVectorConstruct()
491 { Scalar<V> ref = 42; V vec (ref);
493 { Scalar<V> ref = 42; V vec = ref ;
499 {
const Scalar<V> ref = 42; V vec (ref);
501 {
const Scalar<V> ref = 42; V vec = ref ;
509 { Scalar<V> ref = 42; V vec (std::move(ref));
511 { Scalar<V> ref = 42; V vec = std::move(ref) ;
520 void checkBroadcastMaskConstruct()
523 { Scalar<V> ref = 42; V vec (ref);
527 { Scalar<V> ref = 42; V vec {ref};
531 {
const Scalar<V> ref = 42; V vec (ref);
535 {
const Scalar<V> ref = 42; V vec {ref};
541 { Scalar<V> ref = 42; V vec (std::move(ref));
545 { Scalar<V> ref = 42; V vec {std::move(ref)};
552 template<
class FromV,
class ToV>
556 FromV fromVec = make123<FromV>();
557 auto toVec = implCast<ToV>(fromVec);
558 static_assert(std::is_same<decltype(toVec), ToV>::value,
559 "Unexpected result type for implCast<ToV>(FromV&)");
565 const FromV fromVec = make123<FromV>();
566 auto toVec = implCast<ToV>(fromVec);
567 static_assert(std::is_same<decltype(toVec), ToV>::value,
568 "Unexpected result type for implCast<ToV>(const " 574 auto toVec = implCast<ToV>(make123<FromV>());
575 static_assert(std::is_same<decltype(toVec), ToV>::value,
576 "Unexpected result type for implCast<ToV>(FromV&&)");
588 checkImplCast<V, V>();
589 checkImplCast<V, LoopV>();
590 checkImplCast<LoopV, V>();
595 void checkBroadcast()
600 auto vec = broadcast<V>(ref);
601 static_assert(std::is_same<decltype(vec), V>::value,
602 "Unexpected result type for broadcast<V>()");
608 const Scalar<V> ref = 42;
609 auto vec = broadcast<V>(ref);
610 static_assert(std::is_same<decltype(vec), V>::value,
611 "Unexpected result type for broadcast<V>()");
616 auto vec = broadcast<V>(Scalar<V>(42));
617 static_assert(std::is_same<decltype(vec), V>::value,
618 "Unexpected result type for broadcast<V>()");
623 auto vec = broadcast<V>(42);
624 static_assert(std::is_same<decltype(vec), V>::value,
625 "Unexpected result type for broadcast<V>()");
630 auto vec = broadcast<V>(42.0);
631 static_assert(std::is_same<decltype(vec), V>::value,
632 "Unexpected result type for broadcast<V>()");
638 void checkBracedAssign()
641 { V ref = make123<V>(); V vec; vec = {ref};
643 {
const V ref = make123<V>(); V vec; vec = {ref};
651 void checkBracedBroadcastAssign()
670 #define DUNE_SIMD_POSTFIX_OP(NAME, SYMBOL) \ 671 struct OpPostfix##NAME \ 674 auto operator()(V&& v) const \ 675 -> decltype(std::forward<V>(v) SYMBOL) \ 677 return std::forward<V>(v) SYMBOL; \ 681 #define DUNE_SIMD_PREFIX_OP(NAME, SYMBOL) \ 682 struct OpPrefix##NAME \ 685 auto operator()(V&& v) const \ 686 -> decltype(SYMBOL std::forward<V>(v)) \ 688 return SYMBOL std::forward<V>(v); \ 692 DUNE_SIMD_POSTFIX_OP(Decrement, -- );
693 DUNE_SIMD_POSTFIX_OP(Increment, ++ );
695 DUNE_SIMD_PREFIX_OP (Decrement, -- );
696 DUNE_SIMD_PREFIX_OP (Increment, ++ );
698 DUNE_SIMD_PREFIX_OP (Plus, + );
699 DUNE_SIMD_PREFIX_OP (Minus, - );
700 DUNE_SIMD_PREFIX_OP (LogicNot, ! );
706 #pragma GCC diagnostic push 707 #pragma GCC diagnostic ignored "-Wpragmas" 708 #pragma GCC diagnostic ignored "-Wunknown-warning-option" // clang 6.0.1 709 #pragma GCC diagnostic ignored "-Wbool-operation" 710 DUNE_SIMD_PREFIX_OP (BitNot, ~ );
711 #pragma GCC diagnostic pop 713 #undef DUNE_SIMD_POSTFIX_OP 714 #undef DUNE_SIMD_PREFIX_OP 716 template<
class V,
class Op>
718 IsCallable<Op(decltype(lane(0, std::declval<V>())))>::value>
721 #define DUNE_SIMD_OPNAME (className<Op(V)>()) 723 auto val = leftVector<std::decay_t<V>>();
727 auto &&result = op(static_cast<V>(arg));
728 using T = Scalar<std::decay_t<decltype(result)> >;
729 for(std::size_t l = 0; l <
lanes(val); ++l)
746 == static_cast<T>(op(
lane(l, static_cast<V>(val)))));
750 for(std::size_t l = 0; l < lanes<std::decay_t<V> >(); ++l)
752 #undef DUNE_SIMD_OPNAME 755 template<
class V,
class Op>
757 not IsCallable<Op(decltype(lane(0, std::declval<V>())))>::value>
765 template<
class V,
class Op>
766 void checkUnaryOpsV(Op op)
768 checkUnaryOpV<V&>(op);
769 checkUnaryOpV<const V&>(op);
770 checkUnaryOpV<V&&>(op);
784 #define DUNE_SIMD_INFIX_OP(NAME, SYMBOL) \ 785 struct OpInfix##NAME \ 787 template<class V1, class V2> \ 788 decltype(auto) operator()(V1&& v1, V2&& v2) const \ 790 return std::forward<V1>(v1) SYMBOL std::forward<V2>(v2); \ 792 template<class S1, class S2> \ 793 auto scalar(S1&& s1, S2&& s2) const \ 794 -> decltype(std::forward<S1>(s1) SYMBOL std::forward<S2>(s2)); \ 805 #define DUNE_SIMD_ASSIGN_OP(NAME, SYMBOL) \ 806 struct OpInfix##NAME \ 808 template<class V1, class V2> \ 809 decltype(auto) operator()(V1&& v1, V2&& v2) const \ 811 return std::forward<V1>(v1) SYMBOL std::forward<V2>(v2); \ 813 template<class S1, class S2> \ 814 auto scalar(S1& s1, S2&& s2) const \ 815 -> decltype(s1 SYMBOL std::forward<S2>(s2)); \ 818 #define DUNE_SIMD_REPL_OP(NAME, REPLFN, SYMBOL) \ 819 struct OpInfix##NAME \ 821 template<class V1, class V2> \ 822 decltype(auto) operator()(V1&& v1, V2&& v2) const \ 824 return Simd::REPLFN(std::forward<V1>(v1), std::forward<V2>(v2)); \ 826 template<class S1, class S2> \ 827 auto scalar(S1&& s1, S2&& s2) const \ 828 -> decltype(std::forward<S1>(s1) SYMBOL std::forward<S2>(s2)); \ 831 DUNE_SIMD_INFIX_OP(Mul, * );
832 DUNE_SIMD_INFIX_OP(Div, / );
833 DUNE_SIMD_INFIX_OP(Remainder, % );
835 DUNE_SIMD_INFIX_OP(Plus, + );
836 DUNE_SIMD_INFIX_OP(Minus, - );
838 DUNE_SIMD_INFIX_OP(LeftShift, << );
839 DUNE_SIMD_INFIX_OP(RightShift, >> );
841 DUNE_SIMD_INFIX_OP(Less, < );
842 DUNE_SIMD_INFIX_OP(Greater, > );
843 DUNE_SIMD_INFIX_OP(LessEqual, <= );
844 DUNE_SIMD_INFIX_OP(GreaterEqual, >= );
846 DUNE_SIMD_INFIX_OP(Equal, == );
847 DUNE_SIMD_INFIX_OP(NotEqual, != );
849 DUNE_SIMD_INFIX_OP(BitAnd, & );
850 DUNE_SIMD_INFIX_OP(BitXor, ^ );
851 DUNE_SIMD_INFIX_OP(BitOr, | );
855 DUNE_SIMD_REPL_OP(LogicAnd,
maskAnd, && );
856 DUNE_SIMD_REPL_OP(LogicOr,
maskOr, || );
858 DUNE_SIMD_ASSIGN_OP(Assign, = );
859 DUNE_SIMD_ASSIGN_OP(AssignMul, *= );
860 DUNE_SIMD_ASSIGN_OP(AssignDiv, /= );
861 DUNE_SIMD_ASSIGN_OP(AssignRemainder, %= );
862 DUNE_SIMD_ASSIGN_OP(AssignPlus, += );
863 DUNE_SIMD_ASSIGN_OP(AssignMinus, -= );
864 DUNE_SIMD_ASSIGN_OP(AssignLeftShift, <<=);
865 DUNE_SIMD_ASSIGN_OP(AssignRightShift, >>=);
866 DUNE_SIMD_ASSIGN_OP(AssignAnd, &= );
867 DUNE_SIMD_ASSIGN_OP(AssignXor, ^= );
868 DUNE_SIMD_ASSIGN_OP(AssignOr, |= );
870 #undef DUNE_SIMD_INFIX_OP 871 #undef DUNE_SIMD_REPL_OP 872 #undef DUNE_SIMD_ASSIGN_OP 875 struct OpInfixComma {};
877 template<
class T1,
class T2>
878 void checkCommaOp(
const std::decay_t<T1> &val1,
879 const std::decay_t<T2> &val2)
881 #define DUNE_SIMD_OPNAME (className<OpInfixComma(T1, T2)>()) 882 static_assert(std::is_same<decltype((std::declval<T1>(),
883 std::declval<T2>())), T2>::value,
884 "Type and value category of the comma operator must " 885 "match that of the second operand");
894 #pragma GCC diagnostic push 895 #pragma GCC diagnostic ignored "-Wunused-value" 896 auto &&result = (
static_cast<T1
>(arg1),
897 static_cast<T2>(arg2));
898 #pragma GCC diagnostic pop 899 if(std::is_reference<T2>::value)
918 #undef DUNE_SIMD_OPNAME 943 template<
class V1,
class V2,
class Op>
944 std::enable_if_t<Std::is_detected_v<ScalarResult, Op, V1, V2> >
945 checkBinaryOpVV(MetaType<V1>, MetaType<V2>, Op op)
947 #define DUNE_SIMD_OPNAME (className<Op(V1, V2)>()) 948 static_assert(std::is_same<std::decay_t<V1>, std::decay_t<V2> >::value,
949 "Internal testsystem error: called with two types that " 950 "don't decay to the same thing");
953 auto vref1 = leftVector<std::decay_t<V1>>();
954 auto vref2 = rightVector<std::decay_t<V2>>();
961 auto &&vopres = op(static_cast<V1>(vop1), static_cast<V2>(vop2));
962 using VR = decltype(vopres);
965 static_assert(
lanes<std::decay_t<VR> >() ==
lanes<std::decay_t<V1> >(),
966 "The result must have the same number of lanes as the " 971 using T = Scalar<std::decay_t<VR> >;
978 == static_cast<T>(op(
lane(l, static_cast<V1>(vref1)),
979 lane(l, static_cast<V2>(vref2)))));
990 #undef DUNE_SIMD_OPNAME 993 template<
class V1,
class V2,
class Op>
994 std::enable_if_t<!Std::is_detected_v<ScalarResult, Op, V1, V2> >
995 checkBinaryOpVV(MetaType<V1>, MetaType<V2>, Op op)
1003 template<
class V1,
class V2>
1004 void checkBinaryOpVV(MetaType<V1>, MetaType<V2>, OpInfixComma)
1006 static_assert(std::is_same<std::decay_t<V1>, std::decay_t<V2> >::value,
1007 "Internal testsystem error: called with two types that " 1008 "don't decay to the same thing");
1010 checkCommaOp<V1, V2>(leftVector<std::decay_t<V1>>(),
1011 rightVector<std::decay_t<V2>>());
1047 template<
class V1,
class T2,
class Op>
1048 std::enable_if_t<Std::is_detected_v<ScalarResult, Op, V1, T2> >
1049 checkBinaryOpVS(MetaType<V1>, MetaType<T2>, Op op)
1051 #define DUNE_SIMD_OPNAME (className<Op(V1, T2)>()) 1052 static_assert(std::is_same<
Scalar<std::decay_t<V1> >,
1053 std::decay_t<T2> >::value,
1054 "Internal testsystem error: called with a scalar that " 1055 "does not match the vector type.");
1058 auto sinit2 = rightScalar<std::decay_t<T2>>();
1061 auto vref1 = leftVector<std::decay_t<V1>>();
1062 auto sref2 = sinit2;
1069 auto &&vopres = op(static_cast<V1>(vop1), static_cast<T2>(sop2));
1070 using VR = decltype(vopres);
1073 static_assert(
lanes<std::decay_t<VR> >() ==
lanes<std::decay_t<V1> >(),
1074 "The result must have the same number of lanes as the " 1081 using T = Scalar<std::decay_t<decltype(vopres)> >;
1089 == static_cast<T>(op(
lane(l, static_cast<V1>(vref1)),
1090 static_cast<T2>(sref2) )));
1099 #undef DUNE_SIMD_OPNAME 1102 template<
class V1,
class T2,
class Op>
1103 std::enable_if_t<!Std::is_detected_v<ScalarResult, Op, V1, T2> >
1104 checkBinaryOpVS(MetaType<V1>, MetaType<T2>, Op op)
1112 template<
class V1,
class T2>
1113 void checkBinaryOpVS(MetaType<V1>, MetaType<T2>, OpInfixComma)
1115 static_assert(std::is_same<
Scalar<std::decay_t<V1> >,
1116 std::decay_t<T2> >::value,
1117 "Internal testsystem error: called with a scalar that " 1118 "does not match the vector type.");
1120 checkCommaOp<V1, T2>(leftVector<std::decay_t<V1>>(),
1121 rightScalar<std::decay_t<T2>>());
1150 template<
class V1,
class T2,
class Op>
1151 std::enable_if_t<Std::is_detected_v<ScalarResult, Op, V1, T2> >
1152 checkBinaryOpVVAgainstVS(MetaType<V1>, MetaType<T2>, Op op)
1154 #define DUNE_SIMD_OPNAME (className<Op(V1, T2)>()) 1155 static_assert(std::is_same<
Scalar<std::decay_t<V1> >,
1156 std::decay_t<T2> >::value,
1157 "Internal testsystem error: called with a scalar that " 1158 "does not match the vector type.");
1161 auto sinit2 = rightScalar<std::decay_t<T2>>();
1164 auto vop1 = leftVector<std::decay_t<V1>>();
1165 using V2 = CopyRefQual<V1, T2>;
1166 std::decay_t<V2> vop2(sinit2);
1169 op(static_cast<V1>(vop1), static_cast<V2>(vop2));
1175 #undef DUNE_SIMD_OPNAME 1178 template<
class V1,
class T2,
class Op>
1179 std::enable_if_t<!Std::is_detected_v<ScalarResult, Op, V1, T2> >
1180 checkBinaryOpVVAgainstVS(MetaType<V1>, MetaType<T2>, Op op)
1188 template<
class V1,
class T2>
1189 void checkBinaryOpVVAgainstVS(MetaType<V1>, MetaType<T2>, OpInfixComma)
1226 template<
class V1,
class V2,
class Op>
1227 std::enable_if_t<Std::is_detected_v<ScalarResult, Op, V1, V2> >
1228 checkBinaryOpVP(MetaType<V1>, MetaType<V2>, Op op)
1230 using P2 = decltype(
lane(0, std::declval<V2>()));
1231 using T2 = CopyRefQual<Scalar<V2>, V2>;
1232 #define DUNE_SIMD_OPNAME (className<Op(V1, P2)>()) 1233 static_assert(std::is_same<Scalar<V1>, Scalar<V2> >::value,
1234 "Internal testsystem error: called with two vector " 1235 "types whose scalar types don't match.");
1238 auto sinit2 = rightScalar<Scalar<V2>>();
1241 auto vref1 = leftVector<std::decay_t<V1>>();
1242 auto sref2 = sinit2;
1246 auto vop2 = std::decay_t<V2>(Scalar<V2>(0));
1247 lane(0, vop2) = sref2;
1251 op(static_cast<V1>(vop1),
lane(0, static_cast<V2>(vop2)));
1252 using VR = decltype(vopres);
1255 static_assert(
lanes<std::decay_t<VR> >() ==
lanes<std::decay_t<V1> >(),
1256 "The result must have the same number of lanes as the " 1263 using T = Scalar<decltype(vopres)>;
1271 == static_cast<T>(op(
lane(l, static_cast<V1>(vref1)),
1272 static_cast<T2>(sref2) )));
1281 #undef DUNE_SIMD_OPNAME 1284 template<
class V1,
class V2,
class Op>
1285 std::enable_if_t<!Std::is_detected_v<ScalarResult, Op, V1, V2> >
1286 checkBinaryOpVP(MetaType<V1>, MetaType<V2>, Op op)
1294 template<
class V1,
class V2>
1295 void checkBinaryOpVP(MetaType<V1>, MetaType<V2>, OpInfixComma)
1306 struct OpInfixSwappedArgs
1310 template<
class V1,
class V2>
1311 decltype(
auto) operator()(V1&& v1, V2&& v2)
const 1313 return orig(std::forward<V2>(v2), std::forward<V1>(v1));
1315 template<
class S1,
class S2>
1316 auto scalar(S1&& s1, S2&& s2)
const 1317 -> decltype(orig.scalar(std::forward<S2>(s2), std::forward<S1>(s1)));
1320 template<
class T1,
class V2,
class Op>
1321 void checkBinaryOpSV(MetaType<T1> t1, MetaType<V2> v2, Op op)
1323 checkBinaryOpVS(v2, t1, OpInfixSwappedArgs<Op>{op});
1326 template<
class T1,
class V2>
1327 void checkBinaryOpSV(MetaType<T1>, MetaType<V2>, OpInfixComma)
1329 static_assert(std::is_same<std::decay_t<T1>,
1330 Scalar<std::decay_t<V2> > >::value,
1331 "Internal testsystem error: called with a scalar that " 1332 "does not match the vector type.");
1334 checkCommaOp<T1, V2>(leftScalar<std::decay_t<T1>>(),
1335 rightVector<std::decay_t<V2>>());
1338 template<
class V1,
class V2,
class Op>
1339 void checkBinaryOpPV(MetaType<V1> v1, MetaType<V2> v2, Op op)
1341 checkBinaryOpVP(v2, v1, OpInfixSwappedArgs<Op>{op});
1344 template<
class V1,
class V2>
1345 void checkBinaryOpPV(MetaType<V1>, MetaType<V2>, OpInfixComma)
1376 template<
class T1,
class V2,
class Op>
1377 void checkBinaryOpVVAgainstSV(MetaType<T1> t1, MetaType<V2> v2, Op op)
1379 checkBinaryOpVVAgainstVS(v2, t1, OpInfixSwappedArgs<Op>{op});
1382 template<
class V1,
class T2>
1383 void checkBinaryOpVVAgainstSV(MetaType<V1>, MetaType<T2>, OpInfixComma)
1391 template<
class T1,
class T2,
bool condition,
class Checker>
1392 void checkBinaryRefQual(Checker checker)
1394 if constexpr (condition) {
1403 template<
class V,
class Checker>
1406 using std::bool_constant;
1408 constexpr
bool isMask = std::is_same<Scalar<V>,
bool>::value;
1410 constexpr
bool do_ =
false;
1411 constexpr
bool do_SV =
true;
1412 constexpr
bool do_VV =
true;
1413 constexpr
bool do_VS =
true;
1415 #define DUNE_SIMD_DO(M1, M2, M3, V1, V2, V3, NAME) \ 1416 checker(bool_constant<isMask ? do_##M1 : do_##V1>{}, \ 1417 bool_constant<isMask ? do_##M2 : do_##V2>{}, \ 1418 bool_constant<isMask ? do_##M3 : do_##V3>{}, \ 1471 void checkAutoCopy()
1473 using RValueResult = decltype(
autoCopy(
lane(0, std::declval<V>())));
1474 static_assert(std::is_same<RValueResult, Scalar<V> >::value,
1475 "Result of autoCopy() must always be Scalar<V>");
1477 using MutableLValueResult =
1479 static_assert(std::is_same<MutableLValueResult, Scalar<V> >::value,
1480 "Result of autoCopy() must always be Scalar<V>");
1482 using ConstLValueResult =
1484 static_assert(std::is_same<ConstLValueResult, Scalar<V> >::value,
1485 "Result of autoCopy() must always be Scalar<V>");
1487 V vec = make123<V>();
1488 for(std::size_t l = 0; l <
lanes(vec); ++l)
1494 void checkBoolReductions()
1536 auto mixedVec = broadcast<M>(0);
1537 for(std::size_t l = 0; l <
lanes(mixedVec); ++l)
1538 lane(l, mixedVec) = (l % 2);
1542 (
allTrue (static_cast<M&>(mixedVec)) ==
false);
1544 (
anyTrue (static_cast<M&>(mixedVec)) == (lanes<M>() > 1));
1546 (
allFalse(static_cast<M&>(mixedVec)) == (lanes<M>() == 1));
1548 (
anyFalse(static_cast<M&>(mixedVec)) ==
true);
1552 (
allTrue (static_cast<const M&>(mixedVec)) ==
false);
1554 (
anyTrue (static_cast<const M&>(mixedVec)) == (lanes<M>() > 1));
1556 (
allFalse(static_cast<const M&>(mixedVec)) == (lanes<M>() == 1));
1558 (
anyFalse(static_cast<const M&>(mixedVec)) ==
true);
1573 (std::is_same<decltype(
cond(std::declval<M>(), std::declval<V>(),
1574 std::declval<V>())), V>::value,
1575 "The result of cond(M, V, V) should have exactly the type V");
1578 (std::is_same<decltype(
cond(std::declval<const M&>(),
1579 std::declval<const V&>(),
1580 std::declval<const V&>())), V>::value,
1581 "The result of cond(const M&, const V&, const V&) should have " 1582 "exactly the type V");
1585 (std::is_same<decltype(
cond(std::declval<M&>(), std::declval<V&>(),
1586 std::declval<V&>())), V>::value,
1587 "The result of cond(M&, V&, V&) should have exactly the type V");
1589 V vec1 = leftVector<V>();
1590 V vec2 = rightVector<V>();
1595 auto mixedResult = broadcast<V>(0);
1596 auto mixedMask = broadcast<M>(
false);
1597 for(std::size_t l = 0; l <
lanes(mixedMask); ++l)
1599 lane(l, mixedMask ) = (l % 2);
1600 lane(l, mixedResult) =
lane(l, (l % 2) ? vec1 : vec2);
1607 void checkBoolCond()
1610 (std::is_same<decltype(
cond(std::declval<bool>(), std::declval<V>(),
1611 std::declval<V>())), V>::value,
1612 "The result of cond(bool, V, V) should have exactly the type V");
1615 (std::is_same<decltype(
cond(std::declval<const bool&>(),
1616 std::declval<const V&>(),
1617 std::declval<const V&>())), V>::value,
1618 "The result of cond(const bool&, const V&, const V&) should have " 1619 "exactly the type V");
1622 (std::is_same<decltype(
cond(std::declval<bool&>(),
1624 std::declval<V&>())), V>::value,
1625 "The result of cond(bool&, V&, V&) should have exactly the type V");
1627 V vec1 = leftVector<V>();
1628 V vec2 = rightVector<V>();
1635 std::enable_if_t<!Impl::LessThenComparable<Scalar<V> >::value>
1636 checkHorizontalMinMax() {}
1639 std::enable_if_t<Impl::LessThenComparable<Scalar<V> >::value>
1640 checkHorizontalMinMax()
1643 (std::is_same<decltype(
max(std::declval<V>())), Scalar<V> >::value,
1644 "The result of max(V) should be exactly Scalar<V>");
1647 (std::is_same<decltype(
min(std::declval<V>())), Scalar<V> >::value,
1648 "The result of min(V) should be exactly Scalar<V>");
1651 (std::is_same<decltype(
max(std::declval<V&>())), Scalar<V> >::value,
1652 "The result of max(V) should be exactly Scalar<V>");
1655 (std::is_same<decltype(
min(std::declval<V&>())), Scalar<V> >::value,
1656 "The result of min(V) should be exactly Scalar<V>");
1658 const V vec1 = leftVector<V>();
1665 std::enable_if_t<!Impl::LessThenComparable<Scalar<V> >::value>
1666 checkBinaryMinMax() {}
1669 std::enable_if_t<Impl::LessThenComparable<Scalar<V> >::value>
1676 (std::is_same<decltype(
Simd::max(std::declval<V>(),
1677 std::declval<V>())), V>::value,
1678 "The result of Simd::max(V, V) should be exactly V");
1680 (std::is_same<decltype(
Simd::min(std::declval<V>(),
1681 std::declval<V>())), V>::value,
1682 "The result of Simd::min(V, V) should be exactly V");
1685 (std::is_same<decltype(
Simd::max(std::declval<V&>(),
1686 std::declval<V&>())), V>::value,
1687 "The result of Simd::max(V&, V&) should be exactly V");
1689 (std::is_same<decltype(
Simd::min(std::declval<V&>(),
1690 std::declval<V&>())), V>::value,
1691 "The result of Simd::min(V&, V&) should be exactly V");
1693 const V arg1 = leftVector<V>();
1694 const V arg2 = rightVector<V>();
1696 V maxExp(Scalar<V>(0)), minExp(Scalar<V>(0));
1697 for(
auto l :
range(lanes<V>()))
1710 const V vec1 = leftVector<V>();
1712 std::string reference;
1714 const char *sep =
"";
1717 std::ostringstream stream;
1718 stream <<
lane(l, vec1);
1721 reference += stream.str();
1727 std::ostringstream stream;
1729 if(
lanes(vec1) == 1)
1736 std::ostringstream stream;
1737 stream <<
vio(vec1);
1742 #undef DUNE_SIMD_CHECK 1836 template<
class V,
class Rebinds,
1837 template<
class>
class RebindPrune =
IsLoop,
1841 if(seen_.emplace(typeid (V)).second ==
false)
1849 auto recurse = [
this](
auto w) {
1850 using W =
typename decltype(w)::type;
1851 this->
template check<W, Rebinds, RebindPrune, RebindAccept>();
1853 checkRebindOf<V, Rebinds, RebindPrune, RebindAccept>(recurse);
1868 static_assert(std::is_same<V, std::decay_t<V> >::value,
"Simd types " 1869 "must not be references, and must not include " 1872 log_ <<
"Checking SIMD type " << className<V>() << std::endl;
1876 checkBinaryOps<V>();
1880 constexpr
auto isMask =
typename std::is_same<Scalar<V>,
bool>::type{};
1885 checkDefaultConstruct<V>();
1887 checkCopyMoveConstruct<V>();
1889 checkBroadcast<V>();
1890 if constexpr (isMask)
1891 this->
template checkBroadcastMaskConstruct<V>();
1893 this->
template checkBroadcastVectorConstruct<V>();
1894 checkBracedAssign<V>();
1895 checkBracedBroadcastAssign<V>();
1901 if constexpr (isMask)
1902 this->
template checkBoolReductions<V>();
1905 checkHorizontalMinMax<V>();
1906 checkBinaryMinMax<V>();
1911 if constexpr (std::is_same_v<
Scalar<V>,
bool>) {
1913 auto check = [
this](
auto op) {
1914 this->
template checkUnaryOpsV<V>(op);
1929 check(OpPrefixLogicNot{});
1934 auto check = [
this](
auto op) {
1935 this->
template checkUnaryOpsV<V>(op);
1947 check(OpPrefixMinus{});
1948 check(OpPrefixLogicNot{});
1949 check(OpPrefixBitNot{});
1954 checkBinaryOpsVectorVector<V>();
1955 checkBinaryOpsScalarVector<V>();
1956 checkBinaryOpsVectorScalar<V>();
1957 checkBinaryOpsProxyVector<V>();
1958 checkBinaryOpsVectorProxy<V>();
1962 auto checker = [
this](
auto doSV,
auto doVV,
auto doVS,
auto op) {
1963 auto check = [
this,op](
auto t1,
auto t2) {
1964 this->checkBinaryOpVV(t1, t2, op);
1966 this->checkBinaryRefQual<V, V, doVV>(
check);
1968 checkBinaryOps<V>(checker);
1972 auto checker = [
this](
auto doSV,
auto doVV,
auto doVS,
auto op) {
1973 auto check = [
this,op](
auto t1,
auto t2) {
1974 this->checkBinaryOpSV(t1, t2, op);
1976 this->checkBinaryRefQual<Scalar<V>, V, doSV>(
check);
1978 auto crossCheck = [
this,op](
auto t1,
auto t2) {
1979 this->checkBinaryOpVVAgainstSV(t1, t2, op);
1981 this->checkBinaryRefQual<Scalar<V>, V, doSV && doVV>(crossCheck);
1983 checkBinaryOps<V>(checker);
1987 auto checker = [
this](
auto doSV,
auto doVV,
auto doVS,
auto op) {
1988 auto check = [
this,op](
auto t1,
auto t2) {
1989 this->checkBinaryOpVS(t1, t2, op);
1991 this->checkBinaryRefQual<V, Scalar<V>, doVS>(
check);
1993 auto crossCheck = [
this,op](
auto t1,
auto t2) {
1994 this->checkBinaryOpVVAgainstVS(t1, t2, op);
1996 this->checkBinaryRefQual<V, Scalar<V>, doVV && doVS>(crossCheck);
1998 checkBinaryOps<V>(checker);
2002 auto checker = [
this](
auto doSV,
auto doVV,
auto doVS,
auto op) {
2003 auto check = [
this,op](
auto t1,
auto t2) {
2004 this->checkBinaryOpPV(t1, t2, op);
2006 this->checkBinaryRefQual<V, V, doSV>(
check);
2008 checkBinaryOps<V>(checker);
2012 auto checker = [
this](
auto doSV,
auto doVV,
auto doVS,
auto op) {
2013 auto check = [
this,op](
auto t1,
auto t2) {
2014 this->checkBinaryOpVP(t1, t2, op);
2016 this->checkBinaryRefQual<V, V, doVS>(
check);
2018 checkBinaryOps<V>(checker);
2024 #endif // DUNE_COMMON_SIMD_TEST_HH constexpr std::size_t lanes()
Number of lanes in a SIMD type.
Definition: simd/interface.hh:305
void checkBinaryOpsVectorScalar()
Definition: test.hh:1985
typename Impl::RemoveEnd< EndMark, TypeList< Types... > >::type RebindList
A list of types with the final element removed.
Definition: test.hh:158
auto maskAnd(const V1 &v1, const V2 &v2)
Logic and of masks.
Definition: simd/interface.hh:509
auto io(const V &v)
construct a stream inserter
Definition: io.hh:106
void checkUnaryOps()
Definition: test.hh:1909
typename Impl::IsLoop< T >::type IsLoop
check whether a type is an instance of LoopSIMD
Definition: test.hh:162
decltype(auto) lane(std::size_t l, V &&v)
Extract an element of a SIMD type.
Definition: simd/interface.hh:324
constexpr AutonomousValue< T > autoCopy(T &&v)
Autonomous copy of an expression's value for use in auto type deduction.
Definition: typetraits.hh:650
constexpr auto max
Function object that returns the greater of the given values.
Definition: hybridutilities.hh:489
bool anyFalse(const Mask &mask)
Whether any entry is false
Definition: simd/interface.hh:449
constexpr std::integral_constant< std::size_t, sizeof...(II)> size(std::integer_sequence< T, II... >)
Return the size of the sequence.
Definition: integersequence.hh:75
void checkNonOps()
Definition: test.hh:1878
#define DUNE_SIMD_CHECK(expr)
Definition: test.hh:186
void checkBinaryOpsVectorProxy()
Definition: test.hh:2010
auto maskOr(const V1 &v1, const V2 &v2)
Logic or of masks.
Definition: simd/interface.hh:499
typename Impl::voider< Types... >::type void_t
Is void for all valid input types. The workhorse for C++11 SFINAE-techniques.
Definition: typetraits.hh:40
void checkBinaryOpsProxyVector()
Definition: test.hh:2000
void checkBinaryOpsScalarVector()
Definition: test.hh:1970
void checkBinaryOps()
Definition: test.hh:1952
Utilities for reduction like operations on ranges.
IO interface of the SIMD abstractionThis file provides IO interface functions of the SIMD abstraction...
typename Overloads::ScalarType< std::decay_t< V > >::type Scalar
Element type of some SIMD type.
Definition: simd/interface.hh:235
Dune namespace
Definition: alignedallocator.hh:12
A free function to provide the demangled class name of a given object or type as a string...
std::tuple< MetaType< T >... > TypeList
A simple type list.
Definition: typelist.hh:87
static constexpr IntegralRange< std::decay_t< T > > range(T &&from, U &&to) noexcept
free standing function for setting up a range based for loop over an integer range for (auto i: range...
Definition: rangeutilities.hh:288
template which always yields a true value
Definition: typetraits.hh:134
void checkBinaryOpsVectorVector()
Definition: test.hh:1960
typename Overloads::RebindType< std::decay_t< S >, std::decay_t< V > >::type Rebind
Construct SIMD type with different scalar type.
Definition: simd/interface.hh:253
auto max(const V &v1, const V &v2)
The binary maximum value over two simd objects.
Definition: simd/interface.hh:409
constexpr auto min
Function object that returns the smaller of the given values.
Definition: hybridutilities.hh:511
#define DUNE_SIMD_DO(M1, M2, M3, V1, V2, V3, NAME)
bool allTrue(const Mask &mask)
Whether all entries are true
Definition: simd/interface.hh:439
bool allFalse(const Mask &mask)
Whether all entries are false
Definition: simd/interface.hh:459
bool anyTrue(const Mask &mask)
Whether any entry is true
Definition: simd/interface.hh:429
auto vio(const V &v)
construct a stream inserter
Definition: io.hh:90
auto min(const V &v1, const V &v2)
The binary minimum value over two simd objects.
Definition: simd/interface.hh:419
Traits for type conversions and type information.
constexpr void forEach(Range &&range, F &&f)
Range based for loop.
Definition: hybridutilities.hh:261
void check()
run unit tests for simd vector type V
Definition: test.hh:1839
final element marker for RebindList
Definition: test.hh:145
#define DUNE_SIMD_CHECK_OP(expr)
Definition: test.hh:191
Include file for users of the SIMD abstraction layer.
bool good() const
whether all tests succeeded
Definition: test.hh:1859
V cond(M &&mask, const V &ifTrue, const V &ifFalse)
Like the ?: operator.
Definition: simd/interface.hh:386
void checkType()
Definition: test.hh:1866