• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- Utility class to test different flavors of hypot ------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_LIBC_TEST_SRC_MATH_HYPOTTEST_H
10 #define LLVM_LIBC_TEST_SRC_MATH_HYPOTTEST_H
11 
12 #include "src/__support/FPUtil/FPBits.h"
13 #include "test/UnitTest/FEnvSafeTest.h"
14 #include "test/UnitTest/FPMatcher.h"
15 #include "test/UnitTest/Test.h"
16 
17 #include "hdr/math_macros.h"
18 
19 template <typename T>
20 class HypotTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
21 private:
22   using Func = T (*)(T, T);
23   using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>;
24   using StorageType = typename FPBits::StorageType;
25 
26   const T nan = FPBits::quiet_nan().get_val();
27   const T inf = FPBits::inf(Sign::POS).get_val();
28   const T neg_inf = FPBits::inf(Sign::NEG).get_val();
29   const T zero = FPBits::zero(Sign::POS).get_val();
30   const T neg_zero = FPBits::zero(Sign::NEG).get_val();
31 
32   const T max_normal = FPBits::max_normal().get_val();
33   const T min_normal = FPBits::min_normal().get_val();
34   const T max_subnormal = FPBits::max_subnormal().get_val();
35   const T min_subnormal = FPBits::min_subnormal().get_val();
36 
37 public:
test_special_numbers(Func func)38   void test_special_numbers(Func func) {
39     constexpr int N = 4;
40     // Pythagorean triples.
41     constexpr T PYT[N][3] = {{3, 4, 5}, {5, 12, 13}, {8, 15, 17}, {7, 24, 25}};
42 
43     EXPECT_FP_EQ(func(inf, nan), inf);
44     EXPECT_FP_EQ(func(nan, neg_inf), inf);
45     EXPECT_FP_EQ(func(nan, nan), nan);
46     EXPECT_FP_EQ(func(nan, zero), nan);
47     EXPECT_FP_EQ(func(neg_zero, nan), nan);
48 
49     for (int i = 0; i < N; ++i) {
50       EXPECT_FP_EQ_ALL_ROUNDING(PYT[i][2], func(PYT[i][0], PYT[i][1]));
51       EXPECT_FP_EQ_ALL_ROUNDING(PYT[i][2], func(-PYT[i][0], PYT[i][1]));
52       EXPECT_FP_EQ_ALL_ROUNDING(PYT[i][2], func(PYT[i][0], -PYT[i][1]));
53       EXPECT_FP_EQ_ALL_ROUNDING(PYT[i][2], func(-PYT[i][0], -PYT[i][1]));
54 
55       EXPECT_FP_EQ_ALL_ROUNDING(PYT[i][2], func(PYT[i][1], PYT[i][0]));
56       EXPECT_FP_EQ_ALL_ROUNDING(PYT[i][2], func(-PYT[i][1], PYT[i][0]));
57       EXPECT_FP_EQ_ALL_ROUNDING(PYT[i][2], func(PYT[i][1], -PYT[i][0]));
58       EXPECT_FP_EQ_ALL_ROUNDING(PYT[i][2], func(-PYT[i][1], -PYT[i][0]));
59     }
60   }
61 };
62 
63 #endif // LLVM_LIBC_TEST_SRC_MATH_HYPOTTEST_H
64