1*f3087befSAndrew Turner /*
2*f3087befSAndrew Turner * Double-precision SVE asin(x) function.
3*f3087befSAndrew Turner *
4*f3087befSAndrew Turner * Copyright (c) 2023-2024, Arm Limited.
5*f3087befSAndrew Turner * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6*f3087befSAndrew Turner */
7*f3087befSAndrew Turner
8*f3087befSAndrew Turner #include "sv_math.h"
9*f3087befSAndrew Turner #include "sv_poly_f64.h"
10*f3087befSAndrew Turner #include "test_sig.h"
11*f3087befSAndrew Turner #include "test_defs.h"
12*f3087befSAndrew Turner
13*f3087befSAndrew Turner static const struct data
14*f3087befSAndrew Turner {
15*f3087befSAndrew Turner float64_t poly[12];
16*f3087befSAndrew Turner float64_t pi_over_2f;
17*f3087befSAndrew Turner } data = {
18*f3087befSAndrew Turner /* Polynomial approximation of (asin(sqrt(x)) - sqrt(x)) / (x * sqrt(x))
19*f3087befSAndrew Turner on [ 0x1p-106, 0x1p-2 ], relative error: 0x1.c3d8e169p-57. */
20*f3087befSAndrew Turner .poly = { 0x1.555555555554ep-3, 0x1.3333333337233p-4,
21*f3087befSAndrew Turner 0x1.6db6db67f6d9fp-5, 0x1.f1c71fbd29fbbp-6,
22*f3087befSAndrew Turner 0x1.6e8b264d467d6p-6, 0x1.1c5997c357e9dp-6,
23*f3087befSAndrew Turner 0x1.c86a22cd9389dp-7, 0x1.856073c22ebbep-7,
24*f3087befSAndrew Turner 0x1.fd1151acb6bedp-8, 0x1.087182f799c1dp-6,
25*f3087befSAndrew Turner -0x1.6602748120927p-7, 0x1.cfa0dd1f9478p-6, },
26*f3087befSAndrew Turner .pi_over_2f = 0x1.921fb54442d18p+0,
27*f3087befSAndrew Turner };
28*f3087befSAndrew Turner
29*f3087befSAndrew Turner #define P(i) sv_f64 (d->poly[i])
30*f3087befSAndrew Turner
31*f3087befSAndrew Turner /* Double-precision SVE implementation of vector asin(x).
32*f3087befSAndrew Turner
33*f3087befSAndrew Turner For |x| in [0, 0.5], use an order 11 polynomial P such that the final
34*f3087befSAndrew Turner approximation is an odd polynomial: asin(x) ~ x + x^3 P(x^2).
35*f3087befSAndrew Turner
36*f3087befSAndrew Turner The largest observed error in this region is 0.52 ulps,
37*f3087befSAndrew Turner _ZGVsMxv_asin(0x1.d95ae04998b6cp-2) got 0x1.ec13757305f27p-2
38*f3087befSAndrew Turner want 0x1.ec13757305f26p-2.
39*f3087befSAndrew Turner
40*f3087befSAndrew Turner For |x| in [0.5, 1.0], use same approximation with a change of variable
41*f3087befSAndrew Turner
42*f3087befSAndrew Turner asin(x) = pi/2 - (y + y * z * P(z)), with z = (1-x)/2 and y = sqrt(z).
43*f3087befSAndrew Turner
44*f3087befSAndrew Turner The largest observed error in this region is 2.69 ulps,
45*f3087befSAndrew Turner _ZGVsMxv_asin (0x1.044e8cefee301p-1) got 0x1.1111dd54ddf96p-1
46*f3087befSAndrew Turner want 0x1.1111dd54ddf99p-1. */
SV_NAME_D1(asin)47*f3087befSAndrew Turner svfloat64_t SV_NAME_D1 (asin) (svfloat64_t x, const svbool_t pg)
48*f3087befSAndrew Turner {
49*f3087befSAndrew Turner const struct data *d = ptr_barrier (&data);
50*f3087befSAndrew Turner
51*f3087befSAndrew Turner svuint64_t sign = svand_x (pg, svreinterpret_u64 (x), 0x8000000000000000);
52*f3087befSAndrew Turner svfloat64_t ax = svabs_x (pg, x);
53*f3087befSAndrew Turner svbool_t a_ge_half = svacge (pg, x, 0.5);
54*f3087befSAndrew Turner
55*f3087befSAndrew Turner /* Evaluate polynomial Q(x) = y + y * z * P(z) with
56*f3087befSAndrew Turner z = x ^ 2 and y = |x| , if |x| < 0.5
57*f3087befSAndrew Turner z = (1 - |x|) / 2 and y = sqrt(z), if |x| >= 0.5. */
58*f3087befSAndrew Turner svfloat64_t z2 = svsel (a_ge_half, svmls_x (pg, sv_f64 (0.5), ax, 0.5),
59*f3087befSAndrew Turner svmul_x (pg, x, x));
60*f3087befSAndrew Turner svfloat64_t z = svsqrt_m (ax, a_ge_half, z2);
61*f3087befSAndrew Turner
62*f3087befSAndrew Turner /* Use a single polynomial approximation P for both intervals. */
63*f3087befSAndrew Turner svfloat64_t z4 = svmul_x (pg, z2, z2);
64*f3087befSAndrew Turner svfloat64_t z8 = svmul_x (pg, z4, z4);
65*f3087befSAndrew Turner svfloat64_t z16 = svmul_x (pg, z8, z8);
66*f3087befSAndrew Turner svfloat64_t p = sv_estrin_11_f64_x (pg, z2, z4, z8, z16, d->poly);
67*f3087befSAndrew Turner /* Finalize polynomial: z + z * z2 * P(z2). */
68*f3087befSAndrew Turner p = svmla_x (pg, z, svmul_x (pg, z, z2), p);
69*f3087befSAndrew Turner
70*f3087befSAndrew Turner /* asin(|x|) = Q(|x|) , for |x| < 0.5
71*f3087befSAndrew Turner = pi/2 - 2 Q(|x|), for |x| >= 0.5. */
72*f3087befSAndrew Turner svfloat64_t y = svmad_m (a_ge_half, p, sv_f64 (-2.0), d->pi_over_2f);
73*f3087befSAndrew Turner
74*f3087befSAndrew Turner /* Copy sign. */
75*f3087befSAndrew Turner return svreinterpret_f64 (svorr_x (pg, svreinterpret_u64 (y), sign));
76*f3087befSAndrew Turner }
77*f3087befSAndrew Turner
78*f3087befSAndrew Turner TEST_SIG (SV, D, 1, asin, -1.0, 1.0)
79*f3087befSAndrew Turner TEST_ULP (SV_NAME_D1 (asin), 2.20)
80*f3087befSAndrew Turner TEST_DISABLE_FENV (SV_NAME_D1 (asin))
81*f3087befSAndrew Turner TEST_INTERVAL (SV_NAME_D1 (asin), 0, 0.5, 50000)
82*f3087befSAndrew Turner TEST_INTERVAL (SV_NAME_D1 (asin), 0.5, 1.0, 50000)
83*f3087befSAndrew Turner TEST_INTERVAL (SV_NAME_D1 (asin), 1.0, 0x1p11, 50000)
84*f3087befSAndrew Turner TEST_INTERVAL (SV_NAME_D1 (asin), 0x1p11, inf, 20000)
85*f3087befSAndrew Turner TEST_INTERVAL (SV_NAME_D1 (asin), -0, -inf, 20000)
86*f3087befSAndrew Turner CLOSE_SVE_ATTR
87