Debian Codegraph @

src/lib/core/highway/hwy/tests/float_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 // Tests some ops specific to floating-point types (Div, Round etc.)
17
18 #include <stddef.h>
19 #include <stdint.h>
20
21 #include <algorithm> // std::copy, std::fill
22 #include <limits>
23 #include <cmath> // std::abs, std::isnan, std::isinf, std::ceil, std::floor
24
25 #undef HWY_TARGET_INCLUDE
26 #define HWY_TARGET_INCLUDE "tests/float_test.cc"
27 #include "hwy/foreach_target.h" // IWYU pragma: keep
28 #include "hwy/highway.h"
29 #include "hwy/tests/test_util-inl.h"
30
31 HWY_BEFORE_NAMESPACE();
32 namespace hwy {
33 namespace HWY_NAMESPACE {
34
35 struct TestDiv {
36 template <typename T, class D>
37 HWY_NOINLINE void operator()(T /*unused*/, D d) {
38 const auto v = Iota(d, T(-2));
39 const auto v1 = Set(d, T(1));
40
41 // Unchanged after division by 1.
42 HWY_ASSERT_VEC_EQ(d, v, Div(v, v1));
43
44 const size_t N = Lanes(d);
45 auto expected = AllocateAligned<T>(N);
46 for (size_t i = 0; i < N; ++i) {
47 expected[i] = (T(i) - 2) / T(2);
48 }
49 HWY_ASSERT_VEC_EQ(d, expected.get(), Div(v, Set(d, T(2))));
50 }
51 };
52
53 HWY_NOINLINE void TestAllDiv() { ForFloatTypes(ForPartialVectors<TestDiv>()); }
54
55 struct TestApproximateReciprocal {
56 template <typename T, class D>
57 HWY_NOINLINE void operator()(T /*unused*/, D d) {
58 const auto v = Iota(d, T(-2));
59 const auto nonzero = IfThenElse(Eq(v, Zero(d)), Set(d, T(1)), v);
60 const size_t N = Lanes(d);
61 auto input = AllocateAligned<T>(N);
62 Store(nonzero, d, input.get());
63
64 auto actual = AllocateAligned<T>(N);
65 Store(ApproximateReciprocal(nonzero), d, actual.get());
66
67 double max_l1 = 0.0;
68 double worst_expected = 0.0;
69 double worst_actual = 0.0;
70 for (size_t i = 0; i < N; ++i) {
71 const double expected = 1.0 / input[i];
72 const double l1 = std::abs(expected - actual[i]);
73 if (l1 > max_l1) {
74 max_l1 = l1;
75 worst_expected = expected;
76 worst_actual = actual[i];
77 }
78 }
79 const double abs_worst_expected = std::abs(worst_expected);
80 if (abs_worst_expected > 1E-5) {
81 const double max_rel = max_l1 / abs_worst_expected;
82 fprintf(stderr, "max l1 %f rel %f (%f vs %f)\n", max_l1, max_rel,
83 worst_expected, worst_actual);
84 HWY_ASSERT(max_rel < 0.004);
85 }
86 }
87 };
88
89 HWY_NOINLINE void TestAllApproximateReciprocal() {
90 ForPartialVectors<TestApproximateReciprocal>()(float());
91 }
92
93 struct TestSquareRoot {
94 template <typename T, class D>
95 HWY_NOINLINE void operator()(T /*unused*/, D d) {
96 const auto vi = Iota(d, 0);
97 HWY_ASSERT_VEC_EQ(d, vi, Sqrt(Mul(vi, vi)));
98 }
99 };
100
101 HWY_NOINLINE void TestAllSquareRoot() {
102 ForFloatTypes(ForPartialVectors<TestSquareRoot>());
103 }
104
105 struct TestReciprocalSquareRoot {
106 template <typename T, class D>
107 HWY_NOINLINE void operator()(T /*unused*/, D d) {
108 const auto v = Set(d, 123.0f);
109 const size_t N = Lanes(d);
110 auto lanes = AllocateAligned<T>(N);
111 Store(ApproximateReciprocalSqrt(v), d, lanes.get());
112 for (size_t i = 0; i < N; ++i) {
113 float err = lanes[i] - 0.090166f;
114 if (err < 0.0f) err = -err;
115 if (err >= 4E-4f) {
116 HWY_ABORT("Lane %d (%d): actual %f err %f\n", static_cast<int>(i),
117 static_cast<int>(N), lanes[i], err);
118 }
119 }
120 }
121 };
122
123 HWY_NOINLINE void TestAllReciprocalSquareRoot() {
124 ForPartialVectors<TestReciprocalSquareRoot>()(float());
125 }
126
127 template <typename T, class D>
128 AlignedFreeUniquePtr<T[]> RoundTestCases(T /*unused*/, D d, size_t& padded) {
129 const T eps = std::numeric_limits<T>::epsilon();
130 const T test_cases[] = {
131 // +/- 1
132 T(1),
133 T(-1),
134 // +/- 0
135 T(0),
136 T(-0),
137 // near 0
138 T(0.4),
139 T(-0.4),
140 // +/- integer
141 T(4),
142 T(-32),
143 // positive near limit
144 MantissaEnd<T>() - T(1.5),
145 MantissaEnd<T>() + T(1.5),
146 // negative near limit
147 -MantissaEnd<T>() - T(1.5),
148 -MantissaEnd<T>() + T(1.5),
149 // positive tiebreak
150 T(1.5),
151 T(2.5),
152 // negative tiebreak
153 T(-1.5),
154 T(-2.5),
155 // positive +/- delta
156 T(2.0001),
157 T(3.9999),
158 // negative +/- delta
159 T(-999.9999),
160 T(-998.0001),
161 // positive +/- epsilon
162 T(1) + eps,
163 T(1) - eps,
164 // negative +/- epsilon
165 T(-1) + eps,
166 T(-1) - eps,
167 // +/- huge (but still fits in float)
168 T(1E34),
169 T(-1E35),
170 // +/- infinity
171 std::numeric_limits<T>::infinity(),
172 -std::numeric_limits<T>::infinity(),
173 // qNaN
174 GetLane(NaN(d))
175 };
176 const size_t kNumTestCases = sizeof(test_cases) / sizeof(test_cases[0]);
177 const size_t N = Lanes(d);
178 padded = RoundUpTo(kNumTestCases, N); // allow loading whole vectors
179 auto in = AllocateAligned<T>(padded);
180 auto expected = AllocateAligned<T>(padded);
181 std::copy(test_cases, test_cases + kNumTestCases, in.get());
182 std::fill(in.get() + kNumTestCases, in.get() + padded, T(0));
183 return in;
184 }
185
186 struct TestRound {
187 template <typename T, class D>
188 HWY_NOINLINE void operator()(T t, D d) {
189 size_t padded;
190 auto in = RoundTestCases(t, d, padded);
191 auto expected = AllocateAligned<T>(padded);
192
193 for (size_t i = 0; i < padded; ++i) {
194 // Avoid [std::]round, which does not round to nearest *even*.
195 // NOTE: std:: version from C++11 cmath is not defined in RVV GCC, see
196 // https://lists.freebsd.org/pipermail/freebsd-current/2014-January/048130.html
197 expected[i] = static_cast<T>(nearbyint(in[i]));
198 }
199 for (size_t i = 0; i < padded; i += Lanes(d)) {
200 HWY_ASSERT_VEC_EQ(d, &expected[i], Round(Load(d, &in[i])));
201 }
202 }
203 };
204
205 HWY_NOINLINE void TestAllRound() {
206 ForFloatTypes(ForPartialVectors<TestRound>());
207 }
208
209 struct TestNearestInt {
210 template <typename TF, class DF>
211 HWY_NOINLINE void operator()(TF tf, const DF df) {
212 using TI = MakeSigned<TF>;
213 const RebindToSigned<DF> di;
214
215 size_t padded;
216 auto in = RoundTestCases(tf, df, padded);
217 auto expected = AllocateAligned<TI>(padded);
218
219 constexpr double max = static_cast<double>(LimitsMax<TI>());
220 for (size_t i = 0; i < padded; ++i) {
221 if (std::isnan(in[i])) {
222 // We replace NaN with 0 below (no_nan)
223 expected[i] = 0;
224 } else if (std::isinf(in[i]) || double{std::abs(in[i])} >= max) {
225 // Avoid undefined result for lrintf
226 expected[i] = std::signbit(in[i]) ? LimitsMin<TI>() : LimitsMax<TI>();
227 } else {
228 expected[i] = static_cast<TI>(lrintf(in[i]));
229 }
230 }
231 for (size_t i = 0; i < padded; i += Lanes(df)) {
232 const auto v = Load(df, &in[i]);
233 const auto no_nan = IfThenElse(Eq(v, v), v, Zero(df));
234 HWY_ASSERT_VEC_EQ(di, &expected[i], NearestInt(no_nan));
235 }
236 }
237 };
238
239 HWY_NOINLINE void TestAllNearestInt() {
240 ForPartialVectors<TestNearestInt>()(float());
241 }
242
243 struct TestTrunc {
244 template <typename T, class D>
245 HWY_NOINLINE void operator()(T t, D d) {
246 size_t padded;
247 auto in = RoundTestCases(t, d, padded);
248 auto expected = AllocateAligned<T>(padded);
249
250 for (size_t i = 0; i < padded; ++i) {
251 // NOTE: std:: version from C++11 cmath is not defined in RVV GCC, see
252 // https://lists.freebsd.org/pipermail/freebsd-current/2014-January/048130.html
253 expected[i] = static_cast<T>(trunc(in[i]));
254 }
255 for (size_t i = 0; i < padded; i += Lanes(d)) {
256 HWY_ASSERT_VEC_EQ(d, &expected[i], Trunc(Load(d, &in[i])));
257 }
258 }
259 };
260
261 HWY_NOINLINE void TestAllTrunc() {
262 ForFloatTypes(ForPartialVectors<TestTrunc>());
263 }
264
265 struct TestCeil {
266 template <typename T, class D>
267 HWY_NOINLINE void operator()(T t, D d) {
268 size_t padded;
269 auto in = RoundTestCases(t, d, padded);
270 auto expected = AllocateAligned<T>(padded);
271
272 for (size_t i = 0; i < padded; ++i) {
273 expected[i] = std::ceil(in[i]);
274 }
275 for (size_t i = 0; i < padded; i += Lanes(d)) {
276 HWY_ASSERT_VEC_EQ(d, &expected[i], Ceil(Load(d, &in[i])));
277 }
278 }
279 };
280
281 HWY_NOINLINE void TestAllCeil() {
282 ForFloatTypes(ForPartialVectors<TestCeil>());
283 }
284
285 struct TestFloor {
286 template <typename T, class D>
287 HWY_NOINLINE void operator()(T t, D d) {
288 size_t padded;
289 auto in = RoundTestCases(t, d, padded);
290 auto expected = AllocateAligned<T>(padded);
291
292 for (size_t i = 0; i < padded; ++i) {
293 expected[i] = std::floor(in[i]);
294 }
295 for (size_t i = 0; i < padded; i += Lanes(d)) {
296 HWY_ASSERT_VEC_EQ(d, &expected[i], Floor(Load(d, &in[i])));
297 }
298 }
299 };
300
301 HWY_NOINLINE void TestAllFloor() {
302 ForFloatTypes(ForPartialVectors<TestFloor>());
303 }
304
305 struct TestAbsDiff {
306 template <typename T, class D>
307 HWY_NOINLINE void operator()(T /*unused*/, D d) {
308 const size_t N = Lanes(d);
309 auto in_lanes_a = AllocateAligned<T>(N);
310 auto in_lanes_b = AllocateAligned<T>(N);
311 auto out_lanes = AllocateAligned<T>(N);
312 for (size_t i = 0; i < N; ++i) {
313 in_lanes_a[i] = static_cast<T>((i ^ 1u) << i);
314 in_lanes_b[i] = static_cast<T>(i << i);
315 out_lanes[i] = std::abs(in_lanes_a[i] - in_lanes_b[i]);
316 }
317 const auto a = Load(d, in_lanes_a.get());
318 const auto b = Load(d, in_lanes_b.get());
319 const auto expected = Load(d, out_lanes.get());
320 HWY_ASSERT_VEC_EQ(d, expected, AbsDiff(a, b));
321 HWY_ASSERT_VEC_EQ(d, expected, AbsDiff(b, a));
322 }
323 };
324
325 HWY_NOINLINE void TestAllAbsDiff() {
326 ForPartialVectors<TestAbsDiff>()(float());
327 }
328
329 // NOLINTNEXTLINE(google-readability-namespace-comments)
330 } // namespace HWY_NAMESPACE
331 } // namespace hwy
332 HWY_AFTER_NAMESPACE();
333
334 #if HWY_ONCE
335
336 namespace hwy {
337 HWY_BEFORE_TEST(HwyFloatTest);
338 HWY_EXPORT_AND_TEST_P(HwyFloatTest, TestAllDiv);
339 HWY_EXPORT_AND_TEST_P(HwyFloatTest, TestAllApproximateReciprocal);
340 HWY_EXPORT_AND_TEST_P(HwyFloatTest, TestAllSquareRoot);
341 HWY_EXPORT_AND_TEST_P(HwyFloatTest, TestAllReciprocalSquareRoot);
342 HWY_EXPORT_AND_TEST_P(HwyFloatTest, TestAllRound);
343 HWY_EXPORT_AND_TEST_P(HwyFloatTest, TestAllNearestInt);
344 HWY_EXPORT_AND_TEST_P(HwyFloatTest, TestAllTrunc);
345 HWY_EXPORT_AND_TEST_P(HwyFloatTest, TestAllCeil);
346 HWY_EXPORT_AND_TEST_P(HwyFloatTest, TestAllFloor);
347 HWY_EXPORT_AND_TEST_P(HwyFloatTest, TestAllAbsDiff);
348 } // namespace hwy
349
350 #endif