Debian Codegraph @

src/lib/core/highway/hwy/tests/interleaved_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
19 #undef HWY_TARGET_INCLUDE
20 #define HWY_TARGET_INCLUDE "tests/interleaved_test.cc"
21 #include "hwy/foreach_target.h" // IWYU pragma: keep
22 #include "hwy/highway.h"
23 #include "hwy/tests/test_util-inl.h"
24
25 HWY_BEFORE_NAMESPACE();
26 namespace hwy {
27 namespace HWY_NAMESPACE {
28
29 struct TestLoadStoreInterleaved2 {
30 template <class T, class D>
31 HWY_NOINLINE void operator()(T /*unused*/, D d) {
32 const size_t N = Lanes(d);
33
34 RandomState rng;
35
36 // Data to be interleaved
37 auto bytes = AllocateAligned<T>(2 * N);
38 for (size_t i = 0; i < 2 * N; ++i) {
39 bytes[i] = static_cast<T>(Random32(&rng) & 0xFF);
40 }
41 const auto in0 = Load(d, &bytes[0 * N]);
42 const auto in1 = Load(d, &bytes[1 * N]);
43
44 // Interleave here, ensure vector results match scalar
45 auto expected = AllocateAligned<T>(3 * N);
46 auto actual_aligned = AllocateAligned<T>(3 * N + 1);
47 T* actual = actual_aligned.get() + 1;
48
49 for (size_t rep = 0; rep < 100; ++rep) {
50 for (size_t i = 0; i < N; ++i) {
51 expected[2 * i + 0] = bytes[0 * N + i];
52 expected[2 * i + 1] = bytes[1 * N + i];
53 // Ensure we do not write more than 2*N bytes
54 expected[2 * N + i] = actual[2 * N + i] = 0;
55 }
56 StoreInterleaved2(in0, in1, d, actual);
57 size_t pos = 0;
58 if (!BytesEqual(expected.get(), actual, 3 * N * sizeof(T), &pos)) {
59 Print(d, "in0", in0, pos / 4);
60 Print(d, "in1", in1, pos / 4);
61 const size_t i = pos;
62 fprintf(stderr, "interleaved i=%d %f %f %f %f %f %f %f %f\n",
63 static_cast<int>(i), static_cast<double>(actual[i]),
64 static_cast<double>(actual[i + 1]),
65 static_cast<double>(actual[i + 2]),
66 static_cast<double>(actual[i + 3]),
67 static_cast<double>(actual[i + 4]),
68 static_cast<double>(actual[i + 5]),
69 static_cast<double>(actual[i + 6]),
70 static_cast<double>(actual[i + 7]));
71 HWY_ASSERT(false);
72 }
73
74 Vec<D> out0, out1;
75 LoadInterleaved2(d, actual, out0, out1);
76 HWY_ASSERT_VEC_EQ(d, in0, out0);
77 HWY_ASSERT_VEC_EQ(d, in1, out1);
78 }
79 }
80 };
81
82 HWY_NOINLINE void TestAllLoadStoreInterleaved2() {
83 #if HWY_TARGET == HWY_RVV
84 // Segments are limited to 8 registers, so we can only go up to LMUL=2.
85 const ForExtendableVectors<TestLoadStoreInterleaved2, 2> test;
86 #else
87 const ForPartialVectors<TestLoadStoreInterleaved2> test;
88 #endif
89 ForAllTypes(test);
90 }
91
92 // Workaround for build timeout on GCC 12 aarch64, see #776
93 #if HWY_COMPILER_GCC_ACTUAL >= 1200 && HWY_ARCH_ARM_A64
94 #define HWY_BROKEN_LOAD34 1
95 #else
96 #define HWY_BROKEN_LOAD34 0
97 #endif
98
99 #if !HWY_BROKEN_LOAD34
100
101 struct TestLoadStoreInterleaved3 {
102 template <class T, class D>
103 HWY_NOINLINE void operator()(T /*unused*/, D d) {
104 const size_t N = Lanes(d);
105
106 RandomState rng;
107
108 // Data to be interleaved
109 auto bytes = AllocateAligned<T>(3 * N);
110 for (size_t i = 0; i < 3 * N; ++i) {
111 bytes[i] = static_cast<T>(Random32(&rng) & 0xFF);
112 }
113 const auto in0 = Load(d, &bytes[0 * N]);
114 const auto in1 = Load(d, &bytes[1 * N]);
115 const auto in2 = Load(d, &bytes[2 * N]);
116
117 // Interleave here, ensure vector results match scalar
118 auto expected = AllocateAligned<T>(4 * N);
119 auto actual_aligned = AllocateAligned<T>(4 * N + 1);
120 T* actual = actual_aligned.get() + 1;
121
122 for (size_t rep = 0; rep < 100; ++rep) {
123 for (size_t i = 0; i < N; ++i) {
124 expected[3 * i + 0] = bytes[0 * N + i];
125 expected[3 * i + 1] = bytes[1 * N + i];
126 expected[3 * i + 2] = bytes[2 * N + i];
127 // Ensure we do not write more than 3*N bytes
128 expected[3 * N + i] = actual[3 * N + i] = 0;
129 }
130 StoreInterleaved3(in0, in1, in2, d, actual);
131 size_t pos = 0;
132 if (!BytesEqual(expected.get(), actual, 4 * N * sizeof(T), &pos)) {
133 Print(d, "in0", in0, pos / 3, N);
134 Print(d, "in1", in1, pos / 3, N);
135 Print(d, "in2", in2, pos / 3, N);
136 const size_t i = pos;
137 fprintf(stderr, "interleaved i=%d %f %f %f %f %f %f\n",
138 static_cast<int>(i), static_cast<double>(actual[i]),
139 static_cast<double>(actual[i + 1]),
140 static_cast<double>(actual[i + 2]),
141 static_cast<double>(actual[i + 3]),
142 static_cast<double>(actual[i + 4]),
143 static_cast<double>(actual[i + 5]));
144 HWY_ASSERT(false);
145 }
146
147 Vec<D> out0, out1, out2;
148 LoadInterleaved3(d, actual, out0, out1, out2);
149 HWY_ASSERT_VEC_EQ(d, in0, out0);
150 HWY_ASSERT_VEC_EQ(d, in1, out1);
151 HWY_ASSERT_VEC_EQ(d, in2, out2);
152 }
153 }
154 };
155
156 HWY_NOINLINE void TestAllLoadStoreInterleaved3() {
157 #if HWY_TARGET == HWY_RVV
158 // Segments are limited to 8 registers, so we can only go up to LMUL=2.
159 const ForExtendableVectors<TestLoadStoreInterleaved3, 2> test;
160 #else
161 const ForPartialVectors<TestLoadStoreInterleaved3> test;
162 #endif
163 ForAllTypes(test);
164 }
165
166 struct TestLoadStoreInterleaved4 {
167 template <class T, class D>
168 HWY_NOINLINE void operator()(T /*unused*/, D d) {
169 const size_t N = Lanes(d);
170
171 RandomState rng;
172
173 // Data to be interleaved
174 auto bytes = AllocateAligned<T>(4 * N);
175
176 for (size_t i = 0; i < 4 * N; ++i) {
177 bytes[i] = static_cast<T>(Random32(&rng) & 0xFF);
178 }
179 const auto in0 = Load(d, &bytes[0 * N]);
180 const auto in1 = Load(d, &bytes[1 * N]);
181 const auto in2 = Load(d, &bytes[2 * N]);
182 const auto in3 = Load(d, &bytes[3 * N]);
183
184 // Interleave here, ensure vector results match scalar
185 auto expected = AllocateAligned<T>(5 * N);
186 auto actual_aligned = AllocateAligned<T>(5 * N + 1);
187 T* actual = actual_aligned.get() + 1;
188
189 for (size_t rep = 0; rep < 100; ++rep) {
190 for (size_t i = 0; i < N; ++i) {
191 expected[4 * i + 0] = bytes[0 * N + i];
192 expected[4 * i + 1] = bytes[1 * N + i];
193 expected[4 * i + 2] = bytes[2 * N + i];
194 expected[4 * i + 3] = bytes[3 * N + i];
195 // Ensure we do not write more than 4*N bytes
196 expected[4 * N + i] = actual[4 * N + i] = 0;
197 }
198 StoreInterleaved4(in0, in1, in2, in3, d, actual);
199 size_t pos = 0;
200 if (!BytesEqual(expected.get(), actual, 5 * N * sizeof(T), &pos)) {
201 Print(d, "in0", in0, pos / 4);
202 Print(d, "in1", in1, pos / 4);
203 Print(d, "in2", in2, pos / 4);
204 Print(d, "in3", in3, pos / 4);
205 const size_t i = pos;
206 fprintf(stderr, "interleaved i=%d %f %f %f %f %f %f %f %f\n",
207 static_cast<int>(i), static_cast<double>(actual[i]),
208 static_cast<double>(actual[i + 1]),
209 static_cast<double>(actual[i + 2]),
210 static_cast<double>(actual[i + 3]),
211 static_cast<double>(actual[i + 4]),
212 static_cast<double>(actual[i + 5]),
213 static_cast<double>(actual[i + 6]),
214 static_cast<double>(actual[i + 7]));
215 HWY_ASSERT(false);
216 }
217
218 Vec<D> out0, out1, out2, out3;
219 LoadInterleaved4(d, actual, out0, out1, out2, out3);
220 HWY_ASSERT_VEC_EQ(d, in0, out0);
221 HWY_ASSERT_VEC_EQ(d, in1, out1);
222 HWY_ASSERT_VEC_EQ(d, in2, out2);
223 HWY_ASSERT_VEC_EQ(d, in3, out3);
224 }
225 }
226 };
227
228 HWY_NOINLINE void TestAllLoadStoreInterleaved4() {
229 #if HWY_TARGET == HWY_RVV
230 // Segments are limited to 8 registers, so we can only go up to LMUL=2.
231 const ForExtendableVectors<TestLoadStoreInterleaved4, 2> test;
232 #else
233 const ForPartialVectors<TestLoadStoreInterleaved4> test;
234 #endif
235 ForAllTypes(test);
236 }
237
238 #endif // !HWY_BROKEN_LOAD34
239
240 // NOLINTNEXTLINE(google-readability-namespace-comments)
241 } // namespace HWY_NAMESPACE
242 } // namespace hwy
243 HWY_AFTER_NAMESPACE();
244
245 #if HWY_ONCE
246
247 namespace hwy {
248 HWY_BEFORE_TEST(HwyInterleavedTest);
249 HWY_EXPORT_AND_TEST_P(HwyInterleavedTest, TestAllLoadStoreInterleaved2);
250 #if !HWY_BROKEN_LOAD34
251 HWY_EXPORT_AND_TEST_P(HwyInterleavedTest, TestAllLoadStoreInterleaved3);
252 HWY_EXPORT_AND_TEST_P(HwyInterleavedTest, TestAllLoadStoreInterleaved4);
253 #endif
254 } // namespace hwy
255
256 #endif