|
| 1 | +// RUN: %clangxx -fsycl -fsyntax-only %s |
| 2 | + |
| 3 | +// Tests the existance and specializations of known identities. |
| 4 | + |
| 5 | +#include <sycl/sycl.hpp> |
| 6 | + |
| 7 | +#include <cassert> |
| 8 | +#include <limits> |
| 9 | +#include <type_traits> |
| 10 | + |
| 11 | +template <typename BinOp, typename OperandT> constexpr void checkNoIdentity() { |
| 12 | + static_assert(!sycl::has_known_identity<BinOp, OperandT>::value, |
| 13 | + "Operation should not have a known identity!"); |
| 14 | + static_assert(!sycl::has_known_identity_v<BinOp, OperandT>, |
| 15 | + "Operation should not have a known identity!"); |
| 16 | +} |
| 17 | + |
| 18 | +#define CHECK_IDENTITY(BINOP, OPERAND, EXPECTED) \ |
| 19 | + static_assert(sycl::has_known_identity<BINOP, OPERAND>::value, \ |
| 20 | + "No trait specialization for known identity!"); \ |
| 21 | + static_assert(sycl::has_known_identity_v<BINOP, OPERAND>, \ |
| 22 | + "No trait specialization for known identity!"); \ |
| 23 | + static_assert(sycl::known_identity<BINOP, OPERAND>::value == EXPECTED, \ |
| 24 | + "Identity does not match expected."); \ |
| 25 | + static_assert(sycl::known_identity_v<BINOP, OPERAND> == EXPECTED, \ |
| 26 | + "Identity does not match expected."); |
| 27 | + |
| 28 | +template <typename OperandT> constexpr void checkAll() { |
| 29 | + if constexpr (std::is_arithmetic_v<OperandT> || |
| 30 | + std::is_same_v<std::remove_cv_t<OperandT>, sycl::half>) { |
| 31 | + CHECK_IDENTITY(sycl::plus<OperandT>, OperandT, OperandT{}); |
| 32 | + CHECK_IDENTITY(sycl::plus<>, OperandT, OperandT{}); |
| 33 | + CHECK_IDENTITY(sycl::multiplies<OperandT>, OperandT, OperandT{1}); |
| 34 | + CHECK_IDENTITY(sycl::multiplies<>, OperandT, OperandT{1}); |
| 35 | + } else { |
| 36 | + checkNoIdentity<sycl::plus<OperandT>, OperandT>(); |
| 37 | + checkNoIdentity<sycl::plus<>, OperandT>(); |
| 38 | + checkNoIdentity<sycl::multiplies<OperandT>, OperandT>(); |
| 39 | + checkNoIdentity<sycl::multiplies<>, OperandT>(); |
| 40 | + } |
| 41 | + |
| 42 | + if constexpr (std::is_integral_v<OperandT>) { |
| 43 | + CHECK_IDENTITY(sycl::bit_and<OperandT>, OperandT, |
| 44 | + static_cast<OperandT>(~OperandT{})); |
| 45 | + CHECK_IDENTITY(sycl::bit_and<>, OperandT, |
| 46 | + static_cast<OperandT>(~OperandT{})); |
| 47 | + CHECK_IDENTITY(sycl::bit_or<OperandT>, OperandT, OperandT{}); |
| 48 | + CHECK_IDENTITY(sycl::bit_or<>, OperandT, OperandT{}); |
| 49 | + CHECK_IDENTITY(sycl::bit_xor<OperandT>, OperandT, OperandT{}); |
| 50 | + CHECK_IDENTITY(sycl::bit_xor<>, OperandT, OperandT{}); |
| 51 | + CHECK_IDENTITY(sycl::minimum<OperandT>, OperandT, |
| 52 | + std::numeric_limits<OperandT>::max()); |
| 53 | + CHECK_IDENTITY(sycl::minimum<>, OperandT, |
| 54 | + std::numeric_limits<OperandT>::max()); |
| 55 | + CHECK_IDENTITY(sycl::maximum<OperandT>, OperandT, |
| 56 | + std::numeric_limits<OperandT>::lowest()); |
| 57 | + CHECK_IDENTITY(sycl::maximum<>, OperandT, |
| 58 | + std::numeric_limits<OperandT>::lowest()); |
| 59 | + } else { |
| 60 | + checkNoIdentity<sycl::bit_and<OperandT>, OperandT>(); |
| 61 | + checkNoIdentity<sycl::bit_and<>, OperandT>(); |
| 62 | + checkNoIdentity<sycl::bit_or<OperandT>, OperandT>(); |
| 63 | + checkNoIdentity<sycl::bit_or<>, OperandT>(); |
| 64 | + checkNoIdentity<sycl::bit_xor<OperandT>, OperandT>(); |
| 65 | + checkNoIdentity<sycl::bit_xor<>, OperandT>(); |
| 66 | + } |
| 67 | + |
| 68 | + // The implementation is relaxed about logical operators to allow implicit |
| 69 | + // conversions for logical operators, so negative checks are not used for this |
| 70 | + // case. |
| 71 | + if constexpr (std::is_same_v<std::remove_cv_t<OperandT>, bool>) { |
| 72 | + CHECK_IDENTITY(sycl::logical_and<OperandT>, OperandT, true); |
| 73 | + CHECK_IDENTITY(sycl::logical_and<>, OperandT, true); |
| 74 | + CHECK_IDENTITY(sycl::logical_or<OperandT>, OperandT, false); |
| 75 | + CHECK_IDENTITY(sycl::logical_or<>, OperandT, false); |
| 76 | + } |
| 77 | + |
| 78 | + if constexpr (std::is_floating_point_v<OperandT> || |
| 79 | + std::is_same_v<std::remove_cv_t<OperandT>, sycl::half>) { |
| 80 | + CHECK_IDENTITY(sycl::minimum<OperandT>, OperandT, |
| 81 | + std::numeric_limits<OperandT>::infinity()); |
| 82 | + CHECK_IDENTITY(sycl::minimum<>, OperandT, |
| 83 | + std::numeric_limits<OperandT>::infinity()); |
| 84 | + CHECK_IDENTITY(sycl::maximum<OperandT>, OperandT, |
| 85 | + -std::numeric_limits<OperandT>::infinity()); |
| 86 | + CHECK_IDENTITY(sycl::maximum<>, OperandT, |
| 87 | + -std::numeric_limits<OperandT>::infinity()); |
| 88 | + } |
| 89 | + |
| 90 | + if constexpr (!std::is_integral_v<OperandT> && |
| 91 | + !std::is_floating_point_v<OperandT> && |
| 92 | + !std::is_same_v<std::remove_cv_t<OperandT>, sycl::half>) { |
| 93 | + checkNoIdentity<sycl::minimum<OperandT>, OperandT>(); |
| 94 | + checkNoIdentity<sycl::minimum<>, OperandT>(); |
| 95 | + checkNoIdentity<sycl::maximum<OperandT>, OperandT>(); |
| 96 | + checkNoIdentity<sycl::maximum<>, OperandT>(); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +struct CustomType {}; |
| 101 | + |
| 102 | +int main() { |
| 103 | + checkAll<bool>(); |
| 104 | + checkAll<char>(); |
| 105 | + checkAll<short>(); |
| 106 | + checkAll<int>(); |
| 107 | + checkAll<long>(); |
| 108 | + checkAll<long long>(); |
| 109 | + checkAll<signed char>(); |
| 110 | + checkAll<unsigned char>(); |
| 111 | + checkAll<unsigned int>(); |
| 112 | + checkAll<unsigned long>(); |
| 113 | + checkAll<unsigned long long>(); |
| 114 | + checkAll<float>(); |
| 115 | + checkAll<double>(); |
| 116 | + checkAll<sycl::half>(); |
| 117 | + checkAll<CustomType>(); |
| 118 | + return 0; |
| 119 | +} |
0 commit comments