xref: /llvm-project/libc/test/src/math/log10f_test.cpp (revision 46944b0cbc9a9d8daad0182c40fcd3560bc9ca35)
1e581841eSTue Ly //===-- Unittests for log10f ----------------------------------------------===//
2e581841eSTue Ly //
3e581841eSTue Ly // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e581841eSTue Ly // See https://llvm.org/LICENSE.txt for license information.
5e581841eSTue Ly // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e581841eSTue Ly //
7e581841eSTue Ly //===----------------------------------------------------------------------===//
8e581841eSTue Ly 
9*5748ad84Slntue #include "hdr/math_macros.h"
10e581841eSTue Ly #include "src/__support/FPUtil/FPBits.h"
11e581841eSTue Ly #include "src/math/log10f.h"
12af1315c2SSiva Chandra Reddy #include "test/UnitTest/FPMatcher.h"
13af1315c2SSiva Chandra Reddy #include "test/UnitTest/Test.h"
14e581841eSTue Ly #include "utils/MPFRWrapper/MPFRUtils.h"
15e581841eSTue Ly 
16e581841eSTue Ly #include <stdint.h>
17e581841eSTue Ly 
183fd5113cSlntue using LlvmLibcLog10fTest = LIBC_NAMESPACE::testing::FPTest<float>;
193fd5113cSlntue 
20b6bc9d72SGuillaume Chatelet namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
21e581841eSTue Ly 
223fd5113cSlntue TEST_F(LlvmLibcLog10fTest, SpecialNumbers) {
23b6bc9d72SGuillaume Chatelet   EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::log10f(aNaN));
24b6bc9d72SGuillaume Chatelet   EXPECT_FP_EQ(inf, LIBC_NAMESPACE::log10f(inf));
25b6bc9d72SGuillaume Chatelet   EXPECT_FP_IS_NAN_WITH_EXCEPTION(LIBC_NAMESPACE::log10f(neg_inf), FE_INVALID);
26b6bc9d72SGuillaume Chatelet   EXPECT_FP_EQ_WITH_EXCEPTION(neg_inf, LIBC_NAMESPACE::log10f(0.0f),
27ae2d8b49STue Ly                               FE_DIVBYZERO);
28b6bc9d72SGuillaume Chatelet   EXPECT_FP_EQ_WITH_EXCEPTION(neg_inf, LIBC_NAMESPACE::log10f(-0.0f),
29b6bc9d72SGuillaume Chatelet                               FE_DIVBYZERO);
30b6bc9d72SGuillaume Chatelet   EXPECT_FP_IS_NAN_WITH_EXCEPTION(LIBC_NAMESPACE::log10f(-1.0f), FE_INVALID);
31b6bc9d72SGuillaume Chatelet   EXPECT_FP_EQ_ALL_ROUNDING(zero, LIBC_NAMESPACE::log10f(1.0f));
32e581841eSTue Ly }
33e581841eSTue Ly 
343fd5113cSlntue TEST_F(LlvmLibcLog10fTest, TrickyInputs) {
359af8dca7STue Ly   constexpr int N = 21;
36e581841eSTue Ly   constexpr uint32_t INPUTS[N] = {
379af8dca7STue Ly       0x3f800000U /*1.0f*/,
38e581841eSTue Ly       0x41200000U /*10.0f*/,
39e581841eSTue Ly       0x42c80000U /*100.0f*/,
40e581841eSTue Ly       0x447a0000U /*1,000.0f*/,
41e581841eSTue Ly       0x461c4000U /*10,000.0f*/,
42e581841eSTue Ly       0x47c35000U /*100,000.0f*/,
43e581841eSTue Ly       0x49742400U /*1,000,000.0f*/,
44e581841eSTue Ly       0x4b189680U /*10,000,000.0f*/,
45e581841eSTue Ly       0x4cbebc20U /*100,000,000.0f*/,
46e581841eSTue Ly       0x4e6e6b28U /*1,000,000,000.0f*/,
47e581841eSTue Ly       0x501502f9U /*10,000,000,000.0f*/,
48e581841eSTue Ly       0x4f134f83U /*2471461632.0f*/,
49ae2d8b49STue Ly       0x7956ba5eU /*69683218960000541503257137270226944.0f*/,
50ae2d8b49STue Ly       0x08ae'a356U /*0x1.5d46acp-110f*/,
51ae2d8b49STue Ly       0x1c7d'a337U /*0x1.fb466ep-71f*/,
52ae2d8b49STue Ly       0x69c8'c583U /*0x1.918b06p+84f*/,
539af8dca7STue Ly       0x0efe'ee7aU /*0x1.fddcf4p-98f*/,
549af8dca7STue Ly       0x3f5f'de1bU /*0x1.bfbc36p-1f*/,
559af8dca7STue Ly       0x3f80'70d8U /*0x1.00e1bp0f*/,
569af8dca7STue Ly       0x120b'93dcU /*0x1.1727b8p-91f*/,
579af8dca7STue Ly       0x13ae'78d3U /*0x1.5cf1a6p-88f*/,
58ae2d8b49STue Ly   };
59e581841eSTue Ly 
60e581841eSTue Ly   for (int i = 0; i < N; ++i) {
612856db0dSGuillaume Chatelet     float x = FPBits(INPUTS[i]).get_val();
62e581841eSTue Ly     EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Log10, x,
63b6bc9d72SGuillaume Chatelet                                    LIBC_NAMESPACE::log10f(x), 0.5);
64e581841eSTue Ly   }
65e581841eSTue Ly }
66e581841eSTue Ly 
673fd5113cSlntue TEST_F(LlvmLibcLog10fTest, InFloatRange) {
68ae5c4724SGuillaume Chatelet   constexpr uint32_t COUNT = 100'000;
69e581841eSTue Ly   constexpr uint32_t STEP = UINT32_MAX / COUNT;
70e581841eSTue Ly   for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
712856db0dSGuillaume Chatelet     float x = FPBits(v).get_val();
72e581841eSTue Ly     ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Log10, x,
73b6bc9d72SGuillaume Chatelet                                    LIBC_NAMESPACE::log10f(x), 0.5);
74e581841eSTue Ly   }
75e581841eSTue Ly }
76