Debian Codegraph @

src/lib/core/highway/hwy/tests/reduction_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/reduction_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 TestSumOfLanes {
30 template <typename T, size_t N, int P,
31 hwy::EnableIf<!IsSigned<T>() || ((N & 1) != 0)>* = nullptr>
32 HWY_NOINLINE void SignedEvenLengthVectorTests(Simd<T, N, P>) {
33 // do nothing
34 }
35 template <typename T, size_t N, int P,
36 hwy::EnableIf<IsSigned<T>() && ((N & 1) == 0)>* = nullptr>
37 HWY_NOINLINE void SignedEvenLengthVectorTests(Simd<T, N, P> d) {
38 const T pairs = static_cast<T>(Lanes(d)) / static_cast<T>(2);
39
40 // Lanes are the repeated sequence -2, 1, [...]; each pair sums to -1,
41 // so the eventual total is just -(N/2).
42 Vec<decltype(d)> v =
43 InterleaveLower(Set(d, static_cast<T>(-2)), Set(d, static_cast<T>(1)));
44 HWY_ASSERT_VEC_EQ(d, Set(d, pairs * static_cast<T>(-1)), SumOfLanes(d, v));
45
46 // Similar test with a positive result.
47 v = InterleaveLower(Set(d, static_cast<T>(-2)), Set(d, static_cast<T>(4)));
48 HWY_ASSERT_VEC_EQ(d, Set(d, pairs * static_cast<T>(2)), SumOfLanes(d, v));
49 }
50
51 template <typename T, class D>
52 HWY_NOINLINE void operator()(T /*unused*/, D d) {
53 const size_t N = Lanes(d);
54 auto in_lanes = AllocateAligned<T>(N);
55
56 // Lane i = bit i, higher lanes 0
57 double sum = 0.0;
58 // Avoid setting sign bit and cap at double precision
59 constexpr size_t kBits = HWY_MIN(sizeof(T) * 8 - 1, 51);
60 for (size_t i = 0; i < N; ++i) {
61 in_lanes[i] = i < kBits ? static_cast<T>(1ull << i) : 0;
62 sum += static_cast<double>(in_lanes[i]);
63 }
64 HWY_ASSERT_VEC_EQ(d, Set(d, T(sum)),
65 SumOfLanes(d, Load(d, in_lanes.get())));
66
67 // Lane i = i (iota) to include upper lanes
68 sum = 0.0;
69 for (size_t i = 0; i < N; ++i) {
70 sum += static_cast<double>(i);
71 }
72 HWY_ASSERT_VEC_EQ(d, Set(d, T(sum)), SumOfLanes(d, Iota(d, 0)));
73
74 // Run more tests only for signed types with even vector lengths. Some of
75 // this code may not otherwise compile, so put it in a templated function.
76 SignedEvenLengthVectorTests(d);
77 }
78 };
79
80 HWY_NOINLINE void TestAllSumOfLanes() {
81 ForUIF3264(ForPartialVectors<TestSumOfLanes>());
82 ForUI16(ForPartialVectors<TestSumOfLanes>());
83
84 #if HWY_TARGET == HWY_NEON || HWY_TARGET == HWY_SSE4 || HWY_TARGET == HWY_SSSE3
85 ForUI8(ForGEVectors<64, TestSumOfLanes>());
86 #endif
87 }
88
89 struct TestMinOfLanes {
90 template <typename T, class D>
91 HWY_NOINLINE void operator()(T /*unused*/, D d) {
92 const size_t N = Lanes(d);
93 auto in_lanes = AllocateAligned<T>(N);
94
95 // Lane i = bit i, higher lanes = 2 (not the minimum)
96 T min = HighestValue<T>();
97 // Avoid setting sign bit and cap at double precision
98 constexpr size_t kBits = HWY_MIN(sizeof(T) * 8 - 1, 51);
99 for (size_t i = 0; i < N; ++i) {
100 in_lanes[i] = i < kBits ? static_cast<T>(1ull << i) : 2;
101 min = HWY_MIN(min, in_lanes[i]);
102 }
103 HWY_ASSERT_VEC_EQ(d, Set(d, min), MinOfLanes(d, Load(d, in_lanes.get())));
104
105 // Lane i = N - i to include upper lanes
106 min = HighestValue<T>();
107 for (size_t i = 0; i < N; ++i) {
108 in_lanes[i] = static_cast<T>(N - i); // no 8-bit T so no wraparound
109 min = HWY_MIN(min, in_lanes[i]);
110 }
111 HWY_ASSERT_VEC_EQ(d, Set(d, min), MinOfLanes(d, Load(d, in_lanes.get())));
112
113 // Bug #910: also check negative values
114 min = HighestValue<T>();
115 const T input_copy[] = {static_cast<T>(-1),
116 static_cast<T>(-2),
117 1,
118 2,
119 3,
120 4,
121 5,
122 6,
123 7,
124 8,
125 9,
126 10,
127 11,
128 12,
129 13,
130 14};
131 size_t i = 0;
132 for (; i < HWY_MIN(N, sizeof(input_copy) / sizeof(T)); ++i) {
133 in_lanes[i] = input_copy[i];
134 min = HWY_MIN(min, input_copy[i]);
135 }
136 // Pad with neutral element to full vector (so we can load)
137 for (; i < N; ++i) {
138 in_lanes[i] = min;
139 }
140 HWY_ASSERT_VEC_EQ(d, Set(d, min), MinOfLanes(d, Load(d, in_lanes.get())));
141 }
142 };
143
144 struct TestMaxOfLanes {
145 template <typename T, class D>
146 HWY_NOINLINE void operator()(T /*unused*/, D d) {
147 const size_t N = Lanes(d);
148 auto in_lanes = AllocateAligned<T>(N);
149
150 T max = LowestValue<T>();
151 // Avoid setting sign bit and cap at double precision
152 constexpr size_t kBits = HWY_MIN(sizeof(T) * 8 - 1, 51);
153 for (size_t i = 0; i < N; ++i) {
154 in_lanes[i] = i < kBits ? static_cast<T>(1ull << i) : 0;
155 max = HWY_MAX(max, in_lanes[i]);
156 }
157 HWY_ASSERT_VEC_EQ(d, Set(d, max), MaxOfLanes(d, Load(d, in_lanes.get())));
158
159 // Lane i = i to include upper lanes
160 max = LowestValue<T>();
161 for (size_t i = 0; i < N; ++i) {
162 in_lanes[i] = static_cast<T>(i); // no 8-bit T so no wraparound
163 max = HWY_MAX(max, in_lanes[i]);
164 }
165 HWY_ASSERT_VEC_EQ(d, Set(d, max), MaxOfLanes(d, Load(d, in_lanes.get())));
166
167 // Bug #910: also check negative values
168 max = LowestValue<T>();
169 const T input_copy[] = {static_cast<T>(-1),
170 static_cast<T>(-2),
171 1,
172 2,
173 3,
174 4,
175 5,
176 6,
177 7,
178 8,
179 9,
180 10,
181 11,
182 12,
183 13,
184 14};
185 size_t i = 0;
186 for (; i < HWY_MIN(N, sizeof(input_copy) / sizeof(T)); ++i) {
187 in_lanes[i] = input_copy[i];
188 max = HWY_MAX(max, in_lanes[i]);
189 }
190 // Pad with neutral element to full vector (so we can load)
191 for (; i < N; ++i) {
192 in_lanes[i] = max;
193 }
194 HWY_ASSERT_VEC_EQ(d, Set(d, max), MaxOfLanes(d, Load(d, in_lanes.get())));
195 }
196 };
197
198 HWY_NOINLINE void TestAllMinMaxOfLanes() {
199 const ForPartialVectors<TestMinOfLanes> test_min;
200 const ForPartialVectors<TestMaxOfLanes> test_max;
201 ForUIF3264(test_min);
202 ForUIF3264(test_max);
203 ForUI16(test_min);
204 ForUI16(test_max);
205
206 #if HWY_TARGET == HWY_NEON || HWY_TARGET == HWY_SSE4 || HWY_TARGET == HWY_SSSE3
207 ForUI8(ForGEVectors<64, TestMinOfLanes>());
208 ForUI8(ForGEVectors<64, TestMaxOfLanes>());
209 #endif
210 }
211
212 struct TestSumsOf8 {
213 template <typename T, class D>
214 HWY_NOINLINE void operator()(T /*unused*/, D d) {
215 RandomState rng;
216
217 const size_t N = Lanes(d);
218 if (N < 8) return;
219 const Repartition<uint64_t, D> du64;
220
221 auto in_lanes = AllocateAligned<T>(N);
222 auto sum_lanes = AllocateAligned<uint64_t>(N / 8);
223
224 for (size_t rep = 0; rep < 100; ++rep) {
225 for (size_t i = 0; i < N; ++i) {
226 in_lanes[i] = Random64(&rng) & 0xFF;
227 }
228
229 for (size_t idx_sum = 0; idx_sum < N / 8; ++idx_sum) {
230 uint64_t sum = 0;
231 for (size_t i = 0; i < 8; ++i) {
232 sum += in_lanes[idx_sum * 8 + i];
233 }
234 sum_lanes[idx_sum] = sum;
235 }
236
237 const Vec<D> in = Load(d, in_lanes.get());
238 HWY_ASSERT_VEC_EQ(du64, sum_lanes.get(), SumsOf8(in));
239 }
240 }
241 };
242
243 HWY_NOINLINE void TestAllSumsOf8() {
244 ForGEVectors<64, TestSumsOf8>()(uint8_t());
245 }
246
247 // NOLINTNEXTLINE(google-readability-namespace-comments)
248 } // namespace HWY_NAMESPACE
249 } // namespace hwy
250 HWY_AFTER_NAMESPACE();
251
252 #if HWY_ONCE
253
254 namespace hwy {
255 HWY_BEFORE_TEST(HwyReductionTest);
256 HWY_EXPORT_AND_TEST_P(HwyReductionTest, TestAllSumOfLanes);
257 HWY_EXPORT_AND_TEST_P(HwyReductionTest, TestAllMinMaxOfLanes);
258 HWY_EXPORT_AND_TEST_P(HwyReductionTest, TestAllSumsOf8);
259 } // namespace hwy
260
261 #endif