xref: /aosp_15_r20/external/llvm-libc/test/src/math/atan2_test.cpp (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1 //===-- Unittests for atan2 -----------------------------------------------===//
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/atan2.h"
11 #include "test/UnitTest/FPMatcher.h"
12 #include "test/UnitTest/Test.h"
13 #include "utils/MPFRWrapper/MPFRUtils.h"
14 
15 using LlvmLibcAtan2Test = LIBC_NAMESPACE::testing::FPTest<double>;
16 using LIBC_NAMESPACE::testing::tlog;
17 
18 namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
19 
TEST_F(LlvmLibcAtan2Test,TrickyInputs)20 TEST_F(LlvmLibcAtan2Test, TrickyInputs) {
21   mpfr::BinaryInput<double> inputs[] = {
22       {0x1.0853408534085p-2, 0x1.e7b54166c6126p-2},
23       {FPBits::inf().get_val(), 0x0.0000000000001p-1022},
24   };
25 
26   for (mpfr::BinaryInput<double> &input : inputs) {
27     double x = input.x;
28     double y = input.y;
29     mpfr::RoundingMode rm = mpfr::RoundingMode::Downward;
30     mpfr::ForceRoundingMode rr(rm);
31     ASSERT_MPFR_MATCH(mpfr::Operation::Atan2, input,
32                       LIBC_NAMESPACE::atan2(x, y), 0.5, rm);
33     input.x = -input.x;
34     ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Atan2, input,
35                                    LIBC_NAMESPACE::atan2(-x, y), 0.5);
36     input.y = -input.y;
37     ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Atan2, input,
38                                    LIBC_NAMESPACE::atan2(-x, -y), 0.5);
39     input.x = -input.x;
40     ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Atan2, input,
41                                    LIBC_NAMESPACE::atan2(x, -y), 0.5);
42   }
43 }
44 
TEST_F(LlvmLibcAtan2Test,InDoubleRange)45 TEST_F(LlvmLibcAtan2Test, InDoubleRange) {
46   constexpr uint64_t X_COUNT = 123;
47   constexpr uint64_t X_START = FPBits(0.25).uintval();
48   constexpr uint64_t X_STOP = FPBits(4.0).uintval();
49   constexpr uint64_t X_STEP = (X_STOP - X_START) / X_COUNT;
50 
51   constexpr uint64_t Y_COUNT = 137;
52   constexpr uint64_t Y_START = FPBits(0.25).uintval();
53   constexpr uint64_t Y_STOP = FPBits(4.0).uintval();
54   constexpr uint64_t Y_STEP = (Y_STOP - Y_START) / Y_COUNT;
55 
56   auto test = [&](mpfr::RoundingMode rounding_mode) {
57     mpfr::ForceRoundingMode __r(rounding_mode);
58     if (!__r.success)
59       return;
60 
61     uint64_t fails = 0;
62     uint64_t finite_count = 0;
63     uint64_t total_count = 0;
64     double failed_x = 0.0, failed_y = 0.0, failed_r = 0.0;
65     double tol = 0.5;
66 
67     for (uint64_t i = 0, v = X_START; i <= X_COUNT; ++i, v += X_STEP) {
68       double x = FPBits(v).get_val();
69       if (FPBits(x).is_inf_or_nan() || x < 0.0)
70         continue;
71 
72       for (uint64_t j = 0, w = Y_START; j <= Y_COUNT; ++j, w += Y_STEP) {
73         double y = FPBits(w).get_val();
74         if (FPBits(y).is_inf_or_nan())
75           continue;
76 
77         double result = LIBC_NAMESPACE::atan2(x, y);
78         ++total_count;
79         if (FPBits(result).is_inf_or_nan())
80           continue;
81 
82         ++finite_count;
83         mpfr::BinaryInput<double> inputs{x, y};
84 
85         if (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(mpfr::Operation::Atan2, inputs,
86                                                result, 0.5, rounding_mode)) {
87           ++fails;
88           while (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(
89               mpfr::Operation::Atan2, inputs, result, tol, rounding_mode)) {
90             failed_x = x;
91             failed_y = y;
92             failed_r = result;
93 
94             if (tol > 1000.0)
95               break;
96 
97             tol *= 2.0;
98           }
99         }
100       }
101     }
102     if (fails || (finite_count < total_count)) {
103       tlog << " Atan2 failed: " << fails << "/" << finite_count << "/"
104            << total_count << " tests.\n"
105            << "   Max ULPs is at most: " << static_cast<uint64_t>(tol) << ".\n";
106     }
107     if (fails) {
108       mpfr::BinaryInput<double> inputs{failed_x, failed_y};
109       EXPECT_MPFR_MATCH(mpfr::Operation::Atan2, inputs, failed_r, 0.5,
110                         rounding_mode);
111     }
112   };
113 
114   tlog << " Test Rounding To Nearest...\n";
115   test(mpfr::RoundingMode::Nearest);
116 
117   tlog << " Test Rounding Downward...\n";
118   test(mpfr::RoundingMode::Downward);
119 
120   tlog << " Test Rounding Upward...\n";
121   test(mpfr::RoundingMode::Upward);
122 
123   tlog << " Test Rounding Toward Zero...\n";
124   test(mpfr::RoundingMode::TowardZero);
125 }
126