Path: blob/main/contrib/arm-optimized-routines/math/aarch64/advsimd/asinf.c
48375 views
/*1* Single-precision vector 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 "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;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};2324#define AbsMask 0x7fffffff25#define Half 0x3f00000026#define One 0x3f80000027#define Small 0x39800000 /* 2^-12. */2829#if WANT_SIMD_EXCEPT30static float32x4_t VPCS_ATTR NOINLINE31special_case (float32x4_t x, float32x4_t y, uint32x4_t special)32{33return v_call_f32 (asinf, x, y, special);34}35#endif3637/* Single-precision implementation of vector asin(x).3839For |x| < Small, approximate asin(x) by x. Small = 2^-12 for correct40rounding. If WANT_SIMD_EXCEPT = 0, Small = 0 and we proceed with the41following approximation.4243For |x| in [Small, 0.5], use order 4 polynomial P such that the final44approximation is an odd polynomial: asin(x) ~ x + x^3 P(x^2).4546The largest observed error in this region is 0.83 ulps,47_ZGVnN4v_asinf (0x1.ea00f4p-2) got 0x1.fef15ep-2 want 0x1.fef15cp-2.4849For |x| in [0.5, 1.0], use same approximation with a change of variable5051asin(x) = pi/2 - (y + y * z * P(z)), with z = (1-x)/2 and y = sqrt(z).5253The largest observed error in this region is 2.41 ulps,54_ZGVnN4v_asinf (0x1.00203ep-1) got 0x1.0c3a64p-1 want 0x1.0c3a6p-1. */55float32x4_t VPCS_ATTR NOINLINE V_NAME_F1 (asin) (float32x4_t x)56{57const struct data *d = ptr_barrier (&data);5859uint32x4_t ix = vreinterpretq_u32_f32 (x);60uint32x4_t ia = vandq_u32 (ix, v_u32 (AbsMask));6162#if WANT_SIMD_EXCEPT63/* Special values need to be computed with scalar fallbacks so64that appropriate fp exceptions are raised. */65uint32x4_t special66= vcgtq_u32 (vsubq_u32 (ia, v_u32 (Small)), v_u32 (One - Small));67if (unlikely (v_any_u32 (special)))68return special_case (x, x, v_u32 (0xffffffff));69#endif7071float32x4_t ax = vreinterpretq_f32_u32 (ia);72uint32x4_t a_lt_half = vcltq_u32 (ia, v_u32 (Half));7374/* Evaluate polynomial Q(x) = y + y * z * P(z) with75z = x ^ 2 and y = |x| , if |x| < 0.576z = (1 - |x|) / 2 and y = sqrt(z), if |x| >= 0.5. */77float32x4_t z2 = vbslq_f32 (a_lt_half, vmulq_f32 (x, x),78vfmsq_n_f32 (v_f32 (0.5), ax, 0.5));79float32x4_t z = vbslq_f32 (a_lt_half, ax, vsqrtq_f32 (z2));8081/* Use a single polynomial approximation P for both intervals. */82float32x4_t p = v_horner_4_f32 (z2, d->poly);83/* Finalize polynomial: z + z * z2 * P(z2). */84p = vfmaq_f32 (z, vmulq_f32 (z, z2), p);8586/* asin(|x|) = Q(|x|) , for |x| < 0.587= pi/2 - 2 Q(|x|), for |x| >= 0.5. */88float32x4_t y89= vbslq_f32 (a_lt_half, p, vfmsq_n_f32 (d->pi_over_2f, p, 2.0));9091/* Copy sign. */92return vbslq_f32 (v_u32 (AbsMask), y, x);93}9495HALF_WIDTH_ALIAS_F1 (asin)9697TEST_SIG (V, F, 1, asin, -1.0, 1.0)98TEST_ULP (V_NAME_F1 (asin), 1.91)99TEST_DISABLE_FENV_IF_NOT (V_NAME_F1 (asin), WANT_SIMD_EXCEPT)100TEST_INTERVAL (V_NAME_F1 (asin), 0, 0x1p-12, 5000)101TEST_INTERVAL (V_NAME_F1 (asin), 0x1p-12, 0.5, 50000)102TEST_INTERVAL (V_NAME_F1 (asin), 0.5, 1.0, 50000)103TEST_INTERVAL (V_NAME_F1 (asin), 1.0, 0x1p11, 50000)104TEST_INTERVAL (V_NAME_F1 (asin), 0x1p11, inf, 20000)105TEST_INTERVAL (V_NAME_F1 (asin), -0, -inf, 20000)106107108