1 | /* Single-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_f32.h" |
22 | |
23 | static const struct data |
24 | { |
25 | float32x4_t poly[8]; |
26 | float32x4_t pi_over_2; |
27 | } data = { |
28 | /* Coefficients of polynomial P such that atan(x)~x+x*P(x^2) on |
29 | [2**-128, 1.0]. |
30 | Generated using fpminimax between FLT_MIN and 1. */ |
31 | .poly = { V4 (-0x1.55555p-2f), V4 (0x1.99935ep-3f), V4 (-0x1.24051ep-3f), |
32 | V4 (0x1.bd7368p-4f), V4 (-0x1.491f0ep-4f), V4 (0x1.93a2c0p-5f), |
33 | V4 (-0x1.4c3c60p-6f), V4 (0x1.01fd88p-8f) }, |
34 | .pi_over_2 = V4 (0x1.921fb6p+0f), |
35 | }; |
36 | |
37 | #define SignMask v_u32 (0x80000000) |
38 | |
39 | #define P(i) d->poly[i] |
40 | |
41 | #define TinyBound 0x30800000 /* asuint(0x1p-30). */ |
42 | #define BigBound 0x4e800000 /* asuint(0x1p30). */ |
43 | |
44 | #if WANT_SIMD_EXCEPT |
45 | static float32x4_t VPCS_ATTR NOINLINE |
46 | special_case (float32x4_t x, float32x4_t y, uint32x4_t special) |
47 | { |
48 | return v_call_f32 (atanf, x, y, special); |
49 | } |
50 | #endif |
51 | |
52 | /* Fast implementation of vector atanf based on |
53 | atan(x) ~ shift + z + z^3 * P(z^2) with reduction to [0,1] |
54 | using z=-1/x and shift = pi/2. Maximum observed error is 2.9ulps: |
55 | _ZGVnN4v_atanf (0x1.0468f6p+0) got 0x1.967f06p-1 want 0x1.967fp-1. */ |
56 | float32x4_t VPCS_ATTR NOINLINE V_NAME_F1 (atan) (float32x4_t x) |
57 | { |
58 | const struct data *d = ptr_barrier (&data); |
59 | |
60 | /* Small cases, infs and nans are supported by our approximation technique, |
61 | but do not set fenv flags correctly. Only trigger special case if we need |
62 | fenv. */ |
63 | uint32x4_t ix = vreinterpretq_u32_f32 (x); |
64 | uint32x4_t sign = vandq_u32 (ix, SignMask); |
65 | |
66 | #if WANT_SIMD_EXCEPT |
67 | uint32x4_t ia = vandq_u32 (ix, v_u32 (0x7ff00000)); |
68 | uint32x4_t special = vcgtq_u32 (vsubq_u32 (ia, v_u32 (TinyBound)), |
69 | v_u32 (BigBound - TinyBound)); |
70 | /* If any lane is special, fall back to the scalar routine for all lanes. */ |
71 | if (__glibc_unlikely (v_any_u32 (special))) |
72 | return special_case (x, x, v_u32 (-1)); |
73 | #endif |
74 | |
75 | /* Argument reduction: |
76 | y := arctan(x) for x < 1 |
77 | y := pi/2 + arctan(-1/x) for x > 1 |
78 | Hence, use z=-1/a if x>=1, otherwise z=a. */ |
79 | uint32x4_t red = vcagtq_f32 (x, v_f32 (1.0)); |
80 | /* Avoid dependency in abs(x) in division (and comparison). */ |
81 | float32x4_t z = vbslq_f32 (red, vdivq_f32 (v_f32 (1.0f), x), x); |
82 | float32x4_t shift = vreinterpretq_f32_u32 ( |
83 | vandq_u32 (red, vreinterpretq_u32_f32 (d->pi_over_2))); |
84 | /* Use absolute value only when needed (odd powers of z). */ |
85 | float32x4_t az = vbslq_f32 ( |
86 | SignMask, vreinterpretq_f32_u32 (vandq_u32 (SignMask, red)), z); |
87 | |
88 | /* Calculate the polynomial approximation. |
89 | Use 2-level Estrin scheme for P(z^2) with deg(P)=7. However, |
90 | a standard implementation using z8 creates spurious underflow |
91 | in the very last fma (when z^8 is small enough). |
92 | Therefore, we split the last fma into a mul and an fma. |
93 | Horner and single-level Estrin have higher errors that exceed |
94 | threshold. */ |
95 | float32x4_t z2 = vmulq_f32 (z, z); |
96 | float32x4_t z4 = vmulq_f32 (z2, z2); |
97 | |
98 | float32x4_t y = vfmaq_f32 ( |
99 | v_pairwise_poly_3_f32 (z2, z4, d->poly), z4, |
100 | vmulq_f32 (z4, v_pairwise_poly_3_f32 (z2, z4, d->poly + 4))); |
101 | |
102 | /* y = shift + z * P(z^2). */ |
103 | y = vaddq_f32 (vfmaq_f32 (az, y, vmulq_f32 (z2, az)), shift); |
104 | |
105 | /* y = atan(x) if x>0, -atan(-x) otherwise. */ |
106 | y = vreinterpretq_f32_u32 (veorq_u32 (vreinterpretq_u32_f32 (y), sign)); |
107 | |
108 | return y; |
109 | } |
110 | libmvec_hidden_def (V_NAME_F1 (atan)) |
111 | HALF_WIDTH_ALIAS_F1 (atan) |
112 | |