Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libc / test / src / math / atanhf_test.cpp
blobf62830ac9d4ef78ad3d4ac457b24db8609a65271
1 //===-- Unittests for atanhf ----------------------------------------------===//
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 #include "src/__support/FPUtil/FPBits.h"
10 #include "src/errno/libc_errno.h"
11 #include "src/math/atanhf.h"
12 #include "test/UnitTest/FPMatcher.h"
13 #include "test/UnitTest/Test.h"
14 #include "utils/MPFRWrapper/MPFRUtils.h"
15 #include <math.h>
17 #include <errno.h>
18 #include <stdint.h>
20 using LlvmLibcAtanhfTest = LIBC_NAMESPACE::testing::FPTest<float>;
22 namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
24 TEST_F(LlvmLibcAtanhfTest, SpecialNumbers) {
25 libc_errno = 0;
26 LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT);
27 EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::atanhf(aNaN));
28 EXPECT_FP_EXCEPTION(0);
29 EXPECT_MATH_ERRNO(0);
31 LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT);
32 EXPECT_FP_EQ_ALL_ROUNDING(0.0f, LIBC_NAMESPACE::atanhf(0.0f));
33 EXPECT_FP_EXCEPTION(0);
34 EXPECT_MATH_ERRNO(0);
36 LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT);
37 EXPECT_FP_EQ_ALL_ROUNDING(-0.0f, LIBC_NAMESPACE::atanhf(-0.0f));
38 EXPECT_FP_EXCEPTION(0);
39 EXPECT_MATH_ERRNO(0);
41 LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT);
42 EXPECT_FP_EQ_ALL_ROUNDING(inf, LIBC_NAMESPACE::atanhf(1.0f));
43 EXPECT_FP_EXCEPTION(FE_DIVBYZERO);
44 EXPECT_MATH_ERRNO(ERANGE);
46 LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT);
47 EXPECT_FP_EQ_ALL_ROUNDING(neg_inf, LIBC_NAMESPACE::atanhf(-1.0f));
48 EXPECT_FP_EXCEPTION(FE_DIVBYZERO);
49 EXPECT_MATH_ERRNO(ERANGE);
51 auto bt = FPBits(1.0f);
52 bt.bits += 1;
54 LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT);
55 EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::atanhf(bt.get_val()));
56 EXPECT_FP_EXCEPTION(FE_INVALID);
57 EXPECT_MATH_ERRNO(EDOM);
59 LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT);
60 bt.set_sign(true);
61 EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::atanhf(bt.get_val()));
62 EXPECT_FP_EXCEPTION(FE_INVALID);
63 EXPECT_MATH_ERRNO(EDOM);
65 LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT);
66 EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::atanhf(2.0f));
67 EXPECT_FP_EXCEPTION(FE_INVALID);
68 EXPECT_MATH_ERRNO(EDOM);
70 LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT);
71 EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::atanhf(-2.0f));
72 EXPECT_FP_EXCEPTION(FE_INVALID);
73 EXPECT_MATH_ERRNO(EDOM);
75 LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT);
76 EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::atanhf(inf));
77 EXPECT_FP_EXCEPTION(FE_INVALID);
78 EXPECT_MATH_ERRNO(EDOM);
80 bt.set_sign(true);
81 EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::atanhf(neg_inf));
82 EXPECT_FP_EXCEPTION(FE_INVALID);
83 EXPECT_MATH_ERRNO(EDOM);
86 TEST_F(LlvmLibcAtanhfTest, InFloatRange) {
87 constexpr uint32_t COUNT = 100'000;
88 const uint32_t STEP = FPBits(1.0f).uintval() / COUNT;
89 for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
90 float x = float(FPBits(v));
91 ASSERT_MPFR_MATCH(mpfr::Operation::Atanh, x, LIBC_NAMESPACE::atanhf(x),
92 0.5);
93 ASSERT_MPFR_MATCH(mpfr::Operation::Atanh, -x, LIBC_NAMESPACE::atanhf(-x),
94 0.5);
98 // For small values, atanh(x) is x.
99 TEST_F(LlvmLibcAtanhfTest, SmallValues) {
100 float x = float(FPBits(uint32_t(0x17800000)));
101 float result = LIBC_NAMESPACE::atanhf(x);
102 EXPECT_MPFR_MATCH(mpfr::Operation::Atanh, x, result, 0.5);
103 EXPECT_FP_EQ(x, result);
105 x = float(FPBits(uint32_t(0x00400000)));
106 result = LIBC_NAMESPACE::atanhf(x);
107 EXPECT_MPFR_MATCH(mpfr::Operation::Atanh, x, result, 0.5);
108 EXPECT_FP_EQ(x, result);