1 | /* Double-precision vector (Advanced SIMD) tan function |
2 | |
3 | Copyright (C) 2023-2024 Free Software Foundation, Inc. |
4 | This file is part of the GNU C Library. |
5 | |
6 | The GNU C Library is free software; you can redistribute it and/or |
7 | modify it under the terms of the GNU Lesser General Public |
8 | License as published by the Free Software Foundation; either |
9 | version 2.1 of the License, or (at your option) any later version. |
10 | |
11 | The GNU C Library is distributed in the hope that it will be useful, |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | Lesser General Public License for more details. |
15 | |
16 | You should have received a copy of the GNU Lesser General Public |
17 | License along with the GNU C Library; if not, see |
18 | <https://www.gnu.org/licenses/>. */ |
19 | |
20 | #include "v_math.h" |
21 | #include "poly_advsimd_f64.h" |
22 | |
23 | static const struct data |
24 | { |
25 | float64x2_t poly[9]; |
26 | float64x2_t half_pi, two_over_pi, shift; |
27 | #if !WANT_SIMD_EXCEPT |
28 | float64x2_t range_val; |
29 | #endif |
30 | } data = { |
31 | /* Coefficients generated using FPMinimax. */ |
32 | .poly = { V2 (0x1.5555555555556p-2), V2 (0x1.1111111110a63p-3), |
33 | V2 (0x1.ba1ba1bb46414p-5), V2 (0x1.664f47e5b5445p-6), |
34 | V2 (0x1.226e5e5ecdfa3p-7), V2 (0x1.d6c7ddbf87047p-9), |
35 | V2 (0x1.7ea75d05b583ep-10), V2 (0x1.289f22964a03cp-11), |
36 | V2 (0x1.4e4fd14147622p-12) }, |
37 | .half_pi = { 0x1.921fb54442d18p0, 0x1.1a62633145c07p-54 }, |
38 | .two_over_pi = V2 (0x1.45f306dc9c883p-1), |
39 | .shift = V2 (0x1.8p52), |
40 | #if !WANT_SIMD_EXCEPT |
41 | .range_val = V2 (0x1p23), |
42 | #endif |
43 | }; |
44 | |
45 | #define RangeVal 0x4160000000000000 /* asuint64(0x1p23). */ |
46 | #define TinyBound 0x3e50000000000000 /* asuint64(2^-26). */ |
47 | #define Thresh 0x310000000000000 /* RangeVal - TinyBound. */ |
48 | |
49 | /* Special cases (fall back to scalar calls). */ |
50 | static float64x2_t VPCS_ATTR NOINLINE |
51 | special_case (float64x2_t x) |
52 | { |
53 | return v_call_f64 (tan, x, x, v_u64 (x: -1)); |
54 | } |
55 | |
56 | /* Vector approximation for double-precision tan. |
57 | Maximum measured error is 3.48 ULP: |
58 | _ZGVnN2v_tan(0x1.4457047ef78d8p+20) got -0x1.f6ccd8ecf7dedp+37 |
59 | want -0x1.f6ccd8ecf7deap+37. */ |
60 | float64x2_t VPCS_ATTR V_NAME_D1 (tan) (float64x2_t x) |
61 | { |
62 | const struct data *dat = ptr_barrier (&data); |
63 | /* Our argument reduction cannot calculate q with sufficient accuracy for |
64 | very large inputs. Fall back to scalar routine for all lanes if any are |
65 | too large, or Inf/NaN. If fenv exceptions are expected, also fall back for |
66 | tiny input to avoid underflow. */ |
67 | #if WANT_SIMD_EXCEPT |
68 | uint64x2_t iax = vreinterpretq_u64_f64 (vabsq_f64 (x)); |
69 | /* iax - tiny_bound > range_val - tiny_bound. */ |
70 | uint64x2_t special |
71 | = vcgtq_u64 (vsubq_u64 (iax, v_u64 (TinyBound)), v_u64 (Thresh)); |
72 | if (__glibc_unlikely (v_any_u64 (special))) |
73 | return special_case (x); |
74 | #endif |
75 | |
76 | /* q = nearest integer to 2 * x / pi. */ |
77 | float64x2_t q |
78 | = vsubq_f64 (vfmaq_f64 (dat->shift, x, dat->two_over_pi), dat->shift); |
79 | int64x2_t qi = vcvtq_s64_f64 (q); |
80 | |
81 | /* Use q to reduce x to r in [-pi/4, pi/4], by: |
82 | r = x - q * pi/2, in extended precision. */ |
83 | float64x2_t r = x; |
84 | r = vfmsq_laneq_f64 (r, q, dat->half_pi, 0); |
85 | r = vfmsq_laneq_f64 (r, q, dat->half_pi, 1); |
86 | /* Further reduce r to [-pi/8, pi/8], to be reconstructed using double angle |
87 | formula. */ |
88 | r = vmulq_n_f64 (r, 0.5); |
89 | |
90 | /* Approximate tan(r) using order 8 polynomial. |
91 | tan(x) is odd, so polynomial has the form: |
92 | tan(x) ~= x + C0 * x^3 + C1 * x^5 + C3 * x^7 + ... |
93 | Hence we first approximate P(r) = C1 + C2 * r^2 + C3 * r^4 + ... |
94 | Then compute the approximation by: |
95 | tan(r) ~= r + r^3 * (C0 + r^2 * P(r)). */ |
96 | float64x2_t r2 = vmulq_f64 (r, r), r4 = vmulq_f64 (r2, r2), |
97 | r8 = vmulq_f64 (r4, r4); |
98 | /* Offset coefficients to evaluate from C1 onwards. */ |
99 | float64x2_t p = v_estrin_7_f64 (r2, r4, r8, dat->poly + 1); |
100 | p = vfmaq_f64 (dat->poly[0], p, r2); |
101 | p = vfmaq_f64 (r, r2, vmulq_f64 (p, r)); |
102 | |
103 | /* Recombination uses double-angle formula: |
104 | tan(2x) = 2 * tan(x) / (1 - (tan(x))^2) |
105 | and reciprocity around pi/2: |
106 | tan(x) = 1 / (tan(pi/2 - x)) |
107 | to assemble result using change-of-sign and conditional selection of |
108 | numerator/denominator, dependent on odd/even-ness of q (hence quadrant). |
109 | */ |
110 | float64x2_t n = vfmaq_f64 (v_f64 (-1), p, p); |
111 | float64x2_t d = vaddq_f64 (p, p); |
112 | |
113 | uint64x2_t no_recip = vtstq_u64 (vreinterpretq_u64_s64 (qi), v_u64 (1)); |
114 | |
115 | #if !WANT_SIMD_EXCEPT |
116 | uint64x2_t special = vcageq_f64 (x, dat->range_val); |
117 | if (__glibc_unlikely (v_any_u64 (special))) |
118 | return special_case (x); |
119 | #endif |
120 | |
121 | return vdivq_f64 (vbslq_f64 (no_recip, n, vnegq_f64 (d)), |
122 | vbslq_f64 (no_recip, d, n)); |
123 | } |
124 | |