• 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 #include "utils/MPFRWrapper/MPFRUtils.h"
17 
18 #include "hdr/math_macros.h"
19 
20 namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
21 
22 template <typename T>
23 class HypotTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
24 private:
25   using Func = T (*)(T, T);
26   using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>;
27 
28   using StorageType = typename FPBits::StorageType;
29   const T nan = FPBits::quiet_nan().get_val();
30   const T inf = FPBits::inf().get_val();
31   const T neg_inf = FPBits::inf(Sign::NEG).get_val();
32   const T zero = FPBits::zero().get_val();
33   const T neg_zero = FPBits::zero(Sign::NEG).get_val();
34   const T max_normal = FPBits::max_normal().get_val();
35   const T min_normal = FPBits::min_normal().get_val();
36   const T max_subnormal = FPBits::max_subnormal().get_val();
37   const T min_subnormal = FPBits::min_subnormal().get_val();
38 
39   static constexpr StorageType MAX_NORMAL = FPBits::max_normal().uintval();
40   static constexpr StorageType MIN_NORMAL = FPBits::min_normal().uintval();
41   static constexpr StorageType MAX_SUBNORMAL =
42       FPBits::max_subnormal().uintval();
43   static constexpr StorageType MIN_SUBNORMAL =
44       FPBits::min_subnormal().uintval();
45 
46 public:
test_special_numbers(Func func)47   void test_special_numbers(Func func) {
48     constexpr int N = 13;
49     const T SpecialInputs[N] = {inf,           neg_inf,        zero,
50                                 neg_zero,      max_normal,     min_normal,
51                                 max_subnormal, min_subnormal,  -max_normal,
52                                 -min_normal,   -max_subnormal, -min_subnormal};
53 
54     EXPECT_FP_EQ(func(inf, nan), inf);
55     EXPECT_FP_EQ(func(nan, neg_inf), inf);
56     EXPECT_FP_EQ(func(nan, nan), nan);
57     EXPECT_FP_EQ(func(nan, zero), nan);
58     EXPECT_FP_EQ(func(neg_zero, nan), nan);
59 
60     for (int i = 0; i < N; ++i) {
61       for (int j = 0; j < N; ++j) {
62         mpfr::BinaryInput<T> input{SpecialInputs[i], SpecialInputs[j]};
63         EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Hypot, input,
64                                        func(SpecialInputs[i], SpecialInputs[j]),
65                                        0.5);
66       }
67     }
68   }
69 
test_subnormal_range(Func func)70   void test_subnormal_range(Func func) {
71     constexpr StorageType COUNT = 10'001;
72     for (unsigned scale = 0; scale < 4; ++scale) {
73       StorageType max_value = MAX_SUBNORMAL << scale;
74       StorageType step = (max_value - MIN_SUBNORMAL) / COUNT;
75       for (int signs = 0; signs < 4; ++signs) {
76         for (StorageType v = MIN_SUBNORMAL, w = max_value;
77              v <= max_value && w >= MIN_SUBNORMAL; v += step, w -= step) {
78           T x = FPBits(v).get_val(), y = FPBits(w).get_val();
79           if (signs % 2 == 1) {
80             x = -x;
81           }
82           if (signs >= 2) {
83             y = -y;
84           }
85 
86           mpfr::BinaryInput<T> input{x, y};
87           ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Hypot, input,
88                                          func(x, y), 0.5);
89         }
90       }
91     }
92   }
93 
94   void test_normal_range(Func func) {
95     constexpr StorageType COUNT = 10'001;
96     constexpr StorageType STEP = (MAX_NORMAL - MIN_NORMAL) / COUNT;
97     for (int signs = 0; signs < 4; ++signs) {
98       for (StorageType v = MIN_NORMAL, w = MAX_NORMAL;
99            v <= MAX_NORMAL && w >= MIN_NORMAL; v += STEP, w -= STEP) {
100         T x = FPBits(v).get_val(), y = FPBits(w).get_val();
101         if (signs % 2 == 1) {
102           x = -x;
103         }
104         if (signs >= 2) {
105           y = -y;
106         }
107 
108         mpfr::BinaryInput<T> input{x, y};
109         ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Hypot, input,
110                                        func(x, y), 0.5);
111       }
112     }
113   }
114 
test_input_list(Func func,int n,const mpfr::BinaryInput<T> * inputs)115   void test_input_list(Func func, int n, const mpfr::BinaryInput<T> *inputs) {
116     for (int i = 0; i < n; ++i) {
117       ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Hypot, inputs[i],
118                                      func(inputs[i].x, inputs[i].y), 0.5);
119     }
120   }
121 };
122 
123 #endif // LLVM_LIBC_TEST_SRC_MATH_HYPOTTEST_H
124