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