1 //===-- Unittests for sinhf -----------------------------------------------===//
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 "hdr/math_macros.h"
10 #include "src/__support/CPP/array.h"
11 #include "src/__support/FPUtil/FPBits.h"
12 #include "src/errno/libc_errno.h"
13 #include "src/math/sinhf.h"
14 #include "test/UnitTest/FPMatcher.h"
15 #include "test/UnitTest/Test.h"
16 #include "utils/MPFRWrapper/MPFRUtils.h"
20 using LlvmLibcSinhfTest
= LIBC_NAMESPACE::testing::FPTest
<float>;
22 namespace mpfr
= LIBC_NAMESPACE::testing::mpfr
;
24 TEST_F(LlvmLibcSinhfTest
, SpecialNumbers
) {
25 LIBC_NAMESPACE::libc_errno
= 0;
27 EXPECT_FP_EQ(aNaN
, LIBC_NAMESPACE::sinhf(aNaN
));
30 EXPECT_FP_EQ(0.0f
, LIBC_NAMESPACE::sinhf(0.0f
));
33 EXPECT_FP_EQ(-0.0f
, LIBC_NAMESPACE::sinhf(-0.0f
));
36 EXPECT_FP_EQ(inf
, LIBC_NAMESPACE::sinhf(inf
));
39 EXPECT_FP_EQ(neg_inf
, LIBC_NAMESPACE::sinhf(neg_inf
));
43 TEST_F(LlvmLibcSinhfTest
, InFloatRange
) {
44 constexpr uint32_t COUNT
= 100'000;
45 constexpr uint32_t STEP
= UINT32_MAX
/ COUNT
;
46 for (uint32_t i
= 0, v
= 0; i
<= COUNT
; ++i
, v
+= STEP
) {
47 float x
= FPBits(v
).get_val();
48 if (FPBits(v
).is_nan() || FPBits(v
).is_inf())
50 ASSERT_MPFR_MATCH(mpfr::Operation::Sinh
, x
, LIBC_NAMESPACE::sinhf(x
), 0.5);
54 // For small values, sinh(x) is x.
55 TEST_F(LlvmLibcSinhfTest
, SmallValues
) {
56 float x
= FPBits(uint32_t(0x17800000)).get_val();
57 float result
= LIBC_NAMESPACE::sinhf(x
);
58 EXPECT_MPFR_MATCH(mpfr::Operation::Sinh
, x
, result
, 0.5);
59 EXPECT_FP_EQ(x
, result
);
61 x
= FPBits(uint32_t(0x00400000)).get_val();
62 result
= LIBC_NAMESPACE::sinhf(x
);
63 EXPECT_MPFR_MATCH(mpfr::Operation::Sinh
, x
, result
, 0.5);
64 EXPECT_FP_EQ(x
, result
);
67 TEST_F(LlvmLibcSinhfTest
, Overflow
) {
68 LIBC_NAMESPACE::libc_errno
= 0;
69 EXPECT_FP_EQ_WITH_EXCEPTION(
70 inf
, LIBC_NAMESPACE::sinhf(FPBits(0x7f7fffffU
).get_val()), FE_OVERFLOW
);
71 EXPECT_MATH_ERRNO(ERANGE
);
73 EXPECT_FP_EQ_WITH_EXCEPTION(
74 inf
, LIBC_NAMESPACE::sinhf(FPBits(0x42cffff8U
).get_val()), FE_OVERFLOW
);
75 EXPECT_MATH_ERRNO(ERANGE
);
77 EXPECT_FP_EQ_WITH_EXCEPTION(
78 inf
, LIBC_NAMESPACE::sinhf(FPBits(0x42d00008U
).get_val()), FE_OVERFLOW
);
79 EXPECT_MATH_ERRNO(ERANGE
);
82 TEST_F(LlvmLibcSinhfTest
, ExceptionalValues
) {
83 float x
= FPBits(uint32_t(0x3a12'85ffU
)).get_val();
84 EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Sinh
, x
,
85 LIBC_NAMESPACE::sinhf(x
), 0.5);
87 x
= -FPBits(uint32_t(0x3a12'85ffU
)).get_val();
88 EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Sinh
, x
,
89 LIBC_NAMESPACE::sinhf(x
), 0.5);