Skip to content

Commit 75e4c19

Browse files
committed
[libc][math] Implement double precision acos correctly rounded for all rounding modes.
1 parent cb068dc commit 75e4c19

File tree

14 files changed

+467
-1
lines changed

14 files changed

+467
-1
lines changed

libc/config/darwin/arm/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ set(TARGET_LIBM_ENTRYPOINTS
135135
libc.src.fenv.feupdateenv
136136

137137
# math.h entrypoints
138+
libc.src.math.acos
138139
libc.src.math.acosf
139140
libc.src.math.acoshf
140141
libc.src.math.asin

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,7 @@ set(TARGET_LIBM_ENTRYPOINTS
410410
libc.src.fenv.feupdateenv
411411

412412
# math.h entrypoints
413+
libc.src.math.acos
413414
libc.src.math.acosf
414415
libc.src.math.acoshf
415416
libc.src.math.asin

libc/config/linux/arm/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ set(TARGET_LIBM_ENTRYPOINTS
242242
libc.src.fenv.feupdateenv
243243

244244
# math.h entrypoints
245+
libc.src.math.acos
245246
libc.src.math.acosf
246247
libc.src.math.acoshf
247248
libc.src.math.asin

libc/config/linux/riscv/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,7 @@ set(TARGET_LIBM_ENTRYPOINTS
396396
libc.src.fenv.feupdateenv
397397

398398
# math.h entrypoints
399+
libc.src.math.acos
399400
libc.src.math.acosf
400401
libc.src.math.acoshf
401402
libc.src.math.asin

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,7 @@ set(TARGET_LIBM_ENTRYPOINTS
415415
libc.src.fenv.feupdateenv
416416

417417
# math.h entrypoints
418+
libc.src.math.acos
418419
libc.src.math.acosf
419420
libc.src.math.acoshf
420421
libc.src.math.asin

libc/config/windows/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ set(TARGET_LIBM_ENTRYPOINTS
127127
libc.src.fenv.feupdateenv
128128

129129
# math.h entrypoints
130+
libc.src.math.acos
130131
libc.src.math.acosf
131132
libc.src.math.acoshf
132133
libc.src.math.asin

libc/docs/headers/math/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ Higher Math Functions
249249
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
250250
| <Func> | <Func_f> (float) | <Func> (double) | <Func_l> (long double) | <Func_f16> (float16) | <Func_f128> (float128) | C23 Definition Section | C23 Error Handling Section |
251251
+===========+==================+=================+========================+======================+========================+========================+============================+
252-
| acos | |check| | | | |check| | | 7.12.4.1 | F.10.1.1 |
252+
| acos | |check| | |check| | | |check| | | 7.12.4.1 | F.10.1.1 |
253253
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
254254
| acosh | |check| | | | |check| | | 7.12.5.1 | F.10.2.1 |
255255
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+

libc/src/math/generic/CMakeLists.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4117,6 +4117,7 @@ add_entrypoint_object(
41174117
HDRS
41184118
../asin.h
41194119
DEPENDS
4120+
.asin_utils
41204121
libc.src.__support.FPUtil.double_double
41214122
libc.src.__support.FPUtil.dyadic_float
41224123
libc.src.__support.FPUtil.fenv_impl
@@ -4164,6 +4165,26 @@ add_entrypoint_object(
41644165
libc.src.__support.macros.properties.types
41654166
)
41664167

4168+
add_entrypoint_object(
4169+
acos
4170+
SRCS
4171+
acos.cpp
4172+
HDRS
4173+
../acos.h
4174+
DEPENDS
4175+
.asin_utils
4176+
libc.src.__support.FPUtil.double_double
4177+
libc.src.__support.FPUtil.dyadic_float
4178+
libc.src.__support.FPUtil.fenv_impl
4179+
libc.src.__support.FPUtil.fp_bits
4180+
libc.src.__support.FPUtil.multiply_add
4181+
libc.src.__support.FPUtil.polyeval
4182+
libc.src.__support.FPUtil.sqrt
4183+
libc.src.__support.macros.optimization
4184+
libc.src.__support.macros.properties.types
4185+
libc.src.__support.macros.properties.cpu_features
4186+
)
4187+
41674188
add_entrypoint_object(
41684189
acospif16
41694190
SRCS

libc/src/math/generic/acos.cpp

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

libc/src/math/generic/asin_utils.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ namespace {
2525
using DoubleDouble = fputil::DoubleDouble;
2626
using Float128 = fputil::DyadicFloat<128>;
2727

28+
constexpr DoubleDouble PI = {0x1.1a62633145c07p-53, 0x1.921fb54442d18p1};
29+
2830
constexpr DoubleDouble PI_OVER_TWO = {0x1.1a62633145c07p-54,
2931
0x1.921fb54442d18p0};
3032

@@ -548,6 +550,9 @@ constexpr Float128 ASIN_COEFFS_F128[17][16] = {
548550
constexpr Float128 PI_OVER_TWO_F128 = {
549551
Sign::POS, -127, 0xc90fdaa2'2168c234'c4c6628b'80dc1cd1_u128};
550552

553+
constexpr Float128 PI_F128 = {Sign::POS, -126,
554+
0xc90fdaa2'2168c234'c4c6628b'80dc1cd1_u128};
555+
551556
LIBC_INLINE Float128 asin_eval(const Float128 &u, unsigned idx) {
552557
return fputil::polyeval(u, ASIN_COEFFS_F128[idx][0], ASIN_COEFFS_F128[idx][1],
553558
ASIN_COEFFS_F128[idx][2], ASIN_COEFFS_F128[idx][3],

0 commit comments

Comments
 (0)