[flang] Accept polymorphic component element in storage_size
[llvm-project.git] / libc / test / src / math / HypotTest.h
blobe8b4cf06ad973a8e642eee39aa53c79bc6c4c43b
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 //===----------------------------------------------------------------------===//
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"
17 #include <math.h>
19 namespace mpfr = __llvm_libc::testing::mpfr;
21 template <typename T>
22 class HypotTestTemplate : public __llvm_libc::testing::Test {
23 private:
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));
37 public:
38 void test_special_numbers(Func func) {
39 constexpr int N = 13;
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]),
56 0.5);
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));
71 if (signs % 2 == 1) {
72 x = -x;
74 if (signs >= 2) {
75 y = -y;
78 mpfr::BinaryInput<T> input{x, y};
79 ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Hypot, input,
80 func(x, y), 0.5);
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));
94 if (signs % 2 == 1) {
95 x = -x;
97 if (signs >= 2) {
98 y = -y;
101 mpfr::BinaryInput<T> input{x, y};
102 ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Hypot, input,
103 func(x, y), 0.5);
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