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