Debian Codegraph @

src/lib/core/highway/hwy/tests/blockwise_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>
19
20 #include <algorithm> // std::fill
21
22 #undef HWY_TARGET_INCLUDE
23 #define HWY_TARGET_INCLUDE "tests/blockwise_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 template <typename D, int kLane>
33 struct TestBroadcastR {
34 HWY_NOINLINE void operator()() const {
35 using T = typename D::T;
36 const D d;
37 const size_t N = Lanes(d);
38 if (kLane >= N) return;
39 auto in_lanes = AllocateAligned<T>(N);
40 std::fill(in_lanes.get(), in_lanes.get() + N, T(0));
41 const size_t blockN = HWY_MIN(N * sizeof(T), 16) / sizeof(T);
42 // Need to set within each 128-bit block
43 for (size_t block = 0; block < N; block += blockN) {
44 in_lanes[block + kLane] = static_cast<T>(block + 1);
45 }
46 const auto in = Load(d, in_lanes.get());
47 auto expected = AllocateAligned<T>(N);
48 for (size_t block = 0; block < N; block += blockN) {
49 for (size_t i = 0; i < blockN; ++i) {
50 expected[block + i] = T(block + 1);
51 }
52 }
53 HWY_ASSERT_VEC_EQ(d, expected.get(), Broadcast<kLane>(in));
54
55 TestBroadcastR<D, kLane - 1>()();
56 }
57 };
58
59 template <class D>
60 struct TestBroadcastR<D, -1> {
61 void operator()() const {}
62 };
63
64 struct TestBroadcast {
65 template <class T, class D>
66 HWY_NOINLINE void operator()(T /*unused*/, D d) {
67 TestBroadcastR<D, HWY_MIN(MaxLanes(d), 16 / sizeof(T)) - 1>()();
68 }
69 };
70
71 HWY_NOINLINE void TestAllBroadcast() {
72 const ForPartialVectors<TestBroadcast> test;
73 // No u/i8.
74 test(uint16_t());
75 test(int16_t());
76 ForUIF3264(test);
77 }
78
79 template <bool kFull>
80 struct ChooseTableSize {
81 template <typename T, typename DIdx>
82 using type = DIdx;
83 };
84 template <>
85 struct ChooseTableSize<true> {
86 template <typename T, typename DIdx>
87 using type = ScalableTag<T>;
88 };
89
90 template <bool kFull>
91 struct TestTableLookupBytes {
92 template <class T, class D>
93 HWY_NOINLINE void operator()(T /*unused*/, D d) {
94 #if HWY_TARGET != HWY_SCALAR
95 RandomState rng;
96
97 const typename ChooseTableSize<kFull>::template type<T, D> d_tbl;
98 const Repartition<uint8_t, decltype(d_tbl)> d_tbl8;
99 const size_t NT8 = Lanes(d_tbl8);
100
101 const Repartition<uint8_t, D> d8;
102 const size_t N8 = Lanes(d8);
103
104 // Random input bytes
105 auto in_bytes = AllocateAligned<uint8_t>(NT8);
106 for (size_t i = 0; i < NT8; ++i) {
107 in_bytes[i] = Random32(&rng) & 0xFF;
108 }
109 const auto in = BitCast(d_tbl, Load(d_tbl8, in_bytes.get()));
110
111 // Enough test data; for larger vectors, upper lanes will be zero.
112 const uint8_t index_bytes_source[64] = {
113 // Same index as source, multiple outputs from same input,
114 // unused input (9), ascending/descending and nonconsecutive neighbors.
115 0, 2, 1, 2, 15, 12, 13, 14, 6, 7, 8, 5, 4, 3, 10, 11,
116 11, 10, 3, 4, 5, 8, 7, 6, 14, 13, 12, 15, 2, 1, 2, 0,
117 4, 3, 2, 2, 5, 6, 7, 7, 15, 15, 15, 15, 15, 15, 0, 1};
118 auto index_bytes = AllocateAligned<uint8_t>(N8);
119 const size_t max_index = HWY_MIN(NT8, 16) - 1;
120 for (size_t i = 0; i < N8; ++i) {
121 index_bytes[i] = (i < 64) ? index_bytes_source[i] : 0;
122 // Avoid asan error for partial vectors.
123 index_bytes[i] = static_cast<uint8_t>(HWY_MIN(index_bytes[i], max_index));
124 }
125 const auto indices = Load(d, reinterpret_cast<const T*>(index_bytes.get()));
126
127 const size_t N = Lanes(d);
128 auto expected = AllocateAligned<T>(N);
129 uint8_t* expected_bytes = reinterpret_cast<uint8_t*>(expected.get());
130
131 for (size_t block = 0; block < N8; block += 16) {
132 for (size_t i = 0; i < 16 && (block + i) < N8; ++i) {
133 const uint8_t index = index_bytes[block + i];
134 HWY_ASSERT(index <= max_index);
135 // Note that block + index may exceed NT8 on RVV, which is fine because
136 // the operation uses the larger of the table and index vector size.
137 HWY_ASSERT(block + index < HWY_MAX(N8, NT8));
138 // For large vectors, the lane index may wrap around due to block,
139 // also wrap around after 8-bit overflow.
140 expected_bytes[block + i] =
141 in_bytes[(block + index) % HWY_MIN(NT8, 256)];
142 }
143 }
144 HWY_ASSERT_VEC_EQ(d, expected.get(), TableLookupBytes(in, indices));
145
146 // Individually test zeroing each byte position.
147 for (size_t i = 0; i < N8; ++i) {
148 const uint8_t prev_expected = expected_bytes[i];
149 const uint8_t prev_index = index_bytes[i];
150 expected_bytes[i] = 0;
151
152 const int idx = 0x80 + (static_cast<int>(Random32(&rng) & 7) << 4);
153 HWY_ASSERT(0x80 <= idx && idx < 256);
154 index_bytes[i] = static_cast<uint8_t>(idx);
155
156 const auto indices =
157 Load(d, reinterpret_cast<const T*>(index_bytes.get()));
158 HWY_ASSERT_VEC_EQ(d, expected.get(), TableLookupBytesOr0(in, indices));
159 expected_bytes[i] = prev_expected;
160 index_bytes[i] = prev_index;
161 }
162 #else
163 (void)d;
164 #endif
165 }
166 };
167
168 HWY_NOINLINE void TestAllTableLookupBytesSame() {
169 // Partial index, same-sized table.
170 ForIntegerTypes(ForPartialVectors<TestTableLookupBytes<false>>());
171 }
172
173 HWY_NOINLINE void TestAllTableLookupBytesMixed() {
174 // Partial index, full-size table.
175 ForIntegerTypes(ForPartialVectors<TestTableLookupBytes<true>>());
176 }
177
178 struct TestInterleaveLower {
179 template <class T, class D>
180 HWY_NOINLINE void operator()(T /*unused*/, D d) {
181 using TU = MakeUnsigned<T>;
182 const size_t N = Lanes(d);
183 auto even_lanes = AllocateAligned<T>(N);
184 auto odd_lanes = AllocateAligned<T>(N);
185 auto expected = AllocateAligned<T>(N);
186 for (size_t i = 0; i < N; ++i) {
187 even_lanes[i] = static_cast<T>(2 * i + 0);
188 odd_lanes[i] = static_cast<T>(2 * i + 1);
189 }
190 const auto even = Load(d, even_lanes.get());
191 const auto odd = Load(d, odd_lanes.get());
192
193 const size_t blockN = HWY_MIN(16 / sizeof(T), N);
194 for (size_t i = 0; i < Lanes(d); ++i) {
195 const size_t block = i / blockN;
196 const size_t index = (i % blockN) + block * 2 * blockN;
197 expected[i] = static_cast<T>(index & LimitsMax<TU>());
198 }
199 HWY_ASSERT_VEC_EQ(d, expected.get(), InterleaveLower(even, odd));
200 HWY_ASSERT_VEC_EQ(d, expected.get(), InterleaveLower(d, even, odd));
201 }
202 };
203
204 struct TestInterleaveUpper {
205 template <class T, class D>
206 HWY_NOINLINE void operator()(T /*unused*/, D d) {
207 const size_t N = Lanes(d);
208 if (N == 1) return;
209 auto even_lanes = AllocateAligned<T>(N);
210 auto odd_lanes = AllocateAligned<T>(N);
211 auto expected = AllocateAligned<T>(N);
212 for (size_t i = 0; i < N; ++i) {
213 even_lanes[i] = static_cast<T>(2 * i + 0);
214 odd_lanes[i] = static_cast<T>(2 * i + 1);
215 }
216 const auto even = Load(d, even_lanes.get());
217 const auto odd = Load(d, odd_lanes.get());
218
219 const size_t blockN = HWY_MIN(16 / sizeof(T), N);
220 for (size_t i = 0; i < Lanes(d); ++i) {
221 const size_t block = i / blockN;
222 expected[i] = T((i % blockN) + block * 2 * blockN + blockN);
223 }
224 HWY_ASSERT_VEC_EQ(d, expected.get(), InterleaveUpper(d, even, odd));
225 }
226 };
227
228 HWY_NOINLINE void TestAllInterleave() {
229 // Not DemoteVectors because this cannot be supported by HWY_SCALAR.
230 ForAllTypes(ForShrinkableVectors<TestInterleaveLower>());
231 ForAllTypes(ForShrinkableVectors<TestInterleaveUpper>());
232 }
233
234 struct TestZipLower {
235 template <class T, class D>
236 HWY_NOINLINE void operator()(T /*unused*/, D d) {
237 using WideT = MakeWide<T>;
238 static_assert(sizeof(T) * 2 == sizeof(WideT), "Must be double-width");
239 static_assert(IsSigned<T>() == IsSigned<WideT>(), "Must have same sign");
240 const size_t N = Lanes(d);
241 auto even_lanes = AllocateAligned<T>(N);
242 auto odd_lanes = AllocateAligned<T>(N);
243 // At least 2 lanes for HWY_SCALAR
244 auto zip_lanes = AllocateAligned<T>(HWY_MAX(N, 2));
245 const T kMaxT = LimitsMax<T>();
246 for (size_t i = 0; i < N; ++i) {
247 even_lanes[i] = static_cast<T>((2 * i + 0) & kMaxT);
248 odd_lanes[i] = static_cast<T>((2 * i + 1) & kMaxT);
249 }
250 const auto even = Load(d, even_lanes.get());
251 const auto odd = Load(d, odd_lanes.get());
252
253 const Repartition<WideT, D> dw;
254 #if HWY_TARGET == HWY_SCALAR
255 // Safely handle big-endian
256 const auto expected = Set(dw, static_cast<WideT>(1ULL << (sizeof(T) * 8)));
257 #else
258 const size_t blockN = HWY_MIN(size_t(16) / sizeof(T), N);
259 for (size_t i = 0; i < N; i += 2) {
260 const size_t base = (i / blockN) * blockN;
261 const size_t mod = i % blockN;
262 zip_lanes[i + 0] = even_lanes[mod / 2 + base];
263 zip_lanes[i + 1] = odd_lanes[mod / 2 + base];
264 }
265 const auto expected =
266 Load(dw, reinterpret_cast<const WideT*>(zip_lanes.get()));
267 #endif // HWY_TARGET == HWY_SCALAR
268 HWY_ASSERT_VEC_EQ(dw, expected, ZipLower(even, odd));
269 HWY_ASSERT_VEC_EQ(dw, expected, ZipLower(dw, even, odd));
270 }
271 };
272
273 HWY_NOINLINE void TestAllZipLower() {
274 const ForDemoteVectors<TestZipLower> lower_unsigned;
275 lower_unsigned(uint8_t());
276 lower_unsigned(uint16_t());
277 #if HWY_HAVE_INTEGER64
278 lower_unsigned(uint32_t()); // generates u64
279 #endif
280
281 const ForDemoteVectors<TestZipLower> lower_signed;
282 lower_signed(int8_t());
283 lower_signed(int16_t());
284 #if HWY_HAVE_INTEGER64
285 lower_signed(int32_t()); // generates i64
286 #endif
287
288 // No float - concatenating f32 does not result in a f64
289 }
290
291 // Remove this test (so it does not show as having run) if the only target is
292 // HWY_SCALAR, which does not support this op.
293 #if HWY_TARGETS != HWY_SCALAR
294
295 struct TestZipUpper {
296 template <class T, class D>
297 HWY_NOINLINE void operator()(T /*unused*/, D d) {
298 #if HWY_TARGET == HWY_SCALAR
299 (void)d;
300 #else
301 using WideT = MakeWide<T>;
302 static_assert(sizeof(T) * 2 == sizeof(WideT), "Must be double-width");
303 static_assert(IsSigned<T>() == IsSigned<WideT>(), "Must have same sign");
304 const size_t N = Lanes(d);
305 if (N < 16 / sizeof(T)) return;
306 auto even_lanes = AllocateAligned<T>(N);
307 auto odd_lanes = AllocateAligned<T>(N);
308 auto zip_lanes = AllocateAligned<T>(N);
309 const T kMaxT = LimitsMax<T>();
310 for (size_t i = 0; i < N; ++i) {
311 even_lanes[i] = static_cast<T>((2 * i + 0) & kMaxT);
312 odd_lanes[i] = static_cast<T>((2 * i + 1) & kMaxT);
313 }
314 const auto even = Load(d, even_lanes.get());
315 const auto odd = Load(d, odd_lanes.get());
316
317 const size_t blockN = HWY_MIN(size_t(16) / sizeof(T), N);
318
319 for (size_t i = 0; i < N; i += 2) {
320 const size_t base = (i / blockN) * blockN + blockN / 2;
321 const size_t mod = i % blockN;
322 zip_lanes[i + 0] = even_lanes[mod / 2 + base];
323 zip_lanes[i + 1] = odd_lanes[mod / 2 + base];
324 }
325 const Repartition<WideT, D> dw;
326 const auto expected =
327 Load(dw, reinterpret_cast<const WideT*>(zip_lanes.get()));
328 HWY_ASSERT_VEC_EQ(dw, expected, ZipUpper(dw, even, odd));
329 #endif // HWY_TARGET == HWY_SCALAR
330 }
331 };
332
333 HWY_NOINLINE void TestAllZipUpper() {
334 const ForShrinkableVectors<TestZipUpper> upper_unsigned;
335 upper_unsigned(uint8_t());
336 upper_unsigned(uint16_t());
337 #if HWY_HAVE_INTEGER64
338 upper_unsigned(uint32_t()); // generates u64
339 #endif
340
341 const ForShrinkableVectors<TestZipUpper> upper_signed;
342 upper_signed(int8_t());
343 upper_signed(int16_t());
344 #if HWY_HAVE_INTEGER64
345 upper_signed(int32_t()); // generates i64
346 #endif
347
348 // No float - concatenating f32 does not result in a f64
349 }
350
351 #endif // HWY_TARGETS != HWY_SCALAR
352
353 class TestSpecialShuffle32 {
354 public:
355 template <class T, class D>
356 HWY_NOINLINE void operator()(T /*unused*/, D d) {
357 const auto v = Iota(d, 0);
358 VerifyLanes32(d, Shuffle2301(v), 2, 3, 0, 1, __FILE__, __LINE__);
359 VerifyLanes32(d, Shuffle1032(v), 1, 0, 3, 2, __FILE__, __LINE__);
360 VerifyLanes32(d, Shuffle0321(v), 0, 3, 2, 1, __FILE__, __LINE__);
361 VerifyLanes32(d, Shuffle2103(v), 2, 1, 0, 3, __FILE__, __LINE__);
362 VerifyLanes32(d, Shuffle0123(v), 0, 1, 2, 3, __FILE__, __LINE__);
363 }
364
365 private:
366 // HWY_INLINE works around a Clang SVE compiler bug where all but the first
367 // 128 bits (the NEON register) of actual are zero.
368 template <class D, class V>
369 HWY_INLINE void VerifyLanes32(D d, VecArg<V> actual, const size_t i3,
370 const size_t i2, const size_t i1,
371 const size_t i0, const char* filename,
372 const int line) {
373 using T = TFromD<D>;
374 constexpr size_t kBlockN = 16 / sizeof(T);
375 const size_t N = Lanes(d);
376 if (N < 4) return;
377 auto expected = AllocateAligned<T>(N);
378 for (size_t block = 0; block < N; block += kBlockN) {
379 expected[block + 3] = static_cast<T>(block + i3);
380 expected[block + 2] = static_cast<T>(block + i2);
381 expected[block + 1] = static_cast<T>(block + i1);
382 expected[block + 0] = static_cast<T>(block + i0);
383 }
384 AssertVecEqual(d, expected.get(), actual, filename, line);
385 }
386 };
387
388 class TestSpecialShuffle64 {
389 public:
390 template <class T, class D>
391 HWY_NOINLINE void operator()(T /*unused*/, D d) {
392 const auto v = Iota(d, 0);
393 VerifyLanes64(d, Shuffle01(v), 0, 1, __FILE__, __LINE__);
394 }
395
396 private:
397 // HWY_INLINE works around a Clang SVE compiler bug where all but the first
398 // 128 bits (the NEON register) of actual are zero.
399 template <class D, class V>
400 HWY_INLINE void VerifyLanes64(D d, VecArg<V> actual, const size_t i1,
401 const size_t i0, const char* filename,
402 const int line) {
403 using T = TFromD<D>;
404 constexpr size_t kBlockN = 16 / sizeof(T);
405 const size_t N = Lanes(d);
406 if (N < 2) return;
407 auto expected = AllocateAligned<T>(N);
408 for (size_t block = 0; block < N; block += kBlockN) {
409 expected[block + 1] = static_cast<T>(block + i1);
410 expected[block + 0] = static_cast<T>(block + i0);
411 }
412 AssertVecEqual(d, expected.get(), actual, filename, line);
413 }
414 };
415
416 HWY_NOINLINE void TestAllSpecialShuffles() {
417 const ForGEVectors<128, TestSpecialShuffle32> test32;
418 test32(uint32_t());
419 test32(int32_t());
420 test32(float());
421
422 #if HWY_HAVE_INTEGER64
423 const ForGEVectors<128, TestSpecialShuffle64> test64;
424 test64(uint64_t());
425 test64(int64_t());
426 #endif
427
428 #if HWY_HAVE_FLOAT64
429 const ForGEVectors<128, TestSpecialShuffle64> test_d;
430 test_d(double());
431 #endif
432 }
433
434 // NOLINTNEXTLINE(google-readability-namespace-comments)
435 } // namespace HWY_NAMESPACE
436 } // namespace hwy
437 HWY_AFTER_NAMESPACE();
438
439 #if HWY_ONCE
440
441 namespace hwy {
442 HWY_BEFORE_TEST(HwyBlockwiseTest);
443 HWY_EXPORT_AND_TEST_P(HwyBlockwiseTest, TestAllBroadcast);
444 HWY_EXPORT_AND_TEST_P(HwyBlockwiseTest, TestAllTableLookupBytesSame);
445 HWY_EXPORT_AND_TEST_P(HwyBlockwiseTest, TestAllTableLookupBytesMixed);
446 HWY_EXPORT_AND_TEST_P(HwyBlockwiseTest, TestAllInterleave);
447 HWY_EXPORT_AND_TEST_P(HwyBlockwiseTest, TestAllZipLower);
448 #if HWY_TARGETS != HWY_SCALAR
449 HWY_EXPORT_AND_TEST_P(HwyBlockwiseTest, TestAllZipUpper);
450 #endif
451 HWY_EXPORT_AND_TEST_P(HwyBlockwiseTest, TestAllSpecialShuffles);
452 } // namespace hwy
453
454 #endif