1//===-- Implementation header for asinpi ------------------------*- 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_ASINPI_H
10#define LLVM_LIBC_SRC___SUPPORT_MATH_ASINPI_H
11
12#include "hdr/errno_macros.h"
13#include "hdr/fenv_macros.h"
14#include "src/__support/FPUtil/FEnvImpl.h"
15#include "src/__support/FPUtil/FPBits.h"
16#include "src/__support/FPUtil/double_double.h"
17#include "src/__support/FPUtil/dyadic_float.h"
18#include "src/__support/FPUtil/multiply_add.h"
19#include "src/__support/FPUtil/nearest_integer.h"
20#include "src/__support/FPUtil/rounding_mode.h"
21#include "src/__support/FPUtil/sqrt.h"
22#include "src/__support/macros/attributes.h"
23#include "src/__support/macros/config.h"
24#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
25#include "src/__support/macros/properties/cpu_features.h" // LIBC_TARGET_CPU_HAS_FMA
26#include "src/__support/math/asin_utils.h"
27#include "src/__support/sign.h"
28
29namespace LIBC_NAMESPACE_DECL {
30
31namespace math {
32
33LIBC_INLINE double asinpi(double x) {
34 using namespace asin_internal;
35 using FPBits = fputil::FPBits<double>;
36
37 FPBits xbits(x);
38 int x_exp = xbits.get_biased_exponent();
39
40 // |x| < 0.5.
41 if (x_exp < FPBits::EXP_BIAS - 1) {
42 // |x| < 2^-26.
43 if (LIBC_UNLIKELY(x_exp < FPBits::EXP_BIAS - 26)) {
44 // asinpi(+-0) = +-0.
45 if (LIBC_UNLIKELY(xbits.abs().uintval() == 0))
46 return x;
47 // When |x| < 2^-26, asinpi(x) ~ x/pi.
48 // The relative error of x/pi is:
49 // |asinpi(x) - x/pi| / |asinpi(x)| < x^2/6 < 2^-54.
50#ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
51 return x * asin_internal::ASINPIF_COEFFS[0];
52#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS
53 }
54
55#ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
56 double xsq = x * x;
57 return x * fputil::multiply_add(xsq, asin_internal::asinpi_eval(xsq),
58 asin_internal::ASINPIF_COEFFS[0]);
59#else
60 using DFloat128 = fputil::DyadicFloat<128>;
61 using DoubleDouble = fputil::DoubleDouble;
62
63 // For |x| < 2^-511, x^2 would underflow to subnormal, raising a
64 // spurious underflow exception. Since asinpi(x) = x/pi with correction
65 // x^2/(6*pi) < 2^-1024 relative (negligible), compute x/pi directly
66 // in DFloat128.
67 if (LIBC_UNLIKELY(x_exp < 512)) {
68 DFloat128 x_f128(x);
69 DFloat128 r = fputil::quick_mul(a: x_f128, b: ONE_OVER_PI_F128);
70 double result = static_cast<double>(r);
71
72 // IEEE 754 "after rounding" tininess: the 53-bit unlimited-exponent
73 // result is strictly between +-2^-1022. DyadicFloat's conversion
74 // checks the *IEEE subnormal* result (52-bit at the boundary), not
75 // the 53-bit unlimited-exponent result, so we detect it here.
76 int exp_hi = r.exponent + 127 + FPBits::EXP_BIAS;
77 if (LIBC_UNLIKELY(exp_hi <= 0) && !r.mantissa.is_zero()) {
78 bool raise_underflow = true;
79 // When exp_hi == 0, a carry in 53-bit rounding can push the
80 // result to exactly 2^-1022 (not tiny). Check for this.
81 if (exp_hi == 0) {
82 constexpr unsigned SHIFT_53 = 128 - FPBits::SIG_LEN - 1;
83 using MantT = typename DFloat128::MantissaType;
84 MantT m53 = r.mantissa >> SHIFT_53;
85 constexpr MantT ALL_ONES_53 = (MantT(1) << (FPBits::SIG_LEN + 1)) - 1;
86 if (m53 == ALL_ONES_53) {
87 // All 53 bits set. carry happens if rounding rounds away
88 // from zero at this precision.
89 bool round_bit =
90 static_cast<bool>((r.mantissa >> (SHIFT_53 - 1)) & 1);
91 MantT sticky_mask = (MantT(1) << (SHIFT_53 - 1)) - 1;
92 bool sticky = (r.mantissa & sticky_mask) != 0;
93 bool lsb = static_cast<bool>(m53 & 1);
94#ifdef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
95 // Carry if round_bit && (lsb || sticky) (round half to even).
96 raise_underflow = !(round_bit && (lsb || sticky));
97#else
98 switch (fputil::quick_get_round()) {
99 case FE_TONEAREST:
100 // Carry if round_bit && (lsb || sticky) (round half to even).
101 raise_underflow = !(round_bit && (lsb || sticky));
102 break;
103 case FE_UPWARD:
104 raise_underflow = xbits.is_neg() || !(round_bit || sticky);
105 break;
106 case FE_DOWNWARD:
107 raise_underflow = !xbits.is_neg() || !(round_bit || sticky);
108 break;
109 case FE_TOWARDZERO:
110 default:
111 raise_underflow = true; // truncation never carries
112 break;
113 }
114#endif // LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
115 }
116 }
117 if (raise_underflow)
118 fputil::raise_underflow_except_if_required<double>();
119 }
120 return result;
121 }
122
123 unsigned idx = 0;
124 DoubleDouble x_sq = fputil::exact_mult(a: x, b: x);
125 double err = xbits.abs().get_val() * 0x1.0p-51;
126 // Polynomial approximation:
127 // p ~ asin(x)/(pi*x)
128
129 DoubleDouble p = asinpi_eval(u: x_sq, idx, err);
130 // asinpi(x) ~ x * p
131 DoubleDouble r0 = fputil::exact_mult(a: x, b: p.hi);
132 double r_lo = fputil::multiply_add(x, y: p.lo, z: r0.lo);
133
134 // Ziv's accuracy test.
135 double r_upper = r0.hi + (r_lo + err);
136 double r_lower = r0.hi + (r_lo - err);
137
138 if (LIBC_LIKELY(r_upper == r_lower))
139 return r_upper;
140
141 // Ziv's accuracy test failed, perform 128-bit calculation.
142
143 // Recalculate mod 1/64.
144 idx = static_cast<unsigned>(fputil::nearest_integer(x: x_sq.hi * 0x1.0p6));
145
146 DFloat128 x_f128(x);
147
148#ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE
149 DFloat128 u_hi(
150 fputil::sub_multiply(x_sq.hi, static_cast<double>(idx), 0x1.0p-6));
151 DFloat128 u = fputil::quick_add(u_hi, DFloat128(x_sq.lo));
152#else
153 DFloat128 x_sq_f128 = fputil::quick_mul(a: x_f128, b: x_f128);
154 DFloat128 u = fputil::quick_add(
155 a: x_sq_f128, b: DFloat128(static_cast<double>(idx) * (-0x1.0p-6)));
156#endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE
157
158 DFloat128 p_f128 = asinpi_eval(u, idx);
159 DFloat128 r = fputil::quick_mul(a: x_f128, b: p_f128);
160
161 return static_cast<double>(r);
162#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS
163 }
164 // |x| >= 0.5
165
166 double x_abs = xbits.abs().get_val();
167
168 // Maintaining the sign:
169 constexpr double SIGN[2] = {1.0, -1.0};
170 double x_sign = SIGN[xbits.is_neg()];
171
172 // |x| >= 1
173 if (LIBC_UNLIKELY(x_exp >= FPBits::EXP_BIAS)) {
174 // x = +-1, asinpi(x) = +- 0.5
175 if (x_abs == 1.0) {
176 return x_sign * 0.5;
177 }
178 // |x| > 1, return NaN.
179 if (xbits.is_quiet_nan())
180 return x;
181
182 // Set domain error for non-NaN input.
183 if (!xbits.is_nan())
184 fputil::set_errno_if_required(EDOM);
185
186 fputil::raise_except_if_required(FE_INVALID);
187 return FPBits::quiet_nan().get_val();
188 }
189
190 // When |x| >= 0.5, we perform range reduction as follow:
191 //
192 // Assume further that 0.5 <= x < 1, and let:
193 // y = asin(x)
194 // Using the identity:
195 // asin(x) = pi/2 - 2 * asin( sqrt( (1 - x)/2 ) )
196 // We get:
197 // asinpi(x) = asin(x)/pi = 0.5 - 2 * asin(sqrt(u)) / pi
198 // = 0.5 - 2 * sqrt(u) * [asin(sqrt(u)) / (pi * sqrt(u))]
199 // = 0.5 - 2 * sqrt(u) * asinpi_eval(u)
200 // where u = (1 - |x|) / 2.
201
202 // u = (1 - |x|)/2
203 double u = fputil::sub_multiply(x: 0.5, y: x_abs, z: 0.5);
204 // v_hi ~ sqrt(u).
205 double v_hi = fputil::sqrt<double>(x: u);
206
207#ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
208 double neg2_v = -2.0 * v_hi;
209 double r = x_sign * fputil::multiply_add(
210 neg2_v * u, asin_internal::asinpi_eval(u),
211 0.5 + neg2_v * asin_internal::ASINPIF_COEFFS[0]);
212 return r;
213#else
214 using DFloat128 = fputil::DyadicFloat<128>;
215 using DoubleDouble = fputil::DoubleDouble;
216
217#ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE
218 double h = fputil::sub_multiply(u, v_hi, v_hi);
219#else
220 DoubleDouble v_hi_sq = fputil::exact_mult(a: v_hi, b: v_hi);
221 double h = (u - v_hi_sq.hi) - v_hi_sq.lo;
222#endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE
223
224 // Scale v_lo and v_hi by 2 from the formula:
225 // vh = v_hi * 2
226 // vl = 2*v_lo = h / v_hi.
227 double vh = v_hi * 2.0;
228 double vl = h / v_hi;
229
230 // Polynomial approximation:
231 // p ~ asin(sqrt(u))/(pi*sqrt(u))
232 unsigned idx = 0;
233 double err = vh * 0x1.0p-51;
234
235 DoubleDouble p = asinpi_eval(u: DoubleDouble{.lo: 0.0, .hi: u}, idx, err);
236
237 // Perform computations in double-double arithmetic:
238 // asinpi(x) = 0.5 - (vh + vl) * p
239 DoubleDouble r0 = fputil::quick_mult(a: DoubleDouble{.lo: vl, .hi: vh}, b: p);
240 DoubleDouble r = fputil::exact_add(a: 0.5, b: -r0.hi);
241
242 double r_lo = -r0.lo + r.lo;
243
244 // Ziv's accuracy test.
245
246#ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE
247 double r_upper = fputil::multiply_add(
248 r.hi, x_sign, fputil::multiply_add(r_lo, x_sign, err));
249 double r_lower = fputil::multiply_add(
250 r.hi, x_sign, fputil::multiply_sub(r_lo, x_sign, err));
251#else
252 r_lo *= x_sign;
253 r.hi *= x_sign;
254 double r_upper = r.hi + (r_lo + err);
255 double r_lower = r.hi + (r_lo - err);
256#endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE
257
258 if (LIBC_LIKELY(r_upper == r_lower))
259 return r_upper;
260
261 // Ziv's accuracy test failed, we redo the computations in DFloat128.
262 // Recalculate mod 1/64.
263 idx = static_cast<unsigned>(fputil::nearest_integer(x: u * 0x1.0p6));
264
265 // After the first step of Newton-Raphson approximating v = sqrt(u):
266 // sqrt(u) = v_hi + h / (sqrt(u) + v_hi)
267 // v_lo = h / (2 * v_hi)
268 // Add second-order correction:
269 // v_ll = -v_lo * (h / (4u))
270
271 // Get the rounding error of vl = 2 * v_lo ~ h / vh
272#ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE
273 double vl_lo = fputil::sub_multiply(h, v_hi, vl) / v_hi;
274#else
275 DoubleDouble vh_vl = fputil::exact_mult(a: v_hi, b: vl);
276 double vl_lo = ((h - vh_vl.hi) - vh_vl.lo) / v_hi;
277#endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE
278 // vll = 2*v_ll = -vl * (h / (4u)).
279 double t = h * (-0.25) / u;
280 double vll = fputil::multiply_add(x: vl, y: t, z: vl_lo);
281 // m_v = -(v_hi + v_lo + v_ll).
282 DFloat128 m_v = fputil::quick_add(
283 a: DFloat128(vh), b: fputil::quick_add(a: DFloat128(vl), b: DFloat128(vll)));
284 m_v.sign = Sign::NEG;
285
286 // Perform computations in DFloat128:
287 // asinpi(x) = 0.5 - (v_hi + v_lo + vll) * P_pi(u).
288 DFloat128 y_f128(fputil::sub_multiply(x: u, y: static_cast<double>(idx), z: 0x1.0p-6));
289
290 DFloat128 p_f128 = asinpi_eval(u: y_f128, idx);
291 DFloat128 r0_f128 = fputil::quick_mul(a: m_v, b: p_f128);
292 DFloat128 r_f128 = fputil::quick_add(a: HALF_F128, b: r0_f128);
293
294 if (xbits.is_neg())
295 r_f128.sign = Sign::NEG;
296
297 return static_cast<double>(r_f128);
298#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS
299}
300
301} // namespace math
302
303} // namespace LIBC_NAMESPACE_DECL
304
305#endif // LLVM_LIBC_SRC___SUPPORT_MATH_ASINPI_H
306