| 1 |
// -*- C++ -*- |
| 2 |
// { dg-options "-ltbb" } |
| 3 |
// { dg-do run { target c++17 } } |
| 4 |
// { dg-timeout-factor 3 } |
| 5 |
// { dg-require-effective-target tbb_backend } |
| 6 |
|
| 7 |
//===-- find_if.pass.cpp --------------------------------------------------===// |
| 8 |
// |
| 9 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 10 |
// See https://llvm.org/LICENSE.txt for license information. |
| 11 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 12 |
// |
| 13 |
//===----------------------------------------------------------------------===// |
| 14 |
|
| 15 |
// Tests for find_if and find_if_not |
| 16 |
#include "pstl/pstl_test_config.h" |
| 17 |
|
| 18 |
#ifdef PSTL_STANDALONE_TESTS |
| 19 |
#include "pstl/execution" |
| 20 |
#include "pstl/algorithm" |
| 21 |
#else |
| 22 |
#include <execution> |
| 23 |
#include <algorithm> |
| 24 |
#endif // PSTL_STANDALONE_TESTS |
| 25 |
|
| 26 |
#include "pstl/test_utils.h" |
| 27 |
|
| 28 |
using namespace TestUtils; |
| 29 |
|
| 30 |
struct test_find_if |
| 31 |
{ |
| 32 |
#if defined(_PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) || \ |
| 33 |
defined(_PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) //dummy specialization by policy type, in case of broken configuration |
| 34 |
template <typename Iterator, typename Predicate, typename NotPredicate> |
| 35 |
void |
| 36 |
operator()(__pstl::execution::unsequenced_policy, Iterator first, Iterator last, Predicate pred, |
| 37 |
NotPredicate not_pred) |
| 38 |
{ |
| 39 |
} |
| 40 |
template <typename Iterator, typename Predicate, typename NotPredicate> |
| 41 |
void |
| 42 |
operator()(__pstl::execution::parallel_unsequenced_policy, Iterator first, Iterator last, Predicate pred, |
| 43 |
NotPredicate not_pred) |
| 44 |
{ |
| 45 |
} |
| 46 |
#endif |
| 47 |
|
| 48 |
template <typename Policy, typename Iterator, typename Predicate, typename NotPredicate> |
| 49 |
void |
| 50 |
operator()(Policy&& exec, Iterator first, Iterator last, Predicate pred, NotPredicate not_pred) |
| 51 |
{ |
| 52 |
auto i = std::find_if(first, last, pred); |
| 53 |
auto j = find_if(exec, first, last, pred); |
| 54 |
EXPECT_TRUE(i == j, "wrong return value from find_if"); |
| 55 |
auto i_not = find_if_not(exec, first, last, not_pred); |
| 56 |
EXPECT_TRUE(i_not == i, "wrong return value from find_if_not"); |
| 57 |
} |
| 58 |
}; |
| 59 |
|
| 60 |
template <typename T, typename Predicate, typename Hit, typename Miss> |
| 61 |
void |
| 62 |
test(Predicate pred, Hit hit, Miss miss) |
| 63 |
{ |
| 64 |
auto not_pred = [pred](T x) { return !pred(x); }; |
| 65 |
// Try sequences of various lengths. |
| 66 |
for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) |
| 67 |
{ |
| 68 |
Sequence<T> in(n, [&](size_t k) -> T { return miss(n ^ k); }); |
| 69 |
// Try different find positions, including not found. |
| 70 |
// By going backwards, we can add extra matches that are *not* supposed to be found. |
| 71 |
// The decreasing exponential gives us O(n) total work for the loop since each find takes O(m) time. |
| 72 |
for (size_t m = n; m > 0; m *= 0.6) |
| 73 |
{ |
| 74 |
if (m < n) |
| 75 |
in[m] = hit(n ^ m); |
| 76 |
invoke_on_all_policies(test_find_if(), in.begin(), in.end(), pred, not_pred); |
| 77 |
invoke_on_all_policies(test_find_if(), in.cbegin(), in.cend(), pred, not_pred); |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
struct test_non_const |
| 83 |
{ |
| 84 |
template <typename Policy, typename Iterator> |
| 85 |
void |
| 86 |
operator()(Policy&& exec, Iterator iter) |
| 87 |
{ |
| 88 |
auto is_even = [&](float64_t v) { |
| 89 |
uint32_t i = (uint32_t)v; |
| 90 |
return i % 2 == 0; |
| 91 |
}; |
| 92 |
|
| 93 |
invoke_if(exec, [&]() { |
| 94 |
find_if(exec, iter, iter, non_const(is_even)); |
| 95 |
find_if_not(exec, iter, iter, non_const(is_even)); |
| 96 |
}); |
| 97 |
} |
| 98 |
}; |
| 99 |
|
| 100 |
int |
| 101 |
main() |
| 102 |
{ |
| 103 |
#if !defined(_PSTL_ICC_17_TEST_MAC_RELEASE_32_BROKEN) |
| 104 |
// Note that the "hit" and "miss" functions here avoid overflow issues. |
| 105 |
test<Number>(IsMultiple(5, OddTag()), [](int32_t j) { return Number(j - j % 5, OddTag()); }, // hit |
| 106 |
[](int32_t j) { return Number(j % 5 == 0 ? j ^ 1 : j, OddTag()); }); // miss |
| 107 |
#endif |
| 108 |
|
| 109 |
// Try type for which algorithm can really be vectorized. |
| 110 |
test<float32_t>([](float32_t x) { return x >= 0; }, [](float32_t j) { return j * j; }, |
| 111 |
[](float32_t j) { return -1 - j * j; }); |
| 112 |
|
| 113 |
test_algo_basic_single<int32_t>(run_for_rnd_fw<test_non_const>()); |
| 114 |
|
| 115 |
std::cout << done() << std::endl; |
| 116 |
return 0; |
| 117 |
} |