1 //===-- Unittests for logf-----------------------------------------------===//
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/logf.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(LlvmLibcLogfTest, SpecialNumbers) {
24   EXPECT_FP_EQ(aNaN, __llvm_libc::logf(aNaN));
25   EXPECT_FP_EQ(inf, __llvm_libc::logf(inf));
26   EXPECT_TRUE(FPBits((__llvm_libc::logf(neg_inf))).is_nan());
27   EXPECT_FP_EQ(neg_inf, __llvm_libc::logf(0.0f));
28   EXPECT_FP_EQ(neg_inf, __llvm_libc::logf(-0.0f));
29   EXPECT_TRUE(FPBits(__llvm_libc::logf(-1.0f)).is_nan());
30   EXPECT_FP_EQ(zero, __llvm_libc::logf(1.0f));
31 }
32 
33 TEST(LlvmLibcLogfTest, TrickyInputs) {
34   constexpr int N = 24;
35   constexpr uint32_t INPUTS[N] = {
36       0x3509dcf6U, 0x3bf86ef0U, 0x3ca1c99fU, 0x3d13e105U, 0x3f7ff1f2U,
37       0x3f7fffffU, 0x3f800006U, 0x3f800014U, 0x3f80001cU, 0x3f80c777U,
38       0x3f80ce72U, 0x3f80d19fU, 0x3f80f7bfU, 0x3f80fcfeU, 0x3f81feb4U,
39       0x3f83d731U, 0x3f90cb1dU, 0x3fc55379U, 0x3fd364d7U, 0x41178febU,
40       0x4c5d65a5U, 0x4e85f412U, 0x65d890d3U, 0x6f31a8ecU};
41   for (int i = 0; i < N; ++i) {
42     float x = float(FPBits(INPUTS[i]));
43     EXPECT_MPFR_MATCH(mpfr::Operation::Log, x, __llvm_libc::logf(x), 0.5);
44   }
45 }
46 
47 TEST(LlvmLibcLogfTest, InFloatRange) {
48   constexpr uint32_t COUNT = 1000000;
49   constexpr uint32_t STEP = UINT32_MAX / COUNT;
50   for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
51     float x = float(FPBits(v));
52     if (isnan(x) || isinf(x))
53       continue;
54     errno = 0;
55     float result = __llvm_libc::logf(x);
56     // If the computation resulted in an error or did not produce valid result
57     // in the single-precision floating point range, then ignore comparing with
58     // MPFR result as MPFR can still produce valid results because of its
59     // wider precision.
60     if (isnan(result) || isinf(result) || errno != 0)
61       continue;
62     ASSERT_MPFR_MATCH(mpfr::Operation::Log, x, __llvm_libc::logf(x), 0.5);
63   }
64 }
65