xref: /llvm-project/libc/test/src/math/exp2m1f_test.cpp (revision f8f5b17564cb839101ba99390bcecc564de57e65)
1a8c59750SOverMighty //===-- Unittests for exp2m1f ---------------------------------------------===//
2a8c59750SOverMighty //
3a8c59750SOverMighty // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4a8c59750SOverMighty // See https://llvm.org/LICENSE.txt for license information.
5a8c59750SOverMighty // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6a8c59750SOverMighty //
7a8c59750SOverMighty //===----------------------------------------------------------------------===//
8a8c59750SOverMighty 
95748ad84Slntue #include "hdr/math_macros.h"
10a8c59750SOverMighty #include "src/__support/CPP/array.h"
11a8c59750SOverMighty #include "src/__support/FPUtil/FPBits.h"
12a8c59750SOverMighty #include "src/errno/libc_errno.h"
13a8c59750SOverMighty #include "src/math/exp2m1f.h"
14a8c59750SOverMighty #include "test/UnitTest/FPMatcher.h"
15a8c59750SOverMighty #include "test/UnitTest/Test.h"
16a8c59750SOverMighty #include "utils/MPFRWrapper/MPFRUtils.h"
17a8c59750SOverMighty 
18a8c59750SOverMighty #include <stdint.h>
19a8c59750SOverMighty 
20a8c59750SOverMighty using LlvmLibcExp2m1fTest = LIBC_NAMESPACE::testing::FPTest<float>;
21a8c59750SOverMighty 
22a8c59750SOverMighty namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
23a8c59750SOverMighty 
24a8c59750SOverMighty TEST_F(LlvmLibcExp2m1fTest, TrickyInputs) {
25a8c59750SOverMighty   constexpr LIBC_NAMESPACE::cpp::array<float, 10> INPUTS = {
26a8c59750SOverMighty       // EXP2M1F_EXCEPTS_LO
27a8c59750SOverMighty       0x1.36dc8ep-36,
28a8c59750SOverMighty       0x1.224936p-19,
29a8c59750SOverMighty       0x1.d16d2p-20,
30a8c59750SOverMighty       0x1.17949ep-14,
31a8c59750SOverMighty       -0x1.9c3e1ep-38,
32a8c59750SOverMighty       -0x1.4d89b4p-32,
33a8c59750SOverMighty       -0x1.a6eac4p-10,
34a8c59750SOverMighty       -0x1.e7526ep-6,
35a8c59750SOverMighty       // EXP2M1F_EXCEPTS_HI
36a8c59750SOverMighty       0x1.16a972p-1,
37a8c59750SOverMighty       -0x1.9f12acp-5,
38a8c59750SOverMighty   };
39a8c59750SOverMighty 
40a8c59750SOverMighty   for (float x : INPUTS) {
41a8c59750SOverMighty     LIBC_NAMESPACE::libc_errno = 0;
42a8c59750SOverMighty     EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Exp2m1, x,
43a8c59750SOverMighty                                    LIBC_NAMESPACE::exp2m1f(x), 0.5);
44a8c59750SOverMighty   }
45a8c59750SOverMighty }
46a8c59750SOverMighty 
47a8c59750SOverMighty TEST_F(LlvmLibcExp2m1fTest, InFloatRange) {
48a8c59750SOverMighty   constexpr uint32_t COUNT = 100'000;
49a8c59750SOverMighty   constexpr uint32_t STEP = UINT32_MAX / COUNT;
50a8c59750SOverMighty   for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
51a8c59750SOverMighty     float x = FPBits(v).get_val();
52*f8f5b175SNhat Nguyen     if (FPBits(v).is_nan() || FPBits(v).is_inf())
53a8c59750SOverMighty       continue;
54a8c59750SOverMighty     LIBC_NAMESPACE::libc_errno = 0;
55a8c59750SOverMighty     float result = LIBC_NAMESPACE::exp2m1f(x);
56a8c59750SOverMighty 
57a8c59750SOverMighty     // If the computation resulted in an error or did not produce valid result
58a8c59750SOverMighty     // in the single-precision floating point range, then ignore comparing with
59a8c59750SOverMighty     // MPFR result as MPFR can still produce valid results because of its
60a8c59750SOverMighty     // wider precision.
61*f8f5b175SNhat Nguyen     if (FPBits(result).is_nan() || FPBits(result).is_inf() ||
62*f8f5b175SNhat Nguyen         LIBC_NAMESPACE::libc_errno != 0)
63a8c59750SOverMighty       continue;
64a8c59750SOverMighty     ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Exp2m1, x,
65a8c59750SOverMighty                                    LIBC_NAMESPACE::exp2m1f(x), 0.5);
66a8c59750SOverMighty   }
67a8c59750SOverMighty }
68