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