Debian Codegraph @

src/lib/core/highway/hwy/tests/combine_test.cc

raw symbols SCIP
1 // Copyright 2019 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 <stddef.h>
17 #include <stdint.h>
18 #include <string.h> // memcpy
19
20 #include <algorithm> // std::fill
21
22 #undef HWY_TARGET_INCLUDE
23 #define HWY_TARGET_INCLUDE "tests/combine_test.cc"
24 #include "hwy/foreach_target.h" // IWYU pragma: keep
25 #include "hwy/highway.h"
26 #include "hwy/tests/test_util-inl.h"
27
28 HWY_BEFORE_NAMESPACE();
29 namespace hwy {
30 namespace HWY_NAMESPACE {
31
32 struct TestLowerHalf {
33 template <class T, class D>
34 HWY_NOINLINE void operator()(T /*unused*/, D d) {
35 const Half<D> d2;
36
37 const size_t N = Lanes(d);
38 auto lanes = AllocateAligned<T>(N);
39 auto lanes2 = AllocateAligned<T>(N);
40 std::fill(lanes.get(), lanes.get() + N, T(0));
41 std::fill(lanes2.get(), lanes2.get() + N, T(0));
42 const auto v = Iota(d, 1);
43 Store(LowerHalf(d2, v), d2, lanes.get());
44 Store(LowerHalf(v), d2, lanes2.get()); // optionally without D
45 size_t i = 0;
46 for (; i < Lanes(d2); ++i) {
47 HWY_ASSERT_EQ(T(1 + i), lanes[i]);
48 HWY_ASSERT_EQ(T(1 + i), lanes2[i]);
49 }
50 // Other half remains unchanged
51 for (; i < N; ++i) {
52 HWY_ASSERT_EQ(T(0), lanes[i]);
53 HWY_ASSERT_EQ(T(0), lanes2[i]);
54 }
55 }
56 };
57
58 struct TestLowerQuarter {
59 template <class T, class D>
60 HWY_NOINLINE void operator()(T /*unused*/, D d) {
61 const Half<D> d2;
62 const Half<decltype(d2)> d4;
63
64 const size_t N = Lanes(d);
65 auto lanes = AllocateAligned<T>(N);
66 auto lanes2 = AllocateAligned<T>(N);
67 std::fill(lanes.get(), lanes.get() + N, T(0));
68 std::fill(lanes2.get(), lanes2.get() + N, T(0));
69 const auto v = Iota(d, 1);
70 const auto lo = LowerHalf(d4, LowerHalf(d2, v));
71 const auto lo2 = LowerHalf(LowerHalf(v)); // optionally without D
72 Store(lo, d4, lanes.get());
73 Store(lo2, d4, lanes2.get());
74 size_t i = 0;
75 for (; i < Lanes(d4); ++i) {
76 HWY_ASSERT_EQ(T(i + 1), lanes[i]);
77 HWY_ASSERT_EQ(T(i + 1), lanes2[i]);
78 }
79 // Upper 3/4 remain unchanged
80 for (; i < N; ++i) {
81 HWY_ASSERT_EQ(T(0), lanes[i]);
82 HWY_ASSERT_EQ(T(0), lanes2[i]);
83 }
84 }
85 };
86
87 HWY_NOINLINE void TestAllLowerHalf() {
88 ForAllTypes(ForHalfVectors<TestLowerHalf>());
89
90 // The minimum vector size is 128 bits, so there's no guarantee we can have
91 // quarters of 64-bit lanes, hence test 'all' other types.
92 ForHalfVectors<TestLowerQuarter, 2> test_quarter;
93 ForUI8(test_quarter);
94 ForUI16(test_quarter); // exclude float16_t - cannot compare
95 ForUIF32(test_quarter);
96 }
97
98 struct TestUpperHalf {
99 template <class T, class D>
100 HWY_NOINLINE void operator()(T /*unused*/, D d) {
101 // Scalar does not define UpperHalf.
102 #if HWY_TARGET != HWY_SCALAR
103 const Half<D> d2;
104 const size_t N2 = Lanes(d2);
105 HWY_ASSERT(N2 * 2 == Lanes(d));
106 auto expected = AllocateAligned<T>(N2);
107 size_t i = 0;
108 for (; i < N2; ++i) {
109 expected[i] = static_cast<T>(N2 + 1 + i);
110 }
111 HWY_ASSERT_VEC_EQ(d2, expected.get(), UpperHalf(d2, Iota(d, 1)));
112 #else
113 (void)d;
114 #endif
115 }
116 };
117
118 HWY_NOINLINE void TestAllUpperHalf() {
119 ForAllTypes(ForHalfVectors<TestUpperHalf>());
120 }
121
122 struct TestZeroExtendVector {
123 template <class T, class D>
124 HWY_NOINLINE void operator()(T /*unused*/, D d) {
125 const Twice<D> d2;
126
127 const auto v = Iota(d, 1);
128 const size_t N = Lanes(d);
129 const size_t N2 = Lanes(d2);
130 // If equal, then N was already MaxLanes(d) and it's not clear what
131 // Combine or ZeroExtendVector should return.
132 if (N2 == N) return;
133 HWY_ASSERT(N2 == 2 * N);
134 auto lanes = AllocateAligned<T>(N2);
135 Store(v, d, &lanes[0]);
136 Store(v, d, &lanes[N]);
137
138 const auto ext = ZeroExtendVector(d2, v);
139 Store(ext, d2, lanes.get());
140
141 // Lower half is unchanged
142 HWY_ASSERT_VEC_EQ(d, v, Load(d, &lanes[0]));
143 // Upper half is zero
144 HWY_ASSERT_VEC_EQ(d, Zero(d), Load(d, &lanes[N]));
145 }
146 };
147
148 HWY_NOINLINE void TestAllZeroExtendVector() {
149 ForAllTypes(ForExtendableVectors<TestZeroExtendVector>());
150 }
151
152 struct TestCombine {
153 template <class T, class D>
154 HWY_NOINLINE void operator()(T /*unused*/, D d) {
155 const Twice<D> d2;
156 const size_t N2 = Lanes(d2);
157 auto lanes = AllocateAligned<T>(N2);
158
159 const auto lo = Iota(d, 1);
160 const auto hi = Iota(d, static_cast<T>(N2 / 2 + 1));
161 const auto combined = Combine(d2, hi, lo);
162 Store(combined, d2, lanes.get());
163
164 const auto expected = Iota(d2, 1);
165 HWY_ASSERT_VEC_EQ(d2, expected, combined);
166 }
167 };
168
169 HWY_NOINLINE void TestAllCombine() {
170 ForAllTypes(ForExtendableVectors<TestCombine>());
171 }
172
173 struct TestConcat {
174 template <class T, class D>
175 HWY_NOINLINE void operator()(T /*unused*/, D d) {
176 const size_t N = Lanes(d);
177 if (N == 1) return;
178 const size_t half_bytes = N * sizeof(T) / 2;
179
180 auto hi = AllocateAligned<T>(N);
181 auto lo = AllocateAligned<T>(N);
182 auto expected = AllocateAligned<T>(N);
183 RandomState rng;
184 for (size_t rep = 0; rep < 10; ++rep) {
185 for (size_t i = 0; i < N; ++i) {
186 hi[i] = static_cast<T>(Random64(&rng) & 0xFF);
187 lo[i] = static_cast<T>(Random64(&rng) & 0xFF);
188 }
189
190 {
191 memcpy(&expected[N / 2], &hi[N / 2], half_bytes);
192 memcpy(&expected[0], &lo[0], half_bytes);
193 const auto vhi = Load(d, hi.get());
194 const auto vlo = Load(d, lo.get());
195 HWY_ASSERT_VEC_EQ(d, expected.get(), ConcatUpperLower(d, vhi, vlo));
196 }
197
198 {
199 memcpy(&expected[N / 2], &hi[N / 2], half_bytes);
200 memcpy(&expected[0], &lo[N / 2], half_bytes);
201 const auto vhi = Load(d, hi.get());
202 const auto vlo = Load(d, lo.get());
203 HWY_ASSERT_VEC_EQ(d, expected.get(), ConcatUpperUpper(d, vhi, vlo));
204 }
205
206 {
207 memcpy(&expected[N / 2], &hi[0], half_bytes);
208 memcpy(&expected[0], &lo[N / 2], half_bytes);
209 const auto vhi = Load(d, hi.get());
210 const auto vlo = Load(d, lo.get());
211 HWY_ASSERT_VEC_EQ(d, expected.get(), ConcatLowerUpper(d, vhi, vlo));
212 }
213
214 {
215 memcpy(&expected[N / 2], &hi[0], half_bytes);
216 memcpy(&expected[0], &lo[0], half_bytes);
217 const auto vhi = Load(d, hi.get());
218 const auto vlo = Load(d, lo.get());
219 HWY_ASSERT_VEC_EQ(d, expected.get(), ConcatLowerLower(d, vhi, vlo));
220 }
221 }
222 }
223 };
224
225 HWY_NOINLINE void TestAllConcat() {
226 ForAllTypes(ForShrinkableVectors<TestConcat>());
227 }
228
229 struct TestConcatOddEven {
230 template <class T, class D>
231 HWY_NOINLINE void operator()(T /*unused*/, D d) {
232 #if HWY_TARGET != HWY_SCALAR
233 const size_t N = Lanes(d);
234 const auto hi = Iota(d, static_cast<T>(N));
235 const auto lo = Iota(d, 0);
236 const auto even = Add(Iota(d, 0), Iota(d, 0));
237 const auto odd = Add(even, Set(d, 1));
238 HWY_ASSERT_VEC_EQ(d, odd, ConcatOdd(d, hi, lo));
239 HWY_ASSERT_VEC_EQ(d, even, ConcatEven(d, hi, lo));
240
241 // This test catches inadvertent saturation.
242 const auto min = Set(d, LowestValue<T>());
243 const auto max = Set(d, HighestValue<T>());
244 HWY_ASSERT_VEC_EQ(d, max, ConcatOdd(d, max, max));
245 HWY_ASSERT_VEC_EQ(d, max, ConcatEven(d, max, max));
246 HWY_ASSERT_VEC_EQ(d, min, ConcatOdd(d, min, min));
247 HWY_ASSERT_VEC_EQ(d, min, ConcatEven(d, min, min));
248 #else
249 (void)d;
250 #endif
251 }
252 };
253
254 HWY_NOINLINE void TestAllConcatOddEven() {
255 ForAllTypes(ForShrinkableVectors<TestConcatOddEven>());
256 }
257
258 // NOLINTNEXTLINE(google-readability-namespace-comments)
259 } // namespace HWY_NAMESPACE
260 } // namespace hwy
261 HWY_AFTER_NAMESPACE();
262
263 #if HWY_ONCE
264
265 namespace hwy {
266 HWY_BEFORE_TEST(HwyCombineTest);
267 HWY_EXPORT_AND_TEST_P(HwyCombineTest, TestAllLowerHalf);
268 HWY_EXPORT_AND_TEST_P(HwyCombineTest, TestAllUpperHalf);
269 HWY_EXPORT_AND_TEST_P(HwyCombineTest, TestAllZeroExtendVector);
270 HWY_EXPORT_AND_TEST_P(HwyCombineTest, TestAllCombine);
271 HWY_EXPORT_AND_TEST_P(HwyCombineTest, TestAllConcat);
272 HWY_EXPORT_AND_TEST_P(HwyCombineTest, TestAllConcatOddEven);
273 } // namespace hwy
274
275 #endif // HWY_ONCE