1 //===-- Unittests for coshf -----------------------------------------------===//
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/CPP/array.h"
10 #include "src/__support/FPUtil/FPBits.h"
11 #include "src/errno/libc_errno.h"
12 #include "src/math/coshf.h"
13 #include "test/UnitTest/FPMatcher.h"
14 #include "test/UnitTest/Test.h"
15 #include "utils/MPFRWrapper/MPFRUtils.h"
21 using LlvmLibcCoshfTest
= LIBC_NAMESPACE::testing::FPTest
<float>;
23 namespace mpfr
= LIBC_NAMESPACE::testing::mpfr
;
25 TEST_F(LlvmLibcCoshfTest
, SpecialNumbers
) {
28 EXPECT_FP_EQ(aNaN
, LIBC_NAMESPACE::coshf(aNaN
));
31 EXPECT_FP_EQ(inf
, LIBC_NAMESPACE::coshf(inf
));
34 EXPECT_FP_EQ(inf
, LIBC_NAMESPACE::coshf(neg_inf
));
37 EXPECT_FP_EQ(1.0f
, LIBC_NAMESPACE::coshf(0.0f
));
40 EXPECT_FP_EQ(1.0f
, LIBC_NAMESPACE::coshf(-0.0f
));
44 TEST_F(LlvmLibcCoshfTest
, Overflow
) {
46 EXPECT_FP_EQ_WITH_EXCEPTION(
47 inf
, LIBC_NAMESPACE::coshf(float(FPBits(0x7f7fffffU
))), FE_OVERFLOW
);
48 EXPECT_MATH_ERRNO(ERANGE
);
50 EXPECT_FP_EQ_WITH_EXCEPTION(
51 inf
, LIBC_NAMESPACE::coshf(float(FPBits(0x42cffff8U
))), FE_OVERFLOW
);
52 EXPECT_MATH_ERRNO(ERANGE
);
54 EXPECT_FP_EQ_WITH_EXCEPTION(
55 inf
, LIBC_NAMESPACE::coshf(float(FPBits(0x42d00008U
))), FE_OVERFLOW
);
56 EXPECT_MATH_ERRNO(ERANGE
);
59 TEST_F(LlvmLibcCoshfTest
, InFloatRange
) {
60 constexpr uint32_t COUNT
= 100'000;
61 constexpr uint32_t STEP
= UINT32_MAX
/ COUNT
;
62 for (uint32_t i
= 0, v
= 0; i
<= COUNT
; ++i
, v
+= STEP
) {
63 float x
= float(FPBits(v
));
64 if (isnan(x
) || isinf(x
))
66 ASSERT_MPFR_MATCH(mpfr::Operation::Cosh
, x
, LIBC_NAMESPACE::coshf(x
), 0.5);
70 TEST_F(LlvmLibcCoshfTest
, SmallValues
) {
71 float x
= float(FPBits(0x17800000U
));
72 float result
= LIBC_NAMESPACE::coshf(x
);
73 EXPECT_MPFR_MATCH(mpfr::Operation::Cosh
, x
, result
, 0.5);
74 EXPECT_FP_EQ(1.0f
, result
);
76 x
= float(FPBits(0x0040000U
));
77 result
= LIBC_NAMESPACE::coshf(x
);
78 EXPECT_MPFR_MATCH(mpfr::Operation::Cosh
, x
, result
, 0.5);
79 EXPECT_FP_EQ(1.0f
, result
);