Path: blob/main/contrib/arm-optimized-routines/math/aarch64/advsimd/acosf.c
48378 views
/*1* Single-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_f32.h"9#include "test_sig.h"10#include "test_defs.h"1112static const struct data13{14float32x4_t poly[5];15float32x4_t pi_over_2f, pif;16} data = {17/* Polynomial approximation of (asin(sqrt(x)) - sqrt(x)) / (x * sqrt(x)) on18[ 0x1p-24 0x1p-2 ] order = 4 rel error: 0x1.00a23bbp-29 . */19.poly = { V4 (0x1.55555ep-3), V4 (0x1.33261ap-4), V4 (0x1.70d7dcp-5),20V4 (0x1.b059dp-6), V4 (0x1.3af7d8p-5) },21.pi_over_2f = V4 (0x1.921fb6p+0f),22.pif = V4 (0x1.921fb6p+1f),23};2425#define AbsMask 0x7fffffff26#define Half 0x3f00000027#define One 0x3f80000028#define Small 0x32800000 /* 2^-26. */2930#if WANT_SIMD_EXCEPT31static float32x4_t VPCS_ATTR NOINLINE32special_case (float32x4_t x, float32x4_t y, uint32x4_t special)33{34return v_call_f32 (acosf, x, y, special);35}36#endif3738/* Single-precision implementation of vector acos(x).3940For |x| < Small, approximate acos(x) by pi/2 - x. Small = 2^-26 for correct41rounding.42If WANT_SIMD_EXCEPT = 0, Small = 0 and we proceed with the following43approximation.4445For |x| in [Small, 0.5], use order 4 polynomial P such that the final46approximation of asin is an odd polynomial:4748acos(x) ~ pi/2 - (x + x^3 P(x^2)).4950The largest observed error in this region is 1.26 ulps,51_ZGVnN4v_acosf (0x1.843bfcp-2) got 0x1.2e934cp+0 want 0x1.2e934ap+0.5253For |x| in [0.5, 1.0], use same approximation with a change of variable5455acos(x) = y + y * z * P(z), with z = (1-x)/2 and y = sqrt(z).5657The largest observed error in this region is 1.32 ulps,58_ZGVnN4v_acosf (0x1.15ba56p-1) got 0x1.feb33p-159want 0x1.feb32ep-1. */60float32x4_t VPCS_ATTR NOINLINE V_NAME_F1 (acos) (float32x4_t x)61{62const struct data *d = ptr_barrier (&data);6364uint32x4_t ix = vreinterpretq_u32_f32 (x);65uint32x4_t ia = vandq_u32 (ix, v_u32 (AbsMask));6667#if WANT_SIMD_EXCEPT68/* A single comparison for One, Small and QNaN. */69uint32x4_t special70= vcgtq_u32 (vsubq_u32 (ia, v_u32 (Small)), v_u32 (One - Small));71if (unlikely (v_any_u32 (special)))72return special_case (x, x, v_u32 (0xffffffff));73#endif7475float32x4_t ax = vreinterpretq_f32_u32 (ia);76uint32x4_t a_le_half = vcleq_u32 (ia, v_u32 (Half));7778/* Evaluate polynomial Q(x) = z + z * z2 * P(z2) with79z2 = x ^ 2 and z = |x| , if |x| < 0.580z2 = (1 - |x|) / 2 and z = sqrt(z2), if |x| >= 0.5. */81float32x4_t z2 = vbslq_f32 (a_le_half, vmulq_f32 (x, x),82vfmsq_n_f32 (v_f32 (0.5), ax, 0.5));83float32x4_t z = vbslq_f32 (a_le_half, ax, vsqrtq_f32 (z2));8485/* Use a single polynomial approximation P for both intervals. */86float32x4_t p = v_horner_4_f32 (z2, d->poly);87/* Finalize polynomial: z + z * z2 * P(z2). */88p = vfmaq_f32 (z, vmulq_f32 (z, z2), p);8990/* acos(|x|) = pi/2 - sign(x) * Q(|x|), for |x| < 0.591= 2 Q(|x|) , for 0.5 < x < 1.092= pi - 2 Q(|x|) , for -1.0 < x < -0.5. */93float32x4_t y = vbslq_f32 (v_u32 (AbsMask), p, x);9495uint32x4_t is_neg = vcltzq_f32 (x);96float32x4_t off = vreinterpretq_f32_u32 (97vandq_u32 (vreinterpretq_u32_f32 (d->pif), is_neg));98float32x4_t mul = vbslq_f32 (a_le_half, v_f32 (-1.0), v_f32 (2.0));99float32x4_t add = vbslq_f32 (a_le_half, d->pi_over_2f, off);100101return vfmaq_f32 (add, mul, y);102}103104HALF_WIDTH_ALIAS_F1 (acos)105106TEST_SIG (V, F, 1, acos, -1.0, 1.0)107TEST_ULP (V_NAME_F1 (acos), 0.82)108TEST_DISABLE_FENV_IF_NOT (V_NAME_F1 (acos), WANT_SIMD_EXCEPT)109TEST_INTERVAL (V_NAME_F1 (acos), 0, 0x1p-26, 5000)110TEST_INTERVAL (V_NAME_F1 (acos), 0x1p-26, 0.5, 50000)111TEST_INTERVAL (V_NAME_F1 (acos), 0.5, 1.0, 50000)112TEST_INTERVAL (V_NAME_F1 (acos), 1.0, 0x1p11, 50000)113TEST_INTERVAL (V_NAME_F1 (acos), 0x1p11, inf, 20000)114TEST_INTERVAL (V_NAME_F1 (acos), -0, -inf, 20000)115116117