xref: /llvm-project/libc/test/src/math/log2f_test.cpp (revision f8f5b17564cb839101ba99390bcecc564de57e65)
163d2df00STue Ly //===-- Unittests for log2f -----------------------------------------------===//
263d2df00STue Ly //
363d2df00STue Ly // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
463d2df00STue Ly // See https://llvm.org/LICENSE.txt for license information.
563d2df00STue Ly // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
663d2df00STue Ly //
763d2df00STue Ly //===----------------------------------------------------------------------===//
863d2df00STue Ly 
95748ad84Slntue #include "hdr/math_macros.h"
1063d2df00STue Ly #include "src/__support/FPUtil/FPBits.h"
1131c39439STue Ly #include "src/errno/libc_errno.h"
1263d2df00STue Ly #include "src/math/log2f.h"
13af1315c2SSiva Chandra Reddy #include "test/UnitTest/FPMatcher.h"
14af1315c2SSiva Chandra Reddy #include "test/UnitTest/Test.h"
1563d2df00STue Ly #include "utils/MPFRWrapper/MPFRUtils.h"
1663d2df00STue Ly 
1763d2df00STue Ly #include <stdint.h>
1863d2df00STue Ly 
193fd5113cSlntue using LlvmLibcLog2fTest = LIBC_NAMESPACE::testing::FPTest<float>;
203fd5113cSlntue 
21b6bc9d72SGuillaume Chatelet namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
2263d2df00STue Ly 
233fd5113cSlntue TEST_F(LlvmLibcLog2fTest, SpecialNumbers) {
24b6bc9d72SGuillaume Chatelet   EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::log2f(aNaN));
25b6bc9d72SGuillaume Chatelet   EXPECT_FP_EQ(inf, LIBC_NAMESPACE::log2f(inf));
26b6bc9d72SGuillaume Chatelet   EXPECT_FP_IS_NAN_WITH_EXCEPTION(LIBC_NAMESPACE::log2f(neg_inf), FE_INVALID);
27b6bc9d72SGuillaume Chatelet   EXPECT_FP_EQ_WITH_EXCEPTION(neg_inf, LIBC_NAMESPACE::log2f(0.0f),
28b6bc9d72SGuillaume Chatelet                               FE_DIVBYZERO);
29b6bc9d72SGuillaume Chatelet   EXPECT_FP_EQ_WITH_EXCEPTION(neg_inf, LIBC_NAMESPACE::log2f(-0.0f),
30b6bc9d72SGuillaume Chatelet                               FE_DIVBYZERO);
31b6bc9d72SGuillaume Chatelet   EXPECT_FP_IS_NAN_WITH_EXCEPTION(LIBC_NAMESPACE::log2f(-1.0f), FE_INVALID);
32b6bc9d72SGuillaume Chatelet   EXPECT_FP_EQ_ALL_ROUNDING(zero, LIBC_NAMESPACE::log2f(1.0f));
3363d2df00STue Ly }
3463d2df00STue Ly 
353fd5113cSlntue TEST_F(LlvmLibcLog2fTest, TrickyInputs) {
361f3f90abSTue Ly   constexpr int N = 10;
371f3f90abSTue Ly   constexpr uint32_t INPUTS[N] = {
381f3f90abSTue Ly       0x3f7d57f5U, 0x3f7e3274U, 0x3f7ed848U, 0x3f7fd6ccU, 0x3f7fffffU,
391f3f90abSTue Ly       0x3f80079bU, 0x3f81d0b5U, 0x3f82e602U, 0x3f83c98dU, 0x3f8cba39U};
4063d2df00STue Ly 
4163d2df00STue Ly   for (int i = 0; i < N; ++i) {
422856db0dSGuillaume Chatelet     float x = FPBits(INPUTS[i]).get_val();
4363d2df00STue Ly     EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Log2, x,
44b6bc9d72SGuillaume Chatelet                                    LIBC_NAMESPACE::log2f(x), 0.5);
4563d2df00STue Ly   }
4663d2df00STue Ly }
4763d2df00STue Ly 
483fd5113cSlntue TEST_F(LlvmLibcLog2fTest, InFloatRange) {
49ae5c4724SGuillaume Chatelet   constexpr uint32_t COUNT = 100'000;
5063d2df00STue Ly   constexpr uint32_t STEP = UINT32_MAX / COUNT;
5163d2df00STue Ly   for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
522856db0dSGuillaume Chatelet     float x = FPBits(v).get_val();
53*f8f5b175SNhat Nguyen     if (FPBits(v).is_nan() || FPBits(v).is_inf())
5463d2df00STue Ly       continue;
553eb1e6d8Smichaelrj-google     LIBC_NAMESPACE::libc_errno = 0;
56b6bc9d72SGuillaume Chatelet     float result = LIBC_NAMESPACE::log2f(x);
5763d2df00STue Ly     // If the computation resulted in an error or did not produce valid result
5863d2df00STue Ly     // in the single-precision floating point range, then ignore comparing with
5963d2df00STue Ly     // MPFR result as MPFR can still produce valid results because of its
6063d2df00STue Ly     // wider precision.
61*f8f5b175SNhat Nguyen     if (FPBits(result).is_nan() || FPBits(result).is_inf() ||
62*f8f5b175SNhat Nguyen         LIBC_NAMESPACE::libc_errno != 0)
6363d2df00STue Ly       continue;
6463d2df00STue Ly     ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Log2, x,
65b6bc9d72SGuillaume Chatelet                                    LIBC_NAMESPACE::log2f(x), 0.5);
6663d2df00STue Ly   }
6763d2df00STue Ly }
68