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

source code of libc/test/src/math/atan2f_test.cpp