Path: blob/main/contrib/arm-optimized-routines/math/aarch64/experimental/asinf_2u5.c
48375 views
/*1* Single-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_f32.h"8#include "math_config.h"9#include "test_sig.h"10#include "test_defs.h"1112#define AbsMask 0x7fffffff13#define Half 0x3f00000014#define One 0x3f80000015#define PiOver2f 0x1.921fb6p+0f16#define Small 0x39800000 /* 2^-12. */17#define Small12 0x39818#define QNaN 0x7fc1920/* Fast implementation of single-precision asin(x) based on polynomial21approximation.2223For x < Small, approximate asin(x) by x. Small = 2^-12 for correct rounding.2425For x in [Small, 0.5], use order 4 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 0.83 ulps,29asinf(0x1.ea00f4p-2) got 0x1.fef15ep-2 want 0x1.fef15cp-2.3031No cheap approximation can be obtained near x = 1, since the function is not32continuously differentiable on 1.3334For x in [0.5, 1.0], we use a method based on a trigonometric identity3536asin(x) = pi/2 - acos(x)3738and a generalized power series expansion of acos(y) near y=1, that reads as3940acos(y)/sqrt(2y) ~ 1 + 1/12 * y + 3/160 * y^2 + ... (1)4142The Taylor series of asin(z) near z = 0, reads as4344asin(z) ~ z + z^3 P(z^2) = z + z^3 * (1/6 + 3/40 z^2 + ...).4546Therefore, (1) can be written in terms of P(y/2) or even asin(y/2)4748acos(y) ~ sqrt(2y) (1 + y/2 * P(y/2)) = 2 * sqrt(y/2) (1 + y/2 * P(y/2)4950Hence, if we write z = (1-x)/2, z is near 0 when x approaches 1 and5152asin(x) ~ pi/2 - acos(x) ~ pi/2 - 2 * sqrt(z) (1 + z * P(z)).5354The largest observed error in this region is 2.41 ulps,55asinf(0x1.00203ep-1) got 0x1.0c3a64p-1 want 0x1.0c3a6p-1. */56float57asinf (float x)58{59uint32_t ix = asuint (x);60uint32_t ia = ix & AbsMask;61uint32_t ia12 = ia >> 20;62float ax = asfloat (ia);63uint32_t sign = ix & ~AbsMask;6465/* Special values and invalid range. */66if (unlikely (ia12 == QNaN))67return x;68if (ia > One)69return __math_invalidf (x);70if (ia12 < Small12)71return x;7273/* Evaluate polynomial Q(x) = y + y * z * P(z) with74z2 = x ^ 2 and z = |x| , if |x| < 0.575z2 = (1 - |x|) / 2 and z = sqrt(z2), if |x| >= 0.5. */76float z2 = ax < 0.5 ? x * x : fmaf (-0.5f, ax, 0.5f);77float z = ax < 0.5 ? ax : sqrtf (z2);7879/* Use a single polynomial approximation P for both intervals. */80float p = horner_4_f32 (z2, __asinf_poly);81/* Finalize polynomial: z + z * z2 * P(z2). */82p = fmaf (z * z2, p, z);8384/* asin(|x|) = Q(|x|) , for |x| < 0.585= pi/2 - 2 Q(|x|), for |x| >= 0.5. */86float y = ax < 0.5 ? p : fmaf (-2.0f, p, PiOver2f);8788/* Copy sign. */89return asfloat (asuint (y) | sign);90}9192TEST_SIG (S, F, 1, asin, -1.0, 1.0)93TEST_ULP (asinf, 1.91)94TEST_INTERVAL (asinf, 0, Small, 5000)95TEST_INTERVAL (asinf, Small, 0.5, 50000)96TEST_INTERVAL (asinf, 0.5, 1.0, 50000)97TEST_INTERVAL (asinf, 1.0, 0x1p11, 50000)98TEST_INTERVAL (asinf, 0x1p11, inf, 20000)99TEST_INTERVAL (asinf, -0, -inf, 20000)100101102