xref: /freebsd/contrib/arm-optimized-routines/pl/math/sv_asinf_2u5.c (revision 59c8e88e72633afbc47a4ace0d2170d00d51f7dc)
1 /*
2  * Single-precision SVE 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 "sv_math.h"
9 #include "poly_sve_f32.h"
10 #include "pl_sig.h"
11 #include "pl_test.h"
12 
13 static const struct data
14 {
15   float32_t poly[5];
16   float32_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 = { 0x1.55555ep-3, 0x1.33261ap-4, 0x1.70d7dcp-5, 0x1.b059dp-6,
21 	    0x1.3af7d8p-5, },
22   .pi_over_2f = 0x1.921fb6p+0f,
23 };
24 
25 /* Single-precision SVE implementation of vector asin(x).
26 
27    For |x| in [0, 0.5], use order 4 polynomial P such that the final
28    approximation is an odd polynomial: asin(x) ~ x + x^3 P(x^2).
29 
30     The largest observed error in this region is 0.83 ulps,
31       _ZGVsMxv_asinf (0x1.ea00f4p-2) got 0x1.fef15ep-2
32 				    want 0x1.fef15cp-2.
33 
34     For |x| in [0.5, 1.0], use same approximation with a change of variable
35 
36     asin(x) = pi/2 - (y + y * z * P(z)), with  z = (1-x)/2 and y = sqrt(z).
37 
38    The largest observed error in this region is 2.41 ulps,
39      _ZGVsMxv_asinf (-0x1.00203ep-1) got -0x1.0c3a64p-1
40 				    want -0x1.0c3a6p-1.  */
41 svfloat32_t SV_NAME_F1 (asin) (svfloat32_t x, const svbool_t pg)
42 {
43   const struct data *d = ptr_barrier (&data);
44 
45   svuint32_t sign = svand_x (pg, svreinterpret_u32 (x), 0x80000000);
46 
47   svfloat32_t ax = svabs_x (pg, x);
48   svbool_t a_ge_half = svacge (pg, x, 0.5);
49 
50   /* Evaluate polynomial Q(x) = y + y * z * P(z) with
51    z = x ^ 2 and y = |x|            , if |x| < 0.5
52    z = (1 - |x|) / 2 and y = sqrt(z), if |x| >= 0.5.  */
53   svfloat32_t z2 = svsel (a_ge_half, svmls_x (pg, sv_f32 (0.5), ax, 0.5),
54 			  svmul_x (pg, x, x));
55   svfloat32_t z = svsqrt_m (ax, a_ge_half, z2);
56 
57   /* Use a single polynomial approximation P for both intervals.  */
58   svfloat32_t p = sv_horner_4_f32_x (pg, z2, d->poly);
59   /* Finalize polynomial: z + z * z2 * P(z2).  */
60   p = svmla_x (pg, z, svmul_x (pg, z, z2), p);
61 
62   /* asin(|x|) = Q(|x|)         , for |x| < 0.5
63 		 = pi/2 - 2 Q(|x|), for |x| >= 0.5.  */
64   svfloat32_t y = svmad_m (a_ge_half, p, sv_f32 (-2.0), d->pi_over_2f);
65 
66   /* Copy sign.  */
67   return svreinterpret_f32 (svorr_x (pg, svreinterpret_u32 (y), sign));
68 }
69 
70 PL_SIG (SV, F, 1, asin, -1.0, 1.0)
71 PL_TEST_ULP (SV_NAME_F1 (asin), 1.91)
72 PL_TEST_INTERVAL (SV_NAME_F1 (asin), 0, 0.5, 50000)
73 PL_TEST_INTERVAL (SV_NAME_F1 (asin), 0.5, 1.0, 50000)
74 PL_TEST_INTERVAL (SV_NAME_F1 (asin), 1.0, 0x1p11, 50000)
75 PL_TEST_INTERVAL (SV_NAME_F1 (asin), 0x1p11, inf, 20000)
76 PL_TEST_INTERVAL (SV_NAME_F1 (asin), -0, -inf, 20000)