xref: /llvm-project/libc/test/src/math/explogxf_test.cpp (revision f8f5b17564cb839101ba99390bcecc564de57e65)
177e1d9beSKirill Okhotnikov //===-- Unittests for supfuncf --------------------------------------------===//
289ed5b7cSKirill Okhotnikov //
389ed5b7cSKirill Okhotnikov // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
489ed5b7cSKirill Okhotnikov // See https://llvm.org/LICENSE.txt for license information.
589ed5b7cSKirill Okhotnikov // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
689ed5b7cSKirill Okhotnikov //
789ed5b7cSKirill Okhotnikov //===----------------------------------------------------------------------===//
889ed5b7cSKirill Okhotnikov 
95748ad84Slntue #include "hdr/math_macros.h"
1089ed5b7cSKirill Okhotnikov #include "in_float_range_test_helper.h"
1189ed5b7cSKirill Okhotnikov #include "src/__support/FPUtil/FPBits.h"
125d56b348SMichael Jones #include "src/math/fabs.h"
135d56b348SMichael Jones #include "src/math/fabsf.h"
1489ed5b7cSKirill Okhotnikov #include "src/math/generic/explogxf.h"
15af1315c2SSiva Chandra Reddy #include "test/UnitTest/FPMatcher.h"
16af1315c2SSiva Chandra Reddy #include "test/UnitTest/Test.h"
1789ed5b7cSKirill Okhotnikov #include "utils/MPFRWrapper/MPFRUtils.h"
1889ed5b7cSKirill Okhotnikov 
193fd5113cSlntue using LlvmLibcExplogfTest = LIBC_NAMESPACE::testing::FPTest<float>;
20*f8f5b175SNhat Nguyen using FPBits = LIBC_NAMESPACE::fputil::FPBits<float>;
2189ed5b7cSKirill Okhotnikov 
223fd5113cSlntue namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
2389ed5b7cSKirill Okhotnikov 
2489ed5b7cSKirill Okhotnikov constexpr int def_count = 100003;
2589ed5b7cSKirill Okhotnikov constexpr float def_prec = 0.500001f;
2689ed5b7cSKirill Okhotnikov 
276c75240dSKirill Okhotnikov auto f_normal = [](float x) -> bool {
28*f8f5b175SNhat Nguyen   return !(FPBits(x).is_nan() || FPBits(x).is_inf() ||
29*f8f5b175SNhat Nguyen            LIBC_NAMESPACE::fabs(x) < 2E-38);
306c75240dSKirill Okhotnikov };
316c75240dSKirill Okhotnikov 
323fd5113cSlntue TEST_F(LlvmLibcExplogfTest, ExpInFloatRange) {
3389ed5b7cSKirill Okhotnikov   auto fx = [](float x) -> float {
34b6bc9d72SGuillaume Chatelet     auto result = LIBC_NAMESPACE::exp_b_range_reduc<LIBC_NAMESPACE::ExpBase>(x);
35b6bc9d72SGuillaume Chatelet     double r = LIBC_NAMESPACE::ExpBase::powb_lo(result.lo);
364973eee1STue Ly     return static_cast<float>(result.mh * r);
3789ed5b7cSKirill Okhotnikov   };
3889ed5b7cSKirill Okhotnikov   auto f_check = [](float x) -> bool {
39*f8f5b175SNhat Nguyen     return !((FPBits(x).is_nan() || FPBits(x).is_inf() || x < -70 || x > 70 ||
405d56b348SMichael Jones               LIBC_NAMESPACE::fabsf(x) < 0x1.0p-10));
4189ed5b7cSKirill Okhotnikov   };
4289ed5b7cSKirill Okhotnikov   CHECK_DATA(0.0f, neg_inf, mpfr::Operation::Exp, fx, f_check, def_count,
4389ed5b7cSKirill Okhotnikov              def_prec);
4489ed5b7cSKirill Okhotnikov }
4589ed5b7cSKirill Okhotnikov 
463fd5113cSlntue TEST_F(LlvmLibcExplogfTest, Log2InFloatRange) {
47b6bc9d72SGuillaume Chatelet   CHECK_DATA(0.0f, inf, mpfr::Operation::Log2, LIBC_NAMESPACE::log2_eval,
48b6bc9d72SGuillaume Chatelet              f_normal, def_count, def_prec);
4989ed5b7cSKirill Okhotnikov }
5089ed5b7cSKirill Okhotnikov 
513fd5113cSlntue TEST_F(LlvmLibcExplogfTest, LogInFloatRange) {
52b6bc9d72SGuillaume Chatelet   CHECK_DATA(0.0f, inf, mpfr::Operation::Log, LIBC_NAMESPACE::log_eval,
53b6bc9d72SGuillaume Chatelet              f_normal, def_count, def_prec);
5489ed5b7cSKirill Okhotnikov }
55