Debian Codegraph @

src/lib/core/highway/hwy/contrib/algo/copy_test.cc

raw symbols SCIP
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 "hwy/aligned_allocator.h"
17
18 // clang-format off
19 #undef HWY_TARGET_INCLUDE
20 #define HWY_TARGET_INCLUDE "hwy/contrib/algo/copy_test.cc"
21 #include "hwy/foreach_target.h" // IWYU pragma: keep
22
23 #include "hwy/contrib/algo/copy-inl.h"
24 #include "hwy/tests/test_util-inl.h"
25 // clang-format on
26
27 // If your project requires C++14 or later, you can ignore this and pass lambdas
28 // directly to Transform, without requiring an lvalue as we do here for C++11.
29 #if __cplusplus < 201402L
30 #define HWY_GENERIC_LAMBDA 0
31 #else
32 #define HWY_GENERIC_LAMBDA 1
33 #endif
34
35 HWY_BEFORE_NAMESPACE();
36 namespace hwy {
37 namespace HWY_NAMESPACE {
38
39 // Returns random integer in [0, 128), which fits in any lane type.
40 template <typename T>
41 T Random7Bit(RandomState& rng) {
42 return static_cast<T>(Random32(&rng) & 127);
43 }
44
45 // In C++14, we can instead define these as generic lambdas next to where they
46 // are invoked.
47 #if !HWY_GENERIC_LAMBDA
48
49 struct IsOdd {
50 template <class D, class V>
51 Mask<D> operator()(D d, V v) const {
52 return TestBit(v, Set(d, TFromD<D>{1}));
53 }
54 };
55
56 #endif // !HWY_GENERIC_LAMBDA
57
58 // Invokes Test (e.g. TestCopyIf) with all arg combinations. T comes from
59 // ForFloatTypes.
60 template <class Test>
61 struct ForeachCountAndMisalign {
62 template <typename T, class D>
63 HWY_NOINLINE void operator()(T /*unused*/, D d) const {
64 RandomState rng;
65 const size_t N = Lanes(d);
66 const size_t misalignments[3] = {0, N / 4, 3 * N / 5};
67
68 for (size_t count = 0; count < 2 * N; ++count) {
69 for (size_t ma : misalignments) {
70 for (size_t mb : misalignments) {
71 Test()(d, count, ma, mb, rng);
72 }
73 }
74 }
75 }
76 };
77
78 struct TestFill {
79 template <class D>
80 void operator()(D d, size_t count, size_t misalign_a, size_t misalign_b,
81 RandomState& rng) {
82 using T = TFromD<D>;
83 // HWY_MAX prevents error when misalign == count == 0.
84 AlignedFreeUniquePtr<T[]> pa =
85 AllocateAligned<T>(HWY_MAX(1, misalign_a + count));
86 T* expected = pa.get() + misalign_a;
87 const T value = Random7Bit<T>(rng);
88 for (size_t i = 0; i < count; ++i) {
89 expected[i] = value;
90 }
91 AlignedFreeUniquePtr<T[]> pb = AllocateAligned<T>(misalign_b + count + 1);
92 T* actual = pb.get() + misalign_b;
93
94 actual[count] = T{0}; // sentinel
95 Fill(d, value, count, actual);
96 HWY_ASSERT_EQ(T{0}, actual[count]); // did not write past end
97
98 const auto info = hwy::detail::MakeTypeInfo<T>();
99 const char* target_name = hwy::TargetName(HWY_TARGET);
100 hwy::detail::AssertArrayEqual(info, expected, actual, count, target_name,
101 __FILE__, __LINE__);
102 }
103 };
104
105 void TestAllFill() {
106 ForAllTypes(ForPartialVectors<ForeachCountAndMisalign<TestFill>>());
107 }
108
109 struct TestCopy {
110 template <class D>
111 void operator()(D d, size_t count, size_t misalign_a, size_t misalign_b,
112 RandomState& rng) {
113 using T = TFromD<D>;
114 // Prevents error if size to allocate is zero.
115 AlignedFreeUniquePtr<T[]> pa =
116 AllocateAligned<T>(HWY_MAX(1, misalign_a + count));
117 T* a = pa.get() + misalign_a;
118 for (size_t i = 0; i < count; ++i) {
119 a[i] = Random7Bit<T>(rng);
120 }
121 AlignedFreeUniquePtr<T[]> pb =
122 AllocateAligned<T>(HWY_MAX(1, misalign_b + count));
123 T* b = pb.get() + misalign_b;
124
125 Copy(d, a, count, b);
126
127 const auto info = hwy::detail::MakeTypeInfo<T>();
128 const char* target_name = hwy::TargetName(HWY_TARGET);
129 hwy::detail::AssertArrayEqual(info, a, b, count, target_name, __FILE__,
130 __LINE__);
131 }
132 };
133
134 void TestAllCopy() {
135 ForAllTypes(ForPartialVectors<ForeachCountAndMisalign<TestCopy>>());
136 }
137
138 struct TestCopyIf {
139 template <class D>
140 void operator()(D d, size_t count, size_t misalign_a, size_t misalign_b,
141 RandomState& rng) {
142 using T = TFromD<D>;
143 // Prevents error if size to allocate is zero.
144 AlignedFreeUniquePtr<T[]> pa =
145 AllocateAligned<T>(HWY_MAX(1, misalign_a + count));
146 T* a = pa.get() + misalign_a;
147 for (size_t i = 0; i < count; ++i) {
148 a[i] = Random7Bit<T>(rng);
149 }
150 const size_t padding = Lanes(ScalableTag<T>());
151 AlignedFreeUniquePtr<T[]> pb =
152 AllocateAligned<T>(HWY_MAX(1, misalign_b + count + padding));
153 T* b = pb.get() + misalign_b;
154
155 AlignedFreeUniquePtr<T[]> expected = AllocateAligned<T>(HWY_MAX(1, count));
156 size_t num_odd = 0;
157 for (size_t i = 0; i < count; ++i) {
158 if (a[i] & 1) {
159 expected[num_odd++] = a[i];
160 }
161 }
162
163 #if HWY_GENERIC_LAMBDA
164 const auto is_odd = [](const auto d, const auto v) HWY_ATTR {
165 return TestBit(v, Set(d, TFromD<decltype(d)>{1}));
166 };
167 #else
168 const IsOdd is_odd;
169 #endif
170 T* end = CopyIf(d, a, count, b, is_odd);
171 const size_t num_written = static_cast<size_t>(end - b);
172 HWY_ASSERT_EQ(num_odd, num_written);
173
174 const auto info = hwy::detail::MakeTypeInfo<T>();
175 const char* target_name = hwy::TargetName(HWY_TARGET);
176 hwy::detail::AssertArrayEqual(info, expected.get(), b, num_odd, target_name,
177 __FILE__, __LINE__);
178 }
179 };
180
181 void TestAllCopyIf() {
182 ForUI163264(ForPartialVectors<ForeachCountAndMisalign<TestCopyIf>>());
183 }
184
185 // NOLINTNEXTLINE(google-readability-namespace-comments)
186 } // namespace HWY_NAMESPACE
187 } // namespace hwy
188 HWY_AFTER_NAMESPACE();
189
190 #if HWY_ONCE
191
192 namespace hwy {
193 HWY_BEFORE_TEST(CopyTest);
194 HWY_EXPORT_AND_TEST_P(CopyTest, TestAllFill);
195 HWY_EXPORT_AND_TEST_P(CopyTest, TestAllCopy);
196 HWY_EXPORT_AND_TEST_P(CopyTest, TestAllCopyIf);
197 } // namespace hwy
198
199 #endif