1 //===-- Unittests for expf ------------------------------------------------===//
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 "src/__support/FPUtil/FPBits.h"
10 #include "src/errno/libc_errno.h"
11 #include "src/math/expf.h"
12 #include "test/UnitTest/FPMatcher.h"
13 #include "test/UnitTest/Test.h"
14 #include "utils/MPFRWrapper/MPFRUtils.h"
19 using LlvmLibcExpfTest
= LIBC_NAMESPACE::testing::FPTest
<float>;
21 namespace mpfr
= LIBC_NAMESPACE::testing::mpfr
;
23 TEST_F(LlvmLibcExpfTest
, SpecialNumbers
) {
26 EXPECT_FP_EQ(aNaN
, LIBC_NAMESPACE::expf(aNaN
));
29 EXPECT_FP_EQ(inf
, LIBC_NAMESPACE::expf(inf
));
32 EXPECT_FP_EQ(0.0f
, LIBC_NAMESPACE::expf(neg_inf
));
35 EXPECT_FP_EQ(1.0f
, LIBC_NAMESPACE::expf(0.0f
));
38 EXPECT_FP_EQ(1.0f
, LIBC_NAMESPACE::expf(-0.0f
));
42 TEST_F(LlvmLibcExpfTest
, Overflow
) {
44 EXPECT_FP_EQ_WITH_EXCEPTION(
45 inf
, LIBC_NAMESPACE::expf(float(FPBits(0x7f7fffffU
))), FE_OVERFLOW
);
46 EXPECT_MATH_ERRNO(ERANGE
);
48 EXPECT_FP_EQ_WITH_EXCEPTION(
49 inf
, LIBC_NAMESPACE::expf(float(FPBits(0x42cffff8U
))), FE_OVERFLOW
);
50 EXPECT_MATH_ERRNO(ERANGE
);
52 EXPECT_FP_EQ_WITH_EXCEPTION(
53 inf
, LIBC_NAMESPACE::expf(float(FPBits(0x42d00008U
))), FE_OVERFLOW
);
54 EXPECT_MATH_ERRNO(ERANGE
);
57 TEST_F(LlvmLibcExpfTest
, Underflow
) {
59 EXPECT_FP_EQ_WITH_EXCEPTION(
60 0.0f
, LIBC_NAMESPACE::expf(float(FPBits(0xff7fffffU
))), FE_UNDERFLOW
);
61 EXPECT_MATH_ERRNO(ERANGE
);
63 float x
= float(FPBits(0xc2cffff8U
));
64 EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Exp
, x
,
65 LIBC_NAMESPACE::expf(x
), 0.5);
66 EXPECT_MATH_ERRNO(ERANGE
);
68 x
= float(FPBits(0xc2d00008U
));
69 EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Exp
, x
,
70 LIBC_NAMESPACE::expf(x
), 0.5);
71 EXPECT_MATH_ERRNO(ERANGE
);
74 // Test with inputs which are the borders of underflow/overflow but still
75 // produce valid results without setting errno.
76 TEST_F(LlvmLibcExpfTest
, Borderline
) {
80 x
= float(FPBits(0x42affff8U
));
81 ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Exp
, x
,
82 LIBC_NAMESPACE::expf(x
), 0.5);
85 x
= float(FPBits(0x42b00008U
));
86 ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Exp
, x
,
87 LIBC_NAMESPACE::expf(x
), 0.5);
90 x
= float(FPBits(0xc2affff8U
));
91 ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Exp
, x
,
92 LIBC_NAMESPACE::expf(x
), 0.5);
95 x
= float(FPBits(0xc2b00008U
));
96 ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Exp
, x
,
97 LIBC_NAMESPACE::expf(x
), 0.5);
100 x
= float(FPBits(0xc236bd8cU
));
101 EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Exp
, x
,
102 LIBC_NAMESPACE::expf(x
), 0.5);
103 EXPECT_MATH_ERRNO(0);
106 TEST_F(LlvmLibcExpfTest
, InFloatRange
) {
107 constexpr uint32_t COUNT
= 100'000;
108 constexpr uint32_t STEP
= UINT32_MAX
/ COUNT
;
109 for (uint32_t i
= 0, v
= 0; i
<= COUNT
; ++i
, v
+= STEP
) {
110 float x
= float(FPBits(v
));
111 if (isnan(x
) || isinf(x
))
114 float result
= LIBC_NAMESPACE::expf(x
);
116 // If the computation resulted in an error or did not produce valid result
117 // in the single-precision floating point range, then ignore comparing with
118 // MPFR result as MPFR can still produce valid results because of its
120 if (isnan(result
) || isinf(result
) || libc_errno
!= 0)
122 EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Exp
, x
,
123 LIBC_NAMESPACE::expf(x
), 0.5);