1 //===-- Unittests for log1pf ----------------------------------------------===//
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/log1pf.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
TEST(LlvmLibclog1pfTest,SpecialNumbers)23 TEST(LlvmLibclog1pfTest, SpecialNumbers) {
24 EXPECT_FP_EQ(aNaN, __llvm_libc::log1pf(aNaN));
25 EXPECT_FP_EQ(inf, __llvm_libc::log1pf(inf));
26 EXPECT_TRUE(FPBits((__llvm_libc::log1pf(neg_inf))).is_nan());
27 EXPECT_FP_EQ(zero, __llvm_libc::log1pf(0.0f));
28 EXPECT_FP_EQ(neg_zero, __llvm_libc::log1pf(-0.0f));
29 EXPECT_FP_EQ(neg_inf, __llvm_libc::log1pf(-1.0f));
30 }
31
TEST(LlvmLibclog1pfTest,TrickyInputs)32 TEST(LlvmLibclog1pfTest, TrickyInputs) {
33 constexpr int N = 20;
34 constexpr uint32_t INPUTS[N] = {
35 0x35c00006U, /*0x1.80000cp-20f*/
36 0x35400003U, /*0x1.800006p-21f*/
37 0x3640000cU, /*0x1.800018p-19f*/
38 0x36c00018U, /*0x1.80003p-18f*/
39 0x3710001bU, /*0x1.200036p-17f*/
40 0x37400030U, /*0x1.80006p-17f*/
41 0x3770004bU, /*0x1.e00096p-17f*/
42 0x3b9315c8U, /*0x1.262b9p-8f*/
43 0x3c6eb7afU, /*0x1.dd6f5ep-7f*/
44 0x41078febU, /*0x1.0f1fd6p+3f*/
45 0x5cd69e88U, /*0x1.ad3d1p+58f*/
46 0x65d890d3U, /*0x1.b121a6p+76f*/
47 0x6f31a8ecU, /*0x1.6351d8p+95f*/
48 0x7a17f30aU, /*0x1.2fe614p+117f*/
49 0xb53ffffdU, /*-0x1.7ffffap-21f*/
50 0xb70fffe5U, /*-0x1.1fffcap-17f*/
51 0xbb0ec8c4U, /*-0x1.1d9188p-9f*/
52 0xbc4d092cU, /*-0x1.9a1258p-7f*/
53 0xbc657728U, /*-0x1.caee5p-7f*/
54 0xbd1d20afU, /*-0x1.3a415ep-5f*/
55 };
56 for (int i = 0; i < N; ++i) {
57 float x = float(FPBits(INPUTS[i]));
58 EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Log1p, x,
59 __llvm_libc::log1pf(x), 0.5);
60 }
61 }
62
TEST(LlvmLibclog1pfTest,InFloatRange)63 TEST(LlvmLibclog1pfTest, InFloatRange) {
64 constexpr uint32_t COUNT = 1000000;
65 constexpr uint32_t STEP = UINT32_MAX / COUNT;
66 for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
67 float x = float(FPBits(v));
68 if (isnan(x) || isinf(x))
69 continue;
70 errno = 0;
71 float result = __llvm_libc::log1pf(x);
72 // If the computation resulted in an error or did not produce valid result
73 // in the single-precision floating point range, then ignore comparing with
74 // MPFR result as MPFR can still produce valid results because of its
75 // wider precision.
76 if (isnan(result) || isinf(result) || errno != 0)
77 continue;
78 ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Log1p, x,
79 __llvm_libc::log1pf(x), 0.5);
80 }
81 }
82