• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Single-precision vector tan(x) function.
3  *
4  * Copyright (c) 2021-2023, Arm Limited.
5  * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6  */
7 
8 #include "v_math.h"
9 #include "estrinf.h"
10 #include "pl_sig.h"
11 #include "pl_test.h"
12 
13 #if V_SUPPORTED
14 
15 /* Constants.  */
16 #define NegPio2_1 (v_f32 (-0x1.921fb6p+0f))
17 #define NegPio2_2 (v_f32 (0x1.777a5cp-25f))
18 #define NegPio2_3 (v_f32 (0x1.ee59dap-50f))
19 #define InvPio2 (v_f32 (0x1.45f306p-1f))
20 #define RangeVal (0x47000000)  /* asuint32(0x1p15f).  */
21 #define TinyBound (0x30000000) /* asuint32 (0x1p-31).  */
22 #define Shift (v_f32 (0x1.8p+23f))
23 #define AbsMask (v_u32 (0x7fffffff))
24 
25 #define poly(i) v_f32 (__tanf_poly_data.poly_tan[i])
26 
27 /* Special cases (fall back to scalar calls).  */
28 VPCS_ATTR
29 NOINLINE static v_f32_t
specialcase(v_f32_t x,v_f32_t y,v_u32_t cmp)30 specialcase (v_f32_t x, v_f32_t y, v_u32_t cmp)
31 {
32   return v_call_f32 (tanf, x, y, cmp);
33 }
34 
35 /* Use a full Estrin scheme to evaluate polynomial.  */
36 static inline v_f32_t
eval_poly(v_f32_t z)37 eval_poly (v_f32_t z)
38 {
39   v_f32_t z2 = z * z;
40 #if WANT_SIMD_EXCEPT
41   /* Tiny z (<= 0x1p-31) will underflow when calculating z^4. If fp exceptions
42      are to be triggered correctly, sidestep this by fixing such lanes to 0.  */
43   v_u32_t will_uflow = v_cond_u32 ((v_as_u32_f32 (z) & AbsMask) <= TinyBound);
44   if (unlikely (v_any_u32 (will_uflow)))
45     z2 = v_sel_f32 (will_uflow, v_f32 (0), z2);
46 #endif
47   v_f32_t z4 = z2 * z2;
48   return ESTRIN_5 (z, z2, z4, poly);
49 }
50 
51 /* Fast implementation of Neon tanf.
52    Maximum error is 3.45 ULP:
53    __v_tanf(-0x1.e5f0cap+13) got 0x1.ff9856p-1
54 			    want 0x1.ff9850p-1.  */
55 VPCS_ATTR
V_NAME(tanf)56 v_f32_t V_NAME (tanf) (v_f32_t x)
57 {
58   v_f32_t special_arg = x;
59   v_u32_t ix = v_as_u32_f32 (x);
60   v_u32_t iax = ix & AbsMask;
61 
62   /* iax >= RangeVal means x, if not inf or NaN, is too large to perform fast
63      regression.  */
64 #if WANT_SIMD_EXCEPT
65   /* If fp exceptions are to be triggered correctly, also special-case tiny
66      input, as this will load to overflow later. Fix any special lanes to 1 to
67      prevent any exceptions being triggered.  */
68   v_u32_t special = v_cond_u32 (iax - TinyBound >= RangeVal - TinyBound);
69   if (unlikely (v_any_u32 (special)))
70     x = v_sel_f32 (special, v_f32 (1.0f), x);
71 #else
72   /* Otherwise, special-case large and special values.  */
73   v_u32_t special = v_cond_u32 (iax >= RangeVal);
74 #endif
75 
76   /* n = rint(x/(pi/2)).  */
77   v_f32_t q = v_fma_f32 (InvPio2, x, Shift);
78   v_f32_t n = q - Shift;
79   /* n is representable as a signed integer, simply convert it.  */
80   v_s32_t in = v_round_s32 (n);
81   /* Determine if x lives in an interval, where |tan(x)| grows to infinity.  */
82   v_s32_t alt = in & 1;
83   v_u32_t pred_alt = (alt != 0);
84 
85   /* r = x - n * (pi/2)  (range reduction into -pi./4 .. pi/4).  */
86   v_f32_t r;
87   r = v_fma_f32 (NegPio2_1, n, x);
88   r = v_fma_f32 (NegPio2_2, n, r);
89   r = v_fma_f32 (NegPio2_3, n, r);
90 
91   /* If x lives in an interval, where |tan(x)|
92      - is finite, then use a polynomial approximation of the form
93        tan(r) ~ r + r^3 * P(r^2) = r + r * r^2 * P(r^2).
94      - grows to infinity then use symmetries of tangent and the identity
95        tan(r) = cotan(pi/2 - r) to express tan(x) as 1/tan(-r). Finally, use
96        the same polynomial approximation of tan as above.  */
97 
98   /* Perform additional reduction if required.  */
99   v_f32_t z = v_sel_f32 (pred_alt, -r, r);
100 
101   /* Evaluate polynomial approximation of tangent on [-pi/4, pi/4].  */
102   v_f32_t z2 = r * r;
103   v_f32_t p = eval_poly (z2);
104   v_f32_t y = v_fma_f32 (z * z2, p, z);
105 
106   /* Compute reciprocal and apply if required.  */
107   v_f32_t inv_y = v_div_f32 (v_f32 (1.0f), y);
108   y = v_sel_f32 (pred_alt, inv_y, y);
109 
110   /* Fast reduction does not handle the x = -0.0 case well,
111      therefore it is fixed here.  */
112   y = v_sel_f32 (x == v_f32 (-0.0), x, y);
113 
114   if (unlikely (v_any_u32 (special)))
115     return specialcase (special_arg, y, special);
116   return y;
117 }
118 VPCS_ALIAS
119 
120 PL_SIG (V, F, 1, tan, -3.1, 3.1)
121 PL_TEST_ULP (V_NAME (tanf), 2.96)
122 PL_TEST_EXPECT_FENV (V_NAME (tanf), WANT_SIMD_EXCEPT)
123 PL_TEST_INTERVAL (V_NAME (tanf), -0.0, -0x1p126, 100)
124 PL_TEST_INTERVAL (V_NAME (tanf), 0x1p-149, 0x1p-126, 4000)
125 PL_TEST_INTERVAL (V_NAME (tanf), 0x1p-126, 0x1p-23, 50000)
126 PL_TEST_INTERVAL (V_NAME (tanf), 0x1p-23, 0.7, 50000)
127 PL_TEST_INTERVAL (V_NAME (tanf), 0.7, 1.5, 50000)
128 PL_TEST_INTERVAL (V_NAME (tanf), 1.5, 100, 50000)
129 PL_TEST_INTERVAL (V_NAME (tanf), 100, 0x1p17, 50000)
130 PL_TEST_INTERVAL (V_NAME (tanf), 0x1p17, inf, 50000)
131 #endif
132