| 1 |
// -*- C++ -*- |
| 2 |
// { dg-options "-ltbb" } |
| 3 |
// { dg-do run { target c++17 } } |
| 4 |
// { dg-timeout-factor 4 } |
| 5 |
// { dg-require-effective-target tbb_backend } |
| 6 |
|
| 7 |
//===-- find_end.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 |
#include "pstl/pstl_test_config.h" |
| 16 |
|
| 17 |
#ifdef PSTL_STANDALONE_TESTS |
| 18 |
#include "pstl/execution" |
| 19 |
#include "pstl/algorithm" |
| 20 |
#else |
| 21 |
#include <execution> |
| 22 |
#include <algorithm> |
| 23 |
#endif // PSTL_STANDALONE_TESTS |
| 24 |
|
| 25 |
#include "pstl/test_utils.h" |
| 26 |
|
| 27 |
using namespace TestUtils; |
| 28 |
|
| 29 |
struct test_find |
| 30 |
{ |
| 31 |
#if defined(_PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) || \ |
| 32 |
defined(_PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) //dummy specialization by policy type, in case of broken configuration |
| 33 |
template <typename Iterator, typename Value> |
| 34 |
void |
| 35 |
operator()(__pstl::execution::unsequenced_policy, Iterator first, Iterator last, Value value) |
| 36 |
{ |
| 37 |
} |
| 38 |
template <typename Iterator, typename Value> |
| 39 |
void |
| 40 |
operator()(__pstl::execution::parallel_unsequenced_policy, Iterator first, Iterator last, Value value) |
| 41 |
{ |
| 42 |
} |
| 43 |
#endif |
| 44 |
|
| 45 |
template <typename Policy, typename Iterator, typename Value> |
| 46 |
void |
| 47 |
operator()(Policy&& exec, Iterator first, Iterator last, Value value) |
| 48 |
{ |
| 49 |
auto i = std::find(first, last, value); |
| 50 |
auto j = find(exec, first, last, value); |
| 51 |
EXPECT_TRUE(i == j, "wrong return value from find"); |
| 52 |
} |
| 53 |
}; |
| 54 |
|
| 55 |
template <typename T, typename Value, typename Hit, typename Miss> |
| 56 |
void |
| 57 |
test(Value value, Hit hit, Miss miss) |
| 58 |
{ |
| 59 |
// Try sequences of various lengths. |
| 60 |
for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) |
| 61 |
{ |
| 62 |
Sequence<T> in(n, [&](size_t k) -> T { return miss(n ^ k); }); |
| 63 |
// Try different find positions, including not found. |
| 64 |
// By going backwards, we can add extra matches that are *not* supposed to be found. |
| 65 |
// The decreasing exponential gives us O(n) total work for the loop since each find takes O(m) time. |
| 66 |
for (size_t m = n; m > 0; m *= 0.6) |
| 67 |
{ |
| 68 |
if (m < n) |
| 69 |
in[m] = hit(n ^ m); |
| 70 |
invoke_on_all_policies(test_find(), in.begin(), in.end(), value); |
| 71 |
invoke_on_all_policies(test_find(), in.cbegin(), in.cend(), value); |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
// Type defined for sake of checking that std::find works with asymmetric ==. |
| 77 |
class Weird |
| 78 |
{ |
| 79 |
Number value; |
| 80 |
|
| 81 |
public: |
| 82 |
friend bool |
| 83 |
operator==(Number x, Weird y) |
| 84 |
{ |
| 85 |
return x == y.value; |
| 86 |
} |
| 87 |
Weird(int32_t val, OddTag) : value(val, OddTag()) {} |
| 88 |
}; |
| 89 |
|
| 90 |
int |
| 91 |
main() |
| 92 |
{ |
| 93 |
// Note that the "hit" and "miss" functions here avoid overflow issues. |
| 94 |
test<Number>(Weird(42, OddTag()), [](int32_t) { return Number(42, OddTag()); }, // hit |
| 95 |
[](int32_t j) { return Number(j == 42 ? 0 : j, OddTag()); }); // miss |
| 96 |
|
| 97 |
// Test with value that is equal to two different bit patterns (-0.0 and 0.0) |
| 98 |
test<float32_t>(-0.0, [](int32_t j) { return j & 1 ? 0.0 : -0.0; }, // hit |
| 99 |
[](int32_t j) { return j == 0 ? ~j : j; }); // miss |
| 100 |
|
| 101 |
std::cout << done() << std::endl; |
| 102 |
return 0; |
| 103 |
} |