Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libc / test / src / math / sinhf_test.cpp
blobdc80a1b7aedf98399306780b5271c814cc900d50
1 //===-- Unittests for sinhf -----------------------------------------------===//
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/sinhf.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 LlvmLibcSinhfTest = LIBC_NAMESPACE::testing::FPTest<float>;
23 namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
25 TEST_F(LlvmLibcSinhfTest, SpecialNumbers) {
26 libc_errno = 0;
28 EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::sinhf(aNaN));
29 EXPECT_MATH_ERRNO(0);
31 EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::sinhf(0.0f));
32 EXPECT_MATH_ERRNO(0);
34 EXPECT_FP_EQ(-0.0f, LIBC_NAMESPACE::sinhf(-0.0f));
35 EXPECT_MATH_ERRNO(0);
37 EXPECT_FP_EQ(inf, LIBC_NAMESPACE::sinhf(inf));
38 EXPECT_MATH_ERRNO(0);
40 EXPECT_FP_EQ(neg_inf, LIBC_NAMESPACE::sinhf(neg_inf));
41 EXPECT_MATH_ERRNO(0);
44 TEST_F(LlvmLibcSinhfTest, InFloatRange) {
45 constexpr uint32_t COUNT = 100'000;
46 constexpr uint32_t STEP = UINT32_MAX / COUNT;
47 for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
48 float x = float(FPBits(v));
49 if (isnan(x) || isinf(x))
50 continue;
51 ASSERT_MPFR_MATCH(mpfr::Operation::Sinh, x, LIBC_NAMESPACE::sinhf(x), 0.5);
55 // For small values, sinh(x) is x.
56 TEST_F(LlvmLibcSinhfTest, SmallValues) {
57 float x = float(FPBits(uint32_t(0x17800000)));
58 float result = LIBC_NAMESPACE::sinhf(x);
59 EXPECT_MPFR_MATCH(mpfr::Operation::Sinh, x, result, 0.5);
60 EXPECT_FP_EQ(x, result);
62 x = float(FPBits(uint32_t(0x00400000)));
63 result = LIBC_NAMESPACE::sinhf(x);
64 EXPECT_MPFR_MATCH(mpfr::Operation::Sinh, x, result, 0.5);
65 EXPECT_FP_EQ(x, result);
68 TEST_F(LlvmLibcSinhfTest, Overflow) {
69 libc_errno = 0;
70 EXPECT_FP_EQ_WITH_EXCEPTION(
71 inf, LIBC_NAMESPACE::sinhf(float(FPBits(0x7f7fffffU))), FE_OVERFLOW);
72 EXPECT_MATH_ERRNO(ERANGE);
74 EXPECT_FP_EQ_WITH_EXCEPTION(
75 inf, LIBC_NAMESPACE::sinhf(float(FPBits(0x42cffff8U))), FE_OVERFLOW);
76 EXPECT_MATH_ERRNO(ERANGE);
78 EXPECT_FP_EQ_WITH_EXCEPTION(
79 inf, LIBC_NAMESPACE::sinhf(float(FPBits(0x42d00008U))), FE_OVERFLOW);
80 EXPECT_MATH_ERRNO(ERANGE);
83 TEST_F(LlvmLibcSinhfTest, ExceptionalValues) {
84 float x = float(FPBits(uint32_t(0x3a12'85ffU)));
85 EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Sinh, x,
86 LIBC_NAMESPACE::sinhf(x), 0.5);
88 x = -float(FPBits(uint32_t(0x3a12'85ffU)));
89 EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Sinh, x,
90 LIBC_NAMESPACE::sinhf(x), 0.5);