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