| 1 | /* Single-precision vector (Advanced SIMD) tan function |
| 2 | |
| 3 | Copyright (C) 2023-2024 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 | float pi_consts[4]; |
| 27 | float32x4_t shift; |
| 28 | #if !WANT_SIMD_EXCEPT |
| 29 | float32x4_t range_val; |
| 30 | #endif |
| 31 | } data = { |
| 32 | /* Coefficients generated using FPMinimax. */ |
| 33 | .poly = { V4 (0x1.55555p-2f), V4 (0x1.11166p-3f), V4 (0x1.b88a78p-5f), |
| 34 | V4 (0x1.7b5756p-6f), V4 (0x1.4ef4cep-8f), V4 (0x1.0e1e74p-7f) }, |
| 35 | /* Stores constants: (-pi/2)_high, (-pi/2)_mid, (-pi/2)_low, and 2/pi. */ |
| 36 | .pi_consts |
| 37 | = { -0x1.921fb6p+0f, 0x1.777a5cp-25f, 0x1.ee59dap-50f, 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. |
| 62 | If fp exceptions are to be triggered correctly, |
| 63 | sidestep this by fixing such lanes to 0. */ |
| 64 | uint32x4_t will_uflow |
| 65 | = vcleq_u32 (vreinterpretq_u32_f32 (vabsq_f32 (z)), TinyBound); |
| 66 | if (__glibc_unlikely (v_any_u32 (will_uflow))) |
| 67 | z2 = vbslq_f32 (will_uflow, v_f32 (0), z2); |
| 68 | #endif |
| 69 | float32x4_t z4 = vmulq_f32 (z2, z2); |
| 70 | return v_estrin_5_f32 (z, z2, z4, d->poly); |
| 71 | } |
| 72 | |
| 73 | /* Fast implementation of AdvSIMD tanf. |
| 74 | Maximum error is 3.45 ULP: |
| 75 | __v_tanf(-0x1.e5f0cap+13) got 0x1.ff9856p-1 |
| 76 | want 0x1.ff9850p-1. */ |
| 77 | float32x4_t VPCS_ATTR NOINLINE V_NAME_F1 (tan) (float32x4_t x) |
| 78 | { |
| 79 | const struct data *d = ptr_barrier (&data); |
| 80 | float32x4_t special_arg = x; |
| 81 | |
| 82 | /* iax >= RangeVal means x, if not inf or NaN, is too large to perform fast |
| 83 | regression. */ |
| 84 | #if WANT_SIMD_EXCEPT |
| 85 | uint32x4_t iax = vreinterpretq_u32_f32 (vabsq_f32 (x)); |
| 86 | /* If fp exceptions are to be triggered correctly, also special-case tiny |
| 87 | input, as this will load to overflow later. Fix any special lanes to 1 to |
| 88 | prevent any exceptions being triggered. */ |
| 89 | uint32x4_t special = vcgeq_u32 (vsubq_u32 (iax, TinyBound), Thresh); |
| 90 | if (__glibc_unlikely (v_any_u32 (special))) |
| 91 | x = vbslq_f32 (special, v_f32 (1.0f), x); |
| 92 | #else |
| 93 | /* Otherwise, special-case large and special values. */ |
| 94 | uint32x4_t special = vcageq_f32 (x, d->range_val); |
| 95 | #endif |
| 96 | |
| 97 | /* n = rint(x/(pi/2)). */ |
| 98 | float32x4_t pi_consts = vld1q_f32 (d->pi_consts); |
| 99 | float32x4_t q = vfmaq_laneq_f32 (d->shift, x, pi_consts, 3); |
| 100 | float32x4_t n = vsubq_f32 (q, d->shift); |
| 101 | /* Determine if x lives in an interval, where |tan(x)| grows to infinity. */ |
| 102 | uint32x4_t pred_alt = vtstq_u32 (vreinterpretq_u32_f32 (q), v_u32 (1)); |
| 103 | |
| 104 | /* r = x - n * (pi/2) (range reduction into -pi./4 .. pi/4). */ |
| 105 | float32x4_t r; |
| 106 | r = vfmaq_laneq_f32 (x, n, pi_consts, 0); |
| 107 | r = vfmaq_laneq_f32 (r, n, pi_consts, 1); |
| 108 | r = vfmaq_laneq_f32 (r, n, pi_consts, 2); |
| 109 | |
| 110 | /* If x lives in an interval, where |tan(x)| |
| 111 | - is finite, then use a polynomial approximation of the form |
| 112 | tan(r) ~ r + r^3 * P(r^2) = r + r * r^2 * P(r^2). |
| 113 | - grows to infinity then use symmetries of tangent and the identity |
| 114 | tan(r) = cotan(pi/2 - r) to express tan(x) as 1/tan(-r). Finally, use |
| 115 | the same polynomial approximation of tan as above. */ |
| 116 | |
| 117 | /* Invert sign of r if odd quadrant. */ |
| 118 | float32x4_t z = vmulq_f32 (r, vbslq_f32 (pred_alt, v_f32 (-1), v_f32 (1))); |
| 119 | |
| 120 | /* Evaluate polynomial approximation of tangent on [-pi/4, pi/4]. */ |
| 121 | float32x4_t z2 = vmulq_f32 (r, r); |
| 122 | float32x4_t p = eval_poly (z2, d); |
| 123 | float32x4_t y = vfmaq_f32 (z, vmulq_f32 (z, z2), p); |
| 124 | |
| 125 | /* Compute reciprocal and apply if required. */ |
| 126 | float32x4_t inv_y = vdivq_f32 (v_f32 (1.0f), y); |
| 127 | |
| 128 | if (__glibc_unlikely (v_any_u32 (special))) |
| 129 | return special_case (special_arg, vbslq_f32 (pred_alt, inv_y, y), special); |
| 130 | return vbslq_f32 (pred_alt, inv_y, y); |
| 131 | } |
| 132 | libmvec_hidden_def (V_NAME_F1 (tan)) |
| 133 | HALF_WIDTH_ALIAS_F1 (tan) |
| 134 | |