1 //===-- Unittests for log10f ----------------------------------------------===//
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/log10f.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(LlvmLibcLog10fTest,SpecialNumbers)23 TEST(LlvmLibcLog10fTest, SpecialNumbers) {
24   EXPECT_FP_EQ(aNaN, __llvm_libc::log10f(aNaN));
25   EXPECT_FP_EQ(inf, __llvm_libc::log10f(inf));
26   EXPECT_TRUE(FPBits(__llvm_libc::log10f(neg_inf)).is_nan());
27   EXPECT_FP_EQ(neg_inf, __llvm_libc::log10f(0.0f));
28   EXPECT_FP_EQ(neg_inf, __llvm_libc::log10f(-0.0f));
29   EXPECT_TRUE(FPBits(__llvm_libc::log10f(-1.0f)).is_nan());
30   EXPECT_FP_EQ(zero, __llvm_libc::log10f(1.0f));
31 }
32 
TEST(LlvmLibcLog10fTest,TrickyInputs)33 TEST(LlvmLibcLog10fTest, TrickyInputs) {
34   constexpr int N = 12;
35   constexpr uint32_t INPUTS[N] = {
36       0x41200000U /*10.0f*/,
37       0x42c80000U /*100.0f*/,
38       0x447a0000U /*1,000.0f*/,
39       0x461c4000U /*10,000.0f*/,
40       0x47c35000U /*100,000.0f*/,
41       0x49742400U /*1,000,000.0f*/,
42       0x4b189680U /*10,000,000.0f*/,
43       0x4cbebc20U /*100,000,000.0f*/,
44       0x4e6e6b28U /*1,000,000,000.0f*/,
45       0x501502f9U /*10,000,000,000.0f*/,
46       0x4f134f83U /*2471461632.0f*/,
47       0x7956ba5eU /*69683218960000541503257137270226944.0f*/};
48 
49   for (int i = 0; i < N; ++i) {
50     float x = float(FPBits(INPUTS[i]));
51     EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Log10, x,
52                                    __llvm_libc::log10f(x), 0.5);
53   }
54 }
55 
TEST(LlvmLibcLog10fTest,InFloatRange)56 TEST(LlvmLibcLog10fTest, InFloatRange) {
57   constexpr uint32_t COUNT = 1000000;
58   constexpr uint32_t STEP = UINT32_MAX / COUNT;
59   for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
60     float x = float(FPBits(v));
61     if (isnan(x) || isinf(x))
62       continue;
63     errno = 0;
64     float result = __llvm_libc::log10f(x);
65     // If the computation resulted in an error or did not produce valid result
66     // in the single-precision floating point range, then ignore comparing with
67     // MPFR result as MPFR can still produce valid results because of its
68     // wider precision.
69     if (isnan(result) || isinf(result) || errno != 0)
70       continue;
71     ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Log10, x,
72                                    __llvm_libc::log10f(x), 0.5);
73   }
74 }
75