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 "src/__support/FPUtil/Hypot.h"
14 #include "utils/MPFRWrapper/MPFRUtils.h"
15 #include "utils/UnitTest/FPMatcher.h"
16 #include "utils/UnitTest/Test.h"
17 
18 #include <math.h>
19 
20 namespace mpfr = __llvm_libc::testing::mpfr;
21 
22 template <typename T>
23 class HypotTestTemplate : public __llvm_libc::testing::Test {
24 private:
25   using Func = T (*)(T, T);
26   using FPBits = __llvm_libc::fputil::FPBits<T>;
27   using UIntType = typename FPBits::UIntType;
28   const T nan = T(__llvm_libc::fputil::FPBits<T>::build_nan(1));
29   const T inf = T(__llvm_libc::fputil::FPBits<T>::inf());
30   const T neg_inf = T(__llvm_libc::fputil::FPBits<T>::neg_inf());
31   const T zero = T(__llvm_libc::fputil::FPBits<T>::zero());
32   const T neg_zero = T(__llvm_libc::fputil::FPBits<T>::neg_zero());
33 
34 public:
35   void test_special_numbers(Func func) {
36     EXPECT_FP_EQ(func(inf, nan), inf);
37     EXPECT_FP_EQ(func(nan, neg_inf), inf);
38     EXPECT_FP_EQ(func(zero, inf), inf);
39     EXPECT_FP_EQ(func(neg_inf, neg_zero), inf);
40 
41     EXPECT_FP_EQ(func(nan, nan), nan);
42     EXPECT_FP_EQ(func(nan, zero), nan);
43     EXPECT_FP_EQ(func(neg_zero, nan), nan);
44 
45     EXPECT_FP_EQ(func(neg_zero, zero), zero);
46   }
47 
48   void test_subnormal_range(Func func) {
49     constexpr UIntType COUNT = 1000001;
50     for (unsigned scale = 0; scale < 4; ++scale) {
51       UIntType max_value = FPBits::MAX_SUBNORMAL << scale;
52       UIntType step = (max_value - FPBits::MIN_SUBNORMAL) / COUNT;
53       for (int signs = 0; signs < 4; ++signs) {
54         for (UIntType v = FPBits::MIN_SUBNORMAL, w = max_value;
55              v <= max_value && w >= FPBits::MIN_SUBNORMAL;
56              v += step, w -= step) {
57           T x = T(FPBits(v)), y = T(FPBits(w));
58           if (signs % 2 == 1) {
59             x = -x;
60           }
61           if (signs >= 2) {
62             y = -y;
63           }
64 
65           T result = func(x, y);
66           mpfr::BinaryInput<T> input{x, y};
67           ASSERT_MPFR_MATCH(mpfr::Operation::Hypot, input, result, 0.5);
68         }
69       }
70     }
71   }
72 
73   void test_normal_range(Func func) {
74     constexpr UIntType COUNT = 1000001;
75     constexpr UIntType STEP = (FPBits::MAX_NORMAL - FPBits::MIN_NORMAL) / COUNT;
76     for (int signs = 0; signs < 4; ++signs) {
77       for (UIntType v = FPBits::MIN_NORMAL, w = FPBits::MAX_NORMAL;
78            v <= FPBits::MAX_NORMAL && w >= FPBits::MIN_NORMAL;
79            v += STEP, w -= STEP) {
80         T x = T(FPBits(v)), y = T(FPBits(w));
81         if (signs % 2 == 1) {
82           x = -x;
83         }
84         if (signs >= 2) {
85           y = -y;
86         }
87 
88         T result = func(x, y);
89         mpfr::BinaryInput<T> input{x, y};
90         ASSERT_MPFR_MATCH(mpfr::Operation::Hypot, input, result, 0.5);
91       }
92     }
93   }
94 };
95 
96 #endif // LLVM_LIBC_TEST_SRC_MATH_HYPOTTEST_H
97