Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libc / test / src / math / expf_test.cpp
blob521eba705b69fd020ff910af25ecf76bf561c5c7
1 //===-- Unittests for expf ------------------------------------------------===//
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/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"
15 #include <math.h>
17 #include <stdint.h>
19 using LlvmLibcExpfTest = LIBC_NAMESPACE::testing::FPTest<float>;
21 namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
23 TEST_F(LlvmLibcExpfTest, SpecialNumbers) {
24 libc_errno = 0;
26 EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::expf(aNaN));
27 EXPECT_MATH_ERRNO(0);
29 EXPECT_FP_EQ(inf, LIBC_NAMESPACE::expf(inf));
30 EXPECT_MATH_ERRNO(0);
32 EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::expf(neg_inf));
33 EXPECT_MATH_ERRNO(0);
35 EXPECT_FP_EQ(1.0f, LIBC_NAMESPACE::expf(0.0f));
36 EXPECT_MATH_ERRNO(0);
38 EXPECT_FP_EQ(1.0f, LIBC_NAMESPACE::expf(-0.0f));
39 EXPECT_MATH_ERRNO(0);
42 TEST_F(LlvmLibcExpfTest, Overflow) {
43 libc_errno = 0;
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) {
58 libc_errno = 0;
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) {
77 float x;
79 libc_errno = 0;
80 x = float(FPBits(0x42affff8U));
81 ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Exp, x,
82 LIBC_NAMESPACE::expf(x), 0.5);
83 EXPECT_MATH_ERRNO(0);
85 x = float(FPBits(0x42b00008U));
86 ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Exp, x,
87 LIBC_NAMESPACE::expf(x), 0.5);
88 EXPECT_MATH_ERRNO(0);
90 x = float(FPBits(0xc2affff8U));
91 ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Exp, x,
92 LIBC_NAMESPACE::expf(x), 0.5);
93 EXPECT_MATH_ERRNO(0);
95 x = float(FPBits(0xc2b00008U));
96 ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Exp, x,
97 LIBC_NAMESPACE::expf(x), 0.5);
98 EXPECT_MATH_ERRNO(0);
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))
112 continue;
113 libc_errno = 0;
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
119 // wider precision.
120 if (isnan(result) || isinf(result) || libc_errno != 0)
121 continue;
122 EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Exp, x,
123 LIBC_NAMESPACE::expf(x), 0.5);