1/* Single-precision SVE atan2
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_f32.h"
22
23static const struct data
24{
25 float32_t poly[8];
26 float32_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 .poly = { -0x1.55555p-2f, 0x1.99935ep-3f, -0x1.24051ep-3f, 0x1.bd7368p-4f,
31 -0x1.491f0ep-4f, 0x1.93a2c0p-5f, -0x1.4c3c60p-6f, 0x1.01fd88p-8f },
32 .pi_over_2 = 0x1.921fb6p+0f,
33};
34
35/* Special cases i.e. 0, infinity, nan (fall back to scalar calls). */
36static svfloat32_t NOINLINE
37special_case (svfloat32_t y, svfloat32_t x, svfloat32_t ret,
38 const svbool_t cmp)
39{
40 return sv_call2_f32 (f: atan2f, x1: y, x2: x, y: ret, cmp);
41}
42
43/* Returns a predicate indicating true if the input is the bit representation
44 of 0, infinity or nan. */
45static inline svbool_t
46zeroinfnan (svuint32_t i, const svbool_t pg)
47{
48 return svcmpge (pg, svsub_x (pg, svlsl_x (pg, i, 1), 1),
49 sv_u32 (x: 2 * 0x7f800000lu - 1));
50}
51
52/* Fast implementation of SVE atan2f based on atan(x) ~ shift + z + z^3 *
53 P(z^2) with reduction to [0,1] using z=1/x and shift = pi/2. Maximum
54 observed error is 2.95 ULP:
55 _ZGVsMxvv_atan2f (0x1.93836cp+6, 0x1.8cae1p+6) got 0x1.967f06p-1
56 want 0x1.967f00p-1. */
57svfloat32_t SV_NAME_F2 (atan2) (svfloat32_t y, svfloat32_t x, const svbool_t pg)
58{
59 const struct data *data_ptr = ptr_barrier (&data);
60
61 svuint32_t ix = svreinterpret_u32 (x);
62 svuint32_t iy = svreinterpret_u32 (y);
63
64 svbool_t cmp_x = zeroinfnan (i: ix, pg);
65 svbool_t cmp_y = zeroinfnan (i: iy, pg);
66 svbool_t cmp_xy = svorr_z (pg, cmp_x, cmp_y);
67
68 svfloat32_t ax = svabs_x (pg, x);
69 svfloat32_t ay = svabs_x (pg, y);
70 svuint32_t iax = svreinterpret_u32 (ax);
71 svuint32_t iay = svreinterpret_u32 (ay);
72
73 svuint32_t sign_x = sveor_x (pg, ix, iax);
74 svuint32_t sign_y = sveor_x (pg, iy, iay);
75 svuint32_t sign_xy = sveor_x (pg, sign_x, sign_y);
76
77 svbool_t pred_aygtax = svcmpgt (pg, ay, ax);
78
79 /* Set up z for call to atan. */
80 svfloat32_t n = svsel (pred_aygtax, svneg_x (pg, ax), ay);
81 svfloat32_t d = svsel (pred_aygtax, ay, ax);
82 svfloat32_t z = svdiv_x (pg, n, d);
83
84 /* Work out the correct shift. */
85 svfloat32_t shift = svreinterpret_f32 (svlsr_x (pg, sign_x, 1));
86 shift = svsel (pred_aygtax, sv_f32 (x: 1.0), shift);
87 shift = svreinterpret_f32 (svorr_x (pg, sign_x, svreinterpret_u32 (shift)));
88 shift = svmul_x (pg, shift, sv_f32 (x: data_ptr->pi_over_2));
89
90 /* Use pure Estrin scheme for P(z^2) with deg(P)=7. */
91 svfloat32_t z2 = svmul_x (pg, z, z);
92 svfloat32_t z4 = svmul_x (pg, z2, z2);
93 svfloat32_t z8 = svmul_x (pg, z4, z4);
94
95 svfloat32_t ret = sv_estrin_7_f32_x (pg, x: z2, x2: z4, x4: z8, poly: data_ptr->poly);
96
97 /* ret = shift + z + z^3 * P(z^2). */
98 svfloat32_t z3 = svmul_x (pg, z2, z);
99 ret = svmla_x (pg, z, z3, ret);
100
101 ret = svadd_m (pg, ret, shift);
102
103 /* Account for the sign of x and y. */
104
105 if (__glibc_unlikely (svptest_any (pg, cmp_xy)))
106 return special_case (
107 y, x,
108 ret: svreinterpret_f32 (sveor_x (pg, svreinterpret_u32 (ret), sign_xy)),
109 cmp: cmp_xy);
110
111 return svreinterpret_f32 (sveor_x (pg, svreinterpret_u32 (ret), sign_xy));
112}
113

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