1 //===-- Unittests for expm1f-----------------------------------------------===//
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/expm1f.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(LlvmLibcExpm1fTest, SpecialNumbers) {
24   errno = 0;
25 
26   EXPECT_FP_EQ(aNaN, __llvm_libc::expm1f(aNaN));
27   EXPECT_EQ(errno, 0);
28 
29   EXPECT_FP_EQ(inf, __llvm_libc::expm1f(inf));
30   EXPECT_EQ(errno, 0);
31 
32   EXPECT_FP_EQ(-1.0f, __llvm_libc::expm1f(neg_inf));
33   EXPECT_EQ(errno, 0);
34 
35   EXPECT_FP_EQ(0.0f, __llvm_libc::expm1f(0.0f));
36   EXPECT_EQ(errno, 0);
37 
38   EXPECT_FP_EQ(-0.0f, __llvm_libc::expm1f(-0.0f));
39   EXPECT_EQ(errno, 0);
40 }
41 
42 TEST(LlvmLibcExpm1fTest, Overflow) {
43   errno = 0;
44   EXPECT_FP_EQ(inf, __llvm_libc::expm1f(float(FPBits(0x7f7fffffU))));
45   EXPECT_EQ(errno, ERANGE);
46 
47   errno = 0;
48   EXPECT_FP_EQ(inf, __llvm_libc::expm1f(float(FPBits(0x42cffff8U))));
49   EXPECT_EQ(errno, ERANGE);
50 
51   errno = 0;
52   EXPECT_FP_EQ(inf, __llvm_libc::expm1f(float(FPBits(0x42d00008U))));
53   EXPECT_EQ(errno, ERANGE);
54 }
55 
56 TEST(LlvmLibcExpm1fTest, Underflow) {
57   errno = 0;
58   EXPECT_FP_EQ(-1.0f, __llvm_libc::expm1f(float(FPBits(0xff7fffffU))));
59   EXPECT_EQ(errno, ERANGE);
60 
61   errno = 0;
62   float x = float(FPBits(0xc2cffff8U));
63   EXPECT_FP_EQ(-1.0f, __llvm_libc::expm1f(x));
64   EXPECT_EQ(errno, ERANGE);
65 
66   errno = 0;
67   x = float(FPBits(0xc2d00008U));
68   EXPECT_FP_EQ(-1.0f, __llvm_libc::expm1f(x));
69   EXPECT_EQ(errno, ERANGE);
70 }
71 
72 // Test with inputs which are the borders of underflow/overflow but still
73 // produce valid results without setting errno.
74 TEST(LlvmLibcExpm1fTest, Borderline) {
75   float x;
76 
77   errno = 0;
78   x = float(FPBits(0x42affff8U));
79   ASSERT_MPFR_MATCH(mpfr::Operation::Expm1, x, __llvm_libc::expm1f(x), 1.0);
80   EXPECT_EQ(errno, 0);
81 
82   x = float(FPBits(0x42b00008U));
83   ASSERT_MPFR_MATCH(mpfr::Operation::Expm1, x, __llvm_libc::expm1f(x), 1.0);
84   EXPECT_EQ(errno, 0);
85 
86   x = float(FPBits(0xc2affff8U));
87   ASSERT_MPFR_MATCH(mpfr::Operation::Expm1, x, __llvm_libc::expm1f(x), 1.0);
88   EXPECT_EQ(errno, 0);
89 
90   x = float(FPBits(0xc2b00008U));
91   ASSERT_MPFR_MATCH(mpfr::Operation::Expm1, x, __llvm_libc::expm1f(x), 1.0);
92   EXPECT_EQ(errno, 0);
93 }
94 
95 TEST(LlvmLibcExpm1fTest, InFloatRange) {
96   constexpr uint32_t count = 1000000;
97   constexpr uint32_t step = UINT32_MAX / count;
98   for (uint32_t i = 0, v = 0; i <= count; ++i, v += step) {
99     float x = float(FPBits(v));
100     if (isnan(x) || isinf(x))
101       continue;
102     errno = 0;
103     float result = __llvm_libc::expm1f(x);
104 
105     // If the computation resulted in an error or did not produce valid result
106     // in the single-precision floating point range, then ignore comparing with
107     // MPFR result as MPFR can still produce valid results because of its
108     // wider precision.
109     if (isnan(result) || isinf(result) || errno != 0)
110       continue;
111     ASSERT_MPFR_MATCH(mpfr::Operation::Expm1, x, __llvm_libc::expm1f(x), 1.5);
112   }
113 }
114