1 //===-- Utility class to test different flavors of hypot ------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_LIBC_TEST_SRC_MATH_HYPOTTEST_H
10 #define LLVM_LIBC_TEST_SRC_MATH_HYPOTTEST_H
12 #include "src/__support/FPUtil/FPBits.h"
13 #include "test/UnitTest/FPMatcher.h"
14 #include "test/UnitTest/Test.h"
15 #include "utils/MPFRWrapper/MPFRUtils.h"
19 namespace mpfr
= __llvm_libc::testing::mpfr
;
22 class HypotTestTemplate
: public __llvm_libc::testing::Test
{
24 using Func
= T (*)(T
, T
);
25 using FPBits
= __llvm_libc::fputil::FPBits
<T
>;
26 using UIntType
= typename
FPBits::UIntType
;
27 const T nan
= T(FPBits::build_quiet_nan(1));
28 const T inf
= T(FPBits::inf());
29 const T neg_inf
= T(FPBits::neg_inf());
30 const T zero
= T(FPBits::zero());
31 const T neg_zero
= T(FPBits::neg_zero());
32 const T max_normal
= T(FPBits(FPBits::MAX_NORMAL
));
33 const T min_normal
= T(FPBits(FPBits::MIN_NORMAL
));
34 const T max_subnormal
= T(FPBits(FPBits::MAX_SUBNORMAL
));
35 const T min_subnormal
= T(FPBits(FPBits::MIN_SUBNORMAL
));
38 void test_special_numbers(Func func
) {
40 const T SpecialInputs
[N
] = {inf
, neg_inf
, zero
,
41 neg_zero
, max_normal
, min_normal
,
42 max_subnormal
, min_subnormal
, -max_normal
,
43 -min_normal
, -max_subnormal
, -min_subnormal
};
45 EXPECT_FP_EQ(func(inf
, nan
), inf
);
46 EXPECT_FP_EQ(func(nan
, neg_inf
), inf
);
47 EXPECT_FP_EQ(func(nan
, nan
), nan
);
48 EXPECT_FP_EQ(func(nan
, zero
), nan
);
49 EXPECT_FP_EQ(func(neg_zero
, nan
), nan
);
51 for (int i
= 0; i
< N
; ++i
) {
52 for (int j
= 0; j
< N
; ++j
) {
53 mpfr::BinaryInput
<T
> input
{SpecialInputs
[i
], SpecialInputs
[j
]};
54 EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Hypot
, input
,
55 func(SpecialInputs
[i
], SpecialInputs
[j
]),
61 void test_subnormal_range(Func func
) {
62 constexpr UIntType COUNT
= 1000001;
63 for (unsigned scale
= 0; scale
< 4; ++scale
) {
64 UIntType max_value
= FPBits::MAX_SUBNORMAL
<< scale
;
65 UIntType step
= (max_value
- FPBits::MIN_SUBNORMAL
) / COUNT
;
66 for (int signs
= 0; signs
< 4; ++signs
) {
67 for (UIntType v
= FPBits::MIN_SUBNORMAL
, w
= max_value
;
68 v
<= max_value
&& w
>= FPBits::MIN_SUBNORMAL
;
69 v
+= step
, w
-= step
) {
70 T x
= T(FPBits(v
)), y
= T(FPBits(w
));
78 mpfr::BinaryInput
<T
> input
{x
, y
};
79 ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Hypot
, input
,
86 void test_normal_range(Func func
) {
87 constexpr UIntType COUNT
= 1000001;
88 constexpr UIntType STEP
= (FPBits::MAX_NORMAL
- FPBits::MIN_NORMAL
) / COUNT
;
89 for (int signs
= 0; signs
< 4; ++signs
) {
90 for (UIntType v
= FPBits::MIN_NORMAL
, w
= FPBits::MAX_NORMAL
;
91 v
<= FPBits::MAX_NORMAL
&& w
>= FPBits::MIN_NORMAL
;
92 v
+= STEP
, w
-= STEP
) {
93 T x
= T(FPBits(v
)), y
= T(FPBits(w
));
101 mpfr::BinaryInput
<T
> input
{x
, y
};
102 ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Hypot
, input
,
108 void test_input_list(Func func
, int n
, const mpfr::BinaryInput
<T
> *inputs
) {
109 for (int i
= 0; i
< n
; ++i
) {
110 ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Hypot
, inputs
[i
],
111 func(inputs
[i
].x
, inputs
[i
].y
), 0.5);
116 #endif // LLVM_LIBC_TEST_SRC_MATH_HYPOTTEST_H