Path: blob/main/contrib/arm-optimized-routines/math/aarch64/experimental/asin_3u.c
48378 views
/*1* Double-precision asin(x) function.2*3* Copyright (c) 2023-2024, Arm Limited.4* SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception5*/67#include "poly_scalar_f64.h"8#include "math_config.h"9#include "test_sig.h"10#include "test_defs.h"1112#define AbsMask 0x7fffffffffffffff13#define Half 0x3fe000000000000014#define One 0x3ff000000000000015#define PiOver2 0x1.921fb54442d18p+016#define Small 0x3e50000000000000 /* 2^-26. */17#define Small16 0x3e5018#define QNaN 0x7ff81920/* Fast implementation of double-precision asin(x) based on polynomial21approximation.2223For x < Small, approximate asin(x) by x. Small = 2^-26 for correct rounding.2425For x in [Small, 0.5], use an order 11 polynomial P such that the final26approximation is an odd polynomial: asin(x) ~ x + x^3 P(x^2).2728The largest observed error in this region is 1.01 ulps,29asin(0x1.da9735b5a9277p-2) got 0x1.ed78525a927efp-230want 0x1.ed78525a927eep-2.3132No cheap approximation can be obtained near x = 1, since the function is not33continuously differentiable on 1.3435For x in [0.5, 1.0], we use a method based on a trigonometric identity3637asin(x) = pi/2 - acos(x)3839and a generalized power series expansion of acos(y) near y=1, that reads as4041acos(y)/sqrt(2y) ~ 1 + 1/12 * y + 3/160 * y^2 + ... (1)4243The Taylor series of asin(z) near z = 0, reads as4445asin(z) ~ z + z^3 P(z^2) = z + z^3 * (1/6 + 3/40 z^2 + ...).4647Therefore, (1) can be written in terms of P(y/2) or even asin(y/2)4849acos(y) ~ sqrt(2y) (1 + y/2 * P(y/2)) = 2 * sqrt(y/2) (1 + y/2 * P(y/2)5051Hence, if we write z = (1-x)/2, z is near 0 when x approaches 1 and5253asin(x) ~ pi/2 - acos(x) ~ pi/2 - 2 * sqrt(z) (1 + z * P(z)).5455The largest observed error in this region is 2.69 ulps,56asin(0x1.044e8cefee301p-1) got 0x1.1111dd54ddf96p-157want 0x1.1111dd54ddf99p-1. */58double59asin (double x)60{61uint64_t ix = asuint64 (x);62uint64_t ia = ix & AbsMask;63uint64_t ia16 = ia >> 48;64double ax = asdouble (ia);65uint64_t sign = ix & ~AbsMask;6667/* Special values and invalid range. */68if (unlikely (ia16 == QNaN))69return x;70if (ia > One)71return __math_invalid (x);72if (ia16 < Small16)73return x;7475/* Evaluate polynomial Q(x) = y + y * z * P(z) with76z2 = x ^ 2 and z = |x| , if |x| < 0.577z2 = (1 - |x|) / 2 and z = sqrt(z2), if |x| >= 0.5. */78double z2 = ax < 0.5 ? x * x : fma (-0.5, ax, 0.5);79double z = ax < 0.5 ? ax : sqrt (z2);8081/* Use a single polynomial approximation P for both intervals. */82double z4 = z2 * z2;83double z8 = z4 * z4;84double z16 = z8 * z8;85double p = estrin_11_f64 (z2, z4, z8, z16, __asin_poly);8687/* Finalize polynomial: z + z * z2 * P(z2). */88p = fma (z * z2, p, z);8990/* asin(|x|) = Q(|x|) , for |x| < 0.591= pi/2 - 2 Q(|x|), for |x| >= 0.5. */92double y = ax < 0.5 ? p : fma (-2.0, p, PiOver2);9394/* Copy sign. */95return asdouble (asuint64 (y) | sign);96}9798TEST_SIG (S, D, 1, asin, -1.0, 1.0)99TEST_ULP (asin, 2.20)100TEST_INTERVAL (asin, 0, Small, 5000)101TEST_INTERVAL (asin, Small, 0.5, 50000)102TEST_INTERVAL (asin, 0.5, 1.0, 50000)103TEST_INTERVAL (asin, 1.0, 0x1p11, 50000)104TEST_INTERVAL (asin, 0x1p11, inf, 20000)105TEST_INTERVAL (asin, -0, -inf, 20000)106107108