1 /* 2 * Single-precision vector asin(x) function. 3 * 4 * Copyright (c) 2023, Arm Limited. 5 * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception 6 */ 7 8 #include "v_math.h" 9 #include "poly_advsimd_f32.h" 10 #include "pl_sig.h" 11 #include "pl_test.h" 12 13 static const struct data 14 { 15 float32x4_t poly[5]; 16 float32x4_t pi_over_2f; 17 } data = { 18 /* Polynomial approximation of (asin(sqrt(x)) - sqrt(x)) / (x * sqrt(x)) on 19 [ 0x1p-24 0x1p-2 ] order = 4 rel error: 0x1.00a23bbp-29 . */ 20 .poly = { V4 (0x1.55555ep-3), V4 (0x1.33261ap-4), V4 (0x1.70d7dcp-5), 21 V4 (0x1.b059dp-6), V4 (0x1.3af7d8p-5) }, 22 .pi_over_2f = V4 (0x1.921fb6p+0f), 23 }; 24 25 #define AbsMask 0x7fffffff 26 #define Half 0x3f000000 27 #define One 0x3f800000 28 #define Small 0x39800000 /* 2^-12. */ 29 30 #if WANT_SIMD_EXCEPT 31 static float32x4_t VPCS_ATTR NOINLINE 32 special_case (float32x4_t x, float32x4_t y, uint32x4_t special) 33 { 34 return v_call_f32 (asinf, x, y, special); 35 } 36 #endif 37 38 /* Single-precision implementation of vector asin(x). 39 40 For |x| < Small, approximate asin(x) by x. Small = 2^-12 for correct 41 rounding. If WANT_SIMD_EXCEPT = 0, Small = 0 and we proceed with the 42 following approximation. 43 44 For |x| in [Small, 0.5], use order 4 polynomial P such that the final 45 approximation is an odd polynomial: asin(x) ~ x + x^3 P(x^2). 46 47 The largest observed error in this region is 0.83 ulps, 48 _ZGVnN4v_asinf (0x1.ea00f4p-2) got 0x1.fef15ep-2 want 0x1.fef15cp-2. 49 50 For |x| in [0.5, 1.0], use same approximation with a change of variable 51 52 asin(x) = pi/2 - (y + y * z * P(z)), with z = (1-x)/2 and y = sqrt(z). 53 54 The largest observed error in this region is 2.41 ulps, 55 _ZGVnN4v_asinf (0x1.00203ep-1) got 0x1.0c3a64p-1 want 0x1.0c3a6p-1. */ 56 float32x4_t VPCS_ATTR V_NAME_F1 (asin) (float32x4_t x) 57 { 58 const struct data *d = ptr_barrier (&data); 59 60 uint32x4_t ix = vreinterpretq_u32_f32 (x); 61 uint32x4_t ia = vandq_u32 (ix, v_u32 (AbsMask)); 62 63 #if WANT_SIMD_EXCEPT 64 /* Special values need to be computed with scalar fallbacks so 65 that appropriate fp exceptions are raised. */ 66 uint32x4_t special 67 = vcgtq_u32 (vsubq_u32 (ia, v_u32 (Small)), v_u32 (One - Small)); 68 if (unlikely (v_any_u32 (special))) 69 return special_case (x, x, v_u32 (0xffffffff)); 70 #endif 71 72 float32x4_t ax = vreinterpretq_f32_u32 (ia); 73 uint32x4_t a_lt_half = vcltq_u32 (ia, v_u32 (Half)); 74 75 /* Evaluate polynomial Q(x) = y + y * z * P(z) with 76 z = x ^ 2 and y = |x| , if |x| < 0.5 77 z = (1 - |x|) / 2 and y = sqrt(z), if |x| >= 0.5. */ 78 float32x4_t z2 = vbslq_f32 (a_lt_half, vmulq_f32 (x, x), 79 vfmsq_n_f32 (v_f32 (0.5), ax, 0.5)); 80 float32x4_t z = vbslq_f32 (a_lt_half, ax, vsqrtq_f32 (z2)); 81 82 /* Use a single polynomial approximation P for both intervals. */ 83 float32x4_t p = v_horner_4_f32 (z2, d->poly); 84 /* Finalize polynomial: z + z * z2 * P(z2). */ 85 p = vfmaq_f32 (z, vmulq_f32 (z, z2), p); 86 87 /* asin(|x|) = Q(|x|) , for |x| < 0.5 88 = pi/2 - 2 Q(|x|), for |x| >= 0.5. */ 89 float32x4_t y 90 = vbslq_f32 (a_lt_half, p, vfmsq_n_f32 (d->pi_over_2f, p, 2.0)); 91 92 /* Copy sign. */ 93 return vbslq_f32 (v_u32 (AbsMask), y, x); 94 } 95 96 PL_SIG (V, F, 1, asin, -1.0, 1.0) 97 PL_TEST_ULP (V_NAME_F1 (asin), 1.91) 98 PL_TEST_EXPECT_FENV (V_NAME_F1 (asin), WANT_SIMD_EXCEPT) 99 PL_TEST_INTERVAL (V_NAME_F1 (asin), 0, 0x1p-12, 5000) 100 PL_TEST_INTERVAL (V_NAME_F1 (asin), 0x1p-12, 0.5, 50000) 101 PL_TEST_INTERVAL (V_NAME_F1 (asin), 0.5, 1.0, 50000) 102 PL_TEST_INTERVAL (V_NAME_F1 (asin), 1.0, 0x1p11, 50000) 103 PL_TEST_INTERVAL (V_NAME_F1 (asin), 0x1p11, inf, 20000) 104 PL_TEST_INTERVAL (V_NAME_F1 (asin), -0, -inf, 20000) 105