Debian Codegraph @

src/lib/core/highway/hwy/tests/swizzle_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 <string.h> // memset
18
19 #include "hwy/base.h"
20
21 #undef HWY_TARGET_INCLUDE
22 #define HWY_TARGET_INCLUDE "tests/swizzle_test.cc"
23 #include "hwy/foreach_target.h" // IWYU pragma: keep
24 #include "hwy/highway.h"
25 #include "hwy/tests/test_util-inl.h"
26
27 HWY_BEFORE_NAMESPACE();
28 namespace hwy {
29 namespace HWY_NAMESPACE {
30
31 struct TestGetLane {
32 template <class T, class D>
33 HWY_NOINLINE void operator()(T /*unused*/, D d) {
34 const auto v = Iota(d, T(1));
35 HWY_ASSERT_EQ(T(1), GetLane(v));
36 }
37 };
38
39 HWY_NOINLINE void TestAllGetLane() {
40 ForAllTypes(ForPartialVectors<TestGetLane>());
41 }
42
43 struct TestExtractLane {
44 template <class T, class D>
45 HWY_NOINLINE void operator()(T /*unused*/, D d) {
46 const auto v = Iota(d, T(1));
47 for (size_t i = 0; i < Lanes(d); ++i) {
48 const T actual = ExtractLane(v, i);
49 HWY_ASSERT_EQ(static_cast<T>(i + 1), actual);
50 }
51 }
52 };
53
54 HWY_NOINLINE void TestAllExtractLane() {
55 ForAllTypes(ForPartialVectors<TestExtractLane>());
56 }
57
58 struct TestInsertLane {
59 template <class T, class D>
60 HWY_NOINLINE void operator()(T /*unused*/, D d) {
61 using V = Vec<D>;
62 const V v = Iota(d, T(1));
63 const size_t N = Lanes(d);
64 auto lanes = AllocateAligned<T>(N);
65 Store(v, d, lanes.get());
66
67 for (size_t i = 0; i < Lanes(d); ++i) {
68 lanes[i] = T{0};
69 const V actual = InsertLane(v, i, static_cast<T>(i + 1));
70 HWY_ASSERT_VEC_EQ(d, v, actual);
71 Store(v, d, lanes.get()); // restore lane i
72 }
73 }
74 };
75
76 HWY_NOINLINE void TestAllInsertLane() {
77 ForAllTypes(ForPartialVectors<TestInsertLane>());
78 }
79
80 struct TestDupEven {
81 template <class T, class D>
82 HWY_NOINLINE void operator()(T /*unused*/, D d) {
83 const size_t N = Lanes(d);
84 auto expected = AllocateAligned<T>(N);
85 for (size_t i = 0; i < N; ++i) {
86 expected[i] = static_cast<T>((static_cast<int>(i) & ~1) + 1);
87 }
88 HWY_ASSERT_VEC_EQ(d, expected.get(), DupEven(Iota(d, 1)));
89 }
90 };
91
92 HWY_NOINLINE void TestAllDupEven() {
93 ForUIF3264(ForShrinkableVectors<TestDupEven>());
94 }
95
96 struct TestDupOdd {
97 template <class T, class D>
98 HWY_NOINLINE void operator()(T /*unused*/, D d) {
99 #if HWY_TARGET != HWY_SCALAR
100 const size_t N = Lanes(d);
101 auto expected = AllocateAligned<T>(N);
102 for (size_t i = 0; i < N; ++i) {
103 expected[i] = static_cast<T>((static_cast<int>(i) & ~1) + 2);
104 }
105 HWY_ASSERT_VEC_EQ(d, expected.get(), DupOdd(Iota(d, 1)));
106 #else
107 (void)d;
108 #endif
109 }
110 };
111
112 HWY_NOINLINE void TestAllDupOdd() {
113 ForUIF3264(ForShrinkableVectors<TestDupOdd>());
114 }
115
116 struct TestOddEven {
117 template <class T, class D>
118 HWY_NOINLINE void operator()(T /*unused*/, D d) {
119 const size_t N = Lanes(d);
120 const auto even = Iota(d, 1);
121 const auto odd = Iota(d, static_cast<T>(1 + N));
122 auto expected = AllocateAligned<T>(N);
123 for (size_t i = 0; i < N; ++i) {
124 expected[i] = static_cast<T>(1 + i + ((i & 1) ? N : 0));
125 }
126 HWY_ASSERT_VEC_EQ(d, expected.get(), OddEven(odd, even));
127 }
128 };
129
130 HWY_NOINLINE void TestAllOddEven() {
131 ForAllTypes(ForShrinkableVectors<TestOddEven>());
132 }
133
134 struct TestOddEvenBlocks {
135 template <class T, class D>
136 HWY_NOINLINE void operator()(T /*unused*/, D d) {
137 const size_t N = Lanes(d);
138 const auto even = Iota(d, 1);
139 const auto odd = Iota(d, static_cast<T>(1 + N));
140 auto expected = AllocateAligned<T>(N);
141 for (size_t i = 0; i < N; ++i) {
142 const size_t idx_block = i / (16 / sizeof(T));
143 expected[i] = static_cast<T>(1 + i + ((idx_block & 1) ? N : 0));
144 }
145 HWY_ASSERT_VEC_EQ(d, expected.get(), OddEvenBlocks(odd, even));
146 }
147 };
148
149 HWY_NOINLINE void TestAllOddEvenBlocks() {
150 ForAllTypes(ForGEVectors<128, TestOddEvenBlocks>());
151 }
152
153 struct TestSwapAdjacentBlocks {
154 template <class T, class D>
155 HWY_NOINLINE void operator()(T /*unused*/, D d) {
156 const size_t N = Lanes(d);
157 constexpr size_t kLanesPerBlock = 16 / sizeof(T);
158 if (N < 2 * kLanesPerBlock) return;
159 const auto vi = Iota(d, 1);
160 auto expected = AllocateAligned<T>(N);
161 for (size_t i = 0; i < N; ++i) {
162 const size_t idx_block = i / kLanesPerBlock;
163 const size_t base = (idx_block ^ 1) * kLanesPerBlock;
164 const size_t mod = i % kLanesPerBlock;
165 expected[i] = static_cast<T>(1 + base + mod);
166 }
167 HWY_ASSERT_VEC_EQ(d, expected.get(), SwapAdjacentBlocks(vi));
168 }
169 };
170
171 HWY_NOINLINE void TestAllSwapAdjacentBlocks() {
172 ForAllTypes(ForGEVectors<128, TestSwapAdjacentBlocks>());
173 }
174
175 struct TestTableLookupLanes {
176 template <class T, class D>
177 HWY_NOINLINE void operator()(T /*unused*/, D d) {
178 const RebindToSigned<D> di;
179 using TI = TFromD<decltype(di)>;
180 #if HWY_TARGET != HWY_SCALAR
181 const size_t N = Lanes(d);
182 auto idx = AllocateAligned<TI>(N);
183 memset(idx.get(), 0, N * sizeof(TI));
184 auto expected = AllocateAligned<T>(N);
185 const auto v = Iota(d, 1);
186
187 if (N <= 8) { // Test all permutations
188 for (size_t i0 = 0; i0 < N; ++i0) {
189 idx[0] = static_cast<TI>(i0);
190
191 for (size_t i1 = 0; i1 < N; ++i1) {
192 if (N >= 2) idx[1] = static_cast<TI>(i1);
193 for (size_t i2 = 0; i2 < N; ++i2) {
194 if (N >= 4) idx[2] = static_cast<TI>(i2);
195 for (size_t i3 = 0; i3 < N; ++i3) {
196 if (N >= 4) idx[3] = static_cast<TI>(i3);
197
198 for (size_t i = 0; i < N; ++i) {
199 expected[i] = static_cast<T>(idx[i] + 1); // == v[idx[i]]
200 }
201
202 const auto opaque1 = IndicesFromVec(d, Load(di, idx.get()));
203 const auto actual1 = TableLookupLanes(v, opaque1);
204 HWY_ASSERT_VEC_EQ(d, expected.get(), actual1);
205
206 const auto opaque2 = SetTableIndices(d, idx.get());
207 const auto actual2 = TableLookupLanes(v, opaque2);
208 HWY_ASSERT_VEC_EQ(d, expected.get(), actual2);
209 }
210 }
211 }
212 }
213 } else {
214 // Too many permutations to test exhaustively; choose one with repeated
215 // and cross-block indices and ensure indices do not exceed #lanes.
216 // For larger vectors, upper lanes will be zero.
217 HWY_ALIGN TI idx_source[16] = {1, 3, 2, 2, 8, 1, 7, 6,
218 15, 14, 14, 15, 4, 9, 8, 5};
219 for (size_t i = 0; i < N; ++i) {
220 idx[i] = (i < 16) ? idx_source[i] : 0;
221 // Avoid undefined results / asan error for scalar by capping indices.
222 if (idx[i] >= static_cast<TI>(N)) {
223 idx[i] = static_cast<TI>(N - 1);
224 }
225 expected[i] = static_cast<T>(idx[i] + 1); // == v[idx[i]]
226 }
227
228 const auto opaque1 = IndicesFromVec(d, Load(di, idx.get()));
229 const auto actual1 = TableLookupLanes(v, opaque1);
230 HWY_ASSERT_VEC_EQ(d, expected.get(), actual1);
231
232 const auto opaque2 = SetTableIndices(d, idx.get());
233 const auto actual2 = TableLookupLanes(v, opaque2);
234 HWY_ASSERT_VEC_EQ(d, expected.get(), actual2);
235 }
236 #else
237 const TI index = 0;
238 const auto v = Set(d, 1);
239 const auto opaque1 = SetTableIndices(d, &index);
240 HWY_ASSERT_VEC_EQ(d, v, TableLookupLanes(v, opaque1));
241 const auto opaque2 = IndicesFromVec(d, Zero(di));
242 HWY_ASSERT_VEC_EQ(d, v, TableLookupLanes(v, opaque2));
243 #endif
244 }
245 };
246
247 HWY_NOINLINE void TestAllTableLookupLanes() {
248 ForUIF3264(ForPartialVectors<TestTableLookupLanes>());
249 }
250
251
252 // NOLINTNEXTLINE(google-readability-namespace-comments)
253 } // namespace HWY_NAMESPACE
254 } // namespace hwy
255 HWY_AFTER_NAMESPACE();
256
257 #if HWY_ONCE
258
259 namespace hwy {
260 HWY_BEFORE_TEST(HwySwizzleTest);
261 HWY_EXPORT_AND_TEST_P(HwySwizzleTest, TestAllGetLane);
262 HWY_EXPORT_AND_TEST_P(HwySwizzleTest, TestAllExtractLane);
263 HWY_EXPORT_AND_TEST_P(HwySwizzleTest, TestAllInsertLane);
264 HWY_EXPORT_AND_TEST_P(HwySwizzleTest, TestAllDupEven);
265 HWY_EXPORT_AND_TEST_P(HwySwizzleTest, TestAllDupOdd);
266 HWY_EXPORT_AND_TEST_P(HwySwizzleTest, TestAllOddEven);
267 HWY_EXPORT_AND_TEST_P(HwySwizzleTest, TestAllOddEvenBlocks);
268 HWY_EXPORT_AND_TEST_P(HwySwizzleTest, TestAllSwapAdjacentBlocks);
269 HWY_EXPORT_AND_TEST_P(HwySwizzleTest, TestAllTableLookupLanes);
270 } // namespace hwy
271
272 #endif