| 1 | /* Double-precision AdvSIMD inverse tan |
| 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_f64.h" |
| 22 | |
| 23 | static const struct data |
| 24 | { |
| 25 | float64x2_t c0, c2, c4, c6, c8, c10, c12, c14, c16, c18; |
| 26 | float64x2_t pi_over_2; |
| 27 | double c1, c3, c5, c7, c9, c11, c13, c15, c17, c19; |
| 28 | } data = { |
| 29 | /* Coefficients of polynomial P such that atan(x)~x+x*P(x^2) on |
| 30 | [2**-1022, 1.0]. */ |
| 31 | .c0 = V2 (-0x1.5555555555555p-2), .c1 = 0x1.99999999996c1p-3, |
| 32 | .c2 = V2 (-0x1.2492492478f88p-3), .c3 = 0x1.c71c71bc3951cp-4, |
| 33 | .c4 = V2 (-0x1.745d160a7e368p-4), .c5 = 0x1.3b139b6a88ba1p-4, |
| 34 | .c6 = V2 (-0x1.11100ee084227p-4), .c7 = 0x1.e1d0f9696f63bp-5, |
| 35 | .c8 = V2 (-0x1.aebfe7b418581p-5), .c9 = 0x1.842dbe9b0d916p-5, |
| 36 | .c10 = V2 (-0x1.5d30140ae5e99p-5), .c11 = 0x1.338e31eb2fbbcp-5, |
| 37 | .c12 = V2 (-0x1.00e6eece7de8p-5), .c13 = 0x1.860897b29e5efp-6, |
| 38 | .c14 = V2 (-0x1.0051381722a59p-6), .c15 = 0x1.14e9dc19a4a4ep-7, |
| 39 | .c16 = V2 (-0x1.d0062b42fe3bfp-9), .c17 = 0x1.17739e210171ap-10, |
| 40 | .c18 = V2 (-0x1.ab24da7be7402p-13), .c19 = 0x1.358851160a528p-16, |
| 41 | .pi_over_2 = V2 (0x1.921fb54442d18p+0), |
| 42 | }; |
| 43 | |
| 44 | #define SignMask v_u64 (0x8000000000000000) |
| 45 | #define TinyBound 0x3e10000000000000 /* asuint64(0x1p-30). */ |
| 46 | #define BigBound 0x4340000000000000 /* asuint64(0x1p53). */ |
| 47 | |
| 48 | /* Fast implementation of vector atan. |
| 49 | Based on atan(x) ~ shift + z + z^3 * P(z^2) with reduction to [0,1] using |
| 50 | z=1/x and shift = pi/2. Maximum observed error is 2.27 ulps: |
| 51 | _ZGVnN2v_atan (0x1.0005af27c23e9p+0) got 0x1.9225645bdd7c1p-1 |
| 52 | want 0x1.9225645bdd7c3p-1. */ |
| 53 | float64x2_t VPCS_ATTR V_NAME_D1 (atan) (float64x2_t x) |
| 54 | { |
| 55 | const struct data *d = ptr_barrier (&data); |
| 56 | float64x2_t c13 = vld1q_f64 (&d->c1); |
| 57 | float64x2_t c57 = vld1q_f64 (&d->c5); |
| 58 | float64x2_t c911 = vld1q_f64 (&d->c9); |
| 59 | float64x2_t c1315 = vld1q_f64 (&d->c13); |
| 60 | float64x2_t c1719 = vld1q_f64 (&d->c17); |
| 61 | |
| 62 | /* Small cases, infs and nans are supported by our approximation technique, |
| 63 | but do not set fenv flags correctly. Only trigger special case if we need |
| 64 | fenv. */ |
| 65 | uint64x2_t ix = vreinterpretq_u64_f64 (x); |
| 66 | uint64x2_t sign = vandq_u64 (ix, SignMask); |
| 67 | |
| 68 | #if WANT_SIMD_EXCEPT |
| 69 | uint64x2_t ia12 = vandq_u64 (ix, v_u64 (0x7ff0000000000000)); |
| 70 | uint64x2_t special = vcgtq_u64 (vsubq_u64 (ia12, v_u64 (TinyBound)), |
| 71 | v_u64 (BigBound - TinyBound)); |
| 72 | /* If any lane is special, fall back to the scalar routine for all lanes. */ |
| 73 | if (__glibc_unlikely (v_any_u64 (special))) |
| 74 | return v_call_f64 (atan, x, v_f64 (0), v_u64 (-1)); |
| 75 | #endif |
| 76 | |
| 77 | /* Argument reduction: |
| 78 | y := arctan(x) for x < 1 |
| 79 | y := pi/2 + arctan(-1/x) for x > 1 |
| 80 | Hence, use z=-1/a if x>=1, otherwise z=a. */ |
| 81 | uint64x2_t red = vcagtq_f64 (x, v_f64 (1.0)); |
| 82 | /* Avoid dependency in abs(x) in division (and comparison). */ |
| 83 | float64x2_t z = vbslq_f64 (red, vdivq_f64 (v_f64 (1.0), x), x); |
| 84 | float64x2_t shift = vreinterpretq_f64_u64 ( |
| 85 | vandq_u64 (red, vreinterpretq_u64_f64 (d->pi_over_2))); |
| 86 | /* Use absolute value only when needed (odd powers of z). */ |
| 87 | float64x2_t az = vbslq_f64 ( |
| 88 | SignMask, vreinterpretq_f64_u64 (vandq_u64 (SignMask, red)), z); |
| 89 | |
| 90 | /* Calculate the polynomial approximation. |
| 91 | Use split Estrin scheme for P(z^2) with deg(P)=19. Use split instead of |
| 92 | full scheme to avoid underflow in x^16. |
| 93 | The order 19 polynomial P approximates |
| 94 | (atan(sqrt(x))-sqrt(x))/x^(3/2). */ |
| 95 | float64x2_t z2 = vmulq_f64 (z, z); |
| 96 | float64x2_t x2 = vmulq_f64 (z2, z2); |
| 97 | float64x2_t x4 = vmulq_f64 (x2, x2); |
| 98 | float64x2_t x8 = vmulq_f64 (x4, x4); |
| 99 | |
| 100 | /* estrin_7. */ |
| 101 | float64x2_t p01 = vfmaq_laneq_f64 (d->c0, z2, c13, 0); |
| 102 | float64x2_t p23 = vfmaq_laneq_f64 (d->c2, z2, c13, 1); |
| 103 | float64x2_t p03 = vfmaq_f64 (p01, x2, p23); |
| 104 | |
| 105 | float64x2_t p45 = vfmaq_laneq_f64 (d->c4, z2, c57, 0); |
| 106 | float64x2_t p67 = vfmaq_laneq_f64 (d->c6, z2, c57, 1); |
| 107 | float64x2_t p47 = vfmaq_f64 (p45, x2, p67); |
| 108 | |
| 109 | float64x2_t p07 = vfmaq_f64 (p03, x4, p47); |
| 110 | |
| 111 | /* estrin_11. */ |
| 112 | float64x2_t p89 = vfmaq_laneq_f64 (d->c8, z2, c911, 0); |
| 113 | float64x2_t p1011 = vfmaq_laneq_f64 (d->c10, z2, c911, 1); |
| 114 | float64x2_t p811 = vfmaq_f64 (p89, x2, p1011); |
| 115 | |
| 116 | float64x2_t p1213 = vfmaq_laneq_f64 (d->c12, z2, c1315, 0); |
| 117 | float64x2_t p1415 = vfmaq_laneq_f64 (d->c14, z2, c1315, 1); |
| 118 | float64x2_t p1215 = vfmaq_f64 (p1213, x2, p1415); |
| 119 | |
| 120 | float64x2_t p1617 = vfmaq_laneq_f64 (d->c16, z2, c1719, 0); |
| 121 | float64x2_t p1819 = vfmaq_laneq_f64 (d->c18, z2, c1719, 1); |
| 122 | float64x2_t p1619 = vfmaq_f64 (p1617, x2, p1819); |
| 123 | |
| 124 | float64x2_t p815 = vfmaq_f64 (p811, x4, p1215); |
| 125 | float64x2_t p819 = vfmaq_f64 (p815, x8, p1619); |
| 126 | |
| 127 | float64x2_t y = vfmaq_f64 (p07, p819, x8); |
| 128 | |
| 129 | /* Finalize. y = shift + z + z^3 * P(z^2). */ |
| 130 | y = vfmaq_f64 (az, y, vmulq_f64 (z2, az)); |
| 131 | y = vaddq_f64 (y, shift); |
| 132 | |
| 133 | /* y = atan(x) if x>0, -atan(-x) otherwise. */ |
| 134 | y = vreinterpretq_f64_u64 (veorq_u64 (vreinterpretq_u64_f64 (y), sign)); |
| 135 | return y; |
| 136 | } |
| 137 | |