xref: /freebsd/contrib/arm-optimized-routines/pl/math/v_tan_3u5.c (revision b59017c5cad90d0f09a59e68c00457b7faf93e7c)
1 /*
2  * Double-precision vector 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 "v_math.h"
9 #include "poly_advsimd_f64.h"
10 #include "pl_sig.h"
11 #include "pl_test.h"
12 
13 static const struct data
14 {
15   float64x2_t poly[9];
16   float64x2_t half_pi, two_over_pi, shift;
17 #if !WANT_SIMD_EXCEPT
18   float64x2_t range_val;
19 #endif
20 } data = {
21   /* Coefficients generated using FPMinimax.  */
22   .poly = { V2 (0x1.5555555555556p-2), V2 (0x1.1111111110a63p-3),
23 	    V2 (0x1.ba1ba1bb46414p-5), V2 (0x1.664f47e5b5445p-6),
24 	    V2 (0x1.226e5e5ecdfa3p-7), V2 (0x1.d6c7ddbf87047p-9),
25 	    V2 (0x1.7ea75d05b583ep-10), V2 (0x1.289f22964a03cp-11),
26 	    V2 (0x1.4e4fd14147622p-12) },
27   .half_pi = { 0x1.921fb54442d18p0, 0x1.1a62633145c07p-54 },
28   .two_over_pi = V2 (0x1.45f306dc9c883p-1),
29   .shift = V2 (0x1.8p52),
30 #if !WANT_SIMD_EXCEPT
31   .range_val = V2 (0x1p23),
32 #endif
33 };
34 
35 #define RangeVal 0x4160000000000000  /* asuint64(0x1p23).  */
36 #define TinyBound 0x3e50000000000000 /* asuint64(2^-26).  */
37 #define Thresh 0x310000000000000     /* RangeVal - TinyBound.  */
38 
39 /* Special cases (fall back to scalar calls).  */
40 static float64x2_t VPCS_ATTR NOINLINE
41 special_case (float64x2_t x)
42 {
43   return v_call_f64 (tan, x, x, v_u64 (-1));
44 }
45 
46 /* Vector approximation for double-precision tan.
47    Maximum measured error is 3.48 ULP:
48    _ZGVnN2v_tan(0x1.4457047ef78d8p+20) got -0x1.f6ccd8ecf7dedp+37
49 				      want -0x1.f6ccd8ecf7deap+37.  */
50 float64x2_t VPCS_ATTR V_NAME_D1 (tan) (float64x2_t x)
51 {
52   const struct data *dat = ptr_barrier (&data);
53   /* Our argument reduction cannot calculate q with sufficient accuracy for
54      very large inputs. Fall back to scalar routine for all lanes if any are
55      too large, or Inf/NaN. If fenv exceptions are expected, also fall back for
56      tiny input to avoid underflow.  */
57 #if WANT_SIMD_EXCEPT
58   uint64x2_t iax = vreinterpretq_u64_f64 (vabsq_f64 (x));
59   /* iax - tiny_bound > range_val - tiny_bound.  */
60   uint64x2_t special
61       = vcgtq_u64 (vsubq_u64 (iax, v_u64 (TinyBound)), v_u64 (Thresh));
62   if (unlikely (v_any_u64 (special)))
63     return special_case (x);
64 #endif
65 
66   /* q = nearest integer to 2 * x / pi.  */
67   float64x2_t q
68       = vsubq_f64 (vfmaq_f64 (dat->shift, x, dat->two_over_pi), dat->shift);
69   int64x2_t qi = vcvtq_s64_f64 (q);
70 
71   /* Use q to reduce x to r in [-pi/4, pi/4], by:
72      r = x - q * pi/2, in extended precision.  */
73   float64x2_t r = x;
74   r = vfmsq_laneq_f64 (r, q, dat->half_pi, 0);
75   r = vfmsq_laneq_f64 (r, q, dat->half_pi, 1);
76   /* Further reduce r to [-pi/8, pi/8], to be reconstructed using double angle
77      formula.  */
78   r = vmulq_n_f64 (r, 0.5);
79 
80   /* Approximate tan(r) using order 8 polynomial.
81      tan(x) is odd, so polynomial has the form:
82      tan(x) ~= x + C0 * x^3 + C1 * x^5 + C3 * x^7 + ...
83      Hence we first approximate P(r) = C1 + C2 * r^2 + C3 * r^4 + ...
84      Then compute the approximation by:
85      tan(r) ~= r + r^3 * (C0 + r^2 * P(r)).  */
86   float64x2_t r2 = vmulq_f64 (r, r), r4 = vmulq_f64 (r2, r2),
87 	      r8 = vmulq_f64 (r4, r4);
88   /* Offset coefficients to evaluate from C1 onwards.  */
89   float64x2_t p = v_estrin_7_f64 (r2, r4, r8, dat->poly + 1);
90   p = vfmaq_f64 (dat->poly[0], p, r2);
91   p = vfmaq_f64 (r, r2, vmulq_f64 (p, r));
92 
93   /* Recombination uses double-angle formula:
94      tan(2x) = 2 * tan(x) / (1 - (tan(x))^2)
95      and reciprocity around pi/2:
96      tan(x) = 1 / (tan(pi/2 - x))
97      to assemble result using change-of-sign and conditional selection of
98      numerator/denominator, dependent on odd/even-ness of q (hence quadrant).
99    */
100   float64x2_t n = vfmaq_f64 (v_f64 (-1), p, p);
101   float64x2_t d = vaddq_f64 (p, p);
102 
103   uint64x2_t no_recip = vtstq_u64 (vreinterpretq_u64_s64 (qi), v_u64 (1));
104 
105 #if !WANT_SIMD_EXCEPT
106   uint64x2_t special = vcageq_f64 (x, dat->range_val);
107   if (unlikely (v_any_u64 (special)))
108     return special_case (x);
109 #endif
110 
111   return vdivq_f64 (vbslq_f64 (no_recip, n, vnegq_f64 (d)),
112 		    vbslq_f64 (no_recip, d, n));
113 }
114 
115 PL_SIG (V, D, 1, tan, -3.1, 3.1)
116 PL_TEST_ULP (V_NAME_D1 (tan), 2.99)
117 PL_TEST_EXPECT_FENV (V_NAME_D1 (tan), WANT_SIMD_EXCEPT)
118 PL_TEST_SYM_INTERVAL (V_NAME_D1 (tan), 0, TinyBound, 5000)
119 PL_TEST_SYM_INTERVAL (V_NAME_D1 (tan), TinyBound, RangeVal, 100000)
120 PL_TEST_SYM_INTERVAL (V_NAME_D1 (tan), RangeVal, inf, 5000)
121