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