1/* Double-precision SVE 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 "sv_math.h"
21#include "poly_sve_f64.h"
22
23static const struct data
24{
25 float64_t poly[20];
26 float64_t pi_over_2;
27} data = {
28 /* Coefficients of polynomial P such that atan(x)~x+x*P(x^2) on
29 [2**-1022, 1.0]. */
30 .poly = { -0x1.5555555555555p-2, 0x1.99999999996c1p-3, -0x1.2492492478f88p-3,
31 0x1.c71c71bc3951cp-4, -0x1.745d160a7e368p-4, 0x1.3b139b6a88ba1p-4,
32 -0x1.11100ee084227p-4, 0x1.e1d0f9696f63bp-5, -0x1.aebfe7b418581p-5,
33 0x1.842dbe9b0d916p-5, -0x1.5d30140ae5e99p-5, 0x1.338e31eb2fbbcp-5,
34 -0x1.00e6eece7de8p-5, 0x1.860897b29e5efp-6, -0x1.0051381722a59p-6,
35 0x1.14e9dc19a4a4ep-7, -0x1.d0062b42fe3bfp-9, 0x1.17739e210171ap-10,
36 -0x1.ab24da7be7402p-13, 0x1.358851160a528p-16, },
37 .pi_over_2 = 0x1.921fb54442d18p+0,
38};
39
40/* Useful constants. */
41#define SignMask (0x8000000000000000)
42
43/* Fast implementation of SVE atan.
44 Based on atan(x) ~ shift + z + z^3 * P(z^2) with reduction to [0,1] using
45 z=1/x and shift = pi/2. Largest errors are close to 1. The maximum observed
46 error is 2.27 ulps:
47 _ZGVsMxv_atan (0x1.0005af27c23e9p+0) got 0x1.9225645bdd7c1p-1
48 want 0x1.9225645bdd7c3p-1. */
49svfloat64_t SV_NAME_D1 (atan) (svfloat64_t x, const svbool_t pg)
50{
51 const struct data *d = ptr_barrier (&data);
52
53 /* No need to trigger special case. Small cases, infs and nans
54 are supported by our approximation technique. */
55 svuint64_t ix = svreinterpret_u64 (x);
56 svuint64_t sign = svand_x (pg, ix, SignMask);
57
58 /* Argument reduction:
59 y := arctan(x) for x < 1
60 y := pi/2 + arctan(-1/x) for x > 1
61 Hence, use z=-1/a if x>=1, otherwise z=a. */
62 svbool_t red = svacgt (pg, x, 1.0);
63 /* Avoid dependency in abs(x) in division (and comparison). */
64 svfloat64_t z = svsel (red, svdivr_x (pg, x, 1.0), x);
65 /* Use absolute value only when needed (odd powers of z). */
66 svfloat64_t az = svabs_x (pg, z);
67 az = svneg_m (az, red, az);
68
69 /* Use split Estrin scheme for P(z^2) with deg(P)=19. */
70 svfloat64_t z2 = svmul_x (pg, z, z);
71 svfloat64_t x2 = svmul_x (pg, z2, z2);
72 svfloat64_t x4 = svmul_x (pg, x2, x2);
73 svfloat64_t x8 = svmul_x (pg, x4, x4);
74
75 svfloat64_t y
76 = svmla_x (pg, sv_estrin_7_f64_x (pg, x: z2, x2, x4, poly: d->poly),
77 sv_estrin_11_f64_x (pg, x: z2, x2, x4, x8, poly: d->poly + 8), x8);
78
79 /* y = shift + z + z^3 * P(z^2). */
80 svfloat64_t z3 = svmul_x (pg, z2, az);
81 y = svmla_x (pg, az, z3, y);
82
83 /* Apply shift as indicated by `red` predicate. */
84 y = svadd_m (red, y, d->pi_over_2);
85
86 /* y = atan(x) if x>0, -atan(-x) otherwise. */
87 y = svreinterpret_f64 (sveor_x (pg, svreinterpret_u64 (y), sign));
88
89 return y;
90}
91

source code of glibc/sysdeps/aarch64/fpu/atan_sve.c