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-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "src/math/acos.h" |
10 | #include "test/UnitTest/FPMatcher.h" |
11 | #include "test/UnitTest/Test.h" |
12 | #include "utils/MPFRWrapper/MPFRUtils.h" |
13 | |
14 | using LlvmLibcAcosTest = LIBC_NAMESPACE::testing::FPTest<double>; |
15 | |
16 | namespace mpfr = LIBC_NAMESPACE::testing::mpfr; |
17 | |
18 | using LIBC_NAMESPACE::testing::tlog; |
19 | |
20 | TEST_F(LlvmLibcAcosTest, 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_inf_or_nan()) |
40 | continue; |
41 | double result = LIBC_NAMESPACE::acos(x); |
42 | ++cc; |
43 | if (FPBits(result).is_inf_or_nan()) |
44 | continue; |
45 | |
46 | ++count; |
47 | |
48 | if (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(mpfr::Operation::Acos, x, result, |
49 | 0.5, rounding_mode)) { |
50 | ++fails; |
51 | while (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(mpfr::Operation::Acos, x, |
52 | result, tol, rounding_mode)) { |
53 | mx = x; |
54 | mr = result; |
55 | |
56 | if (tol > 1000.0) |
57 | break; |
58 | |
59 | tol *= 2.0; |
60 | } |
61 | } |
62 | } |
63 | if (fails) { |
64 | tlog << " Acos failed: " << fails << "/" << count << "/" << cc |
65 | << " tests.\n" ; |
66 | tlog << " Max ULPs is at most: " << static_cast<uint64_t>(tol) << ".\n" ; |
67 | EXPECT_MPFR_MATCH(mpfr::Operation::Acos, mx, mr, 0.5, rounding_mode); |
68 | } |
69 | }; |
70 | |
71 | tlog << " Test Rounding To Nearest...\n" ; |
72 | test(mpfr::RoundingMode::Nearest); |
73 | |
74 | tlog << " Test Rounding Downward...\n" ; |
75 | test(mpfr::RoundingMode::Downward); |
76 | |
77 | tlog << " Test Rounding Upward...\n" ; |
78 | test(mpfr::RoundingMode::Upward); |
79 | |
80 | tlog << " Test Rounding Toward Zero...\n" ; |
81 | test(mpfr::RoundingMode::TowardZero); |
82 | } |
83 | |