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