xref: /freebsd/contrib/arm-optimized-routines/math/aarch64/sve/tanf.c (revision f3087bef11543b42e0d69b708f367097a4118d24)
1*f3087befSAndrew Turner /*
2*f3087befSAndrew Turner  * Single-precision vector tan(x) function.
3*f3087befSAndrew Turner  *
4*f3087befSAndrew Turner  * Copyright (c) 2020-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   float pio2_1, pio2_2, pio2_3, invpio2;
15*f3087befSAndrew Turner   float c1, c3, c5;
16*f3087befSAndrew Turner   float c0, c2, c4, range_val, shift;
17*f3087befSAndrew Turner } data = {
18*f3087befSAndrew Turner   /* Coefficients generated using:
19*f3087befSAndrew Turner      poly = fpminimax((tan(sqrt(x))-sqrt(x))/x^(3/2),
20*f3087befSAndrew Turner 		      deg,
21*f3087befSAndrew Turner 		      [|single ...|],
22*f3087befSAndrew Turner 		      [a*a;b*b]);
23*f3087befSAndrew Turner      optimize relative error
24*f3087befSAndrew Turner      final prec : 23 bits
25*f3087befSAndrew Turner      deg : 5
26*f3087befSAndrew Turner      a : 0x1p-126 ^ 2
27*f3087befSAndrew Turner      b : ((pi) / 0x1p2) ^ 2
28*f3087befSAndrew Turner      dirty rel error: 0x1.f7c2e4p-25
29*f3087befSAndrew Turner      dirty abs error: 0x1.f7c2ecp-25.  */
30*f3087befSAndrew Turner   .c0 = 0x1.55555p-2,	      .c1 = 0x1.11166p-3,
31*f3087befSAndrew Turner   .c2 = 0x1.b88a78p-5,	      .c3 = 0x1.7b5756p-6,
32*f3087befSAndrew Turner   .c4 = 0x1.4ef4cep-8,	      .c5 = 0x1.0e1e74p-7,
33*f3087befSAndrew Turner 
34*f3087befSAndrew Turner   .pio2_1 = 0x1.921fb6p+0f,   .pio2_2 = -0x1.777a5cp-25f,
35*f3087befSAndrew Turner   .pio2_3 = -0x1.ee59dap-50f, .invpio2 = 0x1.45f306p-1f,
36*f3087befSAndrew Turner   .range_val = 0x1p15f,	      .shift = 0x1.8p+23f
37*f3087befSAndrew Turner };
38*f3087befSAndrew Turner 
39*f3087befSAndrew Turner static svfloat32_t NOINLINE
special_case(svfloat32_t x,svfloat32_t y,svbool_t cmp)40*f3087befSAndrew Turner special_case (svfloat32_t x, svfloat32_t y, svbool_t cmp)
41*f3087befSAndrew Turner {
42*f3087befSAndrew Turner   return sv_call_f32 (tanf, x, y, cmp);
43*f3087befSAndrew Turner }
44*f3087befSAndrew Turner 
45*f3087befSAndrew Turner /* Fast implementation of SVE tanf.
46*f3087befSAndrew Turner    Maximum error is 3.45 ULP:
47*f3087befSAndrew Turner    SV_NAME_F1 (tan)(-0x1.e5f0cap+13) got 0x1.ff9856p-1
48*f3087befSAndrew Turner 				    want 0x1.ff9850p-1.  */
SV_NAME_F1(tan)49*f3087befSAndrew Turner svfloat32_t SV_NAME_F1 (tan) (svfloat32_t x, const svbool_t pg)
50*f3087befSAndrew Turner {
51*f3087befSAndrew Turner   const struct data *d = ptr_barrier (&data);
52*f3087befSAndrew Turner 
53*f3087befSAndrew Turner   svfloat32_t odd_coeffs = svld1rq (svptrue_b32 (), &d->c1);
54*f3087befSAndrew Turner   svfloat32_t pi_vals = svld1rq (svptrue_b32 (), &d->pio2_1);
55*f3087befSAndrew Turner 
56*f3087befSAndrew Turner   /* n = rint(x/(pi/2)).  */
57*f3087befSAndrew Turner   svfloat32_t n = svrintn_x (pg, svmul_lane (x, pi_vals, 3));
58*f3087befSAndrew Turner   /* n is already a signed integer, simply convert it.  */
59*f3087befSAndrew Turner   svint32_t in = svcvt_s32_x (pg, n);
60*f3087befSAndrew Turner   /* Determine if x lives in an interval, where |tan(x)| grows to infinity.  */
61*f3087befSAndrew Turner   svint32_t alt = svand_x (pg, in, 1);
62*f3087befSAndrew Turner   svbool_t pred_alt = svcmpne (pg, alt, 0);
63*f3087befSAndrew Turner   /* r = x - n * (pi/2)  (range reduction into 0 .. pi/4).  */
64*f3087befSAndrew Turner   svfloat32_t r;
65*f3087befSAndrew Turner   r = svmls_lane (x, n, pi_vals, 0);
66*f3087befSAndrew Turner   r = svmls_lane (r, n, pi_vals, 1);
67*f3087befSAndrew Turner   r = svmls_lane (r, n, pi_vals, 2);
68*f3087befSAndrew Turner 
69*f3087befSAndrew Turner   /* If x lives in an interval, where |tan(x)|
70*f3087befSAndrew Turner      - is finite, then use a polynomial approximation of the form
71*f3087befSAndrew Turner        tan(r) ~ r + r^3 * P(r^2) = r + r * r^2 * P(r^2).
72*f3087befSAndrew Turner      - grows to infinity then use symmetries of tangent and the identity
73*f3087befSAndrew Turner        tan(r) = cotan(pi/2 - r) to express tan(x) as 1/tan(-r). Finally, use
74*f3087befSAndrew Turner        the same polynomial approximation of tan as above.  */
75*f3087befSAndrew Turner 
76*f3087befSAndrew Turner   /* Perform additional reduction if required.  */
77*f3087befSAndrew Turner   svfloat32_t z = svneg_m (r, pred_alt, r);
78*f3087befSAndrew Turner 
79*f3087befSAndrew Turner   /* Evaluate polynomial approximation of tangent on [-pi/4, pi/4],
80*f3087befSAndrew Turner      using Estrin on z^2.  */
81*f3087befSAndrew Turner   svfloat32_t z2 = svmul_x (svptrue_b32 (), r, r);
82*f3087befSAndrew Turner   svfloat32_t p01 = svmla_lane (sv_f32 (d->c0), z2, odd_coeffs, 0);
83*f3087befSAndrew Turner   svfloat32_t p23 = svmla_lane (sv_f32 (d->c2), z2, odd_coeffs, 1);
84*f3087befSAndrew Turner   svfloat32_t p45 = svmla_lane (sv_f32 (d->c4), z2, odd_coeffs, 2);
85*f3087befSAndrew Turner 
86*f3087befSAndrew Turner   svfloat32_t z4 = svmul_x (pg, z2, z2);
87*f3087befSAndrew Turner   svfloat32_t p = svmla_x (pg, p01, z4, p23);
88*f3087befSAndrew Turner 
89*f3087befSAndrew Turner   svfloat32_t z8 = svmul_x (pg, z4, z4);
90*f3087befSAndrew Turner   p = svmla_x (pg, p, z8, p45);
91*f3087befSAndrew Turner 
92*f3087befSAndrew Turner   svfloat32_t y = svmla_x (pg, z, p, svmul_x (pg, z, z2));
93*f3087befSAndrew Turner 
94*f3087befSAndrew Turner   /* No need to pass pg to specialcase here since cmp is a strict subset,
95*f3087befSAndrew Turner      guaranteed by the cmpge above.  */
96*f3087befSAndrew Turner 
97*f3087befSAndrew Turner   /* Determine whether input is too large to perform fast regression.  */
98*f3087befSAndrew Turner   svbool_t cmp = svacge (pg, x, d->range_val);
99*f3087befSAndrew Turner   if (unlikely (svptest_any (pg, cmp)))
100*f3087befSAndrew Turner     return special_case (x, svdivr_x (pg, y, 1.0f), cmp);
101*f3087befSAndrew Turner 
102*f3087befSAndrew Turner   svfloat32_t inv_y = svdivr_x (pg, y, 1.0f);
103*f3087befSAndrew Turner   return svsel (pred_alt, inv_y, y);
104*f3087befSAndrew Turner }
105*f3087befSAndrew Turner 
106*f3087befSAndrew Turner TEST_SIG (SV, F, 1, tan, -3.1, 3.1)
107*f3087befSAndrew Turner TEST_ULP (SV_NAME_F1 (tan), 2.96)
108*f3087befSAndrew Turner TEST_DISABLE_FENV (SV_NAME_F1 (tan))
109*f3087befSAndrew Turner TEST_INTERVAL (SV_NAME_F1 (tan), -0.0, -0x1p126, 100)
110*f3087befSAndrew Turner TEST_INTERVAL (SV_NAME_F1 (tan), 0x1p-149, 0x1p-126, 4000)
111*f3087befSAndrew Turner TEST_INTERVAL (SV_NAME_F1 (tan), 0x1p-126, 0x1p-23, 50000)
112*f3087befSAndrew Turner TEST_INTERVAL (SV_NAME_F1 (tan), 0x1p-23, 0.7, 50000)
113*f3087befSAndrew Turner TEST_INTERVAL (SV_NAME_F1 (tan), 0.7, 1.5, 50000)
114*f3087befSAndrew Turner TEST_INTERVAL (SV_NAME_F1 (tan), 1.5, 100, 50000)
115*f3087befSAndrew Turner TEST_INTERVAL (SV_NAME_F1 (tan), 100, 0x1p17, 50000)
116*f3087befSAndrew Turner TEST_INTERVAL (SV_NAME_F1 (tan), 0x1p17, inf, 50000)
117*f3087befSAndrew Turner CLOSE_SVE_ATTR
118