1*71db0c75SAndroid Build Coastguard Worker //===-- Unittests for atan2f ----------------------------------------------===//
2*71db0c75SAndroid Build Coastguard Worker //
3*71db0c75SAndroid Build Coastguard Worker // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*71db0c75SAndroid Build Coastguard Worker // See https://llvm.org/LICENSE.txt for license information.
5*71db0c75SAndroid Build Coastguard Worker // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*71db0c75SAndroid Build Coastguard Worker //
7*71db0c75SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
8*71db0c75SAndroid Build Coastguard Worker
9*71db0c75SAndroid Build Coastguard Worker #include "hdr/math_macros.h"
10*71db0c75SAndroid Build Coastguard Worker #include "src/__support/FPUtil/FPBits.h"
11*71db0c75SAndroid Build Coastguard Worker #include "src/math/atan2f.h"
12*71db0c75SAndroid Build Coastguard Worker #include "test/UnitTest/FPMatcher.h"
13*71db0c75SAndroid Build Coastguard Worker #include "test/UnitTest/Test.h"
14*71db0c75SAndroid Build Coastguard Worker #include "utils/MPFRWrapper/MPFRUtils.h"
15*71db0c75SAndroid Build Coastguard Worker
16*71db0c75SAndroid Build Coastguard Worker using LlvmLibcAtan2fTest = LIBC_NAMESPACE::testing::FPTest<float>;
17*71db0c75SAndroid Build Coastguard Worker using LIBC_NAMESPACE::testing::tlog;
18*71db0c75SAndroid Build Coastguard Worker
19*71db0c75SAndroid Build Coastguard Worker namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
20*71db0c75SAndroid Build Coastguard Worker
TEST_F(LlvmLibcAtan2fTest,TrickyInputs)21*71db0c75SAndroid Build Coastguard Worker TEST_F(LlvmLibcAtan2fTest, TrickyInputs) {
22*71db0c75SAndroid Build Coastguard Worker constexpr int N = 17;
23*71db0c75SAndroid Build Coastguard Worker mpfr::BinaryInput<float> INPUTS[N] = {
24*71db0c75SAndroid Build Coastguard Worker {0x1.0cb3a4p+20f, 0x1.4ebacp+22f}, {0x1.12215p+1f, 0x1.4fabfcp+22f},
25*71db0c75SAndroid Build Coastguard Worker {-0x1.13baaep+41f, 0x1.5bd22ep+23f}, {0x1.1ff7dcp+41f, 0x1.aec0a6p+23f},
26*71db0c75SAndroid Build Coastguard Worker {0x1.2bc794p+23f, 0x1.0bc0c6p+23f}, {0x1.2fba3ap+42f, 0x1.f99456p+23f},
27*71db0c75SAndroid Build Coastguard Worker {0x1.5ea1f8p+27f, 0x1.f2a1aep+23f}, {0x1.7a931p+44f, 0x1.352ac4p+22f},
28*71db0c75SAndroid Build Coastguard Worker {0x1.8802bcp+21f, 0x1.8f130ap+23f}, {0x1.658ef8p+17f, 0x1.3c00f4p+22f},
29*71db0c75SAndroid Build Coastguard Worker {0x1.69fb0cp+21f, 0x1.39e4c4p+23f}, {0x1.8eb24cp+11f, 0x1.36518p+23f},
30*71db0c75SAndroid Build Coastguard Worker {0x1.9e7ebp+30f, 0x1.d80522p+23f}, {0x1.b4bdeep+19f, 0x1.c19b4p+23f},
31*71db0c75SAndroid Build Coastguard Worker {0x1.bc201p+43f, 0x1.617346p+23f}, {0x1.c96c3cp+20f, 0x1.c01d1ep+23f},
32*71db0c75SAndroid Build Coastguard Worker {0x1.781fcp+28f, 0x1.dcb3cap+23f},
33*71db0c75SAndroid Build Coastguard Worker };
34*71db0c75SAndroid Build Coastguard Worker
35*71db0c75SAndroid Build Coastguard Worker for (int i = 0; i < N; ++i) {
36*71db0c75SAndroid Build Coastguard Worker float x = INPUTS[i].x;
37*71db0c75SAndroid Build Coastguard Worker float y = INPUTS[i].y;
38*71db0c75SAndroid Build Coastguard Worker ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Atan2, INPUTS[i],
39*71db0c75SAndroid Build Coastguard Worker LIBC_NAMESPACE::atan2f(x, y), 0.5);
40*71db0c75SAndroid Build Coastguard Worker INPUTS[i].x = -INPUTS[i].x;
41*71db0c75SAndroid Build Coastguard Worker ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Atan2, INPUTS[i],
42*71db0c75SAndroid Build Coastguard Worker LIBC_NAMESPACE::atan2f(-x, y), 0.5);
43*71db0c75SAndroid Build Coastguard Worker INPUTS[i].y = -INPUTS[i].y;
44*71db0c75SAndroid Build Coastguard Worker ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Atan2, INPUTS[i],
45*71db0c75SAndroid Build Coastguard Worker LIBC_NAMESPACE::atan2f(-x, -y), 0.5);
46*71db0c75SAndroid Build Coastguard Worker INPUTS[i].x = -INPUTS[i].x;
47*71db0c75SAndroid Build Coastguard Worker ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Atan2, INPUTS[i],
48*71db0c75SAndroid Build Coastguard Worker LIBC_NAMESPACE::atan2f(x, -y), 0.5);
49*71db0c75SAndroid Build Coastguard Worker }
50*71db0c75SAndroid Build Coastguard Worker }
51*71db0c75SAndroid Build Coastguard Worker
TEST_F(LlvmLibcAtan2fTest,InFloatRange)52*71db0c75SAndroid Build Coastguard Worker TEST_F(LlvmLibcAtan2fTest, InFloatRange) {
53*71db0c75SAndroid Build Coastguard Worker constexpr uint32_t X_COUNT = 1'23;
54*71db0c75SAndroid Build Coastguard Worker constexpr uint32_t X_START = FPBits(0.25f).uintval();
55*71db0c75SAndroid Build Coastguard Worker constexpr uint32_t X_STOP = FPBits(4.0f).uintval();
56*71db0c75SAndroid Build Coastguard Worker constexpr uint32_t X_STEP = (X_STOP - X_START) / X_COUNT;
57*71db0c75SAndroid Build Coastguard Worker
58*71db0c75SAndroid Build Coastguard Worker constexpr uint32_t Y_COUNT = 1'37;
59*71db0c75SAndroid Build Coastguard Worker constexpr uint32_t Y_START = FPBits(0.25f).uintval();
60*71db0c75SAndroid Build Coastguard Worker constexpr uint32_t Y_STOP = FPBits(4.0f).uintval();
61*71db0c75SAndroid Build Coastguard Worker constexpr uint32_t Y_STEP = (Y_STOP - Y_START) / Y_COUNT;
62*71db0c75SAndroid Build Coastguard Worker
63*71db0c75SAndroid Build Coastguard Worker auto test = [&](mpfr::RoundingMode rounding_mode) {
64*71db0c75SAndroid Build Coastguard Worker mpfr::ForceRoundingMode __r(rounding_mode);
65*71db0c75SAndroid Build Coastguard Worker if (!__r.success)
66*71db0c75SAndroid Build Coastguard Worker return;
67*71db0c75SAndroid Build Coastguard Worker
68*71db0c75SAndroid Build Coastguard Worker uint64_t fails = 0;
69*71db0c75SAndroid Build Coastguard Worker uint64_t finite_count = 0;
70*71db0c75SAndroid Build Coastguard Worker uint64_t total_count = 0;
71*71db0c75SAndroid Build Coastguard Worker float failed_x, failed_y, failed_r = 0.0;
72*71db0c75SAndroid Build Coastguard Worker double tol = 0.5;
73*71db0c75SAndroid Build Coastguard Worker
74*71db0c75SAndroid Build Coastguard Worker for (uint32_t i = 0, v = X_START; i <= X_COUNT; ++i, v += X_STEP) {
75*71db0c75SAndroid Build Coastguard Worker float x = FPBits(v).get_val();
76*71db0c75SAndroid Build Coastguard Worker if (FPBits(v).is_nan() || FPBits(v).is_inf() || x < 0.0)
77*71db0c75SAndroid Build Coastguard Worker continue;
78*71db0c75SAndroid Build Coastguard Worker
79*71db0c75SAndroid Build Coastguard Worker for (uint32_t j = 0, w = Y_START; j <= Y_COUNT; ++j, w += Y_STEP) {
80*71db0c75SAndroid Build Coastguard Worker float y = FPBits(w).get_val();
81*71db0c75SAndroid Build Coastguard Worker if (FPBits(w).is_nan() || FPBits(w).is_inf())
82*71db0c75SAndroid Build Coastguard Worker continue;
83*71db0c75SAndroid Build Coastguard Worker
84*71db0c75SAndroid Build Coastguard Worker LIBC_NAMESPACE::libc_errno = 0;
85*71db0c75SAndroid Build Coastguard Worker float result = LIBC_NAMESPACE::atan2f(x, y);
86*71db0c75SAndroid Build Coastguard Worker ++total_count;
87*71db0c75SAndroid Build Coastguard Worker if (FPBits(result).is_nan() || FPBits(result).is_inf())
88*71db0c75SAndroid Build Coastguard Worker continue;
89*71db0c75SAndroid Build Coastguard Worker
90*71db0c75SAndroid Build Coastguard Worker ++finite_count;
91*71db0c75SAndroid Build Coastguard Worker mpfr::BinaryInput<float> inputs{x, y};
92*71db0c75SAndroid Build Coastguard Worker
93*71db0c75SAndroid Build Coastguard Worker if (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(mpfr::Operation::Atan2, inputs,
94*71db0c75SAndroid Build Coastguard Worker result, 0.5, rounding_mode)) {
95*71db0c75SAndroid Build Coastguard Worker ++fails;
96*71db0c75SAndroid Build Coastguard Worker while (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(
97*71db0c75SAndroid Build Coastguard Worker mpfr::Operation::Atan2, inputs, result, tol, rounding_mode)) {
98*71db0c75SAndroid Build Coastguard Worker failed_x = x;
99*71db0c75SAndroid Build Coastguard Worker failed_y = y;
100*71db0c75SAndroid Build Coastguard Worker failed_r = result;
101*71db0c75SAndroid Build Coastguard Worker
102*71db0c75SAndroid Build Coastguard Worker if (tol > 1000.0)
103*71db0c75SAndroid Build Coastguard Worker break;
104*71db0c75SAndroid Build Coastguard Worker
105*71db0c75SAndroid Build Coastguard Worker tol *= 2.0;
106*71db0c75SAndroid Build Coastguard Worker }
107*71db0c75SAndroid Build Coastguard Worker }
108*71db0c75SAndroid Build Coastguard Worker }
109*71db0c75SAndroid Build Coastguard Worker }
110*71db0c75SAndroid Build Coastguard Worker if (fails || (finite_count < total_count)) {
111*71db0c75SAndroid Build Coastguard Worker tlog << " Atan2f failed: " << fails << "/" << finite_count << "/"
112*71db0c75SAndroid Build Coastguard Worker << total_count << " tests.\n"
113*71db0c75SAndroid Build Coastguard Worker << " Max ULPs is at most: " << static_cast<uint64_t>(tol) << ".\n";
114*71db0c75SAndroid Build Coastguard Worker }
115*71db0c75SAndroid Build Coastguard Worker if (fails) {
116*71db0c75SAndroid Build Coastguard Worker mpfr::BinaryInput<float> inputs{failed_x, failed_y};
117*71db0c75SAndroid Build Coastguard Worker EXPECT_MPFR_MATCH(mpfr::Operation::Atan2, inputs, failed_r, 0.5,
118*71db0c75SAndroid Build Coastguard Worker rounding_mode);
119*71db0c75SAndroid Build Coastguard Worker }
120*71db0c75SAndroid Build Coastguard Worker };
121*71db0c75SAndroid Build Coastguard Worker
122*71db0c75SAndroid Build Coastguard Worker tlog << " Test Rounding To Nearest...\n";
123*71db0c75SAndroid Build Coastguard Worker test(mpfr::RoundingMode::Nearest);
124*71db0c75SAndroid Build Coastguard Worker
125*71db0c75SAndroid Build Coastguard Worker tlog << " Test Rounding Downward...\n";
126*71db0c75SAndroid Build Coastguard Worker test(mpfr::RoundingMode::Downward);
127*71db0c75SAndroid Build Coastguard Worker
128*71db0c75SAndroid Build Coastguard Worker tlog << " Test Rounding Upward...\n";
129*71db0c75SAndroid Build Coastguard Worker test(mpfr::RoundingMode::Upward);
130*71db0c75SAndroid Build Coastguard Worker
131*71db0c75SAndroid Build Coastguard Worker tlog << " Test Rounding Toward Zero...\n";
132*71db0c75SAndroid Build Coastguard Worker test(mpfr::RoundingMode::TowardZero);
133*71db0c75SAndroid Build Coastguard Worker }
134