1*f3087befSAndrew Turner /*
2*f3087befSAndrew Turner * Double-precision SVE tan(x) function.
3*f3087befSAndrew Turner *
4*f3087befSAndrew Turner * Copyright (c) 2023-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_sig.h"
10*f3087befSAndrew Turner #include "test_defs.h"
11*f3087befSAndrew Turner
12*f3087befSAndrew Turner static const struct data
13*f3087befSAndrew Turner {
14*f3087befSAndrew Turner double c2, c4, c6, c8;
15*f3087befSAndrew Turner double poly_1357[4];
16*f3087befSAndrew Turner double c0, inv_half_pi;
17*f3087befSAndrew Turner double half_pi_hi, half_pi_lo, range_val;
18*f3087befSAndrew Turner } data = {
19*f3087befSAndrew Turner /* Polynomial generated with FPMinimax. */
20*f3087befSAndrew Turner .c2 = 0x1.ba1ba1bb46414p-5,
21*f3087befSAndrew Turner .c4 = 0x1.226e5e5ecdfa3p-7,
22*f3087befSAndrew Turner .c6 = 0x1.7ea75d05b583ep-10,
23*f3087befSAndrew Turner .c8 = 0x1.4e4fd14147622p-12,
24*f3087befSAndrew Turner .poly_1357 = { 0x1.1111111110a63p-3, 0x1.664f47e5b5445p-6,
25*f3087befSAndrew Turner 0x1.d6c7ddbf87047p-9, 0x1.289f22964a03cp-11 },
26*f3087befSAndrew Turner .c0 = 0x1.5555555555556p-2,
27*f3087befSAndrew Turner .inv_half_pi = 0x1.45f306dc9c883p-1,
28*f3087befSAndrew Turner .half_pi_hi = 0x1.921fb54442d18p0,
29*f3087befSAndrew Turner .half_pi_lo = 0x1.1a62633145c07p-54,
30*f3087befSAndrew Turner .range_val = 0x1p23,
31*f3087befSAndrew Turner };
32*f3087befSAndrew Turner
33*f3087befSAndrew Turner static svfloat64_t NOINLINE
special_case(svfloat64_t x,svfloat64_t p,svfloat64_t q,svbool_t pg,svbool_t special)34*f3087befSAndrew Turner special_case (svfloat64_t x, svfloat64_t p, svfloat64_t q, svbool_t pg,
35*f3087befSAndrew Turner svbool_t special)
36*f3087befSAndrew Turner {
37*f3087befSAndrew Turner svbool_t use_recip = svcmpeq (
38*f3087befSAndrew Turner pg, svand_x (pg, svreinterpret_u64 (svcvt_s64_x (pg, q)), 1), 0);
39*f3087befSAndrew Turner
40*f3087befSAndrew Turner svfloat64_t n = svmad_x (pg, p, p, -1);
41*f3087befSAndrew Turner svfloat64_t d = svmul_x (svptrue_b64 (), p, 2);
42*f3087befSAndrew Turner svfloat64_t swap = n;
43*f3087befSAndrew Turner n = svneg_m (n, use_recip, d);
44*f3087befSAndrew Turner d = svsel (use_recip, swap, d);
45*f3087befSAndrew Turner svfloat64_t y = svdiv_x (svnot_z (pg, special), n, d);
46*f3087befSAndrew Turner return sv_call_f64 (tan, x, y, special);
47*f3087befSAndrew Turner }
48*f3087befSAndrew Turner
49*f3087befSAndrew Turner /* Vector approximation for double-precision tan.
50*f3087befSAndrew Turner Maximum measured error is 3.48 ULP:
51*f3087befSAndrew Turner _ZGVsMxv_tan(0x1.4457047ef78d8p+20) got -0x1.f6ccd8ecf7dedp+37
52*f3087befSAndrew Turner want -0x1.f6ccd8ecf7deap+37. */
SV_NAME_D1(tan)53*f3087befSAndrew Turner svfloat64_t SV_NAME_D1 (tan) (svfloat64_t x, svbool_t pg)
54*f3087befSAndrew Turner {
55*f3087befSAndrew Turner const struct data *dat = ptr_barrier (&data);
56*f3087befSAndrew Turner svfloat64_t half_pi_c0 = svld1rq (svptrue_b64 (), &dat->c0);
57*f3087befSAndrew Turner /* q = nearest integer to 2 * x / pi. */
58*f3087befSAndrew Turner svfloat64_t q = svmul_lane (x, half_pi_c0, 1);
59*f3087befSAndrew Turner q = svrinta_x (pg, q);
60*f3087befSAndrew Turner
61*f3087befSAndrew Turner /* Use q to reduce x to r in [-pi/4, pi/4], by:
62*f3087befSAndrew Turner r = x - q * pi/2, in extended precision. */
63*f3087befSAndrew Turner svfloat64_t r = x;
64*f3087befSAndrew Turner svfloat64_t half_pi = svld1rq (svptrue_b64 (), &dat->half_pi_hi);
65*f3087befSAndrew Turner r = svmls_lane (r, q, half_pi, 0);
66*f3087befSAndrew Turner r = svmls_lane (r, q, half_pi, 1);
67*f3087befSAndrew Turner /* Further reduce r to [-pi/8, pi/8], to be reconstructed using double angle
68*f3087befSAndrew Turner formula. */
69*f3087befSAndrew Turner r = svmul_x (svptrue_b64 (), r, 0.5);
70*f3087befSAndrew Turner
71*f3087befSAndrew Turner /* Approximate tan(r) using order 8 polynomial.
72*f3087befSAndrew Turner tan(x) is odd, so polynomial has the form:
73*f3087befSAndrew Turner tan(x) ~= x + C0 * x^3 + C1 * x^5 + C3 * x^7 + ...
74*f3087befSAndrew Turner Hence we first approximate P(r) = C1 + C2 * r^2 + C3 * r^4 + ...
75*f3087befSAndrew Turner Then compute the approximation by:
76*f3087befSAndrew Turner tan(r) ~= r + r^3 * (C0 + r^2 * P(r)). */
77*f3087befSAndrew Turner
78*f3087befSAndrew Turner svfloat64_t r2 = svmul_x (svptrue_b64 (), r, r);
79*f3087befSAndrew Turner svfloat64_t r4 = svmul_x (svptrue_b64 (), r2, r2);
80*f3087befSAndrew Turner svfloat64_t r8 = svmul_x (svptrue_b64 (), r4, r4);
81*f3087befSAndrew Turner /* Use offset version coeff array by 1 to evaluate from C1 onwards. */
82*f3087befSAndrew Turner svfloat64_t C_24 = svld1rq (svptrue_b64 (), &dat->c2);
83*f3087befSAndrew Turner svfloat64_t C_68 = svld1rq (svptrue_b64 (), &dat->c6);
84*f3087befSAndrew Turner
85*f3087befSAndrew Turner /* Use offset version coeff array by 1 to evaluate from C1 onwards. */
86*f3087befSAndrew Turner svfloat64_t p01 = svmla_lane (sv_f64 (dat->poly_1357[0]), r2, C_24, 0);
87*f3087befSAndrew Turner svfloat64_t p23 = svmla_lane_f64 (sv_f64 (dat->poly_1357[1]), r2, C_24, 1);
88*f3087befSAndrew Turner svfloat64_t p03 = svmla_x (pg, p01, p23, r4);
89*f3087befSAndrew Turner
90*f3087befSAndrew Turner svfloat64_t p45 = svmla_lane (sv_f64 (dat->poly_1357[2]), r2, C_68, 0);
91*f3087befSAndrew Turner svfloat64_t p67 = svmla_lane (sv_f64 (dat->poly_1357[3]), r2, C_68, 1);
92*f3087befSAndrew Turner svfloat64_t p47 = svmla_x (pg, p45, p67, r4);
93*f3087befSAndrew Turner
94*f3087befSAndrew Turner svfloat64_t p = svmla_x (pg, p03, p47, r8);
95*f3087befSAndrew Turner
96*f3087befSAndrew Turner svfloat64_t z = svmul_x (svptrue_b64 (), p, r);
97*f3087befSAndrew Turner z = svmul_x (svptrue_b64 (), r2, z);
98*f3087befSAndrew Turner z = svmla_lane (z, r, half_pi_c0, 0);
99*f3087befSAndrew Turner p = svmla_x (pg, r, r2, z);
100*f3087befSAndrew Turner
101*f3087befSAndrew Turner /* Recombination uses double-angle formula:
102*f3087befSAndrew Turner tan(2x) = 2 * tan(x) / (1 - (tan(x))^2)
103*f3087befSAndrew Turner and reciprocity around pi/2:
104*f3087befSAndrew Turner tan(x) = 1 / (tan(pi/2 - x))
105*f3087befSAndrew Turner to assemble result using change-of-sign and conditional selection of
106*f3087befSAndrew Turner numerator/denominator dependent on odd/even-ness of q (quadrant). */
107*f3087befSAndrew Turner
108*f3087befSAndrew Turner /* Invert condition to catch NaNs and Infs as well as large values. */
109*f3087befSAndrew Turner svbool_t special = svnot_z (pg, svaclt (pg, x, dat->range_val));
110*f3087befSAndrew Turner
111*f3087befSAndrew Turner if (unlikely (svptest_any (pg, special)))
112*f3087befSAndrew Turner {
113*f3087befSAndrew Turner return special_case (x, p, q, pg, special);
114*f3087befSAndrew Turner }
115*f3087befSAndrew Turner svbool_t use_recip = svcmpeq (
116*f3087befSAndrew Turner pg, svand_x (pg, svreinterpret_u64 (svcvt_s64_x (pg, q)), 1), 0);
117*f3087befSAndrew Turner
118*f3087befSAndrew Turner svfloat64_t n = svmad_x (pg, p, p, -1);
119*f3087befSAndrew Turner svfloat64_t d = svmul_x (svptrue_b64 (), p, 2);
120*f3087befSAndrew Turner svfloat64_t swap = n;
121*f3087befSAndrew Turner n = svneg_m (n, use_recip, d);
122*f3087befSAndrew Turner d = svsel (use_recip, swap, d);
123*f3087befSAndrew Turner return svdiv_x (pg, n, d);
124*f3087befSAndrew Turner }
125*f3087befSAndrew Turner
126*f3087befSAndrew Turner TEST_SIG (SV, D, 1, tan, -3.1, 3.1)
127*f3087befSAndrew Turner TEST_ULP (SV_NAME_D1 (tan), 2.99)
128*f3087befSAndrew Turner TEST_DISABLE_FENV (SV_NAME_D1 (tan))
129*f3087befSAndrew Turner TEST_SYM_INTERVAL (SV_NAME_D1 (tan), 0, 0x1p23, 500000)
130*f3087befSAndrew Turner TEST_SYM_INTERVAL (SV_NAME_D1 (tan), 0x1p23, inf, 5000)
131*f3087befSAndrew Turner CLOSE_SVE_ATTR
132