Debian Codegraph @

src/lib/core/highway/hwy/contrib/math/math_test.cc

raw symbols SCIP
1 // Copyright 2020 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 #ifndef __STDC_FORMAT_MACROS
17 #define __STDC_FORMAT_MACROS // before inttypes.h
18 #endif
19 #include <inttypes.h>
20 #include <stdio.h>
21
22 #include <cfloat> // FLT_MAX
23 #include <cmath> // std::abs
24 #include <type_traits>
25
26 // clang-format off
27 #undef HWY_TARGET_INCLUDE
28 #define HWY_TARGET_INCLUDE "hwy/contrib/math/math_test.cc"
29 #include "hwy/foreach_target.h" // IWYU pragma: keep
30
31 #include "hwy/contrib/math/math-inl.h"
32 #include "hwy/tests/test_util-inl.h"
33 // clang-format on
34
35 HWY_BEFORE_NAMESPACE();
36 namespace hwy {
37 namespace HWY_NAMESPACE {
38
39 template <class Out, class In>
40 inline Out BitCast(const In& in) {
41 static_assert(sizeof(Out) == sizeof(In), "");
42 Out out;
43 CopyBytes<sizeof(out)>(&in, &out);
44 return out;
45 }
46
47 template <class T, class D>
48 HWY_NOINLINE void TestMath(const std::string name, T (*fx1)(T),
49 Vec<D> (*fxN)(D, VecArg<Vec<D>>), D d, T min, T max,
50 uint64_t max_error_ulp) {
51 using UintT = MakeUnsigned<T>;
52
53 const UintT min_bits = BitCast<UintT>(min);
54 const UintT max_bits = BitCast<UintT>(max);
55
56 // If min is negative and max is positive, the range needs to be broken into
57 // two pieces, [+0, max] and [-0, min], otherwise [min, max].
58 int range_count = 1;
59 UintT ranges[2][2] = {{min_bits, max_bits}, {0, 0}};
60 if ((min < 0.0) && (max > 0.0)) {
61 ranges[0][0] = BitCast<UintT>(static_cast<T>(+0.0));
62 ranges[0][1] = max_bits;
63 ranges[1][0] = BitCast<UintT>(static_cast<T>(-0.0));
64 ranges[1][1] = min_bits;
65 range_count = 2;
66 }
67
68 uint64_t max_ulp = 0;
69 // Emulation is slower, so cannot afford as many.
70 constexpr UintT kSamplesPerRange = static_cast<UintT>(AdjustedReps(4000));
71 for (int range_index = 0; range_index < range_count; ++range_index) {
72 const UintT start = ranges[range_index][0];
73 const UintT stop = ranges[range_index][1];
74 const UintT step = HWY_MAX(1, ((stop - start) / kSamplesPerRange));
75 for (UintT value_bits = start; value_bits <= stop; value_bits += step) {
76 // For reasons unknown, the HWY_MAX is necessary on RVV, otherwise
77 // value_bits can be less than start, and thus possibly NaN.
78 const T value = BitCast<T>(HWY_MIN(HWY_MAX(start, value_bits), stop));
79 const T actual = GetLane(fxN(d, Set(d, value)));
80 const T expected = fx1(value);
81
82 // Skip small inputs and outputs on armv7, it flushes subnormals to zero.
83 #if HWY_TARGET == HWY_NEON && HWY_ARCH_ARM_V7
84 if ((std::abs(value) < 1e-37f) || (std::abs(expected) < 1e-37f)) {
85 continue;
86 }
87 #endif
88
89 const auto ulp = hwy::detail::ComputeUlpDelta(actual, expected);
90 max_ulp = HWY_MAX(max_ulp, ulp);
91 if (ulp > max_error_ulp) {
92 fprintf(stderr,
93 "%s: %s(%f) expected %f actual %f ulp %" PRIu64 " max ulp %u\n",
94 hwy::TypeName(T(), Lanes(d)).c_str(), name.c_str(), value,
95 expected, actual, static_cast<uint64_t>(ulp),
96 static_cast<uint32_t>(max_error_ulp));
97 }
98 }
99 }
100 fprintf(stderr, "%s: %s max_ulp %" PRIu64 "\n",
101 hwy::TypeName(T(), Lanes(d)).c_str(), name.c_str(), max_ulp);
102 HWY_ASSERT(max_ulp <= max_error_ulp);
103 }
104
105 #define DEFINE_MATH_TEST_FUNC(NAME) \
106 HWY_NOINLINE void TestAll##NAME() { \
107 ForFloatTypes(ForPartialVectors<Test##NAME>()); \
108 }
109
110 #undef DEFINE_MATH_TEST
111 #define DEFINE_MATH_TEST(NAME, F32x1, F32xN, F32_MIN, F32_MAX, F32_ERROR, \
112 F64x1, F64xN, F64_MIN, F64_MAX, F64_ERROR) \
113 struct Test##NAME { \
114 template <class T, class D> \
115 HWY_NOINLINE void operator()(T, D d) { \
116 if (sizeof(T) == 4) { \
117 TestMath<T, D>(HWY_STR(NAME), F32x1, F32xN, d, F32_MIN, F32_MAX, \
118 F32_ERROR); \
119 } else { \
120 TestMath<T, D>(HWY_STR(NAME), F64x1, F64xN, d, \
121 static_cast<T>(F64_MIN), static_cast<T>(F64_MAX), \
122 F64_ERROR); \
123 } \
124 } \
125 }; \
126 DEFINE_MATH_TEST_FUNC(NAME)
127
128 // Floating point values closest to but less than 1.0
129 const float kNearOneF = BitCast<float>(0x3F7FFFFF);
130 const double kNearOneD = BitCast<double>(0x3FEFFFFFFFFFFFFFULL);
131
132 // The discrepancy is unacceptably large for MSYS2 (less accurate libm?), so
133 // only increase the error tolerance there.
134 constexpr uint64_t Cos64ULP() {
135 #if defined(__MINGW32__)
136 return 23;
137 #else
138 return 3;
139 #endif
140 }
141
142 constexpr uint64_t ACosh32ULP() {
143 #if defined(__MINGW32__)
144 return 8;
145 #else
146 return 3;
147 #endif
148 }
149
150 // clang-format off
151 DEFINE_MATH_TEST(Acos,
152 std::acos, CallAcos, -1.0f, +1.0f, 3, // NEON is 3 instead of 2
153 std::acos, CallAcos, -1.0, +1.0, 2)
154 DEFINE_MATH_TEST(Acosh,
155 std::acosh, CallAcosh, +1.0f, +FLT_MAX, ACosh32ULP(),
156 std::acosh, CallAcosh, +1.0, +DBL_MAX, 3)
157 DEFINE_MATH_TEST(Asin,
158 std::asin, CallAsin, -1.0f, +1.0f, 4, // ARMv7 is 4 instead of 2
159 std::asin, CallAsin, -1.0, +1.0, 2)
160 DEFINE_MATH_TEST(Asinh,
161 std::asinh, CallAsinh, -FLT_MAX, +FLT_MAX, 3,
162 std::asinh, CallAsinh, -DBL_MAX, +DBL_MAX, 3)
163 DEFINE_MATH_TEST(Atan,
164 std::atan, CallAtan, -FLT_MAX, +FLT_MAX, 3,
165 std::atan, CallAtan, -DBL_MAX, +DBL_MAX, 3)
166 DEFINE_MATH_TEST(Atanh,
167 std::atanh, CallAtanh, -kNearOneF, +kNearOneF, 4, // NEON is 4 instead of 3
168 std::atanh, CallAtanh, -kNearOneD, +kNearOneD, 3)
169 DEFINE_MATH_TEST(Cos,
170 std::cos, CallCos, -39000.0f, +39000.0f, 3,
171 std::cos, CallCos, -39000.0, +39000.0, Cos64ULP())
172 DEFINE_MATH_TEST(Exp,
173 std::exp, CallExp, -FLT_MAX, +104.0f, 1,
174 std::exp, CallExp, -DBL_MAX, +104.0, 1)
175 DEFINE_MATH_TEST(Expm1,
176 std::expm1, CallExpm1, -FLT_MAX, +104.0f, 4,
177 std::expm1, CallExpm1, -DBL_MAX, +104.0, 4)
178 DEFINE_MATH_TEST(Log,
179 std::log, CallLog, +FLT_MIN, +FLT_MAX, 1,
180 std::log, CallLog, +DBL_MIN, +DBL_MAX, 1)
181 DEFINE_MATH_TEST(Log10,
182 std::log10, CallLog10, +FLT_MIN, +FLT_MAX, 2,
183 std::log10, CallLog10, +DBL_MIN, +DBL_MAX, 2)
184 DEFINE_MATH_TEST(Log1p,
185 std::log1p, CallLog1p, +0.0f, +1e37f, 3, // NEON is 3 instead of 2
186 std::log1p, CallLog1p, +0.0, +DBL_MAX, 2)
187 DEFINE_MATH_TEST(Log2,
188 std::log2, CallLog2, +FLT_MIN, +FLT_MAX, 2,
189 std::log2, CallLog2, +DBL_MIN, +DBL_MAX, 2)
190 DEFINE_MATH_TEST(Sin,
191 std::sin, CallSin, -39000.0f, +39000.0f, 3,
192 std::sin, CallSin, -39000.0, +39000.0, 4) // MSYS is 4 instead of 3
193 DEFINE_MATH_TEST(Sinh,
194 std::sinh, CallSinh, -80.0f, +80.0f, 4,
195 std::sinh, CallSinh, -709.0, +709.0, 4)
196 DEFINE_MATH_TEST(Tanh,
197 std::tanh, CallTanh, -FLT_MAX, +FLT_MAX, 4,
198 std::tanh, CallTanh, -DBL_MAX, +DBL_MAX, 4)
199 // clang-format on
200
201 // NOLINTNEXTLINE(google-readability-namespace-comments)
202 } // namespace HWY_NAMESPACE
203 } // namespace hwy
204 HWY_AFTER_NAMESPACE();
205
206 #if HWY_ONCE
207
208 namespace hwy {
209 HWY_BEFORE_TEST(HwyMathTest);
210 HWY_EXPORT_AND_TEST_P(HwyMathTest, TestAllAcos);
211 HWY_EXPORT_AND_TEST_P(HwyMathTest, TestAllAcosh);
212 HWY_EXPORT_AND_TEST_P(HwyMathTest, TestAllAsin);
213 HWY_EXPORT_AND_TEST_P(HwyMathTest, TestAllAsinh);
214 HWY_EXPORT_AND_TEST_P(HwyMathTest, TestAllAtan);
215 HWY_EXPORT_AND_TEST_P(HwyMathTest, TestAllAtanh);
216 HWY_EXPORT_AND_TEST_P(HwyMathTest, TestAllCos);
217 HWY_EXPORT_AND_TEST_P(HwyMathTest, TestAllExp);
218 HWY_EXPORT_AND_TEST_P(HwyMathTest, TestAllExpm1);
219 HWY_EXPORT_AND_TEST_P(HwyMathTest, TestAllLog);
220 HWY_EXPORT_AND_TEST_P(HwyMathTest, TestAllLog10);
221 HWY_EXPORT_AND_TEST_P(HwyMathTest, TestAllLog1p);
222 HWY_EXPORT_AND_TEST_P(HwyMathTest, TestAllLog2);
223 HWY_EXPORT_AND_TEST_P(HwyMathTest, TestAllSin);
224 HWY_EXPORT_AND_TEST_P(HwyMathTest, TestAllSinh);
225 HWY_EXPORT_AND_TEST_P(HwyMathTest, TestAllTanh);
226 } // namespace hwy
227
228 #endif