Skip to content

Commit cfddb40

Browse files
authored
[libc][math] Refactor acos implementation to header-only in src/__support/math folder. (#148409)
Part of #147386 in preparation for: https://discourse.llvm.org/t/rfc-make-clang-builtin-math-functions-constexpr-with-llvm-libc-to-support-c-23-constexpr-math-functions/86450
1 parent b3c9ed1 commit cfddb40

File tree

9 files changed

+403
-308
lines changed

9 files changed

+403
-308
lines changed

libc/shared/math.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "libc_common.h"
1313

14+
#include "math/acos.h"
1415
#include "math/exp.h"
1516
#include "math/exp10.h"
1617
#include "math/exp10f.h"

libc/shared/math/acos.h

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

libc/src/__support/math/CMakeLists.txt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,36 @@
1+
add_header_library(
2+
acos
3+
HDRS
4+
acos.h
5+
DEPENDS
6+
.asin_utils
7+
libc.src.__support.math.asin_utils
8+
libc.src.__support.FPUtil.double_double
9+
libc.src.__support.FPUtil.dyadic_float
10+
libc.src.__support.FPUtil.fenv_impl
11+
libc.src.__support.FPUtil.fp_bits
12+
libc.src.__support.FPUtil.multiply_add
13+
libc.src.__support.FPUtil.polyeval
14+
libc.src.__support.FPUtil.sqrt
15+
libc.src.__support.macros.optimization
16+
libc.src.__support.macros.properties.types
17+
libc.src.__support.macros.properties.cpu_features
18+
)
19+
20+
add_header_library(
21+
asin_utils
22+
HDRS
23+
asin_utils.h
24+
DEPENDS
25+
libc.src.__support.integer_literals
26+
libc.src.__support.FPUtil.double_double
27+
libc.src.__support.FPUtil.dyadic_float
28+
libc.src.__support.FPUtil.multiply_add
29+
libc.src.__support.FPUtil.nearest_integer
30+
libc.src.__support.FPUtil.polyeval
31+
libc.src.__support.macros.optimization
32+
)
33+
134
add_header_library(
235
exp_float_constants
336
HDRS

libc/src/__support/math/acos.h

Lines changed: 285 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,285 @@
1+
//===-- Implementation header for acos --------------------------*- 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_ACOS_H
10+
#define LLVM_LIBC_SRC___SUPPORT_MATH_ACOS_H
11+
12+
#include "asin_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/dyadic_float.h"
17+
#include "src/__support/FPUtil/multiply_add.h"
18+
#include "src/__support/FPUtil/sqrt.h"
19+
#include "src/__support/macros/config.h"
20+
#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
21+
#include "src/__support/macros/properties/cpu_features.h" // LIBC_TARGET_CPU_HAS_FMA
22+
23+
namespace LIBC_NAMESPACE_DECL {
24+
25+
namespace math {
26+
27+
using DoubleDouble = fputil::DoubleDouble;
28+
using Float128 = fputil::DyadicFloat<128>;
29+
30+
static constexpr double acos(double x) {
31+
using FPBits = fputil::FPBits<double>;
32+
33+
FPBits xbits(x);
34+
int x_exp = xbits.get_biased_exponent();
35+
36+
// |x| < 0.5.
37+
if (x_exp < FPBits::EXP_BIAS - 1) {
38+
// |x| < 2^-55.
39+
if (LIBC_UNLIKELY(x_exp < FPBits::EXP_BIAS - 55)) {
40+
// When |x| < 2^-55, acos(x) = pi/2
41+
#if defined(LIBC_MATH_HAS_SKIP_ACCURATE_PASS)
42+
return PI_OVER_TWO.hi;
43+
#else
44+
// Force the evaluation and prevent constant propagation so that it
45+
// is rounded correctly for FE_UPWARD rounding mode.
46+
return (xbits.abs().get_val() + 0x1.0p-160) + PI_OVER_TWO.hi;
47+
#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS
48+
}
49+
50+
#ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
51+
// acos(x) = pi/2 - asin(x)
52+
// = pi/2 - x * P(x^2)
53+
double p = asin_eval(x * x);
54+
return PI_OVER_TWO.hi + fputil::multiply_add(-x, p, PI_OVER_TWO.lo);
55+
#else
56+
unsigned idx = 0;
57+
DoubleDouble x_sq = fputil::exact_mult(x, x);
58+
double err = xbits.abs().get_val() * 0x1.0p-51;
59+
// Polynomial approximation:
60+
// p ~ asin(x)/x
61+
DoubleDouble p = asin_eval(x_sq, idx, err);
62+
// asin(x) ~ x * p
63+
DoubleDouble r0 = fputil::exact_mult(x, p.hi);
64+
// acos(x) = pi/2 - asin(x)
65+
// ~ pi/2 - x * p
66+
// = pi/2 - x * (p.hi + p.lo)
67+
double r_hi = fputil::multiply_add(-x, p.hi, PI_OVER_TWO.hi);
68+
// Use Dekker's 2SUM algorithm to compute the lower part.
69+
double r_lo = ((PI_OVER_TWO.hi - r_hi) - r0.hi) - r0.lo;
70+
r_lo = fputil::multiply_add(-x, p.lo, r_lo + PI_OVER_TWO.lo);
71+
72+
// Ziv's accuracy test.
73+
74+
double r_upper = r_hi + (r_lo + err);
75+
double r_lower = r_hi + (r_lo - err);
76+
77+
if (LIBC_LIKELY(r_upper == r_lower))
78+
return r_upper;
79+
80+
// Ziv's accuracy test failed, perform 128-bit calculation.
81+
82+
// Recalculate mod 1/64.
83+
idx = static_cast<unsigned>(fputil::nearest_integer(x_sq.hi * 0x1.0p6));
84+
85+
// Get x^2 - idx/64 exactly. When FMA is available, double-double
86+
// multiplication will be correct for all rounding modes. Otherwise we use
87+
// Float128 directly.
88+
Float128 x_f128(x);
89+
90+
#ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE
91+
// u = x^2 - idx/64
92+
Float128 u_hi(
93+
fputil::multiply_add(static_cast<double>(idx), -0x1.0p-6, x_sq.hi));
94+
Float128 u = fputil::quick_add(u_hi, Float128(x_sq.lo));
95+
#else
96+
Float128 x_sq_f128 = fputil::quick_mul(x_f128, x_f128);
97+
Float128 u = fputil::quick_add(
98+
x_sq_f128, Float128(static_cast<double>(idx) * (-0x1.0p-6)));
99+
#endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE
100+
101+
Float128 p_f128 = asin_eval(u, idx);
102+
// Flip the sign of x_f128 to perform subtraction.
103+
x_f128.sign = x_f128.sign.negate();
104+
Float128 r =
105+
fputil::quick_add(PI_OVER_TWO_F128, fputil::quick_mul(x_f128, p_f128));
106+
107+
return static_cast<double>(r);
108+
#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS
109+
}
110+
// |x| >= 0.5
111+
112+
double x_abs = xbits.abs().get_val();
113+
114+
// Maintaining the sign:
115+
constexpr double SIGN[2] = {1.0, -1.0};
116+
double x_sign = SIGN[xbits.is_neg()];
117+
// |x| >= 1
118+
if (LIBC_UNLIKELY(x_exp >= FPBits::EXP_BIAS)) {
119+
// x = +-1, asin(x) = +- pi/2
120+
if (x_abs == 1.0) {
121+
// x = 1, acos(x) = 0,
122+
// x = -1, acos(x) = pi
123+
return x == 1.0 ? 0.0 : fputil::multiply_add(-x_sign, PI.hi, PI.lo);
124+
}
125+
// |x| > 1, return NaN.
126+
if (xbits.is_quiet_nan())
127+
return x;
128+
129+
// Set domain error for non-NaN input.
130+
if (!xbits.is_nan())
131+
fputil::set_errno_if_required(EDOM);
132+
133+
fputil::raise_except_if_required(FE_INVALID);
134+
return FPBits::quiet_nan().get_val();
135+
}
136+
137+
// When |x| >= 0.5, we perform range reduction as follow:
138+
//
139+
// When 0.5 <= x < 1, let:
140+
// y = acos(x)
141+
// We will use the double angle formula:
142+
// cos(2y) = 1 - 2 sin^2(y)
143+
// and the complement angle identity:
144+
// x = cos(y) = 1 - 2 sin^2 (y/2)
145+
// So:
146+
// sin(y/2) = sqrt( (1 - x)/2 )
147+
// And hence:
148+
// y/2 = asin( sqrt( (1 - x)/2 ) )
149+
// Equivalently:
150+
// acos(x) = y = 2 * asin( sqrt( (1 - x)/2 ) )
151+
// Let u = (1 - x)/2, then:
152+
// acos(x) = 2 * asin( sqrt(u) )
153+
// Moreover, since 0.5 <= x < 1:
154+
// 0 < u <= 1/4, and 0 < sqrt(u) <= 0.5,
155+
// And hence we can reuse the same polynomial approximation of asin(x) when
156+
// |x| <= 0.5:
157+
// acos(x) ~ 2 * sqrt(u) * P(u).
158+
//
159+
// When -1 < x <= -0.5, we reduce to the previous case using the formula:
160+
// acos(x) = pi - acos(-x)
161+
// = pi - 2 * asin ( sqrt( (1 + x)/2 ) )
162+
// ~ pi - 2 * sqrt(u) * P(u),
163+
// where u = (1 - |x|)/2.
164+
165+
// u = (1 - |x|)/2
166+
double u = fputil::multiply_add(x_abs, -0.5, 0.5);
167+
// v_hi + v_lo ~ sqrt(u).
168+
// Let:
169+
// h = u - v_hi^2 = (sqrt(u) - v_hi) * (sqrt(u) + v_hi)
170+
// Then:
171+
// sqrt(u) = v_hi + h / (sqrt(u) + v_hi)
172+
// ~ v_hi + h / (2 * v_hi)
173+
// So we can use:
174+
// v_lo = h / (2 * v_hi).
175+
double v_hi = fputil::sqrt<double>(u);
176+
177+
#ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
178+
constexpr DoubleDouble CONST_TERM[2] = {{0.0, 0.0}, PI};
179+
DoubleDouble const_term = CONST_TERM[xbits.is_neg()];
180+
181+
double p = asin_eval(u);
182+
double scale = x_sign * 2.0 * v_hi;
183+
double r = const_term.hi + fputil::multiply_add(scale, p, const_term.lo);
184+
return r;
185+
#else
186+
187+
#ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE
188+
double h = fputil::multiply_add(v_hi, -v_hi, u);
189+
#else
190+
DoubleDouble v_hi_sq = fputil::exact_mult(v_hi, v_hi);
191+
double h = (u - v_hi_sq.hi) - v_hi_sq.lo;
192+
#endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE
193+
194+
// Scale v_lo and v_hi by 2 from the formula:
195+
// vh = v_hi * 2
196+
// vl = 2*v_lo = h / v_hi.
197+
double vh = v_hi * 2.0;
198+
double vl = h / v_hi;
199+
200+
// Polynomial approximation:
201+
// p ~ asin(sqrt(u))/sqrt(u)
202+
unsigned idx = 0;
203+
double err = vh * 0x1.0p-51;
204+
205+
DoubleDouble p = asin_eval(DoubleDouble{0.0, u}, idx, err);
206+
207+
// Perform computations in double-double arithmetic:
208+
// asin(x) = pi/2 - (v_hi + v_lo) * (ASIN_COEFFS[idx][0] + p)
209+
DoubleDouble r0 = fputil::quick_mult(DoubleDouble{vl, vh}, p);
210+
211+
double r_hi = 0, r_lo = 0;
212+
if (xbits.is_pos()) {
213+
r_hi = r0.hi;
214+
r_lo = r0.lo;
215+
} else {
216+
DoubleDouble r = fputil::exact_add(PI.hi, -r0.hi);
217+
r_hi = r.hi;
218+
r_lo = (PI.lo - r0.lo) + r.lo;
219+
}
220+
221+
// Ziv's accuracy test.
222+
223+
double r_upper = r_hi + (r_lo + err);
224+
double r_lower = r_hi + (r_lo - err);
225+
226+
if (LIBC_LIKELY(r_upper == r_lower))
227+
return r_upper;
228+
229+
// Ziv's accuracy test failed, we redo the computations in Float128.
230+
// Recalculate mod 1/64.
231+
idx = static_cast<unsigned>(fputil::nearest_integer(u * 0x1.0p6));
232+
233+
// After the first step of Newton-Raphson approximating v = sqrt(u), we have
234+
// that:
235+
// sqrt(u) = v_hi + h / (sqrt(u) + v_hi)
236+
// v_lo = h / (2 * v_hi)
237+
// With error:
238+
// sqrt(u) - (v_hi + v_lo) = h * ( 1/(sqrt(u) + v_hi) - 1/(2*v_hi) )
239+
// = -h^2 / (2*v * (sqrt(u) + v)^2).
240+
// Since:
241+
// (sqrt(u) + v_hi)^2 ~ (2sqrt(u))^2 = 4u,
242+
// we can add another correction term to (v_hi + v_lo) that is:
243+
// v_ll = -h^2 / (2*v_hi * 4u)
244+
// = -v_lo * (h / 4u)
245+
// = -vl * (h / 8u),
246+
// making the errors:
247+
// sqrt(u) - (v_hi + v_lo + v_ll) = O(h^3)
248+
// well beyond 128-bit precision needed.
249+
250+
// Get the rounding error of vl = 2 * v_lo ~ h / vh
251+
// Get full product of vh * vl
252+
#ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE
253+
double vl_lo = fputil::multiply_add(-v_hi, vl, h) / v_hi;
254+
#else
255+
DoubleDouble vh_vl = fputil::exact_mult(v_hi, vl);
256+
double vl_lo = ((h - vh_vl.hi) - vh_vl.lo) / v_hi;
257+
#endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE
258+
// vll = 2*v_ll = -vl * (h / (4u)).
259+
double t = h * (-0.25) / u;
260+
double vll = fputil::multiply_add(vl, t, vl_lo);
261+
// m_v = -(v_hi + v_lo + v_ll).
262+
Float128 m_v = fputil::quick_add(
263+
Float128(vh), fputil::quick_add(Float128(vl), Float128(vll)));
264+
m_v.sign = xbits.sign();
265+
266+
// Perform computations in Float128:
267+
// acos(x) = (v_hi + v_lo + vll) * P(u) , when 0.5 <= x < 1,
268+
// = pi - (v_hi + v_lo + vll) * P(u) , when -1 < x <= -0.5.
269+
Float128 y_f128(fputil::multiply_add(static_cast<double>(idx), -0x1.0p-6, u));
270+
271+
Float128 p_f128 = asin_eval(y_f128, idx);
272+
Float128 r_f128 = fputil::quick_mul(m_v, p_f128);
273+
274+
if (xbits.is_neg())
275+
r_f128 = fputil::quick_add(PI_F128, r_f128);
276+
277+
return static_cast<double>(r_f128);
278+
#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS
279+
}
280+
281+
} // namespace math
282+
283+
} // namespace LIBC_NAMESPACE_DECL
284+
285+
#endif // LLVM_LIBC_SRC___SUPPORT_MATH_ACOS_H

0 commit comments

Comments
 (0)