xref: /freebsd/contrib/arm-optimized-routines/pl/math/sv_tan_3u5.c (revision 56b17de1e8360fe131d425de20b5e75ff3ea897c)
1 /*
2  * Double-precision SVE tan(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_f64.h"
10 #include "pl_sig.h"
11 #include "pl_test.h"
12 
13 static const struct data
14 {
15   double poly[9];
16   double half_pi_hi, half_pi_lo, inv_half_pi, range_val, shift;
17 } data = {
18   /* Polynomial generated with FPMinimax.  */
19   .poly = { 0x1.5555555555556p-2, 0x1.1111111110a63p-3, 0x1.ba1ba1bb46414p-5,
20 	    0x1.664f47e5b5445p-6, 0x1.226e5e5ecdfa3p-7, 0x1.d6c7ddbf87047p-9,
21 	    0x1.7ea75d05b583ep-10, 0x1.289f22964a03cp-11,
22 	    0x1.4e4fd14147622p-12, },
23   .half_pi_hi = 0x1.921fb54442d18p0,
24   .half_pi_lo = 0x1.1a62633145c07p-54,
25   .inv_half_pi = 0x1.45f306dc9c883p-1,
26   .range_val = 0x1p23,
27   .shift = 0x1.8p52,
28 };
29 
30 static svfloat64_t NOINLINE
31 special_case (svfloat64_t x, svfloat64_t y, svbool_t special)
32 {
33   return sv_call_f64 (tan, x, y, special);
34 }
35 
36 /* Vector approximation for double-precision tan.
37    Maximum measured error is 3.48 ULP:
38    _ZGVsMxv_tan(0x1.4457047ef78d8p+20) got -0x1.f6ccd8ecf7dedp+37
39 				      want -0x1.f6ccd8ecf7deap+37.  */
40 svfloat64_t SV_NAME_D1 (tan) (svfloat64_t x, svbool_t pg)
41 {
42   const struct data *dat = ptr_barrier (&data);
43 
44   /* Invert condition to catch NaNs and Infs as well as large values.  */
45   svbool_t special = svnot_z (pg, svaclt (pg, x, dat->range_val));
46 
47   /* q = nearest integer to 2 * x / pi.  */
48   svfloat64_t shift = sv_f64 (dat->shift);
49   svfloat64_t q = svmla_x (pg, shift, x, dat->inv_half_pi);
50   q = svsub_x (pg, q, shift);
51   svint64_t qi = svcvt_s64_x (pg, q);
52 
53   /* Use q to reduce x to r in [-pi/4, pi/4], by:
54      r = x - q * pi/2, in extended precision.  */
55   svfloat64_t r = x;
56   svfloat64_t half_pi = svld1rq (svptrue_b64 (), &dat->half_pi_hi);
57   r = svmls_lane (r, q, half_pi, 0);
58   r = svmls_lane (r, q, half_pi, 1);
59   /* Further reduce r to [-pi/8, pi/8], to be reconstructed using double angle
60      formula.  */
61   r = svmul_x (pg, r, 0.5);
62 
63   /* Approximate tan(r) using order 8 polynomial.
64      tan(x) is odd, so polynomial has the form:
65      tan(x) ~= x + C0 * x^3 + C1 * x^5 + C3 * x^7 + ...
66      Hence we first approximate P(r) = C1 + C2 * r^2 + C3 * r^4 + ...
67      Then compute the approximation by:
68      tan(r) ~= r + r^3 * (C0 + r^2 * P(r)).  */
69   svfloat64_t r2 = svmul_x (pg, r, r);
70   svfloat64_t r4 = svmul_x (pg, r2, r2);
71   svfloat64_t r8 = svmul_x (pg, r4, r4);
72   /* Use offset version coeff array by 1 to evaluate from C1 onwards.  */
73   svfloat64_t p = sv_estrin_7_f64_x (pg, r2, r4, r8, dat->poly + 1);
74   p = svmad_x (pg, p, r2, dat->poly[0]);
75   p = svmla_x (pg, r, r2, svmul_x (pg, p, r));
76 
77   /* Recombination uses double-angle formula:
78      tan(2x) = 2 * tan(x) / (1 - (tan(x))^2)
79      and reciprocity around pi/2:
80      tan(x) = 1 / (tan(pi/2 - x))
81      to assemble result using change-of-sign and conditional selection of
82      numerator/denominator dependent on odd/even-ness of q (hence quadrant).  */
83   svbool_t use_recip
84       = svcmpeq (pg, svand_x (pg, svreinterpret_u64 (qi), 1), 0);
85 
86   svfloat64_t n = svmad_x (pg, p, p, -1);
87   svfloat64_t d = svmul_x (pg, p, 2);
88   svfloat64_t swap = n;
89   n = svneg_m (n, use_recip, d);
90   d = svsel (use_recip, swap, d);
91   if (unlikely (svptest_any (pg, special)))
92     return special_case (x, svdiv_x (svnot_z (pg, special), n, d), special);
93   return svdiv_x (pg, n, d);
94 }
95 
96 PL_SIG (SV, D, 1, tan, -3.1, 3.1)
97 PL_TEST_ULP (SV_NAME_D1 (tan), 2.99)
98 PL_TEST_SYM_INTERVAL (SV_NAME_D1 (tan), 0, 0x1p23, 500000)
99 PL_TEST_SYM_INTERVAL (SV_NAME_D1 (tan), 0x1p23, inf, 5000)
100