xref: /freebsd/contrib/arm-optimized-routines/math/aarch64/advsimd/tanpi.c (revision f3087bef11543b42e0d69b708f367097a4118d24)
1*f3087befSAndrew Turner /*
2*f3087befSAndrew Turner  * Double-precision vector tanpi(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 "v_math.h"
9*f3087befSAndrew Turner #include "test_sig.h"
10*f3087befSAndrew Turner #include "test_defs.h"
11*f3087befSAndrew Turner 
12*f3087befSAndrew Turner const static struct v_tanpi_data
13*f3087befSAndrew Turner {
14*f3087befSAndrew Turner   float64x2_t c0, c2, c4, c6, c8, c10, c12;
15*f3087befSAndrew Turner   double c1, c3, c5, c7, c9, c11, c13, c14;
16*f3087befSAndrew Turner } tanpi_data = {
17*f3087befSAndrew Turner   /* Coefficents for tan(pi * x) computed with fpminimax
18*f3087befSAndrew Turner      on [ 0x1p-1022 0x1p-2 ]
19*f3087befSAndrew Turner      approx rel error: 0x1.7eap-55
20*f3087befSAndrew Turner      approx abs error: 0x1.7eap-55.  */
21*f3087befSAndrew Turner   .c0 = V2 (0x1.921fb54442d18p1), /* pi.  */
22*f3087befSAndrew Turner   .c1 = 0x1.4abbce625be52p3,	  .c2 = V2 (0x1.466bc6775b0f9p5),
23*f3087befSAndrew Turner   .c3 = 0x1.45fff9b426f5ep7,	  .c4 = V2 (0x1.45f4730dbca5cp9),
24*f3087befSAndrew Turner   .c5 = 0x1.45f3265994f85p11,	  .c6 = V2 (0x1.45f4234b330cap13),
25*f3087befSAndrew Turner   .c7 = 0x1.45dca11be79ebp15,	  .c8 = V2 (0x1.47283fc5eea69p17),
26*f3087befSAndrew Turner   .c9 = 0x1.3a6d958cdefaep19,	  .c10 = V2 (0x1.927896baee627p21),
27*f3087befSAndrew Turner   .c11 = -0x1.89333f6acd922p19,	  .c12 = V2 (0x1.5d4e912bb8456p27),
28*f3087befSAndrew Turner   .c13 = -0x1.a854d53ab6874p29,	  .c14 = 0x1.1b76de7681424p32,
29*f3087befSAndrew Turner };
30*f3087befSAndrew Turner 
31*f3087befSAndrew Turner /* Approximation for double-precision vector tanpi(x)
32*f3087befSAndrew Turner    The maximum error is 3.06 ULP:
33*f3087befSAndrew Turner    _ZGVnN2v_tanpi(0x1.0a4a07dfcca3ep-1) got -0x1.fa30112702c98p+3
34*f3087befSAndrew Turner 				       want -0x1.fa30112702c95p+3.  */
V_NAME_D1(tanpi)35*f3087befSAndrew Turner float64x2_t VPCS_ATTR V_NAME_D1 (tanpi) (float64x2_t x)
36*f3087befSAndrew Turner {
37*f3087befSAndrew Turner   const struct v_tanpi_data *d = ptr_barrier (&tanpi_data);
38*f3087befSAndrew Turner 
39*f3087befSAndrew Turner   float64x2_t n = vrndnq_f64 (x);
40*f3087befSAndrew Turner 
41*f3087befSAndrew Turner   /* inf produces nan that propagates.  */
42*f3087befSAndrew Turner   float64x2_t xr = vsubq_f64 (x, n);
43*f3087befSAndrew Turner   float64x2_t ar = vabdq_f64 (x, n);
44*f3087befSAndrew Turner   uint64x2_t flip = vcgtq_f64 (ar, v_f64 (0.25));
45*f3087befSAndrew Turner   float64x2_t r = vbslq_f64 (flip, vsubq_f64 (v_f64 (0.5), ar), ar);
46*f3087befSAndrew Turner 
47*f3087befSAndrew Turner   /* Order-14 pairwise Horner.  */
48*f3087befSAndrew Turner   float64x2_t r2 = vmulq_f64 (r, r);
49*f3087befSAndrew Turner   float64x2_t r4 = vmulq_f64 (r2, r2);
50*f3087befSAndrew Turner 
51*f3087befSAndrew Turner   float64x2_t c_1_3 = vld1q_f64 (&d->c1);
52*f3087befSAndrew Turner   float64x2_t c_5_7 = vld1q_f64 (&d->c5);
53*f3087befSAndrew Turner   float64x2_t c_9_11 = vld1q_f64 (&d->c9);
54*f3087befSAndrew Turner   float64x2_t c_13_14 = vld1q_f64 (&d->c13);
55*f3087befSAndrew Turner   float64x2_t p01 = vfmaq_laneq_f64 (d->c0, r2, c_1_3, 0);
56*f3087befSAndrew Turner   float64x2_t p23 = vfmaq_laneq_f64 (d->c2, r2, c_1_3, 1);
57*f3087befSAndrew Turner   float64x2_t p45 = vfmaq_laneq_f64 (d->c4, r2, c_5_7, 0);
58*f3087befSAndrew Turner   float64x2_t p67 = vfmaq_laneq_f64 (d->c6, r2, c_5_7, 1);
59*f3087befSAndrew Turner   float64x2_t p89 = vfmaq_laneq_f64 (d->c8, r2, c_9_11, 0);
60*f3087befSAndrew Turner   float64x2_t p1011 = vfmaq_laneq_f64 (d->c10, r2, c_9_11, 1);
61*f3087befSAndrew Turner   float64x2_t p1213 = vfmaq_laneq_f64 (d->c12, r2, c_13_14, 0);
62*f3087befSAndrew Turner 
63*f3087befSAndrew Turner   float64x2_t p = vfmaq_laneq_f64 (p1213, r4, c_13_14, 1);
64*f3087befSAndrew Turner   p = vfmaq_f64 (p1011, r4, p);
65*f3087befSAndrew Turner   p = vfmaq_f64 (p89, r4, p);
66*f3087befSAndrew Turner   p = vfmaq_f64 (p67, r4, p);
67*f3087befSAndrew Turner   p = vfmaq_f64 (p45, r4, p);
68*f3087befSAndrew Turner   p = vfmaq_f64 (p23, r4, p);
69*f3087befSAndrew Turner   p = vfmaq_f64 (p01, r4, p);
70*f3087befSAndrew Turner   p = vmulq_f64 (r, p);
71*f3087befSAndrew Turner 
72*f3087befSAndrew Turner   float64x2_t p_recip = vdivq_f64 (v_f64 (1.0), p);
73*f3087befSAndrew Turner   float64x2_t y = vbslq_f64 (flip, p_recip, p);
74*f3087befSAndrew Turner 
75*f3087befSAndrew Turner   uint64x2_t sign
76*f3087befSAndrew Turner       = veorq_u64 (vreinterpretq_u64_f64 (xr), vreinterpretq_u64_f64 (ar));
77*f3087befSAndrew Turner   return vreinterpretq_f64_u64 (vorrq_u64 (vreinterpretq_u64_f64 (y), sign));
78*f3087befSAndrew Turner }
79*f3087befSAndrew Turner 
80*f3087befSAndrew Turner #if WANT_TRIGPI_TESTS
81*f3087befSAndrew Turner TEST_DISABLE_FENV (V_NAME_D1 (tanpi))
82*f3087befSAndrew Turner TEST_ULP (V_NAME_D1 (tanpi), 2.57)
83*f3087befSAndrew Turner TEST_SYM_INTERVAL (V_NAME_D1 (tanpi), 0, 0x1p-31, 50000)
84*f3087befSAndrew Turner TEST_SYM_INTERVAL (V_NAME_D1 (tanpi), 0x1p-31, 0.5, 50000)
85*f3087befSAndrew Turner TEST_SYM_INTERVAL (V_NAME_D1 (tanpi), 0.5, 1.0, 200000)
86*f3087befSAndrew Turner TEST_SYM_INTERVAL (V_NAME_D1 (tanpi), 1.0, 0x1p23, 50000)
87*f3087befSAndrew Turner TEST_SYM_INTERVAL (V_NAME_D1 (tanpi), 0x1p23, inf, 50000)
88*f3087befSAndrew Turner #endif
89