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 
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 
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 
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(mpfr::Operation::Exp, x, __llvm_libc::expf(x), 1.0);
61   EXPECT_MATH_ERRNO(ERANGE);
62 
63   x = float(FPBits(0xc2d00008U));
64   EXPECT_MPFR_MATCH(mpfr::Operation::Exp, x, __llvm_libc::expf(x), 1.0);
65   EXPECT_MATH_ERRNO(ERANGE);
66 }
67 
68 // Test with inputs which are the borders of underflow/overflow but still
69 // produce valid results without setting errno.
70 TEST(LlvmLibcExpfTest, Borderline) {
71   float x;
72 
73   errno = 0;
74   x = float(FPBits(0x42affff8U));
75   ASSERT_MPFR_MATCH(mpfr::Operation::Exp, x, __llvm_libc::expf(x), 1.0);
76   EXPECT_MATH_ERRNO(0);
77 
78   x = float(FPBits(0x42b00008U));
79   ASSERT_MPFR_MATCH(mpfr::Operation::Exp, x, __llvm_libc::expf(x), 1.0);
80   EXPECT_MATH_ERRNO(0);
81 
82   x = float(FPBits(0xc2affff8U));
83   ASSERT_MPFR_MATCH(mpfr::Operation::Exp, x, __llvm_libc::expf(x), 1.0);
84   EXPECT_MATH_ERRNO(0);
85 
86   x = float(FPBits(0xc2b00008U));
87   ASSERT_MPFR_MATCH(mpfr::Operation::Exp, x, __llvm_libc::expf(x), 1.0);
88   EXPECT_MATH_ERRNO(0);
89 }
90 
91 TEST(LlvmLibcExpfTest, InFloatRange) {
92   constexpr uint32_t COUNT = 1000000;
93   constexpr uint32_t STEP = UINT32_MAX / COUNT;
94   for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
95     float x = float(FPBits(v));
96     if (isnan(x) || isinf(x))
97       continue;
98     errno = 0;
99     float result = __llvm_libc::expf(x);
100 
101     // If the computation resulted in an error or did not produce valid result
102     // in the single-precision floating point range, then ignore comparing with
103     // MPFR result as MPFR can still produce valid results because of its
104     // wider precision.
105     if (isnan(result) || isinf(result) || errno != 0)
106       continue;
107     ASSERT_MPFR_MATCH(mpfr::Operation::Exp, x, __llvm_libc::expf(x), 1.0);
108   }
109 }
110