Skip to content

Commit 84ecd1c

Browse files
committed
[libc][math] Implement double precision asin correctly rounded for all rounding modes.
1 parent 7491ff7 commit 84ecd1c

File tree

14 files changed

+982
-1
lines changed

14 files changed

+982
-1
lines changed

libc/config/darwin/arm/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ set(TARGET_LIBM_ENTRYPOINTS
137137
# math.h entrypoints
138138
libc.src.math.acosf
139139
libc.src.math.acoshf
140+
libc.src.math.asin
140141
libc.src.math.asinf
141142
libc.src.math.asinhf
142143
libc.src.math.atan2

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ set(TARGET_LIBM_ENTRYPOINTS
412412
# math.h entrypoints
413413
libc.src.math.acosf
414414
libc.src.math.acoshf
415+
libc.src.math.asin
415416
libc.src.math.asinf
416417
libc.src.math.asinhf
417418
libc.src.math.atan2

libc/config/linux/arm/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ set(TARGET_LIBM_ENTRYPOINTS
244244
# math.h entrypoints
245245
libc.src.math.acosf
246246
libc.src.math.acoshf
247+
libc.src.math.asin
247248
libc.src.math.asinf
248249
libc.src.math.asinhf
249250
libc.src.math.atan2

libc/config/linux/riscv/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ set(TARGET_LIBM_ENTRYPOINTS
398398
# math.h entrypoints
399399
libc.src.math.acosf
400400
libc.src.math.acoshf
401+
libc.src.math.asin
401402
libc.src.math.asinf
402403
libc.src.math.asinhf
403404
libc.src.math.atan2

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,7 @@ set(TARGET_LIBM_ENTRYPOINTS
417417
# math.h entrypoints
418418
libc.src.math.acosf
419419
libc.src.math.acoshf
420+
libc.src.math.asin
420421
libc.src.math.asinf
421422
libc.src.math.asinhf
422423
libc.src.math.atan2

libc/config/windows/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ set(TARGET_LIBM_ENTRYPOINTS
129129
# math.h entrypoints
130130
libc.src.math.acosf
131131
libc.src.math.acoshf
132+
libc.src.math.asin
132133
libc.src.math.asinf
133134
libc.src.math.asinhf
134135
libc.src.math.atan2

libc/docs/headers/math/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ Higher Math Functions
255255
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
256256
| acospi | | | | | | 7.12.4.8 | F.10.1.8 |
257257
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
258-
| asin | |check| | | | |check| | | 7.12.4.2 | F.10.1.2 |
258+
| asin | |check| | |check| | | | | 7.12.4.2 | F.10.1.2 |
259259
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
260260
| asinh | |check| | | | |check| | | 7.12.5.2 | F.10.2.2 |
261261
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+

libc/src/math/generic/CMakeLists.txt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4076,6 +4076,35 @@ add_entrypoint_object(
40764076
libc.src.__support.macros.properties.types
40774077
)
40784078

4079+
add_header_library(
4080+
asin_utils
4081+
HDRS
4082+
atan_utils.h
4083+
DEPENDS
4084+
libc.src.__support.integer_literals
4085+
libc.src.__support.FPUtil.double_double
4086+
libc.src.__support.FPUtil.dyadic_float
4087+
libc.src.__support.FPUtil.multiply_add
4088+
libc.src.__support.FPUtil.nearest_integer
4089+
libc.src.__support.FPUtil.polyeval
4090+
libc.src.__support.macros.optimization
4091+
)
4092+
4093+
add_entrypoint_object(
4094+
asin
4095+
SRCS
4096+
asin.cpp
4097+
HDRS
4098+
../asin.h
4099+
DEPENDS
4100+
libc.src.__support.FPUtil.fp_bits
4101+
libc.src.__support.FPUtil.multiply_add
4102+
libc.src.__support.FPUtil.polyeval
4103+
libc.src.__support.FPUtil.sqrt
4104+
libc.src.__support.macros.optimization
4105+
.inv_trigf_utils
4106+
)
4107+
40794108
add_entrypoint_object(
40804109
acosf
40814110
SRCS

libc/src/math/generic/asin.cpp

Lines changed: 257 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,257 @@
1+
//===-- Double-precision asin 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/asin.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, asin, (double x)) {
28+
using FPBits = typename 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^-26.
36+
if (LIBC_UNLIKELY(x_exp < FPBits::EXP_BIAS - 26)) {
37+
// When |x| < 2^-26, the relative error of the approximation asin(x) ~ x
38+
// is:
39+
// |asin(x) - x| / |asin(x)| < |x^3| / (6|x|)
40+
// = x^2 / 6
41+
// < 2^-54
42+
// < epsilon(1)/2.
43+
// So the correctly rounded values of asin(x) are:
44+
// = x + sign(x)*eps(x) if rounding mode = FE_TOWARDZERO,
45+
// or (rounding mode = FE_UPWARD and x is
46+
// negative),
47+
// = x otherwise.
48+
// To simplify the rounding decision and make it more efficient, we use
49+
// fma(x, 2^-54, x) instead.
50+
// Note: to use the formula x + 2^-54*x to decide the correct rounding, we
51+
// do need fma(x, 2^-54, x) to prevent underflow caused by 2^-54*x when
52+
// |x| < 2^-1022. For targets without FMA instructions, when x is close to
53+
// denormal range, we normalize x,
54+
#if defined(LIBC_MATH_HAS_SKIP_ACCURATE_PASS)
55+
return x;
56+
#elif defined(LIBC_TARGET_CPU_HAS_FMA_DOUBLE)
57+
return fputil::multiply_add(x, 0x1.0p-54, x);
58+
#else
59+
if (x == 0.0)
60+
return x;
61+
// Get sign(x) * min_normal.
62+
FPBits eps_bits = FPBits::min_normal();
63+
eps_bits.set_sign(xbits.sign());
64+
double eps = eps_bits.get_val();
65+
double normalize_const = (x_exp == 0) ? eps : 0.0;
66+
double scaled_normal =
67+
fputil::multiply_add(x + normalize_const, 0x1.0p54, eps);
68+
return fputil::multiply_add(scaled_normal, 0x1.0p-54, -normalize_const);
69+
#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS
70+
}
71+
72+
unsigned idx;
73+
DoubleDouble x_sq = fputil::exact_mult(x, x);
74+
double err = x * 0x1.0p-52;
75+
// Polynomial approximation:
76+
// p ~ asin(x)/x - ASIN_COEFFS[idx][0]
77+
double p = asin_eval(x_sq, idx, err);
78+
// asin(x) ~ x * (ASIN_COEFFS[idx][0] + p)
79+
DoubleDouble r0 = fputil::exact_mult(x, ASIN_COEFFS[idx][0]);
80+
double r_lo = fputil::multiply_add(x, p, r0.lo);
81+
82+
#ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
83+
return r0.hi + r_lo;
84+
#else
85+
// Ziv's accuracy test.
86+
87+
double r_upper = r0.hi + (r_lo + err);
88+
double r_lower = r0.hi + (r_lo - err);
89+
90+
if (LIBC_LIKELY(r_upper == r_lower))
91+
return r_upper;
92+
93+
// Ziv's accuracy test failed, perform 128-bit calculation.
94+
95+
// Get x^2 - idx/64 exactly. When FMA is available, double-double
96+
// multiplication will be correct for all rounding modes. Otherwise we use
97+
// Float128 directly.
98+
Float128 x_f128(x);
99+
100+
#ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE
101+
// u = x^2 - idx/64
102+
Float128 u_hi(
103+
fputil::multiply_add(static_cast<double>(idx), -0x1.0p-6, x_sq.hi));
104+
Float128 u = fputil::quick_add(u_hi, Float128(x_sq.lo));
105+
#else
106+
Float128 x_sq_f128 = fputil::quick_mul(x_f128, x_f128);
107+
Float128 u = fputil::quick_add(
108+
x_sq_f128, Float128(static_cast<double>(idx) * (-0x1.0p-6)));
109+
#endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE
110+
111+
Float128 p_f128 = asin_eval(u, idx);
112+
Float128 r = fputil::quick_mul(x_f128, p_f128);
113+
114+
return static_cast<double>(r);
115+
#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS
116+
}
117+
// |x| >= 0.5
118+
119+
double x_abs = xbits.abs().get_val();
120+
121+
// |x| >= 1
122+
if (LIBC_UNLIKELY(x_exp >= FPBits::EXP_BIAS)) {
123+
// x = +-1, asin(x) = +- pi/2
124+
if (x_abs == 1.0) {
125+
// return +- pi/2
126+
}
127+
// |x| > 1, return NaN.
128+
if (xbits.is_finite()) {
129+
fputil::set_errno_if_required(EDOM);
130+
fputil::raise_except_if_required(FE_INVALID);
131+
}
132+
return FPBits::quiet_nan().get_val();
133+
}
134+
135+
// When |x| >= 0.5, we perform range reduction as follow:
136+
//
137+
// Assume further that 0.5 < x <= 1, and let:
138+
// y = asin(x)
139+
// We will use the double angle formula:
140+
// cos(2y) = 1 - 2 sin^2(y)
141+
// and the complement angle identity:
142+
// x = sin(y) = cos(pi/2 - y)
143+
// = 1 - 2 sin^2 (pi/4 - y/2)
144+
// So:
145+
// sin(pi/4 - y/2) = sqrt( (1 - x)/2 )
146+
// And hence:
147+
// pi/4 - y/2 = asin( sqrt( (1 - x)/2 ) )
148+
// Equivalently:
149+
// asin(x) = y = pi/2 - 2 * asin( sqrt( (1 - x)/2 ) )
150+
// Let u = (1 - x)/2, then:
151+
// asin(x) = pi/2 - 2 * asin( sqrt(u) )
152+
// Moreover, since 0.5 < x <= 1:
153+
// 0 <= u < 1/4, and 0 <= sqrt(u) < 0.5,
154+
// And hence we can reuse the same polynomial approximation of asin(x) when
155+
// |x| <= 0.5:
156+
// asin(x) ~ pi/2 - 2 * sqrt(u) * P(u),
157+
158+
// u = (1 - |x|)/2
159+
double u = fputil::multiply_add(x_abs, -0.5, 0.5);
160+
// v_hi + v_lo ~ sqrt(u).
161+
// Let:
162+
// h = u - v_hi^2 = (sqrt(u) - v_hi) * (sqrt(u) + v_hi)
163+
// Then:
164+
// sqrt(u) = v_hi + h / (sqrt(u) + v_hi)
165+
// ~ v_hi + h / (2 * v_hi)
166+
// So we can use:
167+
// v_lo = h / (2 * v_hi).
168+
// Then,
169+
// asin(x) ~ pi/2 - 2*(v_hi + v_lo) * P(u)
170+
double v_hi = fputil::sqrt<double>(u);
171+
#ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE
172+
double h = fputil::multiply_add(v_hi, -v_hi, u);
173+
#else
174+
DoubleDouble v_hi_sq = fputil::exact_mult(v_hi, v_hi);
175+
double h = (u - v_hi_sq.hi) - v_hi_sq.lo;
176+
#endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE
177+
178+
// Scale v_lo and v_hi by 2 from the formula:
179+
// vh = v_hi * 2
180+
// vl = 2*v_lo = h / v_hi.
181+
double vh = v_hi * 2.0;
182+
double vl = h / v_hi;
183+
184+
// Polynomial approximation:
185+
// p ~ asin(sqrt(u))/sqrt(u) - ASIN_COEFFS[idx][0]
186+
unsigned idx;
187+
[[maybe_unused]] double err = vh * 0x1.0p-52;
188+
double p = asin_eval(DoubleDouble{0.0, u}, idx, err);
189+
190+
// Perform computations in double-double arithmetic:
191+
// asin(x) = pi/2 - (v_hi + v_lo) * (ASIN_COEFFS[idx][0] + p)
192+
DoubleDouble r0 = fputil::exact_mult(vh, ASIN_COEFFS[idx][0]);
193+
DoubleDouble r = fputil::exact_add(PI_OVER_TWO.hi, -r0.hi);
194+
195+
// Combining all the lower terms.
196+
double lo = r.lo - fputil::multiply_add(vl, ASIN_COEFFS[idx][0],
197+
r0.lo - PI_OVER_TWO.lo);
198+
double r_lo = fputil::multiply_add(vh, -p, lo);
199+
200+
#ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
201+
#else
202+
// Ziv's accuracy test.
203+
204+
double r_upper = r.hi + (r_lo + err);
205+
double r_lower = r.hi + (r_lo - err);
206+
207+
if (LIBC_LIKELY(r_upper == r_lower))
208+
return r_upper;
209+
210+
// Ziv's accuracy test failed, we redo the computations in Float128.
211+
212+
// After the first step of Newton-Raphson approximating v = sqrt(u), we have
213+
// that:
214+
// sqrt(u) = v_hi + h / (sqrt(u) + v_hi)
215+
// v_lo = h / (2 * v_hi)
216+
// With error:
217+
// sqrt(u) - (v_hi + v_lo) = h * ( 1/(sqrt(u) + v_hi) - 1/(2*v_hi) )
218+
// = -h^2 / (2*v * (sqrt(u) + v)^2).
219+
// Since:
220+
// (sqrt(u) + v_hi)^2 ~ (2sqrt(u))^2 = 4u,
221+
// we can add another correction term to (v_hi + v_lo) that is:
222+
// v_ll = -h^2 / (2*v_hi * 4u)
223+
// = -v_lo * (h / 4u)
224+
// = -vl * (h / 8u),
225+
// making the errors:
226+
// sqrt(u) - (v_hi + v_lo + v_ll) = O(h^3)
227+
// well beyond 128-bit precision needed.
228+
229+
// Get the rounding error of vl = 2 * v_lo ~ h / vh
230+
// Get full product of vh * vl
231+
#ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE
232+
double vl_lo = fputil::multiply_add(-v_hi, vl, h) / v_hi;
233+
#else
234+
DoubleDouble vh_vl = fputil::exact_mult(v_hi, vl);
235+
double vl_lo = ((h - vh_vl.hi) - vh_vl.lo) / v_hi;
236+
#endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE
237+
// vll = 2*v_ll = -vl * (h / (4u)).
238+
double t = h * (-0.25) / u;
239+
double vll = fputil::multiply_add(vl, t, vl_lo);
240+
// m_v = -(v_hi + v_lo + v_ll).
241+
Float128 m_v = fputil::quick_add(
242+
Float128(vh), fputil::quick_add(Float128(vl), Float128(vll)));
243+
m_v.sign = Sign::NEG;
244+
245+
// Perform computations in Float128:
246+
// asin(x) = pi/2 - (v_hi + v_lo + vll) * P(u).
247+
Float128 y_f128(fputil::multiply_add(static_cast<double>(idx), -0x1.0p-6, u));
248+
249+
Float128 p_f128 = asin_eval(y_f128, idx);
250+
Float128 r0_f128 = fputil::quick_mul(m_v, p_f128);
251+
Float128 r_f128 = fputil::quick_add(PI_OVER_TWO_F128, r0_f128);
252+
253+
return static_cast<double>(r_f128);
254+
#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS
255+
}
256+
257+
} // namespace LIBC_NAMESPACE_DECL

0 commit comments

Comments
 (0)