Skip to content

Commit d2d3ef1

Browse files
committed
[libc][math] Refactor atan2 implementation to header-only in src/__support/math folder.
1 parent 0f24379 commit d2d3ef1

File tree

9 files changed

+266
-198
lines changed

9 files changed

+266
-198
lines changed

libc/shared/math.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "math/asinhf.h"
2424
#include "math/asinhf16.h"
2525
#include "math/atan.h"
26+
#include "math/atan2.h"
2627
#include "math/atanf.h"
2728
#include "math/atanf16.h"
2829
#include "math/erff.h"

libc/shared/math/atan2.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===-- Shared atan2 function -----------------------------------*- C++ -*-===//
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+
#ifndef LLVM_LIBC_SHARED_MATH_ATAN2_H
10+
#define LLVM_LIBC_SHARED_MATH_ATAN2_H
11+
12+
#include "shared/libc_common.h"
13+
#include "src/__support/math/atan2.h"
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
namespace shared {
17+
18+
using math::atan2;
19+
20+
} // namespace shared
21+
} // namespace LIBC_NAMESPACE_DECL
22+
23+
#endif // LLVM_LIBC_SHARED_MATH_ATAN2_H

libc/src/__support/math/CMakeLists.txt

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ add_header_library(
158158
asinhf16
159159
HDRS
160160
asinhf16.h
161-
DEPENDS
161+
DEPENDS
162162
.acoshf_utils
163163
libc.src.__support.FPUtil.fenv_impl
164164
libc.src.__support.FPUtil.fp_bits
@@ -176,7 +176,7 @@ add_header_library(
176176
atan_utils
177177
HDRS
178178
atan_utils.h
179-
DEPENDS
179+
DEPENDS
180180
libc.src.__support.integer_literals
181181
libc.src.__support.FPUtil.double_double
182182
libc.src.__support.FPUtil.dyadic_float
@@ -189,7 +189,21 @@ add_header_library(
189189
atan
190190
HDRS
191191
atan.h
192-
DEPENDS
192+
DEPENDS
193+
.atan_utils
194+
libc.src.__support.FPUtil.double_double
195+
libc.src.__support.FPUtil.fenv_impl
196+
libc.src.__support.FPUtil.fp_bits
197+
libc.src.__support.FPUtil.multiply_add
198+
libc.src.__support.FPUtil.nearest_integer
199+
libc.src.__support.macros.optimization
200+
)
201+
202+
add_header_library(
203+
atan2
204+
HDRS
205+
atan2.h
206+
DEPENDS
193207
.atan_utils
194208
libc.src.__support.FPUtil.double_double
195209
libc.src.__support.FPUtil.fenv_impl

libc/src/__support/math/atan2.h

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
//===-- Implementation header for atan2 -------------------------*- C++ -*-===//
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+
#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_ATAN2_H
10+
#define LLVM_LIBC_SRC___SUPPORT_MATH_ATAN2_H
11+
12+
#include "atan_utils.h"
13+
#include "src/__support/FPUtil/FEnvImpl.h"
14+
#include "src/__support/FPUtil/FPBits.h"
15+
#include "src/__support/FPUtil/double_double.h"
16+
#include "src/__support/FPUtil/multiply_add.h"
17+
#include "src/__support/FPUtil/nearest_integer.h"
18+
#include "src/__support/macros/config.h"
19+
#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
20+
21+
namespace LIBC_NAMESPACE_DECL {
22+
23+
namespace math {
24+
25+
// There are several range reduction steps we can take for atan2(y, x) as
26+
// follow:
27+
28+
// * Range reduction 1: signness
29+
// atan2(y, x) will return a number between -PI and PI representing the angle
30+
// forming by the 0x axis and the vector (x, y) on the 0xy-plane.
31+
// In particular, we have that:
32+
// atan2(y, x) = atan( y/x ) if x >= 0 and y >= 0 (I-quadrant)
33+
// = pi + atan( y/x ) if x < 0 and y >= 0 (II-quadrant)
34+
// = -pi + atan( y/x ) if x < 0 and y < 0 (III-quadrant)
35+
// = atan( y/x ) if x >= 0 and y < 0 (IV-quadrant)
36+
// Since atan function is odd, we can use the formula:
37+
// atan(-u) = -atan(u)
38+
// to adjust the above conditions a bit further:
39+
// atan2(y, x) = atan( |y|/|x| ) if x >= 0 and y >= 0 (I-quadrant)
40+
// = pi - atan( |y|/|x| ) if x < 0 and y >= 0 (II-quadrant)
41+
// = -pi + atan( |y|/|x| ) if x < 0 and y < 0 (III-quadrant)
42+
// = -atan( |y|/|x| ) if x >= 0 and y < 0 (IV-quadrant)
43+
// Which can be simplified to:
44+
// atan2(y, x) = sign(y) * atan( |y|/|x| ) if x >= 0
45+
// = sign(y) * (pi - atan( |y|/|x| )) if x < 0
46+
47+
// * Range reduction 2: reciprocal
48+
// Now that the argument inside atan is positive, we can use the formula:
49+
// atan(1/x) = pi/2 - atan(x)
50+
// to make the argument inside atan <= 1 as follow:
51+
// atan2(y, x) = sign(y) * atan( |y|/|x|) if 0 <= |y| <= x
52+
// = sign(y) * (pi/2 - atan( |x|/|y| ) if 0 <= x < |y|
53+
// = sign(y) * (pi - atan( |y|/|x| )) if 0 <= |y| <= -x
54+
// = sign(y) * (pi/2 + atan( |x|/|y| )) if 0 <= -x < |y|
55+
56+
// * Range reduction 3: look up table.
57+
// After the previous two range reduction steps, we reduce the problem to
58+
// compute atan(u) with 0 <= u <= 1, or to be precise:
59+
// atan( n / d ) where n = min(|x|, |y|) and d = max(|x|, |y|).
60+
// An accurate polynomial approximation for the whole [0, 1] input range will
61+
// require a very large degree. To make it more efficient, we reduce the input
62+
// range further by finding an integer idx such that:
63+
// | n/d - idx/64 | <= 1/128.
64+
// In particular,
65+
// idx := round(2^6 * n/d)
66+
// Then for the fast pass, we find a polynomial approximation for:
67+
// atan( n/d ) ~ atan( idx/64 ) + (n/d - idx/64) * Q(n/d - idx/64)
68+
// For the accurate pass, we use the addition formula:
69+
// atan( n/d ) - atan( idx/64 ) = atan( (n/d - idx/64)/(1 + (n*idx)/(64*d)) )
70+
// = atan( (n - d*(idx/64))/(d + n*(idx/64)) )
71+
// And for the fast pass, we use degree-9 Taylor polynomial to compute the RHS:
72+
// atan(u) ~ P(u) = u - u^3/3 + u^5/5 - u^7/7 + u^9/9
73+
// with absolute errors bounded by:
74+
// |atan(u) - P(u)| < |u|^11 / 11 < 2^-80
75+
// and relative errors bounded by:
76+
// |(atan(u) - P(u)) / P(u)| < u^10 / 11 < 2^-73.
77+
78+
LIBC_INLINE static constexpr double atan2(double y, double x) {
79+
using namespace atan_internal;
80+
using FPBits = fputil::FPBits<double>;
81+
82+
constexpr double IS_NEG[2] = {1.0, -1.0};
83+
constexpr DoubleDouble ZERO = {0.0, 0.0};
84+
constexpr DoubleDouble MZERO = {-0.0, -0.0};
85+
constexpr DoubleDouble PI = {0x1.1a62633145c07p-53, 0x1.921fb54442d18p+1};
86+
constexpr DoubleDouble MPI = {-0x1.1a62633145c07p-53, -0x1.921fb54442d18p+1};
87+
constexpr DoubleDouble PI_OVER_2 = {0x1.1a62633145c07p-54,
88+
0x1.921fb54442d18p0};
89+
constexpr DoubleDouble MPI_OVER_2 = {-0x1.1a62633145c07p-54,
90+
-0x1.921fb54442d18p0};
91+
constexpr DoubleDouble PI_OVER_4 = {0x1.1a62633145c07p-55,
92+
0x1.921fb54442d18p-1};
93+
constexpr DoubleDouble THREE_PI_OVER_4 = {0x1.a79394c9e8a0ap-54,
94+
0x1.2d97c7f3321d2p+1};
95+
// Adjustment for constant term:
96+
// CONST_ADJ[x_sign][y_sign][recip]
97+
constexpr DoubleDouble CONST_ADJ[2][2][2] = {
98+
{{ZERO, MPI_OVER_2}, {MZERO, MPI_OVER_2}},
99+
{{MPI, PI_OVER_2}, {MPI, PI_OVER_2}}};
100+
101+
FPBits x_bits(x), y_bits(y);
102+
bool x_sign = x_bits.sign().is_neg();
103+
bool y_sign = y_bits.sign().is_neg();
104+
x_bits = x_bits.abs();
105+
y_bits = y_bits.abs();
106+
uint64_t x_abs = x_bits.uintval();
107+
uint64_t y_abs = y_bits.uintval();
108+
bool recip = x_abs < y_abs;
109+
uint64_t min_abs = recip ? x_abs : y_abs;
110+
uint64_t max_abs = !recip ? x_abs : y_abs;
111+
unsigned min_exp = static_cast<unsigned>(min_abs >> FPBits::FRACTION_LEN);
112+
unsigned max_exp = static_cast<unsigned>(max_abs >> FPBits::FRACTION_LEN);
113+
114+
double num = FPBits(min_abs).get_val();
115+
double den = FPBits(max_abs).get_val();
116+
117+
// Check for exceptional cases, whether inputs are 0, inf, nan, or close to
118+
// overflow, or close to underflow.
119+
if (LIBC_UNLIKELY(max_exp > 0x7ffU - 128U || min_exp < 128U)) {
120+
if (x_bits.is_nan() || y_bits.is_nan()) {
121+
if (x_bits.is_signaling_nan() || y_bits.is_signaling_nan())
122+
fputil::raise_except_if_required(FE_INVALID);
123+
return FPBits::quiet_nan().get_val();
124+
}
125+
unsigned x_except = x == 0.0 ? 0 : (FPBits(x_abs).is_inf() ? 2 : 1);
126+
unsigned y_except = y == 0.0 ? 0 : (FPBits(y_abs).is_inf() ? 2 : 1);
127+
128+
// Exceptional cases:
129+
// EXCEPT[y_except][x_except][x_is_neg]
130+
// with x_except & y_except:
131+
// 0: zero
132+
// 1: finite, non-zero
133+
// 2: infinity
134+
constexpr DoubleDouble EXCEPTS[3][3][2] = {
135+
{{ZERO, PI}, {ZERO, PI}, {ZERO, PI}},
136+
{{PI_OVER_2, PI_OVER_2}, {ZERO, ZERO}, {ZERO, PI}},
137+
{{PI_OVER_2, PI_OVER_2},
138+
{PI_OVER_2, PI_OVER_2},
139+
{PI_OVER_4, THREE_PI_OVER_4}},
140+
};
141+
142+
if ((x_except != 1) || (y_except != 1)) {
143+
DoubleDouble r = EXCEPTS[y_except][x_except][x_sign];
144+
return fputil::multiply_add(IS_NEG[y_sign], r.hi, IS_NEG[y_sign] * r.lo);
145+
}
146+
bool scale_up = min_exp < 128U;
147+
bool scale_down = max_exp > 0x7ffU - 128U;
148+
// At least one input is denormal, multiply both numerator and denominator
149+
// by some large enough power of 2 to normalize denormal inputs.
150+
if (scale_up) {
151+
num *= 0x1.0p64;
152+
if (!scale_down)
153+
den *= 0x1.0p64;
154+
} else if (scale_down) {
155+
den *= 0x1.0p-64;
156+
if (!scale_up)
157+
num *= 0x1.0p-64;
158+
}
159+
160+
min_abs = FPBits(num).uintval();
161+
max_abs = FPBits(den).uintval();
162+
min_exp = static_cast<unsigned>(min_abs >> FPBits::FRACTION_LEN);
163+
max_exp = static_cast<unsigned>(max_abs >> FPBits::FRACTION_LEN);
164+
}
165+
166+
double final_sign = IS_NEG[(x_sign != y_sign) != recip];
167+
DoubleDouble const_term = CONST_ADJ[x_sign][y_sign][recip];
168+
unsigned exp_diff = max_exp - min_exp;
169+
// We have the following bound for normalized n and d:
170+
// 2^(-exp_diff - 1) < n/d < 2^(-exp_diff + 1).
171+
if (LIBC_UNLIKELY(exp_diff > 54)) {
172+
return fputil::multiply_add(final_sign, const_term.hi,
173+
final_sign * (const_term.lo + num / den));
174+
}
175+
176+
double k = fputil::nearest_integer(64.0 * num / den);
177+
unsigned idx = static_cast<unsigned>(k);
178+
// k = idx / 64
179+
k *= 0x1.0p-6;
180+
181+
// Range reduction:
182+
// atan(n/d) - atan(k/64) = atan((n/d - k/64) / (1 + (n/d) * (k/64)))
183+
// = atan((n - d * k/64)) / (d + n * k/64))
184+
DoubleDouble num_k = fputil::exact_mult(num, k);
185+
DoubleDouble den_k = fputil::exact_mult(den, k);
186+
187+
// num_dd = n - d * k
188+
DoubleDouble num_dd = fputil::exact_add(num - den_k.hi, -den_k.lo);
189+
// den_dd = d + n * k
190+
DoubleDouble den_dd = fputil::exact_add(den, num_k.hi);
191+
den_dd.lo += num_k.lo;
192+
193+
// q = (n - d * k) / (d + n * k)
194+
DoubleDouble q = fputil::div(num_dd, den_dd);
195+
// p ~ atan(q)
196+
DoubleDouble p = atan_eval(q);
197+
198+
DoubleDouble r = fputil::add(const_term, fputil::add(ATAN_I[idx], p));
199+
r.hi *= final_sign;
200+
r.lo *= final_sign;
201+
202+
return r.hi + r.lo;
203+
}
204+
205+
} // namespace math
206+
207+
} // namespace LIBC_NAMESPACE_DECL
208+
209+
#endif // LLVM_LIBC_SRC___SUPPORT_MATH_ATAN2_H

libc/src/math/generic/CMakeLists.txt

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4079,13 +4079,7 @@ add_entrypoint_object(
40794079
HDRS
40804080
../atan2.h
40814081
DEPENDS
4082-
libc.src.__support.math.atan_utils
4083-
libc.src.__support.FPUtil.double_double
4084-
libc.src.__support.FPUtil.fenv_impl
4085-
libc.src.__support.FPUtil.fp_bits
4086-
libc.src.__support.FPUtil.multiply_add
4087-
libc.src.__support.FPUtil.nearest_integer
4088-
libc.src.__support.macros.optimization
4082+
40894083
)
40904084

40914085
add_entrypoint_object(

0 commit comments

Comments
 (0)