113 lines · cpp
1//===-- Unittests for cbrt ------------------------------------------------===//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-exception6//7//===----------------------------------------------------------------------===//8 9#include "hdr/math_macros.h"10#include "src/__support/FPUtil/FPBits.h"11#include "src/__support/macros/optimization.h"12#include "src/math/cbrt.h"13#include "test/UnitTest/FPMatcher.h"14#include "test/UnitTest/Test.h"15#include "utils/MPFRWrapper/MPFRUtils.h"16 17#ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS18#define TOLERANCE 119#else20#define TOLERANCE 021#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS22 23using LlvmLibcCbrtTest = LIBC_NAMESPACE::testing::FPTest<double>;24 25namespace mpfr = LIBC_NAMESPACE::testing::mpfr;26 27using LIBC_NAMESPACE::testing::tlog;28 29TEST_F(LlvmLibcCbrtTest, InDoubleRange) {30 constexpr uint64_t COUNT = 123'451;31 uint64_t START = FPBits(1.0).uintval();32 uint64_t STOP = FPBits(8.0).uintval();33 uint64_t STEP = (STOP - START) / COUNT;34 35 auto test = [&](mpfr::RoundingMode rounding_mode) {36 mpfr::ForceRoundingMode force_rounding(rounding_mode);37 if (!force_rounding.success)38 return;39 40 uint64_t fails = 0;41 uint64_t tested = 0;42 uint64_t total = 0;43 double worst_input, worst_output = 0.0;44 double ulp = 0.5;45 46 for (uint64_t i = 0, v = START; i <= COUNT; ++i, v += STEP) {47 double x = FPBits(v).get_val();48 if (FPBits(x).is_inf_or_nan())49 continue;50 51 double result = LIBC_NAMESPACE::cbrt(x);52 ++total;53 if (FPBits(result).is_inf_or_nan())54 continue;55 56 ++tested;57 58 if (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(mpfr::Operation::Cbrt, x, result,59 TOLERANCE + 0.5, rounding_mode)) {60 ++fails;61 while (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(mpfr::Operation::Cbrt, x,62 result, ulp, rounding_mode)) {63 worst_input = x;64 worst_output = result;65 66 if (ulp > 1000.0)67 break;68 69 ulp *= 2.0;70 }71 }72 }73 if (fails) {74 tlog << " Cbrt failed: " << fails << "/" << tested << "/" << total75 << " tests.\n";76 tlog << " Max ULPs is at most: " << static_cast<uint64_t>(ulp) << ".\n";77 EXPECT_MPFR_MATCH(mpfr::Operation::Cbrt, worst_input, worst_output, 0.5,78 rounding_mode);79 }80 };81 82 tlog << " Test Rounding To Nearest...\n";83 test(mpfr::RoundingMode::Nearest);84 85 tlog << " Test Rounding Downward...\n";86 test(mpfr::RoundingMode::Downward);87 88 tlog << " Test Rounding Upward...\n";89 test(mpfr::RoundingMode::Upward);90 91 tlog << " Test Rounding Toward Zero...\n";92 test(mpfr::RoundingMode::TowardZero);93}94 95TEST_F(LlvmLibcCbrtTest, SpecialValues) {96 constexpr double INPUTS[] = {97 0x1.4f61672324c8p-1028, -0x1.fffffffffffffp-1021, 0x1.00152f57068b7p-1,98 0x1.006509cda9886p-1, 0x1.018369b92e523p-1, 0x1.10af932ef2bf9p-1,99 0x1.1a41117939fdbp-1, 0x1.2ae8076520d9ap-1, 0x1.a202bfc89ddffp-1,100 0x1.a6bb8c803147bp-1, 0x1.000197b499b1bp+0, 0x1.00065ed266c6cp+0,101 0x1.d4306c202c4c2p+0, 0x1.8fd409efe4851p+1, 0x1.95fd0eb31cc4p+1,102 0x1.7cef1d276e335p+2, 0x1.94910c4fc98p+2, 0x1.a0cc1327bb4c4p+2,103 0x1.e7d6ebed549c4p+2,104 };105 for (double v : INPUTS) {106 double x = FPBits(v).get_val();107 ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Cbrt, x,108 LIBC_NAMESPACE::cbrt(x), TOLERANCE + 0.5);109 ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Cbrt, -x,110 LIBC_NAMESPACE::cbrt(-x), TOLERANCE + 0.5);111 }112}113