Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libc / test / src / math / coshf_test.cpp
blob797cfec566ac8217889c1635d51045ab80846bb2
1 //===-- Unittests for coshf -----------------------------------------------===//
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/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"
16 #include <math.h>
18 #include <errno.h>
19 #include <stdint.h>
21 using LlvmLibcCoshfTest = LIBC_NAMESPACE::testing::FPTest<float>;
23 namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
25 TEST_F(LlvmLibcCoshfTest, SpecialNumbers) {
26 libc_errno = 0;
28 EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::coshf(aNaN));
29 EXPECT_MATH_ERRNO(0);
31 EXPECT_FP_EQ(inf, LIBC_NAMESPACE::coshf(inf));
32 EXPECT_MATH_ERRNO(0);
34 EXPECT_FP_EQ(inf, LIBC_NAMESPACE::coshf(neg_inf));
35 EXPECT_MATH_ERRNO(0);
37 EXPECT_FP_EQ(1.0f, LIBC_NAMESPACE::coshf(0.0f));
38 EXPECT_MATH_ERRNO(0);
40 EXPECT_FP_EQ(1.0f, LIBC_NAMESPACE::coshf(-0.0f));
41 EXPECT_MATH_ERRNO(0);
44 TEST_F(LlvmLibcCoshfTest, Overflow) {
45 libc_errno = 0;
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))
65 continue;
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);