2 * Copyright (C) 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <gtest/gtest.h>
22 template <typename RT, typename T1>
28 template <typename T1>
34 template <typename T1>
35 struct data_long_1_t {
40 template <typename T1>
41 struct data_llong_1_t {
46 template <typename RT, typename T1, typename T2>
53 template <typename RT1, typename RT2, typename T>
60 template <typename RT1, typename T>
61 struct data_1_int_1_t {
67 template <typename RT1, typename T1, typename T2>
68 struct data_1_int_2_t {
75 template <typename RT, typename T1, typename T2, typename T3>
83 template <typename T> union fp_u;
85 template <> union fp_u<float> {
92 uint32_t sign_magnitude;
95 template <> union fp_u<double> {
103 uint64_t sign_magnitude;
106 template <> union fp_u<long double> {
108 #if defined(__LP64__)
117 __int128_t sign_magnitude;
125 uint64_t sign_magnitude;
129 template <typename T>
130 static inline auto SignAndMagnitudeToBiased(const T& value) -> decltype(fp_u<T>::sign_magnitude) {
134 return ~u.sign_magnitude + 1;
137 return u.sign_magnitude;
141 // Based on the existing googletest implementation, which uses a fixed 4 ulp bound.
142 template <typename T>
143 size_t UlpDistance(T lhs, T rhs) {
144 const auto biased1 = SignAndMagnitudeToBiased(lhs);
145 const auto biased2 = SignAndMagnitudeToBiased(rhs);
146 return (biased1 >= biased2) ? (biased1 - biased2) : (biased2 - biased1);
149 template <size_t ULP, typename T>
151 ::testing::AssertionResult operator()(const char* /* expected_expression */,
152 const char* /* actual_expression */,
155 if (!isnan(expected) && !isnan(actual) && UlpDistance(expected, actual) <= ULP) {
156 return ::testing::AssertionSuccess();
159 return ::testing::AssertionFailure()
160 << "expected (" << std::hexfloat << expected << ") != actual (" << actual << ")";
164 // Runs through the array 'data' applying 'f' to each of the input values
165 // and asserting that the result is within ULP ulps of the expected value.
166 // For testing a (double) -> double function like sin(3).
167 template <size_t ULP, typename RT, typename T, size_t N>
168 void DoMathDataTest(data_1_1_t<RT, T> (&data)[N], RT f(T)) {
169 fesetenv(FE_DFL_ENV);
170 FpUlpEq<ULP, RT> predicate;
171 for (size_t i = 0; i < N; ++i) {
172 EXPECT_PRED_FORMAT2(predicate,
173 data[i].expected, f(data[i].input)) << "Failed on element " << i;
177 // Runs through the array 'data' applying 'f' to each of the input values
178 // and asserting that the result is within ULP ulps of the expected value.
179 // For testing a (double) -> int function like ilogb(3).
180 template <size_t ULP, typename T, size_t N>
181 void DoMathDataTest(data_int_1_t<T> (&data)[N], int f(T)) {
182 fesetenv(FE_DFL_ENV);
183 for (size_t i = 0; i < N; ++i) {
184 EXPECT_EQ(data[i].expected, f(data[i].input)) << "Failed on element " << i;
188 // Runs through the array 'data' applying 'f' to each of the input values
189 // and asserting that the result is within ULP ulps of the expected value.
190 // For testing a (double) -> long int function like lrint(3).
191 template <size_t ULP, typename T, size_t N>
192 void DoMathDataTest(data_long_1_t<T> (&data)[N], long f(T)) {
193 fesetenv(FE_DFL_ENV);
194 for (size_t i = 0; i < N; ++i) {
195 EXPECT_EQ(data[i].expected, f(data[i].input)) << "Failed on element " << i;
199 // Runs through the array 'data' applying 'f' to each of the input values
200 // and asserting that the result is within ULP ulps of the expected value.
201 // For testing a (double) -> long long int function like llrint(3).
202 template <size_t ULP, typename T, size_t N>
203 void DoMathDataTest(data_llong_1_t<T> (&data)[N], long long f(T)) {
204 fesetenv(FE_DFL_ENV);
205 for (size_t i = 0; i < N; ++i) {
206 EXPECT_EQ(data[i].expected, f(data[i].input)) << "Failed on element " << i;
210 // Runs through the array 'data' applying 'f' to each of the pairs of input values
211 // and asserting that the result is within ULP ulps of the expected value.
212 // For testing a (double, double) -> double function like pow(3).
213 template <size_t ULP, typename RT, typename T1, typename T2, size_t N>
214 void DoMathDataTest(data_1_2_t<RT, T1, T2> (&data)[N], RT f(T1, T2)) {
215 fesetenv(FE_DFL_ENV);
216 FpUlpEq<ULP, RT> predicate;
217 for (size_t i = 0; i < N; ++i) {
218 EXPECT_PRED_FORMAT2(predicate,
219 data[i].expected, f(data[i].input1, data[i].input2)) << "Failed on element " << i;
223 // Runs through the array 'data' applying 'f' to each of the input values
224 // and asserting that the results are within ULP ulps of the expected values.
225 // For testing a (double, double*, double*) -> void function like sincos(3).
226 template <size_t ULP, typename RT1, typename RT2, typename T1, size_t N>
227 void DoMathDataTest(data_2_1_t<RT1, RT2, T1> (&data)[N], void f(T1, RT1*, RT2*)) {
228 fesetenv(FE_DFL_ENV);
229 FpUlpEq<ULP, RT1> predicate1;
230 FpUlpEq<ULP, RT2> predicate2;
231 for (size_t i = 0; i < N; ++i) {
234 f(data[i].input, &out1, &out2);
235 EXPECT_PRED_FORMAT2(predicate1, data[i].expected1, out1) << "Failed on element " << i;
236 EXPECT_PRED_FORMAT2(predicate2, data[i].expected2, out2) << "Failed on element " << i;
240 // Runs through the array 'data' applying 'f' to each of the input values
241 // and asserting that the results are within ULP ulps of the expected values.
242 // For testing a (double, double*) -> double function like modf(3).
243 template <size_t ULP, typename RT1, typename RT2, typename T1, size_t N>
244 void DoMathDataTest(data_2_1_t<RT1, RT2, T1> (&data)[N], RT1 f(T1, RT2*)) {
245 fesetenv(FE_DFL_ENV);
246 FpUlpEq<ULP, RT1> predicate1;
247 FpUlpEq<ULP, RT2> predicate2;
248 for (size_t i = 0; i < N; ++i) {
251 out1 = f(data[i].input, &out2);
252 EXPECT_PRED_FORMAT2(predicate1, data[i].expected1, out1) << "Failed on element " << i;
253 EXPECT_PRED_FORMAT2(predicate2, data[i].expected2, out2) << "Failed on element " << i;
257 // Runs through the array 'data' applying 'f' to each of the input values
258 // and asserting that the results are within ULP ulps of the expected values.
259 // For testing a (double, int*) -> double function like frexp(3).
260 template <size_t ULP, typename RT1, typename T1, size_t N>
261 void DoMathDataTest(data_1_int_1_t<RT1, T1> (&data)[N], RT1 f(T1, int*)) {
262 fesetenv(FE_DFL_ENV);
263 FpUlpEq<ULP, RT1> predicate1;
264 for (size_t i = 0; i < N; ++i) {
267 out1 = f(data[i].input, &out2);
268 EXPECT_PRED_FORMAT2(predicate1, data[i].expected1, out1) << "Failed on element " << i;
269 EXPECT_EQ(data[i].expected2, out2) << "Failed on element " << i;
273 // Runs through the array 'data' applying 'f' to each of the input values
274 // and asserting that the results are within ULP ulps of the expected values.
275 // For testing a (double, double, int*) -> double function like remquo(3).
276 template <size_t ULP, typename RT1, typename T1, typename T2, size_t N>
277 void DoMathDataTest(data_1_int_2_t<RT1, T1, T2> (&data)[N], RT1 f(T1, T2, int*)) {
278 fesetenv(FE_DFL_ENV);
279 FpUlpEq<ULP, RT1> predicate1;
280 for (size_t i = 0; i < N; ++i) {
283 out1 = f(data[i].input1, data[i].input2, &out2);
284 EXPECT_PRED_FORMAT2(predicate1, data[i].expected1, out1) << "Failed on element " << i;
285 EXPECT_EQ(data[i].expected2, out2) << "Failed on element " << i;
289 // Runs through the array 'data' applying 'f' to each of the pairs of input values
290 // and asserting that the result is within ULP ulps of the expected value.
291 // For testing a (double, double, double) -> double function like fma(3).
292 template <size_t ULP, typename RT, typename T1, typename T2, typename T3, size_t N>
293 void DoMathDataTest(data_1_3_t<RT, T1, T2, T3> (&data)[N], RT f(T1, T2, T3)) {
294 fesetenv(FE_DFL_ENV);
295 FpUlpEq<ULP, RT> predicate;
296 for (size_t i = 0; i < N; ++i) {
297 EXPECT_PRED_FORMAT2(predicate,
298 data[i].expected, f(data[i].input1, data[i].input2, data[i].input3)) << "Failed on element " << i;