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//===----------------------------------------------------------------------===//
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/expf.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
19using LlvmLibcExpfTest = LIBC_NAMESPACE::testing::FPTest<float>;
20
21namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
22
23TEST_F(LlvmLibcExpfTest, SpecialNumbers) {
24 LIBC_NAMESPACE::libc_errno = 0;
25
26 EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::expf(aNaN));
27 EXPECT_MATH_ERRNO(0);
28
29 EXPECT_FP_EQ(inf, LIBC_NAMESPACE::expf(inf));
30 EXPECT_MATH_ERRNO(0);
31
32 EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::expf(neg_inf));
33 EXPECT_MATH_ERRNO(0);
34
35 EXPECT_FP_EQ(1.0f, LIBC_NAMESPACE::expf(0.0f));
36 EXPECT_MATH_ERRNO(0);
37
38 EXPECT_FP_EQ(1.0f, LIBC_NAMESPACE::expf(-0.0f));
39 EXPECT_MATH_ERRNO(0);
40}
41
42TEST_F(LlvmLibcExpfTest, Overflow) {
43 LIBC_NAMESPACE::libc_errno = 0;
44 EXPECT_FP_EQ_WITH_EXCEPTION(
45 inf, LIBC_NAMESPACE::expf(FPBits(0x7f7fffffU).get_val()), FE_OVERFLOW);
46 EXPECT_MATH_ERRNO(ERANGE);
47
48 EXPECT_FP_EQ_WITH_EXCEPTION(
49 inf, LIBC_NAMESPACE::expf(FPBits(0x42cffff8U).get_val()), FE_OVERFLOW);
50 EXPECT_MATH_ERRNO(ERANGE);
51
52 EXPECT_FP_EQ_WITH_EXCEPTION(
53 inf, LIBC_NAMESPACE::expf(FPBits(0x42d00008U).get_val()), FE_OVERFLOW);
54 EXPECT_MATH_ERRNO(ERANGE);
55}
56
57TEST_F(LlvmLibcExpfTest, Underflow) {
58 LIBC_NAMESPACE::libc_errno = 0;
59 EXPECT_FP_EQ_WITH_EXCEPTION(
60 0.0f, LIBC_NAMESPACE::expf(FPBits(0xff7fffffU).get_val()), FE_UNDERFLOW);
61 EXPECT_MATH_ERRNO(ERANGE);
62
63 float x = FPBits(0xc2cffff8U).get_val();
64 EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Exp, x,
65 LIBC_NAMESPACE::expf(x), 0.5);
66 EXPECT_MATH_ERRNO(ERANGE);
67
68 x = FPBits(0xc2d00008U).get_val();
69 EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Exp, x,
70 LIBC_NAMESPACE::expf(x), 0.5);
71 EXPECT_MATH_ERRNO(ERANGE);
72}
73
74// Test with inputs which are the borders of underflow/overflow but still
75// produce valid results without setting errno.
76TEST_F(LlvmLibcExpfTest, Borderline) {
77 float x;
78
79 LIBC_NAMESPACE::libc_errno = 0;
80 x = FPBits(0x42affff8U).get_val();
81 ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Exp, x,
82 LIBC_NAMESPACE::expf(x), 0.5);
83 EXPECT_MATH_ERRNO(0);
84
85 x = FPBits(0x42b00008U).get_val();
86 ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Exp, x,
87 LIBC_NAMESPACE::expf(x), 0.5);
88 EXPECT_MATH_ERRNO(0);
89
90 x = FPBits(0xc2affff8U).get_val();
91 ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Exp, x,
92 LIBC_NAMESPACE::expf(x), 0.5);
93 EXPECT_MATH_ERRNO(0);
94
95 x = FPBits(0xc2b00008U).get_val();
96 ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Exp, x,
97 LIBC_NAMESPACE::expf(x), 0.5);
98 EXPECT_MATH_ERRNO(0);
99
100 x = FPBits(0xc236bd8cU).get_val();
101 EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Exp, x,
102 LIBC_NAMESPACE::expf(x), 0.5);
103 EXPECT_MATH_ERRNO(0);
104}
105
106TEST_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 = FPBits(v).get_val();
111 if (isnan(x: x) || isinf(x: x))
112 continue;
113 LIBC_NAMESPACE::libc_errno = 0;
114 float result = LIBC_NAMESPACE::expf(x);
115
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(x: result) || isinf(x: result) || LIBC_NAMESPACE::libc_errno != 0)
121 continue;
122 EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Exp, x,
123 LIBC_NAMESPACE::expf(x), 0.5);
124 }
125}
126

source code of libc/test/src/math/expf_test.cpp