Skip to content

Commit 8741412

Browse files
authored
[libc][math] Implement a fast pass for atan2f128 with 1ULP error using DyadicFloat<128>. (#133150)
Part of #131642.
1 parent 664745c commit 8741412

File tree

15 files changed

+514
-4
lines changed

15 files changed

+514
-4
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,7 @@ endif()
759759
if(LIBC_TYPES_HAS_FLOAT128)
760760
list(APPEND TARGET_LIBM_ENTRYPOINTS
761761
# math.h C23 _Float128 entrypoints
762+
libc.src.math.atan2f128
762763
libc.src.math.canonicalizef128
763764
libc.src.math.ceilf128
764765
libc.src.math.copysignf128

libc/config/linux/riscv/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,7 @@ endif()
640640
if(LIBC_TYPES_HAS_FLOAT128)
641641
list(APPEND TARGET_LIBM_ENTRYPOINTS
642642
# math.h C23 _Float128 entrypoints
643+
libc.src.math.atan2f128
643644
libc.src.math.canonicalizef128
644645
libc.src.math.ceilf128
645646
libc.src.math.copysignf128

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,7 @@ endif()
776776
if(LIBC_TYPES_HAS_FLOAT128)
777777
list(APPEND TARGET_LIBM_ENTRYPOINTS
778778
# math.h C23 _Float128 entrypoints
779+
libc.src.math.atan2f128
779780
libc.src.math.canonicalizef128
780781
libc.src.math.ceilf128
781782
libc.src.math.copysignf128

libc/docs/headers/math/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ Higher Math Functions
263263
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
264264
| atan | |check| | 1 ULP | | | | 7.12.4.3 | F.10.1.3 |
265265
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
266-
| atan2 | |check| | 1 ULP | | | | 7.12.4.4 | F.10.1.4 |
266+
| atan2 | |check| | 1 ULP | | | 1 ULP | 7.12.4.4 | F.10.1.4 |
267267
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
268268
| atan2pi | | | | | | 7.12.4.11 | F.10.1.11 |
269269
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+

libc/include/math.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ functions:
9393
arguments:
9494
- type: long double
9595
- type: long double
96+
- name: atan2f128
97+
standards:
98+
- stdc
99+
return_type: float128
100+
arguments:
101+
- type: float128
102+
- type: float128
103+
guard: LIBC_TYPES_HAS_FLOAT128
96104
- name: atanf
97105
standards:
98106
- stdc

libc/src/__support/FPUtil/dyadic_float.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ template <size_t Bits> struct DyadicFloat {
104104
normalize();
105105
}
106106

107-
LIBC_INLINE constexpr DyadicFloat(Sign s, int e, MantissaType m)
107+
LIBC_INLINE constexpr DyadicFloat(Sign s, int e, const MantissaType &m)
108108
: sign(s), exponent(e), mantissa(m) {
109109
normalize();
110110
}

libc/src/math/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ add_math_entrypoint_object(atanf)
6262
add_math_entrypoint_object(atan2)
6363
add_math_entrypoint_object(atan2f)
6464
add_math_entrypoint_object(atan2l)
65+
add_math_entrypoint_object(atan2f128)
6566

6667
add_math_entrypoint_object(atanh)
6768
add_math_entrypoint_object(atanhf)

libc/src/math/atan2f128.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation header for atan2f128 ---------------------*- 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_MATH_ATAN2F128_H
10+
#define LLVM_LIBC_SRC_MATH_ATAN2F128_H
11+
12+
#include "src/__support/macros/config.h"
13+
#include "src/__support/macros/properties/types.h"
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
17+
float128 atan2f128(float128 x, float128 y);
18+
19+
} // namespace LIBC_NAMESPACE_DECL
20+
21+
#endif // LLVM_LIBC_SRC_MATH_ATAN2F128_H

libc/src/math/generic/CMakeLists.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4117,8 +4117,11 @@ add_header_library(
41174117
HDRS
41184118
atan_utils.h
41194119
DEPENDS
4120+
libc.src.__support.integer_literals
41204121
libc.src.__support.FPUtil.double_double
4122+
libc.src.__support.FPUtil.dyadic_float
41214123
libc.src.__support.FPUtil.multiply_add
4124+
libc.src.__support.FPUtil.polyeval
41224125
libc.src.__support.macros.optimization
41234126
)
41244127

@@ -4200,6 +4203,24 @@ add_entrypoint_object(
42004203
.atan2
42014204
)
42024205

4206+
add_entrypoint_object(
4207+
atan2f128
4208+
SRCS
4209+
atan2f128.cpp
4210+
HDRS
4211+
../atan2f128.h
4212+
DEPENDS
4213+
.atan_utils
4214+
libc.src.__support.integer_literals
4215+
libc.src.__support.uint128
4216+
libc.src.__support.FPUtil.dyadic_float
4217+
libc.src.__support.FPUtil.fp_bits
4218+
libc.src.__support.FPUtil.multiply_add
4219+
libc.src.__support.FPUtil.nearest_integer
4220+
libc.src.__support.macros.optimization
4221+
libc.src.__support.macros.properties.types
4222+
)
4223+
42034224
add_entrypoint_object(
42044225
scalbln
42054226
SRCS

libc/src/math/generic/atan2f128.cpp

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
//===-- Quad-precision atan2 function -------------------------------------===//
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/math/atan2f128.h"
10+
#include "atan_utils.h"
11+
#include "src/__support/FPUtil/FPBits.h"
12+
#include "src/__support/FPUtil/dyadic_float.h"
13+
#include "src/__support/FPUtil/multiply_add.h"
14+
#include "src/__support/FPUtil/nearest_integer.h"
15+
#include "src/__support/integer_literals.h"
16+
#include "src/__support/macros/config.h"
17+
#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
18+
#include "src/__support/macros/properties/types.h"
19+
#include "src/__support/uint128.h"
20+
21+
namespace LIBC_NAMESPACE_DECL {
22+
23+
namespace {
24+
25+
using Float128 = fputil::DyadicFloat<128>;
26+
27+
static constexpr Float128 ZERO = {Sign::POS, 0, 0_u128};
28+
static constexpr Float128 MZERO = {Sign::NEG, 0, 0_u128};
29+
static constexpr Float128 PI = {Sign::POS, -126,
30+
0xc90fdaa2'2168c234'c4c6628b'80dc1cd1_u128};
31+
static constexpr Float128 MPI = {Sign::NEG, -126,
32+
0xc90fdaa2'2168c234'c4c6628b'80dc1cd1_u128};
33+
static constexpr Float128 PI_OVER_2 = {
34+
Sign::POS, -127, 0xc90fdaa2'2168c234'c4c6628b'80dc1cd1_u128};
35+
static constexpr Float128 MPI_OVER_2 = {
36+
Sign::NEG, -127, 0xc90fdaa2'2168c234'c4c6628b'80dc1cd1_u128};
37+
static constexpr Float128 PI_OVER_4 = {
38+
Sign::POS, -128, 0xc90fdaa2'2168c234'c4c6628b'80dc1cd1_u128};
39+
static constexpr Float128 THREE_PI_OVER_4 = {
40+
Sign::POS, -128, 0x96cbe3f9'990e91a7'9394c9e8'a0a5159d_u128};
41+
42+
// Adjustment for constant term:
43+
// CONST_ADJ[x_sign][y_sign][recip]
44+
static constexpr Float128 CONST_ADJ[2][2][2] = {
45+
{{ZERO, MPI_OVER_2}, {MZERO, MPI_OVER_2}},
46+
{{MPI, PI_OVER_2}, {MPI, PI_OVER_2}}};
47+
48+
} // anonymous namespace
49+
50+
// There are several range reduction steps we can take for atan2(y, x) as
51+
// follow:
52+
53+
// * Range reduction 1: signness
54+
// atan2(y, x) will return a number between -PI and PI representing the angle
55+
// forming by the 0x axis and the vector (x, y) on the 0xy-plane.
56+
// In particular, we have that:
57+
// atan2(y, x) = atan( y/x ) if x >= 0 and y >= 0 (I-quadrant)
58+
// = pi + atan( y/x ) if x < 0 and y >= 0 (II-quadrant)
59+
// = -pi + atan( y/x ) if x < 0 and y < 0 (III-quadrant)
60+
// = atan( y/x ) if x >= 0 and y < 0 (IV-quadrant)
61+
// Since atan function is odd, we can use the formula:
62+
// atan(-u) = -atan(u)
63+
// to adjust the above conditions a bit further:
64+
// atan2(y, x) = atan( |y|/|x| ) if x >= 0 and y >= 0 (I-quadrant)
65+
// = pi - atan( |y|/|x| ) if x < 0 and y >= 0 (II-quadrant)
66+
// = -pi + atan( |y|/|x| ) if x < 0 and y < 0 (III-quadrant)
67+
// = -atan( |y|/|x| ) if x >= 0 and y < 0 (IV-quadrant)
68+
// Which can be simplified to:
69+
// atan2(y, x) = sign(y) * atan( |y|/|x| ) if x >= 0
70+
// = sign(y) * (pi - atan( |y|/|x| )) if x < 0
71+
72+
// * Range reduction 2: reciprocal
73+
// Now that the argument inside atan is positive, we can use the formula:
74+
// atan(1/x) = pi/2 - atan(x)
75+
// to make the argument inside atan <= 1 as follow:
76+
// atan2(y, x) = sign(y) * atan( |y|/|x|) if 0 <= |y| <= x
77+
// = sign(y) * (pi/2 - atan( |x|/|y| ) if 0 <= x < |y|
78+
// = sign(y) * (pi - atan( |y|/|x| )) if 0 <= |y| <= -x
79+
// = sign(y) * (pi/2 + atan( |x|/|y| )) if 0 <= -x < |y|
80+
81+
// * Range reduction 3: look up table.
82+
// After the previous two range reduction steps, we reduce the problem to
83+
// compute atan(u) with 0 <= u <= 1, or to be precise:
84+
// atan( n / d ) where n = min(|x|, |y|) and d = max(|x|, |y|).
85+
// An accurate polynomial approximation for the whole [0, 1] input range will
86+
// require a very large degree. To make it more efficient, we reduce the input
87+
// range further by finding an integer idx such that:
88+
// | n/d - idx/64 | <= 1/128.
89+
// In particular,
90+
// idx := round(2^6 * n/d)
91+
// Then for the fast pass, we find a polynomial approximation for:
92+
// atan( n/d ) ~ atan( idx/64 ) + (n/d - idx/64) * Q(n/d - idx/64)
93+
// For the accurate pass, we use the addition formula:
94+
// atan( n/d ) - atan( idx/64 ) = atan( (n/d - idx/64)/(1 + (n*idx)/(64*d)) )
95+
// = atan( (n - d*(idx/64))/(d + n*(idx/64)) )
96+
// And for the fast pass, we use degree-13 minimax polynomial to compute the
97+
// RHS:
98+
// atan(u) ~ P(u) = u - c_3 * u^3 + c_5 * u^5 - c_7 * u^7 + c_9 *u^9 -
99+
// - c_11 * u^11 + c_13 * u^13
100+
// with absolute errors bounded by:
101+
// |atan(u) - P(u)| < 2^-121
102+
// and relative errors bounded by:
103+
// |(atan(u) - P(u)) / P(u)| < 2^-114.
104+
105+
LLVM_LIBC_FUNCTION(float128, atan2f128, (float128 y, float128 x)) {
106+
using FPBits = fputil::FPBits<float128>;
107+
using Float128 = fputil::DyadicFloat<128>;
108+
109+
FPBits x_bits(x), y_bits(y);
110+
bool x_sign = x_bits.sign().is_neg();
111+
bool y_sign = y_bits.sign().is_neg();
112+
x_bits = x_bits.abs();
113+
y_bits = y_bits.abs();
114+
UInt128 x_abs = x_bits.uintval();
115+
UInt128 y_abs = y_bits.uintval();
116+
bool recip = x_abs < y_abs;
117+
UInt128 min_abs = recip ? x_abs : y_abs;
118+
UInt128 max_abs = !recip ? x_abs : y_abs;
119+
unsigned min_exp = static_cast<unsigned>(min_abs >> FPBits::FRACTION_LEN);
120+
unsigned max_exp = static_cast<unsigned>(max_abs >> FPBits::FRACTION_LEN);
121+
122+
Float128 num(FPBits(min_abs).get_val());
123+
Float128 den(FPBits(max_abs).get_val());
124+
125+
// Check for exceptional cases, whether inputs are 0, inf, nan, or close to
126+
// overflow, or close to underflow.
127+
if (LIBC_UNLIKELY(max_exp >= 0x7fffU || min_exp == 0U)) {
128+
if (x_bits.is_nan() || y_bits.is_nan())
129+
return FPBits::quiet_nan().get_val();
130+
unsigned x_except = x == 0 ? 0 : (FPBits(x_abs).is_inf() ? 2 : 1);
131+
unsigned y_except = y == 0 ? 0 : (FPBits(y_abs).is_inf() ? 2 : 1);
132+
133+
// Exceptional cases:
134+
// EXCEPT[y_except][x_except][x_is_neg]
135+
// with x_except & y_except:
136+
// 0: zero
137+
// 1: finite, non-zero
138+
// 2: infinity
139+
constexpr Float128 EXCEPTS[3][3][2] = {
140+
{{ZERO, PI}, {ZERO, PI}, {ZERO, PI}},
141+
{{PI_OVER_2, PI_OVER_2}, {ZERO, ZERO}, {ZERO, PI}},
142+
{{PI_OVER_2, PI_OVER_2},
143+
{PI_OVER_2, PI_OVER_2},
144+
{PI_OVER_4, THREE_PI_OVER_4}},
145+
};
146+
147+
if ((x_except != 1) || (y_except != 1)) {
148+
Float128 r = EXCEPTS[y_except][x_except][x_sign];
149+
if (y_sign)
150+
r.sign = r.sign.negate();
151+
return static_cast<float128>(r);
152+
}
153+
}
154+
155+
bool final_sign = ((x_sign != y_sign) != recip);
156+
Float128 const_term = CONST_ADJ[x_sign][y_sign][recip];
157+
int exp_diff = den.exponent - num.exponent;
158+
// We have the following bound for normalized n and d:
159+
// 2^(-exp_diff - 1) < n/d < 2^(-exp_diff + 1).
160+
if (LIBC_UNLIKELY(exp_diff > FPBits::FRACTION_LEN + 2)) {
161+
if (final_sign)
162+
const_term.sign = const_term.sign.negate();
163+
return static_cast<float128>(const_term);
164+
}
165+
166+
// Take 24 leading bits of num and den to convert to float for fast division.
167+
// We also multiply the numerator by 64 using integer addition directly to the
168+
// exponent field.
169+
float num_f =
170+
cpp::bit_cast<float>(static_cast<uint32_t>(num.mantissa >> 104) +
171+
(6U << fputil::FPBits<float>::FRACTION_LEN));
172+
float den_f = cpp::bit_cast<float>(
173+
static_cast<uint32_t>(den.mantissa >> 104) +
174+
(static_cast<uint32_t>(exp_diff) << fputil::FPBits<float>::FRACTION_LEN));
175+
176+
float k = fputil::nearest_integer(num_f / den_f);
177+
unsigned idx = static_cast<unsigned>(k);
178+
179+
// k_f128 = idx / 64
180+
Float128 k_f128(Sign::POS, -6, Float128::MantissaType(idx));
181+
182+
// Range reduction:
183+
// atan(n/d) - atan(k) = atan((n/d - k/64) / (1 + (n/d) * (k/64)))
184+
// = atan((n - d * k/64)) / (d + n * k/64))
185+
// num_f128 = n - d * k/64
186+
Float128 num_f128 = fputil::multiply_add(den, -k_f128, num);
187+
// den_f128 = d + n * k/64
188+
Float128 den_f128 = fputil::multiply_add(num, k_f128, den);
189+
190+
// q = (n - d * k) / (d + n * k)
191+
Float128 q = fputil::quick_mul(num_f128, fputil::approx_reciprocal(den_f128));
192+
// p ~ atan(q)
193+
Float128 p = atan_eval(q);
194+
195+
Float128 r =
196+
fputil::quick_add(const_term, fputil::quick_add(ATAN_I_F128[idx], p));
197+
if (final_sign)
198+
r.sign = r.sign.negate();
199+
200+
return static_cast<float128>(r);
201+
}
202+
203+
} // namespace LIBC_NAMESPACE_DECL

0 commit comments

Comments
 (0)