Path: blob/main/contrib/arm-optimized-routines/math/aarch64/advsimd/acos.c
48378 views
/*1* Double-precision vector acos(x) function.2*3* Copyright (c) 2023-2024, Arm Limited.4* SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception5*/67#include "v_math.h"8#include "v_poly_f64.h"9#include "test_sig.h"10#include "test_defs.h"1112static const struct data13{14float64x2_t poly[12];15float64x2_t pi, pi_over_2;16uint64x2_t abs_mask;17} data = {18/* Polynomial approximation of (asin(sqrt(x)) - sqrt(x)) / (x * sqrt(x))19on [ 0x1p-106, 0x1p-2 ], relative error: 0x1.c3d8e169p-57. */20.poly = { V2 (0x1.555555555554ep-3), V2 (0x1.3333333337233p-4),21V2 (0x1.6db6db67f6d9fp-5), V2 (0x1.f1c71fbd29fbbp-6),22V2 (0x1.6e8b264d467d6p-6), V2 (0x1.1c5997c357e9dp-6),23V2 (0x1.c86a22cd9389dp-7), V2 (0x1.856073c22ebbep-7),24V2 (0x1.fd1151acb6bedp-8), V2 (0x1.087182f799c1dp-6),25V2 (-0x1.6602748120927p-7), V2 (0x1.cfa0dd1f9478p-6), },26.pi = V2 (0x1.921fb54442d18p+1),27.pi_over_2 = V2 (0x1.921fb54442d18p+0),28.abs_mask = V2 (0x7fffffffffffffff),29};3031#define AllMask v_u64 (0xffffffffffffffff)32#define Oneu 0x3ff000000000000033#define Small 0x3e50000000000000 /* 2^-53. */3435#if WANT_SIMD_EXCEPT36static float64x2_t VPCS_ATTR NOINLINE37special_case (float64x2_t x, float64x2_t y, uint64x2_t special)38{39return v_call_f64 (acos, x, y, special);40}41#endif4243/* Double-precision implementation of vector acos(x).4445For |x| < Small, approximate acos(x) by pi/2 - x. Small = 2^-53 for correct46rounding.47If WANT_SIMD_EXCEPT = 0, Small = 0 and we proceed with the following48approximation.4950For |x| in [Small, 0.5], use an order 11 polynomial P such that the final51approximation of asin is an odd polynomial:5253acos(x) ~ pi/2 - (x + x^3 P(x^2)).5455The largest observed error in this region is 1.18 ulps,56_ZGVnN2v_acos (0x1.fbab0a7c460f6p-2) got 0x1.0d54d1985c068p+057want 0x1.0d54d1985c069p+0.5859For |x| in [0.5, 1.0], use same approximation with a change of variable6061acos(x) = y + y * z * P(z), with z = (1-x)/2 and y = sqrt(z).6263The largest observed error in this region is 1.52 ulps,64_ZGVnN2v_acos (0x1.23d362722f591p-1) got 0x1.edbbedf8a7d6ep-165want 0x1.edbbedf8a7d6cp-1. */66float64x2_t VPCS_ATTR V_NAME_D1 (acos) (float64x2_t x)67{68const struct data *d = ptr_barrier (&data);6970float64x2_t ax = vabsq_f64 (x);7172#if WANT_SIMD_EXCEPT73/* A single comparison for One, Small and QNaN. */74uint64x2_t special75= vcgtq_u64 (vsubq_u64 (vreinterpretq_u64_f64 (ax), v_u64 (Small)),76v_u64 (Oneu - Small));77if (unlikely (v_any_u64 (special)))78return special_case (x, x, AllMask);79#endif8081uint64x2_t a_le_half = vcleq_f64 (ax, v_f64 (0.5));8283/* Evaluate polynomial Q(x) = z + z * z2 * P(z2) with84z2 = x ^ 2 and z = |x| , if |x| < 0.585z2 = (1 - |x|) / 2 and z = sqrt(z2), if |x| >= 0.5. */86float64x2_t z2 = vbslq_f64 (a_le_half, vmulq_f64 (x, x),87vfmaq_f64 (v_f64 (0.5), v_f64 (-0.5), ax));88float64x2_t z = vbslq_f64 (a_le_half, ax, vsqrtq_f64 (z2));8990/* Use a single polynomial approximation P for both intervals. */91float64x2_t z4 = vmulq_f64 (z2, z2);92float64x2_t z8 = vmulq_f64 (z4, z4);93float64x2_t z16 = vmulq_f64 (z8, z8);94float64x2_t p = v_estrin_11_f64 (z2, z4, z8, z16, d->poly);9596/* Finalize polynomial: z + z * z2 * P(z2). */97p = vfmaq_f64 (z, vmulq_f64 (z, z2), p);9899/* acos(|x|) = pi/2 - sign(x) * Q(|x|), for |x| < 0.5100= 2 Q(|x|) , for 0.5 < x < 1.0101= pi - 2 Q(|x|) , for -1.0 < x < -0.5. */102float64x2_t y = vbslq_f64 (d->abs_mask, p, x);103104uint64x2_t is_neg = vcltzq_f64 (x);105float64x2_t off = vreinterpretq_f64_u64 (106vandq_u64 (is_neg, vreinterpretq_u64_f64 (d->pi)));107float64x2_t mul = vbslq_f64 (a_le_half, v_f64 (-1.0), v_f64 (2.0));108float64x2_t add = vbslq_f64 (a_le_half, d->pi_over_2, off);109110return vfmaq_f64 (add, mul, y);111}112113TEST_SIG (V, D, 1, acos, -1.0, 1.0)114TEST_ULP (V_NAME_D1 (acos), 1.02)115TEST_DISABLE_FENV_IF_NOT (V_NAME_D1 (acos), WANT_SIMD_EXCEPT)116TEST_INTERVAL (V_NAME_D1 (acos), 0, Small, 5000)117TEST_INTERVAL (V_NAME_D1 (acos), Small, 0.5, 50000)118TEST_INTERVAL (V_NAME_D1 (acos), 0.5, 1.0, 50000)119TEST_INTERVAL (V_NAME_D1 (acos), 1.0, 0x1p11, 50000)120TEST_INTERVAL (V_NAME_D1 (acos), 0x1p11, inf, 20000)121TEST_INTERVAL (V_NAME_D1 (acos), -0, -inf, 20000)122123124