1 //===-- Unittests for tanhf -----------------------------------------------===//
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 #include "src/__support/FPUtil/FPBits.h"
10 #include "src/errno/libc_errno.h"
11 #include "src/math/tanhf.h"
12 #include "test/UnitTest/FPMatcher.h"
13 #include "test/UnitTest/Test.h"
14 #include "utils/MPFRWrapper/MPFRUtils.h"
20 using LlvmLibcTanhfTest
= LIBC_NAMESPACE::testing::FPTest
<float>;
22 namespace mpfr
= LIBC_NAMESPACE::testing::mpfr
;
24 TEST_F(LlvmLibcTanhfTest
, SpecialNumbers
) {
27 EXPECT_FP_EQ(aNaN
, LIBC_NAMESPACE::tanhf(aNaN
));
30 EXPECT_FP_EQ(0.0f
, LIBC_NAMESPACE::tanhf(0.0f
));
33 EXPECT_FP_EQ(-0.0f
, LIBC_NAMESPACE::tanhf(-0.0f
));
36 EXPECT_FP_EQ(1.0f
, LIBC_NAMESPACE::tanhf(inf
));
39 EXPECT_FP_EQ(-1.0f
, LIBC_NAMESPACE::tanhf(neg_inf
));
43 TEST_F(LlvmLibcTanhfTest
, InFloatRange
) {
44 constexpr uint32_t COUNT
= 100'001;
45 constexpr uint32_t STEP
= UINT32_MAX
/ COUNT
;
46 for (uint32_t i
= 0, v
= 0; i
<= COUNT
; ++i
, v
+= STEP
) {
47 float x
= float(FPBits(v
));
48 if (isnan(x
) || isinf(x
))
50 ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Tanh
, x
,
51 LIBC_NAMESPACE::tanhf(x
), 0.5);
55 TEST_F(LlvmLibcTanhfTest
, ExceptionalValues
) {
57 constexpr uint32_t INPUTS
[N
] = {
64 for (int i
= 0; i
< N
; ++i
) {
65 float x
= float(FPBits(INPUTS
[i
]));
66 EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Tanh
, x
,
67 LIBC_NAMESPACE::tanhf(x
), 0.5);
68 EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Tanh
, -x
,
69 LIBC_NAMESPACE::tanhf(-x
), 0.5);