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