1 | //===-- Unittests for expm1f-----------------------------------------------===// |
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/FPUtil/FPBits.h" |
11 | #include "src/errno/libc_errno.h" |
12 | #include "src/math/expm1f.h" |
13 | #include "test/UnitTest/FPMatcher.h" |
14 | #include "test/UnitTest/Test.h" |
15 | #include "utils/MPFRWrapper/MPFRUtils.h" |
16 | |
17 | #include <stdint.h> |
18 | |
19 | using LlvmLibcExpm1fTest = LIBC_NAMESPACE::testing::FPTest<float>; |
20 | |
21 | namespace mpfr = LIBC_NAMESPACE::testing::mpfr; |
22 | |
23 | TEST_F(LlvmLibcExpm1fTest, SpecialNumbers) { |
24 | LIBC_NAMESPACE::libc_errno = 0; |
25 | |
26 | EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::expm1f(aNaN)); |
27 | EXPECT_MATH_ERRNO(0); |
28 | |
29 | EXPECT_FP_EQ(inf, LIBC_NAMESPACE::expm1f(inf)); |
30 | EXPECT_MATH_ERRNO(0); |
31 | |
32 | EXPECT_FP_EQ(-1.0f, LIBC_NAMESPACE::expm1f(neg_inf)); |
33 | EXPECT_MATH_ERRNO(0); |
34 | |
35 | EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::expm1f(0.0f)); |
36 | EXPECT_MATH_ERRNO(0); |
37 | |
38 | EXPECT_FP_EQ(-0.0f, LIBC_NAMESPACE::expm1f(-0.0f)); |
39 | EXPECT_MATH_ERRNO(0); |
40 | } |
41 | |
42 | TEST_F(LlvmLibcExpm1fTest, Overflow) { |
43 | LIBC_NAMESPACE::libc_errno = 0; |
44 | EXPECT_FP_EQ_WITH_EXCEPTION( |
45 | inf, LIBC_NAMESPACE::expm1f(FPBits(0x7f7fffffU).get_val()), FE_OVERFLOW); |
46 | EXPECT_MATH_ERRNO(ERANGE); |
47 | |
48 | EXPECT_FP_EQ_WITH_EXCEPTION( |
49 | inf, LIBC_NAMESPACE::expm1f(FPBits(0x42cffff8U).get_val()), FE_OVERFLOW); |
50 | EXPECT_MATH_ERRNO(ERANGE); |
51 | |
52 | EXPECT_FP_EQ_WITH_EXCEPTION( |
53 | inf, LIBC_NAMESPACE::expm1f(FPBits(0x42d00008U).get_val()), FE_OVERFLOW); |
54 | EXPECT_MATH_ERRNO(ERANGE); |
55 | } |
56 | |
57 | TEST_F(LlvmLibcExpm1fTest, Underflow) { |
58 | LIBC_NAMESPACE::libc_errno = 0; |
59 | EXPECT_FP_EQ(-1.0f, LIBC_NAMESPACE::expm1f(FPBits(0xff7fffffU).get_val())); |
60 | |
61 | float x = FPBits(0xc2cffff8U).get_val(); |
62 | EXPECT_FP_EQ(-1.0f, LIBC_NAMESPACE::expm1f(x)); |
63 | |
64 | x = FPBits(0xc2d00008U).get_val(); |
65 | EXPECT_FP_EQ(-1.0f, LIBC_NAMESPACE::expm1f(x)); |
66 | } |
67 | |
68 | // Test with inputs which are the borders of underflow/overflow but still |
69 | // produce valid results without setting errno. |
70 | TEST_F(LlvmLibcExpm1fTest, Borderline) { |
71 | float x; |
72 | |
73 | LIBC_NAMESPACE::libc_errno = 0; |
74 | x = FPBits(0x42affff8U).get_val(); |
75 | ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Expm1, x, |
76 | LIBC_NAMESPACE::expm1f(x), 0.5); |
77 | EXPECT_MATH_ERRNO(0); |
78 | |
79 | x = FPBits(0x42b00008U).get_val(); |
80 | ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Expm1, x, |
81 | LIBC_NAMESPACE::expm1f(x), 0.5); |
82 | EXPECT_MATH_ERRNO(0); |
83 | |
84 | x = FPBits(0xc2affff8U).get_val(); |
85 | ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Expm1, x, |
86 | LIBC_NAMESPACE::expm1f(x), 0.5); |
87 | EXPECT_MATH_ERRNO(0); |
88 | |
89 | x = FPBits(0xc2b00008U).get_val(); |
90 | ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Expm1, x, |
91 | LIBC_NAMESPACE::expm1f(x), 0.5); |
92 | EXPECT_MATH_ERRNO(0); |
93 | |
94 | x = FPBits(0x3dc252ddU).get_val(); |
95 | ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Expm1, x, |
96 | LIBC_NAMESPACE::expm1f(x), 0.5); |
97 | EXPECT_MATH_ERRNO(0); |
98 | |
99 | x = FPBits(0x3e35bec5U).get_val(); |
100 | ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Expm1, x, |
101 | LIBC_NAMESPACE::expm1f(x), 0.5); |
102 | EXPECT_MATH_ERRNO(0); |
103 | |
104 | x = FPBits(0x942ed494U).get_val(); |
105 | ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Expm1, x, |
106 | LIBC_NAMESPACE::expm1f(x), 0.5); |
107 | EXPECT_MATH_ERRNO(0); |
108 | |
109 | x = FPBits(0xbdc1c6cbU).get_val(); |
110 | ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Expm1, x, |
111 | LIBC_NAMESPACE::expm1f(x), 0.5); |
112 | EXPECT_MATH_ERRNO(0); |
113 | } |
114 | |
115 | TEST_F(LlvmLibcExpm1fTest, InFloatRange) { |
116 | constexpr uint32_t COUNT = 100'000; |
117 | constexpr uint32_t STEP = UINT32_MAX / COUNT; |
118 | for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) { |
119 | float x = FPBits(v).get_val(); |
120 | if (isnan(x: x) || isinf(x: x)) |
121 | continue; |
122 | LIBC_NAMESPACE::libc_errno = 0; |
123 | float result = LIBC_NAMESPACE::expm1f(x); |
124 | |
125 | // If the computation resulted in an error or did not produce valid result |
126 | // in the single-precision floating point range, then ignore comparing with |
127 | // MPFR result as MPFR can still produce valid results because of its |
128 | // wider precision. |
129 | if (isnan(x: result) || isinf(x: result) || LIBC_NAMESPACE::libc_errno != 0) |
130 | continue; |
131 | ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Expm1, x, |
132 | LIBC_NAMESPACE::expm1f(x), 0.5); |
133 | } |
134 | } |
135 | |