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 | //===----------------------------------------------------------------------===// |
8 | |
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" |
17 | |
18 | #include <errno.h> |
19 | #include <stdint.h> |
20 | |
21 | using LlvmLibcSinhfTest = LIBC_NAMESPACE::testing::FPTest<float>; |
22 | |
23 | namespace mpfr = LIBC_NAMESPACE::testing::mpfr; |
24 | |
25 | TEST_F(LlvmLibcSinhfTest, SpecialNumbers) { |
26 | LIBC_NAMESPACE::libc_errno = 0; |
27 | |
28 | EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::sinhf(aNaN)); |
29 | EXPECT_MATH_ERRNO(0); |
30 | |
31 | EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::sinhf(0.0f)); |
32 | EXPECT_MATH_ERRNO(0); |
33 | |
34 | EXPECT_FP_EQ(-0.0f, LIBC_NAMESPACE::sinhf(-0.0f)); |
35 | EXPECT_MATH_ERRNO(0); |
36 | |
37 | EXPECT_FP_EQ(inf, LIBC_NAMESPACE::sinhf(inf)); |
38 | EXPECT_MATH_ERRNO(0); |
39 | |
40 | EXPECT_FP_EQ(neg_inf, LIBC_NAMESPACE::sinhf(neg_inf)); |
41 | EXPECT_MATH_ERRNO(0); |
42 | } |
43 | |
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 = FPBits(v).get_val(); |
49 | if (isnan(x: x) || isinf(x: x)) |
50 | continue; |
51 | ASSERT_MPFR_MATCH(mpfr::Operation::Sinh, x, LIBC_NAMESPACE::sinhf(x), 0.5); |
52 | } |
53 | } |
54 | |
55 | // For small values, sinh(x) is x. |
56 | TEST_F(LlvmLibcSinhfTest, SmallValues) { |
57 | float x = FPBits(uint32_t(0x17800000)).get_val(); |
58 | float result = LIBC_NAMESPACE::sinhf(x); |
59 | EXPECT_MPFR_MATCH(mpfr::Operation::Sinh, x, result, 0.5); |
60 | EXPECT_FP_EQ(x, result); |
61 | |
62 | x = FPBits(uint32_t(0x00400000)).get_val(); |
63 | result = LIBC_NAMESPACE::sinhf(x); |
64 | EXPECT_MPFR_MATCH(mpfr::Operation::Sinh, x, result, 0.5); |
65 | EXPECT_FP_EQ(x, result); |
66 | } |
67 | |
68 | TEST_F(LlvmLibcSinhfTest, Overflow) { |
69 | LIBC_NAMESPACE::libc_errno = 0; |
70 | EXPECT_FP_EQ_WITH_EXCEPTION( |
71 | inf, LIBC_NAMESPACE::sinhf(FPBits(0x7f7fffffU).get_val()), FE_OVERFLOW); |
72 | EXPECT_MATH_ERRNO(ERANGE); |
73 | |
74 | EXPECT_FP_EQ_WITH_EXCEPTION( |
75 | inf, LIBC_NAMESPACE::sinhf(FPBits(0x42cffff8U).get_val()), FE_OVERFLOW); |
76 | EXPECT_MATH_ERRNO(ERANGE); |
77 | |
78 | EXPECT_FP_EQ_WITH_EXCEPTION( |
79 | inf, LIBC_NAMESPACE::sinhf(FPBits(0x42d00008U).get_val()), FE_OVERFLOW); |
80 | EXPECT_MATH_ERRNO(ERANGE); |
81 | } |
82 | |
83 | TEST_F(LlvmLibcSinhfTest, ExceptionalValues) { |
84 | float x = FPBits(uint32_t(0x3a12'85ffU)).get_val(); |
85 | EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Sinh, x, |
86 | LIBC_NAMESPACE::sinhf(x), 0.5); |
87 | |
88 | x = -FPBits(uint32_t(0x3a12'85ffU)).get_val(); |
89 | EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Sinh, x, |
90 | LIBC_NAMESPACE::sinhf(x), 0.5); |
91 | } |
92 | |