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 "estrin.h"
10 #include "pl_sig.h"
11 #include "pl_test.h"
12 
13 #if V_SUPPORTED
14 
15 #define MHalfPiHi v_f64 (__v_tan_data.neg_half_pi_hi)
16 #define MHalfPiLo v_f64 (__v_tan_data.neg_half_pi_lo)
17 #define TwoOverPi v_f64 (0x1.45f306dc9c883p-1)
18 #define Shift v_f64 (0x1.8p52)
19 #define AbsMask 0x7fffffffffffffff
20 #define RangeVal 0x4160000000000000  /* asuint64(2^23).  */
21 #define TinyBound 0x3e50000000000000 /* asuint64(2^-26).  */
22 #define C(i) v_f64 (__v_tan_data.poly[i])
23 
24 /* Special cases (fall back to scalar calls).  */
25 VPCS_ATTR
26 NOINLINE static v_f64_t
27 specialcase (v_f64_t x)
28 {
29   return v_call_f64 (tan, x, x, v_u64 (-1));
30 }
31 
32 /* Vector approximation for double-precision tan.
33    Maximum measured error is 3.48 ULP:
34    __v_tan(0x1.4457047ef78d8p+20) got -0x1.f6ccd8ecf7dedp+37
35 				 want -0x1.f6ccd8ecf7deap+37.   */
36 VPCS_ATTR
37 v_f64_t V_NAME (tan) (v_f64_t x)
38 {
39   v_u64_t iax = v_as_u64_f64 (x) & AbsMask;
40 
41   /* Our argument reduction cannot calculate q with sufficient accuracy for very
42      large inputs. Fall back to scalar routine for all lanes if any are too
43      large, or Inf/NaN. If fenv exceptions are expected, also fall back for tiny
44      input to avoid underflow. Note pl does not supply a scalar double-precision
45      tan, so the fallback will be statically linked from the system libm.  */
46 #if WANT_SIMD_EXCEPT
47   if (unlikely (v_any_u64 (iax - TinyBound > RangeVal - TinyBound)))
48 #else
49   if (unlikely (v_any_u64 (iax > RangeVal)))
50 #endif
51     return specialcase (x);
52 
53   /* q = nearest integer to 2 * x / pi.  */
54   v_f64_t q = v_fma_f64 (x, TwoOverPi, Shift) - Shift;
55   v_s64_t qi = v_to_s64_f64 (q);
56 
57   /* Use q to reduce x to r in [-pi/4, pi/4], by:
58      r = x - q * pi/2, in extended precision.  */
59   v_f64_t r = x;
60   r = v_fma_f64 (q, MHalfPiHi, r);
61   r = v_fma_f64 (q, MHalfPiLo, r);
62   /* Further reduce r to [-pi/8, pi/8], to be reconstructed using double angle
63      formula.  */
64   r = r * 0.5;
65 
66   /* Approximate tan(r) using order 8 polynomial.
67      tan(x) is odd, so polynomial has the form:
68      tan(x) ~= x + C0 * x^3 + C1 * x^5 + C3 * x^7 + ...
69      Hence we first approximate P(r) = C1 + C2 * r^2 + C3 * r^4 + ...
70      Then compute the approximation by:
71      tan(r) ~= r + r^3 * (C0 + r^2 * P(r)).  */
72   v_f64_t r2 = r * r, r4 = r2 * r2, r8 = r4 * r4;
73   /* Use offset version of Estrin wrapper to evaluate from C1 onwards.  */
74   v_f64_t p = ESTRIN_7_ (r2, r4, r8, C, 1);
75   p = v_fma_f64 (p, r2, C (0));
76   p = v_fma_f64 (r2, p * r, r);
77 
78   /* Recombination uses double-angle formula:
79      tan(2x) = 2 * tan(x) / (1 - (tan(x))^2)
80      and reciprocity around pi/2:
81      tan(x) = 1 / (tan(pi/2 - x))
82      to assemble result using change-of-sign and conditional selection of
83      numerator/denominator, dependent on odd/even-ness of q (hence quadrant). */
84   v_f64_t n = v_fma_f64 (p, p, v_f64 (-1));
85   v_f64_t d = p * 2;
86 
87   v_u64_t use_recip = v_cond_u64 ((v_as_u64_s64 (qi) & 1) == 0);
88 
89   return v_sel_f64 (use_recip, -d, n) / v_sel_f64 (use_recip, n, d);
90 }
91 VPCS_ALIAS
92 
93 PL_SIG (V, D, 1, tan, -3.1, 3.1)
94 PL_TEST_ULP (V_NAME (tan), 2.99)
95 PL_TEST_EXPECT_FENV (V_NAME (tan), WANT_SIMD_EXCEPT)
96 PL_TEST_INTERVAL (V_NAME (tan), 0, TinyBound, 5000)
97 PL_TEST_INTERVAL (V_NAME (tan), TinyBound, RangeVal, 100000)
98 PL_TEST_INTERVAL (V_NAME (tan), RangeVal, inf, 5000)
99 PL_TEST_INTERVAL (V_NAME (tan), -0, -TinyBound, 5000)
100 PL_TEST_INTERVAL (V_NAME (tan), -TinyBound, -RangeVal, 100000)
101 PL_TEST_INTERVAL (V_NAME (tan), -RangeVal, -inf, 5000)
102 #endif
103