xref: /freebsd/contrib/arm-optimized-routines/math/aarch64/sve/tanpif.c (revision f3087bef11543b42e0d69b708f367097a4118d24)
1*f3087befSAndrew Turner /*
2*f3087befSAndrew Turner  * Single-precision vector tanpif(x) function.
3*f3087befSAndrew Turner  *
4*f3087befSAndrew Turner  * Copyright (c) 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 "test_defs.h"
10*f3087befSAndrew Turner #include "test_sig.h"
11*f3087befSAndrew Turner 
12*f3087befSAndrew Turner const static struct v_tanpif_data
13*f3087befSAndrew Turner {
14*f3087befSAndrew Turner   float c0, c2, c4, c6;
15*f3087befSAndrew Turner   float c1, c3, c5, c7;
16*f3087befSAndrew Turner } tanpif_data = {
17*f3087befSAndrew Turner   /* Coefficients for tan(pi * x).  */
18*f3087befSAndrew Turner   .c0 = 0x1.921fb4p1f,	.c1 = 0x1.4abbcep3f,  .c2 = 0x1.466b8p5f,
19*f3087befSAndrew Turner   .c3 = 0x1.461c72p7f,	.c4 = 0x1.42e9d4p9f,  .c5 = 0x1.69e2c4p11f,
20*f3087befSAndrew Turner   .c6 = 0x1.e85558p11f, .c7 = 0x1.a52e08p16f,
21*f3087befSAndrew Turner };
22*f3087befSAndrew Turner 
23*f3087befSAndrew Turner /* Approximation for single-precision vector tanpif(x)
24*f3087befSAndrew Turner    The maximum error is 3.34 ULP:
25*f3087befSAndrew Turner    _ZGVsMxv_tanpif(0x1.d6c09ap-2) got 0x1.f70aacp+2
26*f3087befSAndrew Turner 				 want 0x1.f70aa6p+2.  */
SV_NAME_F1(tanpi)27*f3087befSAndrew Turner svfloat32_t SV_NAME_F1 (tanpi) (svfloat32_t x, const svbool_t pg)
28*f3087befSAndrew Turner {
29*f3087befSAndrew Turner   const struct v_tanpif_data *d = ptr_barrier (&tanpif_data);
30*f3087befSAndrew Turner   svfloat32_t odd_coeffs = svld1rq (pg, &d->c1);
31*f3087befSAndrew Turner   svfloat32_t n = svrintn_x (pg, x);
32*f3087befSAndrew Turner 
33*f3087befSAndrew Turner   /* inf produces nan that propagates.  */
34*f3087befSAndrew Turner   svfloat32_t xr = svsub_x (pg, x, n);
35*f3087befSAndrew Turner   svfloat32_t ar = svabd_x (pg, x, n);
36*f3087befSAndrew Turner   svbool_t flip = svcmpgt (pg, ar, 0.25f);
37*f3087befSAndrew Turner   svfloat32_t r = svsel (flip, svsub_x (pg, sv_f32 (0.5f), ar), ar);
38*f3087befSAndrew Turner 
39*f3087befSAndrew Turner   svfloat32_t r2 = svmul_x (pg, r, r);
40*f3087befSAndrew Turner   svfloat32_t r4 = svmul_x (pg, r2, r2);
41*f3087befSAndrew Turner 
42*f3087befSAndrew Turner   /* Order-7 Pairwise Horner.  */
43*f3087befSAndrew Turner   svfloat32_t p01 = svmla_lane (sv_f32 (d->c0), r2, odd_coeffs, 0);
44*f3087befSAndrew Turner   svfloat32_t p23 = svmla_lane (sv_f32 (d->c2), r2, odd_coeffs, 1);
45*f3087befSAndrew Turner   svfloat32_t p45 = svmla_lane (sv_f32 (d->c4), r2, odd_coeffs, 2);
46*f3087befSAndrew Turner   svfloat32_t p67 = svmla_lane (sv_f32 (d->c6), r2, odd_coeffs, 3);
47*f3087befSAndrew Turner   svfloat32_t p = svmad_x (pg, p67, r4, p45);
48*f3087befSAndrew Turner   p = svmad_x (pg, p, r4, p23);
49*f3087befSAndrew Turner   p = svmad_x (pg, p, r4, p01);
50*f3087befSAndrew Turner   svfloat32_t poly = svmul_x (pg, r, p);
51*f3087befSAndrew Turner 
52*f3087befSAndrew Turner   svfloat32_t poly_recip = svdiv_x (pg, sv_f32 (1.0), poly);
53*f3087befSAndrew Turner   svfloat32_t y = svsel (flip, poly_recip, poly);
54*f3087befSAndrew Turner 
55*f3087befSAndrew Turner   svuint32_t sign
56*f3087befSAndrew Turner       = sveor_x (pg, svreinterpret_u32 (xr), svreinterpret_u32 (ar));
57*f3087befSAndrew Turner   return svreinterpret_f32 (svorr_x (pg, svreinterpret_u32 (y), sign));
58*f3087befSAndrew Turner }
59*f3087befSAndrew Turner 
60*f3087befSAndrew Turner #if WANT_TRIGPI_TESTS
61*f3087befSAndrew Turner TEST_DISABLE_FENV (SV_NAME_F1 (tanpi))
62*f3087befSAndrew Turner TEST_ULP (SV_NAME_F1 (tanpi), 2.84)
63*f3087befSAndrew Turner TEST_SYM_INTERVAL (SV_NAME_F1 (tanpi), 0, 0x1p-31, 50000)
64*f3087befSAndrew Turner TEST_SYM_INTERVAL (SV_NAME_F1 (tanpi), 0x1p-31, 0.5, 100000)
65*f3087befSAndrew Turner TEST_SYM_INTERVAL (SV_NAME_F1 (tanpi), 0.5, 0x1p23f, 100000)
66*f3087befSAndrew Turner TEST_SYM_INTERVAL (SV_NAME_F1 (tanpi), 0x1p23f, inf, 100000)
67*f3087befSAndrew Turner #endif
68*f3087befSAndrew Turner CLOSE_SVE_ATTR
69