| 1 | // Copyright 2022 Google LLC |
| 2 | // SPDX-License-Identifier: Apache-2.0 |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | #include <algorithm> // std::find_if |
| 17 | #include <vector> |
| 18 | |
| 19 | #include "hwy/aligned_allocator.h" |
| 20 | #include "hwy/base.h" |
| 21 | #include "hwy/print.h" |
| 22 | |
| 23 | // clang-format off |
| 24 | #undef HWY_TARGET_INCLUDE |
| 25 | #define HWY_TARGET_INCLUDE "hwy/contrib/algo/find_test.cc" |
| 26 | #include "hwy/foreach_target.h" // IWYU pragma: keep |
| 27 | |
| 28 | #include "hwy/contrib/algo/find-inl.h" |
| 29 | #include "hwy/tests/test_util-inl.h" |
| 30 | // clang-format on |
| 31 | |
| 32 | // If your project requires C++14 or later, you can ignore this and pass lambdas |
| 33 | // directly to FindIf, without requiring an lvalue as we do here for C++11. |
| 34 | #if __cplusplus < 201402L |
| 35 | #define HWY_GENERIC_LAMBDA 0 |
| 36 | #else |
| 37 | #define HWY_GENERIC_LAMBDA 1 |
| 38 | #endif |
| 39 | |
| 40 | HWY_BEFORE_NAMESPACE(); |
| 41 | namespace hwy { |
| 42 | namespace HWY_NAMESPACE { |
| 43 | |
| 44 | // Returns random number in [-8, 8) - we use knowledge of the range to Find() |
| 45 | // values we know are not present. |
| 46 | template <typename T> |
| 47 | T Random(RandomState& rng) { |
| 48 | const int32_t bits = static_cast<int32_t>(Random32(&rng)) & 1023; |
| 49 | const double val = (bits - 512) / 64.0; |
| 50 | // Clamp negative to zero for unsigned types. |
| 51 | return static_cast<T>(HWY_MAX(hwy::LowestValue<T>(), val)); |
| 52 | } |
| 53 | |
| 54 | // In C++14, we can instead define these as generic lambdas next to where they |
| 55 | // are invoked. |
| 56 | #if !HWY_GENERIC_LAMBDA |
| 57 | |
| 58 | class GreaterThan { |
| 59 | public: |
| 60 | GreaterThan(int val) : val_(val) {} |
| 61 | template <class D, class V> |
| 62 | Mask<D> operator()(D d, V v) const { |
| 63 | return Gt(v, Set(d, static_cast<TFromD<D>>(val_))); |
| 64 | } |
| 65 | |
| 66 | private: |
| 67 | int val_; |
| 68 | }; |
| 69 | |
| 70 | #endif // !HWY_GENERIC_LAMBDA |
| 71 | |
| 72 | // Invokes Test (e.g. TestFind) with all arg combinations. |
| 73 | template <class Test> |
| 74 | struct ForeachCountAndMisalign { |
| 75 | template <typename T, class D> |
| 76 | HWY_NOINLINE void operator()(T /*unused*/, D d) const { |
| 77 | RandomState rng; |
| 78 | const size_t N = Lanes(d); |
| 79 | const size_t misalignments[3] = {0, N / 4, 3 * N / 5}; |
| 80 | |
| 81 | // Find() checks 8 vectors at a time, so we want to cover a fairly large |
| 82 | // range without oversampling (checking every possible count). |
| 83 | std::vector<size_t> counts(AdjustedReps(512)); |
| 84 | for (size_t& count : counts) { |
| 85 | count = static_cast<size_t>(rng()) % (16 * N + 1); |
| 86 | } |
| 87 | counts[0] = 0; // ensure we test count=0. |
| 88 | |
| 89 | for (size_t count : counts) { |
| 90 | for (size_t m : misalignments) { |
| 91 | Test()(d, count, m, rng); |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | }; |
| 96 | |
| 97 | struct TestFind { |
| 98 | template <class D> |
| 99 | void operator()(D d, size_t count, size_t misalign, RandomState& rng) { |
| 100 | using T = TFromD<D>; |
| 101 | // Must allocate at least one even if count is zero. |
| 102 | AlignedFreeUniquePtr<T[]> storage = |
| 103 | AllocateAligned<T>(HWY_MAX(1, misalign + count)); |
| 104 | T* in = storage.get() + misalign; |
| 105 | for (size_t i = 0; i < count; ++i) { |
| 106 | in[i] = Random<T>(rng); |
| 107 | } |
| 108 | |
| 109 | // For each position, search for that element (which we know is there) |
| 110 | for (size_t pos = 0; pos < count; ++pos) { |
| 111 | const size_t actual = Find(d, in[pos], in, count); |
| 112 | |
| 113 | // We may have found an earlier occurrence of the same value; ensure the |
| 114 | // value is the same, and that it is the first. |
| 115 | if (!IsEqual(in[pos], in[actual])) { |
| 116 | fprintf(stderr, "%s count %d, found %.15f at %d but wanted %.15f\n", |
| 117 | hwy::TypeName(T(), Lanes(d)).c_str(), static_cast<int>(count), |
| 118 | static_cast<double>(in[actual]), static_cast<int>(actual), |
| 119 | static_cast<double>(in[pos])); |
| 120 | HWY_ASSERT(false); |
| 121 | } |
| 122 | for (size_t i = 0; i < actual; ++i) { |
| 123 | if (IsEqual(in[i], in[pos])) { |
| 124 | fprintf(stderr, "%s count %d, found %f at %d but Find returned %d\n", |
| 125 | hwy::TypeName(T(), Lanes(d)).c_str(), static_cast<int>(count), |
| 126 | static_cast<double>(in[i]), static_cast<int>(i), |
| 127 | static_cast<int>(actual)); |
| 128 | HWY_ASSERT(false); |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | // Also search for values we know not to be present (out of range) |
| 134 | HWY_ASSERT_EQ(count, Find(d, T{9}, in, count)); |
| 135 | HWY_ASSERT_EQ(count, Find(d, static_cast<T>(-9), in, count)); |
| 136 | } |
| 137 | }; |
| 138 | |
| 139 | void TestAllFind() { |
| 140 | ForAllTypes(ForPartialVectors<ForeachCountAndMisalign<TestFind>>()); |
| 141 | } |
| 142 | |
| 143 | struct TestFindIf { |
| 144 | template <class D> |
| 145 | void operator()(D d, size_t count, size_t misalign, RandomState& rng) { |
| 146 | using T = TFromD<D>; |
| 147 | using TI = MakeSigned<T>; |
| 148 | // Must allocate at least one even if count is zero. |
| 149 | AlignedFreeUniquePtr<T[]> storage = |
| 150 | AllocateAligned<T>(HWY_MAX(1, misalign + count)); |
| 151 | T* in = storage.get() + misalign; |
| 152 | for (size_t i = 0; i < count; ++i) { |
| 153 | in[i] = Random<T>(rng); |
| 154 | HWY_ASSERT(in[i] < 8); |
| 155 | HWY_ASSERT(!hwy::IsSigned<T>() || static_cast<TI>(in[i]) >= -8); |
| 156 | } |
| 157 | |
| 158 | bool found_any = false; |
| 159 | bool not_found_any = false; |
| 160 | |
| 161 | // unsigned T would be promoted to signed and compare greater than any |
| 162 | // negative val, whereas Set() would just cast to an unsigned value and the |
| 163 | // comparison remains unsigned, so avoid negative numbers there. |
| 164 | const int min_val = IsSigned<T>() ? -9 : 0; |
| 165 | // Includes out-of-range value 9 to test the not-found path. |
| 166 | for (int val = min_val; val <= 9; ++val) { |
| 167 | #if HWY_GENERIC_LAMBDA |
| 168 | const auto greater = [val](const auto d, const auto v) HWY_ATTR { |
| 169 | return Gt(v, Set(d, static_cast<T>(val))); |
| 170 | }; |
| 171 | #else |
| 172 | const GreaterThan greater(val); |
| 173 | #endif |
| 174 | const size_t actual = FindIf(d, in, count, greater); |
| 175 | found_any |= actual < count; |
| 176 | not_found_any |= actual == count; |
| 177 | |
| 178 | const auto pos = std::find_if( |
| 179 | in, in + count, [val](T x) { return x > static_cast<T>(val); }); |
| 180 | // Convert returned iterator to index. |
| 181 | const size_t expected = static_cast<size_t>(pos - in); |
| 182 | if (expected != actual) { |
| 183 | fprintf(stderr, "%s count %d val %d, expected %d actual %d\n", |
| 184 | hwy::TypeName(T(), Lanes(d)).c_str(), static_cast<int>(count), |
| 185 | val, static_cast<int>(expected), static_cast<int>(actual)); |
| 186 | hwy::detail::PrintArray(hwy::detail::MakeTypeInfo<T>(), "in", in, count, |
| 187 | 0, count); |
| 188 | HWY_ASSERT(false); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | // We will always not-find something due to val=9. |
| 193 | HWY_ASSERT(not_found_any); |
| 194 | // We'll find something unless the input is empty or {0} - because 0 > i |
| 195 | // is false for all i=[0,9]. |
| 196 | if (count != 0 && in[0] != 0) { |
| 197 | HWY_ASSERT(found_any); |
| 198 | } |
| 199 | } |
| 200 | }; |
| 201 | |
| 202 | void TestAllFindIf() { |
| 203 | ForAllTypes(ForPartialVectors<ForeachCountAndMisalign<TestFindIf>>()); |
| 204 | } |
| 205 | |
| 206 | // NOLINTNEXTLINE(google-readability-namespace-comments) |
| 207 | } // namespace HWY_NAMESPACE |
| 208 | } // namespace hwy |
| 209 | HWY_AFTER_NAMESPACE(); |
| 210 | |
| 211 | #if HWY_ONCE |
| 212 | |
| 213 | namespace hwy { |
| 214 | HWY_BEFORE_TEST(FindTest); |
| 215 | HWY_EXPORT_AND_TEST_P(FindTest, TestAllFind); |
| 216 | HWY_EXPORT_AND_TEST_P(FindTest, TestAllFindIf); |
| 217 | } // namespace hwy |
| 218 | |
| 219 | #endif |