1 // -*- C++ -*- 2 //===-- adjacent_difference.pass.cpp --------------------------------------===// 3 // 4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 // See https://llvm.org/LICENSE.txt for license information. 6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "support/pstl_test_config.h" 11 12 #include <iterator> 13 #include <execution> 14 #include <numeric> 15 16 #include "support/utils.h" 17 18 using namespace TestUtils; 19 20 template <typename T> 21 struct wrapper 22 { 23 T t; 24 constexpr explicit wrapper(T t_) : t(t_) {} 25 template <typename T2> 26 constexpr wrapper(const wrapper<T2>& a) 27 { 28 t = a.t; 29 } 30 template <typename T2> 31 constexpr void 32 operator=(const wrapper<T2>& a) 33 { 34 t = a.t; 35 } 36 constexpr wrapper<T> 37 operator-(const wrapper<T>& a) const 38 { 39 return wrapper<T>(t - a.t); 40 } 41 }; 42 43 template <typename T> 44 bool 45 compare(const T& a, const T& b) 46 { 47 return a == b; 48 } 49 50 template <typename T> 51 bool 52 compare(const wrapper<T>& a, const wrapper<T>& b) 53 { 54 return a.t == b.t; 55 } 56 57 template <typename Iterator1, typename Iterator2, typename T, typename Function> 58 typename std::enable_if<!std::is_floating_point<T>::value, bool>::type 59 compute_and_check(Iterator1 first, Iterator1 last, Iterator2 d_first, T, Function f) 60 { 61 using T2 = typename std::iterator_traits<Iterator2>::value_type; 62 63 if (first == last) 64 return true; 65 66 { 67 T2 temp(*first); 68 if (!compare(temp, *d_first)) 69 return false; 70 } 71 Iterator1 second = std::next(first); 72 73 ++d_first; 74 for (; second != last; ++first, ++second, ++d_first) 75 { 76 T2 temp(f(*second, *first)); 77 if (!compare(temp, *d_first)) 78 return false; 79 } 80 81 return true; 82 } 83 84 // we don't want to check equality here 85 // because we can't be sure it will be strictly equal for floating point types 86 template <typename Iterator1, typename Iterator2, typename T, typename Function> 87 typename std::enable_if<std::is_floating_point<T>::value, bool>::type 88 compute_and_check(Iterator1, Iterator1, Iterator2, T, Function) 89 { 90 return true; 91 } 92 93 struct test_one_policy 94 { 95 #if _PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN || \ 96 _PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN // dummy specialization by policy type, in case of broken configuration 97 template <typename Iterator1, typename Iterator2, typename T, typename Function> 98 typename std::enable_if<is_same_iterator_category<Iterator1, std::random_access_iterator_tag>::value, void>::type 99 operator()(pstl::execution::unsequenced_policy, Iterator1 data_b, Iterator1 data_e, Iterator2 actual_b, 100 Iterator2 actual_e, T trash, Function f) 101 { 102 } 103 template <typename Iterator1, typename Iterator2, typename T, typename Function> 104 typename std::enable_if<is_same_iterator_category<Iterator1, std::random_access_iterator_tag>::value, void>::type 105 operator()(pstl::execution::parallel_unsequenced_policy, Iterator1 data_b, Iterator1 data_e, Iterator2 actual_b, 106 Iterator2 actual_e, T trash, Function f) 107 { 108 } 109 #endif 110 111 template <typename ExecutionPolicy, typename Iterator1, typename Iterator2, typename T, typename Function> 112 void 113 operator()(ExecutionPolicy&& exec, Iterator1 data_b, Iterator1 data_e, Iterator2 actual_b, Iterator2 actual_e, 114 T trash, Function f) 115 { 116 using namespace std; 117 using T2 = typename std::iterator_traits<Iterator1>::value_type; 118 119 fill(actual_b, actual_e, trash); 120 121 Iterator2 actual_return = adjacent_difference(exec, data_b, data_e, actual_b); 122 EXPECT_TRUE(compute_and_check(data_b, data_e, actual_b, T2(0), std::minus<T2>()), 123 "wrong effect of adjacent_difference"); 124 EXPECT_TRUE(actual_return == actual_e, "wrong result of adjacent_difference"); 125 126 fill(actual_b, actual_e, trash); 127 128 actual_return = adjacent_difference(exec, data_b, data_e, actual_b, f); 129 EXPECT_TRUE(compute_and_check(data_b, data_e, actual_b, T2(0), f), 130 "wrong effect of adjacent_difference with functor"); 131 EXPECT_TRUE(actual_return == actual_e, "wrong result of adjacent_difference with functor"); 132 } 133 }; 134 135 template <typename T1, typename T2, typename Pred> 136 void 137 test(Pred pred) 138 { 139 const std::size_t max_len = 100000; 140 141 static constexpr T2 value = T2(77); 142 static constexpr T1 trash = T1(31); 143 144 Sequence<T1> actual(max_len, [](std::size_t i) { return T1(i); }); 145 146 Sequence<T2> data(max_len, [](std::size_t i) { return i % 3 == 2 ? T2(i * i) : value; }); 147 148 for (std::size_t len = 0; len < max_len; len = len <= 16 ? len + 1 : std::size_t(3.1415 * len)) 149 { 150 invoke_on_all_policies(test_one_policy(), data.begin(), data.begin() + len, actual.begin(), 151 actual.begin() + len, trash, pred); 152 invoke_on_all_policies(test_one_policy(), data.cbegin(), data.cbegin() + len, actual.begin(), 153 actual.begin() + len, trash, pred); 154 } 155 } 156 157 int32_t 158 main() 159 { 160 test<uint8_t, uint32_t>([](uint32_t a, uint32_t b) { return a - b; }); 161 test<int32_t, int64_t>([](int64_t a, int64_t b) { return a / (b + 1); }); 162 test<int64_t, float32_t>([](float32_t a, float32_t b) { return (a + b) / 2; }); 163 test<wrapper<int32_t>, wrapper<int64_t>>( 164 [](const wrapper<int64_t>& a, const wrapper<int64_t>& b) { return a - b; }); 165 166 std::cout << done() << std::endl; 167 return 0; 168 } 169