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