1*412f47f9SXin Li /*
2*412f47f9SXin Li * Single-precision vector tan(x) function.
3*412f47f9SXin Li *
4*412f47f9SXin Li * Copyright (c) 2021-2024, Arm Limited.
5*412f47f9SXin Li * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6*412f47f9SXin Li */
7*412f47f9SXin Li
8*412f47f9SXin Li #include "v_math.h"
9*412f47f9SXin Li #include "poly_advsimd_f32.h"
10*412f47f9SXin Li #include "pl_sig.h"
11*412f47f9SXin Li #include "pl_test.h"
12*412f47f9SXin Li
13*412f47f9SXin Li static const struct data
14*412f47f9SXin Li {
15*412f47f9SXin Li float32x4_t poly[6];
16*412f47f9SXin Li float pi_consts[4];
17*412f47f9SXin Li float32x4_t shift;
18*412f47f9SXin Li #if !WANT_SIMD_EXCEPT
19*412f47f9SXin Li float32x4_t range_val;
20*412f47f9SXin Li #endif
21*412f47f9SXin Li } data = {
22*412f47f9SXin Li /* Coefficients generated using FPMinimax. */
23*412f47f9SXin Li .poly = { V4 (0x1.55555p-2f), V4 (0x1.11166p-3f), V4 (0x1.b88a78p-5f),
24*412f47f9SXin Li V4 (0x1.7b5756p-6f), V4 (0x1.4ef4cep-8f), V4 (0x1.0e1e74p-7f) },
25*412f47f9SXin Li /* Stores constants: (-pi/2)_high, (-pi/2)_mid, (-pi/2)_low, and 2/pi. */
26*412f47f9SXin Li .pi_consts
27*412f47f9SXin Li = { -0x1.921fb6p+0f, 0x1.777a5cp-25f, 0x1.ee59dap-50f, 0x1.45f306p-1f },
28*412f47f9SXin Li .shift = V4 (0x1.8p+23f),
29*412f47f9SXin Li #if !WANT_SIMD_EXCEPT
30*412f47f9SXin Li .range_val = V4 (0x1p15f),
31*412f47f9SXin Li #endif
32*412f47f9SXin Li };
33*412f47f9SXin Li
34*412f47f9SXin Li #define RangeVal v_u32 (0x47000000) /* asuint32(0x1p15f). */
35*412f47f9SXin Li #define TinyBound v_u32 (0x30000000) /* asuint32 (0x1p-31f). */
36*412f47f9SXin Li #define Thresh v_u32 (0x16000000) /* asuint32(RangeVal) - TinyBound. */
37*412f47f9SXin Li
38*412f47f9SXin Li /* Special cases (fall back to scalar calls). */
39*412f47f9SXin Li static float32x4_t VPCS_ATTR NOINLINE
special_case(float32x4_t x,float32x4_t y,uint32x4_t cmp)40*412f47f9SXin Li special_case (float32x4_t x, float32x4_t y, uint32x4_t cmp)
41*412f47f9SXin Li {
42*412f47f9SXin Li return v_call_f32 (tanf, x, y, cmp);
43*412f47f9SXin Li }
44*412f47f9SXin Li
45*412f47f9SXin Li /* Use a full Estrin scheme to evaluate polynomial. */
46*412f47f9SXin Li static inline float32x4_t
eval_poly(float32x4_t z,const struct data * d)47*412f47f9SXin Li eval_poly (float32x4_t z, const struct data *d)
48*412f47f9SXin Li {
49*412f47f9SXin Li float32x4_t z2 = vmulq_f32 (z, z);
50*412f47f9SXin Li #if WANT_SIMD_EXCEPT
51*412f47f9SXin Li /* Tiny z (<= 0x1p-31) will underflow when calculating z^4.
52*412f47f9SXin Li If fp exceptions are to be triggered correctly,
53*412f47f9SXin Li sidestep this by fixing such lanes to 0. */
54*412f47f9SXin Li uint32x4_t will_uflow
55*412f47f9SXin Li = vcleq_u32 (vreinterpretq_u32_f32 (vabsq_f32 (z)), TinyBound);
56*412f47f9SXin Li if (unlikely (v_any_u32 (will_uflow)))
57*412f47f9SXin Li z2 = vbslq_f32 (will_uflow, v_f32 (0), z2);
58*412f47f9SXin Li #endif
59*412f47f9SXin Li float32x4_t z4 = vmulq_f32 (z2, z2);
60*412f47f9SXin Li return v_estrin_5_f32 (z, z2, z4, d->poly);
61*412f47f9SXin Li }
62*412f47f9SXin Li
63*412f47f9SXin Li /* Fast implementation of AdvSIMD tanf.
64*412f47f9SXin Li Maximum error is 3.45 ULP:
65*412f47f9SXin Li __v_tanf(-0x1.e5f0cap+13) got 0x1.ff9856p-1
66*412f47f9SXin Li want 0x1.ff9850p-1. */
V_NAME_F1(tan)67*412f47f9SXin Li float32x4_t VPCS_ATTR V_NAME_F1 (tan) (float32x4_t x)
68*412f47f9SXin Li {
69*412f47f9SXin Li const struct data *d = ptr_barrier (&data);
70*412f47f9SXin Li float32x4_t special_arg = x;
71*412f47f9SXin Li
72*412f47f9SXin Li /* iax >= RangeVal means x, if not inf or NaN, is too large to perform fast
73*412f47f9SXin Li regression. */
74*412f47f9SXin Li #if WANT_SIMD_EXCEPT
75*412f47f9SXin Li uint32x4_t iax = vreinterpretq_u32_f32 (vabsq_f32 (x));
76*412f47f9SXin Li /* If fp exceptions are to be triggered correctly, also special-case tiny
77*412f47f9SXin Li input, as this will load to overflow later. Fix any special lanes to 1 to
78*412f47f9SXin Li prevent any exceptions being triggered. */
79*412f47f9SXin Li uint32x4_t special = vcgeq_u32 (vsubq_u32 (iax, TinyBound), Thresh);
80*412f47f9SXin Li if (unlikely (v_any_u32 (special)))
81*412f47f9SXin Li x = vbslq_f32 (special, v_f32 (1.0f), x);
82*412f47f9SXin Li #else
83*412f47f9SXin Li /* Otherwise, special-case large and special values. */
84*412f47f9SXin Li uint32x4_t special = vcageq_f32 (x, d->range_val);
85*412f47f9SXin Li #endif
86*412f47f9SXin Li
87*412f47f9SXin Li /* n = rint(x/(pi/2)). */
88*412f47f9SXin Li float32x4_t pi_consts = vld1q_f32 (d->pi_consts);
89*412f47f9SXin Li float32x4_t q = vfmaq_laneq_f32 (d->shift, x, pi_consts, 3);
90*412f47f9SXin Li float32x4_t n = vsubq_f32 (q, d->shift);
91*412f47f9SXin Li /* Determine if x lives in an interval, where |tan(x)| grows to infinity. */
92*412f47f9SXin Li uint32x4_t pred_alt = vtstq_u32 (vreinterpretq_u32_f32 (q), v_u32 (1));
93*412f47f9SXin Li
94*412f47f9SXin Li /* r = x - n * (pi/2) (range reduction into -pi./4 .. pi/4). */
95*412f47f9SXin Li float32x4_t r;
96*412f47f9SXin Li r = vfmaq_laneq_f32 (x, n, pi_consts, 0);
97*412f47f9SXin Li r = vfmaq_laneq_f32 (r, n, pi_consts, 1);
98*412f47f9SXin Li r = vfmaq_laneq_f32 (r, n, pi_consts, 2);
99*412f47f9SXin Li
100*412f47f9SXin Li /* If x lives in an interval, where |tan(x)|
101*412f47f9SXin Li - is finite, then use a polynomial approximation of the form
102*412f47f9SXin Li tan(r) ~ r + r^3 * P(r^2) = r + r * r^2 * P(r^2).
103*412f47f9SXin Li - grows to infinity then use symmetries of tangent and the identity
104*412f47f9SXin Li tan(r) = cotan(pi/2 - r) to express tan(x) as 1/tan(-r). Finally, use
105*412f47f9SXin Li the same polynomial approximation of tan as above. */
106*412f47f9SXin Li
107*412f47f9SXin Li /* Invert sign of r if odd quadrant. */
108*412f47f9SXin Li float32x4_t z = vmulq_f32 (r, vbslq_f32 (pred_alt, v_f32 (-1), v_f32 (1)));
109*412f47f9SXin Li
110*412f47f9SXin Li /* Evaluate polynomial approximation of tangent on [-pi/4, pi/4]. */
111*412f47f9SXin Li float32x4_t z2 = vmulq_f32 (r, r);
112*412f47f9SXin Li float32x4_t p = eval_poly (z2, d);
113*412f47f9SXin Li float32x4_t y = vfmaq_f32 (z, vmulq_f32 (z, z2), p);
114*412f47f9SXin Li
115*412f47f9SXin Li /* Compute reciprocal and apply if required. */
116*412f47f9SXin Li float32x4_t inv_y = vdivq_f32 (v_f32 (1.0f), y);
117*412f47f9SXin Li
118*412f47f9SXin Li if (unlikely (v_any_u32 (special)))
119*412f47f9SXin Li return special_case (special_arg, vbslq_f32 (pred_alt, inv_y, y), special);
120*412f47f9SXin Li return vbslq_f32 (pred_alt, inv_y, y);
121*412f47f9SXin Li }
122*412f47f9SXin Li
123*412f47f9SXin Li PL_SIG (V, F, 1, tan, -3.1, 3.1)
124*412f47f9SXin Li PL_TEST_ULP (V_NAME_F1 (tan), 2.96)
125*412f47f9SXin Li PL_TEST_EXPECT_FENV (V_NAME_F1 (tan), WANT_SIMD_EXCEPT)
126*412f47f9SXin Li PL_TEST_SYM_INTERVAL (V_NAME_F1 (tan), 0, 0x1p-31, 5000)
127*412f47f9SXin Li PL_TEST_SYM_INTERVAL (V_NAME_F1 (tan), 0x1p-31, 0x1p15, 500000)
128*412f47f9SXin Li PL_TEST_SYM_INTERVAL (V_NAME_F1 (tan), 0x1p15, inf, 5000)
129