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