17fc9fb9fSlntue //===-- Unittests for cbrt ------------------------------------------------===// 27fc9fb9fSlntue // 37fc9fb9fSlntue // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 47fc9fb9fSlntue // See https://llvm.org/LICENSE.txt for license information. 57fc9fb9fSlntue // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 67fc9fb9fSlntue // 77fc9fb9fSlntue //===----------------------------------------------------------------------===// 87fc9fb9fSlntue 97fc9fb9fSlntue #include "hdr/math_macros.h" 107fc9fb9fSlntue #include "src/__support/FPUtil/FPBits.h" 117fc9fb9fSlntue #include "src/math/cbrt.h" 127fc9fb9fSlntue #include "test/UnitTest/FPMatcher.h" 137fc9fb9fSlntue #include "test/UnitTest/Test.h" 147fc9fb9fSlntue #include "utils/MPFRWrapper/MPFRUtils.h" 157fc9fb9fSlntue 167fc9fb9fSlntue using LlvmLibcCbrtTest = LIBC_NAMESPACE::testing::FPTest<double>; 177fc9fb9fSlntue 187fc9fb9fSlntue namespace mpfr = LIBC_NAMESPACE::testing::mpfr; 197fc9fb9fSlntue 207fc9fb9fSlntue using LIBC_NAMESPACE::testing::tlog; 217fc9fb9fSlntue 227fc9fb9fSlntue TEST_F(LlvmLibcCbrtTest, InDoubleRange) { 237fc9fb9fSlntue constexpr uint64_t COUNT = 123'451; 24dfdef2cbSlntue uint64_t START = FPBits(1.0).uintval(); 25dfdef2cbSlntue uint64_t STOP = FPBits(8.0).uintval(); 267fc9fb9fSlntue uint64_t STEP = (STOP - START) / COUNT; 277fc9fb9fSlntue 287fc9fb9fSlntue auto test = [&](mpfr::RoundingMode rounding_mode) { 297fc9fb9fSlntue mpfr::ForceRoundingMode force_rounding(rounding_mode); 307fc9fb9fSlntue if (!force_rounding.success) 317fc9fb9fSlntue return; 327fc9fb9fSlntue 337fc9fb9fSlntue uint64_t fails = 0; 347fc9fb9fSlntue uint64_t tested = 0; 357fc9fb9fSlntue uint64_t total = 0; 367fc9fb9fSlntue double worst_input, worst_output = 0.0; 377fc9fb9fSlntue double ulp = 0.5; 387fc9fb9fSlntue 397fc9fb9fSlntue for (uint64_t i = 0, v = START; i <= COUNT; ++i, v += STEP) { 407fc9fb9fSlntue double x = FPBits(v).get_val(); 41dfdef2cbSlntue if (FPBits(x).is_inf_or_nan()) 427fc9fb9fSlntue continue; 437fc9fb9fSlntue 447fc9fb9fSlntue double result = LIBC_NAMESPACE::cbrt(x); 457fc9fb9fSlntue ++total; 46dfdef2cbSlntue if (FPBits(result).is_inf_or_nan()) 477fc9fb9fSlntue continue; 487fc9fb9fSlntue 497fc9fb9fSlntue ++tested; 507fc9fb9fSlntue 517fc9fb9fSlntue if (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(mpfr::Operation::Cbrt, x, result, 527fc9fb9fSlntue 0.5, rounding_mode)) { 537fc9fb9fSlntue ++fails; 547fc9fb9fSlntue while (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(mpfr::Operation::Cbrt, x, 557fc9fb9fSlntue result, ulp, rounding_mode)) { 567fc9fb9fSlntue worst_input = x; 577fc9fb9fSlntue worst_output = result; 587fc9fb9fSlntue 597fc9fb9fSlntue if (ulp > 1000.0) 607fc9fb9fSlntue break; 617fc9fb9fSlntue 627fc9fb9fSlntue ulp *= 2.0; 637fc9fb9fSlntue } 647fc9fb9fSlntue } 657fc9fb9fSlntue } 667fc9fb9fSlntue if (fails) { 677fc9fb9fSlntue tlog << " Cbrt failed: " << fails << "/" << tested << "/" << total 687fc9fb9fSlntue << " tests.\n"; 697fc9fb9fSlntue tlog << " Max ULPs is at most: " << static_cast<uint64_t>(ulp) << ".\n"; 707fc9fb9fSlntue EXPECT_MPFR_MATCH(mpfr::Operation::Cbrt, worst_input, worst_output, 0.5, 717fc9fb9fSlntue rounding_mode); 727fc9fb9fSlntue } 737fc9fb9fSlntue }; 747fc9fb9fSlntue 757fc9fb9fSlntue tlog << " Test Rounding To Nearest...\n"; 767fc9fb9fSlntue test(mpfr::RoundingMode::Nearest); 777fc9fb9fSlntue 787fc9fb9fSlntue tlog << " Test Rounding Downward...\n"; 797fc9fb9fSlntue test(mpfr::RoundingMode::Downward); 807fc9fb9fSlntue 817fc9fb9fSlntue tlog << " Test Rounding Upward...\n"; 827fc9fb9fSlntue test(mpfr::RoundingMode::Upward); 837fc9fb9fSlntue 847fc9fb9fSlntue tlog << " Test Rounding Toward Zero...\n"; 857fc9fb9fSlntue test(mpfr::RoundingMode::TowardZero); 867fc9fb9fSlntue } 877fc9fb9fSlntue 887fc9fb9fSlntue TEST_F(LlvmLibcCbrtTest, SpecialValues) { 897fc9fb9fSlntue constexpr double INPUTS[] = { 90*a8790734Slntue 0x1.4f61672324c8p-1028, -0x1.fffffffffffffp-1021, 0x1.00152f57068b7p-1, 91*a8790734Slntue 0x1.006509cda9886p-1, 0x1.018369b92e523p-1, 0x1.10af932ef2bf9p-1, 92*a8790734Slntue 0x1.1a41117939fdbp-1, 0x1.2ae8076520d9ap-1, 0x1.a202bfc89ddffp-1, 93*a8790734Slntue 0x1.a6bb8c803147bp-1, 0x1.000197b499b1bp+0, 0x1.00065ed266c6cp+0, 94*a8790734Slntue 0x1.d4306c202c4c2p+0, 0x1.8fd409efe4851p+1, 0x1.95fd0eb31cc4p+1, 95*a8790734Slntue 0x1.7cef1d276e335p+2, 0x1.94910c4fc98p+2, 0x1.a0cc1327bb4c4p+2, 96*a8790734Slntue 0x1.e7d6ebed549c4p+2, 977fc9fb9fSlntue }; 987fc9fb9fSlntue for (double v : INPUTS) { 997fc9fb9fSlntue double x = FPBits(v).get_val(); 1007fc9fb9fSlntue ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Cbrt, x, 1017fc9fb9fSlntue LIBC_NAMESPACE::cbrt(x), 0.5); 1027fc9fb9fSlntue ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Cbrt, -x, 1037fc9fb9fSlntue LIBC_NAMESPACE::cbrt(-x), 0.5); 1047fc9fb9fSlntue } 1057fc9fb9fSlntue } 106