90 lines · cpp
1//===-- Unittests for acos ------------------------------------------------===//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 "src/__support/macros/optimization.h"10#include "src/math/acos.h"11#include "test/UnitTest/FPMatcher.h"12#include "test/UnitTest/Test.h"13#include "utils/MPFRWrapper/MPFRUtils.h"14 15#ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS16#define TOLERANCE 817#else18#define TOLERANCE 019#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS20 21using LlvmLibcAcosTest = LIBC_NAMESPACE::testing::FPTest<double>;22 23namespace mpfr = LIBC_NAMESPACE::testing::mpfr;24 25using LIBC_NAMESPACE::testing::tlog;26 27TEST_F(LlvmLibcAcosTest, InDoubleRange) {28 constexpr uint64_t COUNT = 123'451;29 uint64_t START = FPBits(0x1.0p-60).uintval();30 uint64_t STOP = FPBits(1.0).uintval();31 uint64_t STEP = (STOP - START) / COUNT;32 33 auto test = [&](mpfr::RoundingMode rounding_mode) {34 mpfr::ForceRoundingMode __r(rounding_mode);35 if (!__r.success)36 return;37 38 uint64_t fails = 0;39 uint64_t count = 0;40 uint64_t cc = 0;41 double mx = 0.0, mr = 0.0;42 double tol = 0.5;43 44 for (uint64_t i = 0, v = START; i <= COUNT; ++i, v += STEP) {45 double x = FPBits(v).get_val();46 if (FPBits(v).is_inf_or_nan())47 continue;48 double result = LIBC_NAMESPACE::acos(x);49 ++cc;50 if (FPBits(result).is_inf_or_nan())51 continue;52 53 ++count;54 55 if (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(mpfr::Operation::Acos, x, result,56 TOLERANCE + 0.5, rounding_mode)) {57 ++fails;58 while (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(mpfr::Operation::Acos, x,59 result, tol, rounding_mode)) {60 mx = x;61 mr = result;62 63 if (tol > 1000.0)64 break;65 66 tol *= 2.0;67 }68 }69 }70 if (fails) {71 tlog << " Acos failed: " << fails << "/" << count << "/" << cc72 << " tests.\n";73 tlog << " Max ULPs is at most: " << static_cast<uint64_t>(tol) << ".\n";74 EXPECT_MPFR_MATCH(mpfr::Operation::Acos, mx, mr, 0.5, rounding_mode);75 }76 };77 78 tlog << " Test Rounding To Nearest...\n";79 test(mpfr::RoundingMode::Nearest);80 81 tlog << " Test Rounding Downward...\n";82 test(mpfr::RoundingMode::Downward);83 84 tlog << " Test Rounding Upward...\n";85 test(mpfr::RoundingMode::Upward);86 87 tlog << " Test Rounding Toward Zero...\n";88 test(mpfr::RoundingMode::TowardZero);89}90