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 "hdr/math_macros.h"
10#include "src/__support/FPUtil/FPBits.h"
11#include "src/__support/macros/properties/cpu_features.h" // LIBC_TARGET_CPU_HAS_FMA
12#include "src/errno/libc_errno.h"
13#include "src/math/exp2f.h"
14#include "test/UnitTest/FPMatcher.h"
15#include "test/UnitTest/Test.h"
16#include "utils/MPFRWrapper/MPFRUtils.h"
17
18#include <stdint.h>
19
20using LlvmLibcExp2fTest = LIBC_NAMESPACE::testing::FPTest<float>;
21
22namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
23
24TEST_F(LlvmLibcExp2fTest, SpecialNumbers) {
25 LIBC_NAMESPACE::libc_errno = 0;
26
27 EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::exp2f(aNaN));
28 EXPECT_MATH_ERRNO(0);
29
30 EXPECT_FP_EQ(inf, LIBC_NAMESPACE::exp2f(inf));
31 EXPECT_MATH_ERRNO(0);
32
33 EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::exp2f(neg_inf));
34 EXPECT_MATH_ERRNO(0);
35
36 EXPECT_FP_EQ(1.0f, LIBC_NAMESPACE::exp2f(0.0f));
37 EXPECT_MATH_ERRNO(0);
38
39 EXPECT_FP_EQ(1.0f, LIBC_NAMESPACE::exp2f(-0.0f));
40 EXPECT_MATH_ERRNO(0);
41}
42
43TEST_F(LlvmLibcExp2fTest, Overflow) {
44 LIBC_NAMESPACE::libc_errno = 0;
45 EXPECT_FP_EQ_WITH_EXCEPTION(
46 inf, LIBC_NAMESPACE::exp2f(FPBits(0x7f7fffffU).get_val()), FE_OVERFLOW);
47 EXPECT_MATH_ERRNO(ERANGE);
48
49 EXPECT_FP_EQ_WITH_EXCEPTION(
50 inf, LIBC_NAMESPACE::exp2f(FPBits(0x43000000U).get_val()), FE_OVERFLOW);
51 EXPECT_MATH_ERRNO(ERANGE);
52
53 EXPECT_FP_EQ_WITH_EXCEPTION(
54 inf, LIBC_NAMESPACE::exp2f(FPBits(0x43000001U).get_val()), FE_OVERFLOW);
55 EXPECT_MATH_ERRNO(ERANGE);
56}
57
58TEST_F(LlvmLibcExp2fTest, TrickyInputs) {
59 constexpr int N = 12;
60 constexpr uint32_t INPUTS[N] = {
61 0x3b429d37U, /*0x1.853a6ep-9f*/
62 0x3c02a9adU, /*0x1.05535ap-7f*/
63 0x3ca66e26U, /*0x1.4cdc4cp-6f*/
64 0x3d92a282U, /*0x1.254504p-4f*/
65 0x42fa0001U, /*0x1.f40002p+6f*/
66 0x42ffffffU, /*0x1.fffffep+6f*/
67 0xb8d3d026U, /*-0x1.a7a04cp-14f*/
68 0xbcf3a937U, /*-0x1.e7526ep-6f*/
69 0xc2fa0001U, /*-0x1.f40002p+6f*/
70 0xc2fc0000U, /*-0x1.f8p+6f*/
71 0xc2fc0001U, /*-0x1.f80002p+6f*/
72 0xc3150000U, /*-0x1.2ap+7f*/
73 };
74 for (int i = 0; i < N; ++i) {
75 LIBC_NAMESPACE::libc_errno = 0;
76 float x = FPBits(INPUTS[i]).get_val();
77 EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Exp2, x,
78 LIBC_NAMESPACE::exp2f(x), 0.5);
79 EXPECT_MATH_ERRNO(0);
80 }
81}
82
83TEST_F(LlvmLibcExp2fTest, Underflow) {
84 LIBC_NAMESPACE::libc_errno = 0;
85 EXPECT_FP_EQ_WITH_EXCEPTION(
86 0.0f, LIBC_NAMESPACE::exp2f(FPBits(0xff7fffffU).get_val()), FE_UNDERFLOW);
87 EXPECT_MATH_ERRNO(ERANGE);
88
89 float x = FPBits(0xc3158000U).get_val();
90 EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Exp2, x,
91 LIBC_NAMESPACE::exp2f(x), 0.5);
92 EXPECT_MATH_ERRNO(0);
93
94 x = FPBits(0xc3160000U).get_val();
95 EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Exp2, x,
96 LIBC_NAMESPACE::exp2f(x), 0.5);
97 EXPECT_MATH_ERRNO(ERANGE);
98
99 x = FPBits(0xc3165432U).get_val();
100 EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Exp2, x,
101 LIBC_NAMESPACE::exp2f(x), 0.5);
102 EXPECT_MATH_ERRNO(ERANGE);
103}
104
105TEST_F(LlvmLibcExp2fTest, InFloatRange) {
106 constexpr uint32_t COUNT = 100'000;
107 constexpr uint32_t STEP = UINT32_MAX / COUNT;
108 for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
109 float x = FPBits(v).get_val();
110 if (isnan(x: x) || isinf(x: x))
111 continue;
112 LIBC_NAMESPACE::libc_errno = 0;
113 float result = LIBC_NAMESPACE::exp2f(x);
114
115 // If the computation resulted in an error or did not produce valid result
116 // in the single-precision floating point range, then ignore comparing with
117 // MPFR result as MPFR can still produce valid results because of its
118 // wider precision.
119 if (isnan(x: result) || isinf(x: result) || LIBC_NAMESPACE::libc_errno != 0)
120 continue;
121 ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Exp2, x,
122 LIBC_NAMESPACE::exp2f(x), 0.5);
123 }
124}
125

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