|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +// UNSUPPORTED: c++03, c++11, c++14 |
| 10 | + |
| 11 | +// <experimental/simd> |
| 12 | +// |
| 13 | +// [simd.reference] |
| 14 | +// template<class U> reference+=(U&& x) && noexcept; |
| 15 | +// template<class U> reference-=(U&& x) && noexcept; |
| 16 | +// template<class U> reference*=(U&& x) && noexcept; |
| 17 | +// template<class U> reference/=(U&& x) && noexcept; |
| 18 | +// template<class U> reference%=(U&& x) && noexcept; |
| 19 | +// template<class U> reference|=(U&& x) && noexcept; |
| 20 | +// template<class U> reference&=(U&& x) && noexcept; |
| 21 | +// template<class U> reference^=(U&& x) && noexcept; |
| 22 | +// template<class U> reference<<=(U&& x) && noexcept; |
| 23 | +// template<class U> reference>>=(U&& x) && noexcept; |
| 24 | + |
| 25 | +#include "../test_utils.h" |
| 26 | +#include <experimental/simd> |
| 27 | + |
| 28 | +namespace ex = std::experimental::parallelism_v2; |
| 29 | + |
| 30 | +struct PlusAssign { |
| 31 | + template <typename T, typename U> |
| 32 | + void operator()(T&& lhs, const U& rhs) const noexcept { |
| 33 | + std::forward<T>(lhs) += rhs; |
| 34 | + } |
| 35 | +}; |
| 36 | + |
| 37 | +struct MinusAssign { |
| 38 | + template <typename T, typename U> |
| 39 | + void operator()(T&& lhs, const U& rhs) const noexcept { |
| 40 | + std::forward<T>(lhs) -= rhs; |
| 41 | + } |
| 42 | +}; |
| 43 | + |
| 44 | +struct MultipliesAssign { |
| 45 | + template <typename T, typename U> |
| 46 | + void operator()(T&& lhs, const U& rhs) const noexcept { |
| 47 | + std::forward<T>(lhs) *= rhs; |
| 48 | + } |
| 49 | +}; |
| 50 | + |
| 51 | +struct DividesAssign { |
| 52 | + template <typename T, typename U> |
| 53 | + void operator()(T&& lhs, const U& rhs) const noexcept { |
| 54 | + std::forward<T>(lhs) /= rhs; |
| 55 | + } |
| 56 | +}; |
| 57 | + |
| 58 | +struct ModulusAssign { |
| 59 | + template <typename T, typename U> |
| 60 | + void operator()(T&& lhs, const U& rhs) const noexcept { |
| 61 | + std::forward<T>(lhs) %= rhs; |
| 62 | + } |
| 63 | +}; |
| 64 | + |
| 65 | +struct AndAssign { |
| 66 | + template <typename T, typename U> |
| 67 | + void operator()(T&& lhs, const U& rhs) const noexcept { |
| 68 | + std::forward<T>(lhs) &= rhs; |
| 69 | + } |
| 70 | +}; |
| 71 | + |
| 72 | +struct OrAssign { |
| 73 | + template <typename T, typename U> |
| 74 | + void operator()(T&& lhs, const U& rhs) const noexcept { |
| 75 | + std::forward<T>(lhs) |= rhs; |
| 76 | + } |
| 77 | +}; |
| 78 | + |
| 79 | +struct XorAssign { |
| 80 | + template <typename T, typename U> |
| 81 | + void operator()(T&& lhs, const U& rhs) const noexcept { |
| 82 | + std::forward<T>(lhs) ^= rhs; |
| 83 | + } |
| 84 | +}; |
| 85 | + |
| 86 | +struct LeftShiftAssign { |
| 87 | + template <typename T, typename U> |
| 88 | + void operator()(T&& lhs, const U& rhs) const noexcept { |
| 89 | + std::forward<T>(lhs) <<= rhs; |
| 90 | + } |
| 91 | +}; |
| 92 | + |
| 93 | +struct RightShiftAssign { |
| 94 | + template <typename T, typename U> |
| 95 | + void operator()(T&& lhs, const U& rhs) const noexcept { |
| 96 | + std::forward<T>(lhs) >>= rhs; |
| 97 | + } |
| 98 | +}; |
| 99 | + |
| 100 | +struct LeftShift { |
| 101 | + template <typename T, typename U> |
| 102 | + T operator()(const T& lhs, const U& rhs) const noexcept { |
| 103 | + return lhs << rhs; |
| 104 | + } |
| 105 | +}; |
| 106 | + |
| 107 | +struct RightShift { |
| 108 | + template <typename T, typename U> |
| 109 | + T operator()(const T& lhs, const U& rhs) const noexcept { |
| 110 | + return lhs >> rhs; |
| 111 | + } |
| 112 | +}; |
| 113 | + |
| 114 | +template <typename T, typename SimdAbi, typename Op, typename OpAssign> |
| 115 | +struct SimdReferenceOperatorHelper { |
| 116 | + template <class U> |
| 117 | + void operator()() const { |
| 118 | + ex::simd<T, SimdAbi> origin_simd(static_cast<T>(3)); |
| 119 | + static_assert(noexcept(OpAssign{}(origin_simd[0], static_cast<U>(2)))); |
| 120 | + OpAssign{}(origin_simd[0], static_cast<U>(2)); |
| 121 | + assert((T)origin_simd[0] == (T)Op{}(static_cast<T>(3), static_cast<T>(std::forward<U>(2)))); |
| 122 | + } |
| 123 | +}; |
| 124 | + |
| 125 | +template <typename T, typename SimdAbi, typename Op, typename OpAssign> |
| 126 | +struct MaskReferenceOperatorHelper { |
| 127 | + template <class U> |
| 128 | + void operator()() const { |
| 129 | + ex::simd_mask<T, SimdAbi> origin_mask(true); |
| 130 | + static_assert(noexcept(OpAssign{}(origin_mask[0], static_cast<U>(false)))); |
| 131 | + OpAssign{}(origin_mask[0], static_cast<U>(false)); |
| 132 | + assert((bool)origin_mask[0] == (bool)Op{}(true, static_cast<bool>(std::forward<U>(false)))); |
| 133 | + } |
| 134 | +}; |
| 135 | + |
| 136 | +template <class T, std::size_t> |
| 137 | +struct CheckReferenceArithOperators { |
| 138 | + template <class SimdAbi> |
| 139 | + void operator()() { |
| 140 | + types::for_each(simd_test_types(), SimdReferenceOperatorHelper<T, SimdAbi, std::plus<>, PlusAssign>()); |
| 141 | + types::for_each(simd_test_types(), SimdReferenceOperatorHelper<T, SimdAbi, std::minus<>, MinusAssign>()); |
| 142 | + types::for_each(simd_test_types(), SimdReferenceOperatorHelper<T, SimdAbi, std::multiplies<>, MultipliesAssign>()); |
| 143 | + types::for_each(simd_test_types(), SimdReferenceOperatorHelper<T, SimdAbi, std::divides<>, DividesAssign>()); |
| 144 | + } |
| 145 | +}; |
| 146 | + |
| 147 | +template <class T, std::size_t> |
| 148 | +struct CheckReferenceIntOperators { |
| 149 | + template <class SimdAbi> |
| 150 | + void operator()() { |
| 151 | + types::for_each( |
| 152 | + simd_test_integer_types(), SimdReferenceOperatorHelper<T, SimdAbi, std::modulus<>, ModulusAssign>()); |
| 153 | + types::for_each(simd_test_integer_types(), SimdReferenceOperatorHelper<T, SimdAbi, std::bit_and<>, AndAssign>()); |
| 154 | + types::for_each(simd_test_integer_types(), SimdReferenceOperatorHelper<T, SimdAbi, std::bit_or<>, OrAssign>()); |
| 155 | + types::for_each(simd_test_integer_types(), SimdReferenceOperatorHelper<T, SimdAbi, std::bit_xor<>, XorAssign>()); |
| 156 | + types::for_each(simd_test_integer_types(), SimdReferenceOperatorHelper<T, SimdAbi, LeftShift, LeftShiftAssign>()); |
| 157 | + types::for_each(simd_test_integer_types(), SimdReferenceOperatorHelper<T, SimdAbi, RightShift, RightShiftAssign>()); |
| 158 | + |
| 159 | + types::for_each(simd_test_integer_types(), MaskReferenceOperatorHelper<T, SimdAbi, std::bit_and<>, AndAssign>()); |
| 160 | + types::for_each(simd_test_integer_types(), MaskReferenceOperatorHelper<T, SimdAbi, std::bit_or<>, OrAssign>()); |
| 161 | + types::for_each(simd_test_integer_types(), MaskReferenceOperatorHelper<T, SimdAbi, std::bit_xor<>, XorAssign>()); |
| 162 | + } |
| 163 | +}; |
| 164 | + |
| 165 | +int main(int, char**) { |
| 166 | + test_all_simd_abi<CheckReferenceArithOperators>(); |
| 167 | + types::for_each(types::integer_types(), TestAllSimdAbiFunctor<CheckReferenceIntOperators>()); |
| 168 | + return 0; |
| 169 | +} |
0 commit comments