From: Dylan Fleming Date: Mon, 19 May 2025 13:58:30 +0000 (+0000) Subject: AArch64: Implement AdvSIMD and SVE atan2pi/f X-Git-Tag: glibc-2.42~233 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=96abd59bf2a11ddd4e7ccaac840ec13c0b62d3ba;p=thirdparty%2Fglibc.git AArch64: Implement AdvSIMD and SVE atan2pi/f Implement double and single precision variants of the C23 routine atan2pi for both AdvSIMD and SVE. Reviewed-by: Wilco Dijkstra --- diff --git a/bits/libm-simd-decl-stubs.h b/bits/libm-simd-decl-stubs.h index f1add553a0..25a5f48ed5 100644 --- a/bits/libm-simd-decl-stubs.h +++ b/bits/libm-simd-decl-stubs.h @@ -406,4 +406,15 @@ #define __DECL_SIMD_atanpif32x #define __DECL_SIMD_atanpif64x #define __DECL_SIMD_atanpif128x + +#define __DECL_SIMD_atan2pi +#define __DECL_SIMD_atan2pif +#define __DECL_SIMD_atan2pil +#define __DECL_SIMD_atan2pif16 +#define __DECL_SIMD_atan2pif32 +#define __DECL_SIMD_atan2pif64 +#define __DECL_SIMD_atan2pif128 +#define __DECL_SIMD_atan2pif32x +#define __DECL_SIMD_atan2pif64x +#define __DECL_SIMD_atan2pif128x #endif diff --git a/math/bits/mathcalls.h b/math/bits/mathcalls.h index 1a06a8cad6..3b9032e69c 100644 --- a/math/bits/mathcalls.h +++ b/math/bits/mathcalls.h @@ -77,6 +77,7 @@ __MATHCALL (atanpi,, (_Mdouble_ __x)); __MATHCALL_VEC (atanpi,, (_Mdouble_ __x)); /* Arc tangent of Y/X, divided by pi. */ __MATHCALL (atan2pi,, (_Mdouble_ __y, _Mdouble_ __x)); +__MATHCALL_VEC (atan2pi,, (_Mdouble_ __y, _Mdouble_ __x)); /* Cosine of pi * X. */ __MATHCALL_VEC (cospi,, (_Mdouble_ __x)); diff --git a/sysdeps/aarch64/fpu/Makefile b/sysdeps/aarch64/fpu/Makefile index d2a52ae34f..7b0ec3e9a9 100644 --- a/sysdeps/aarch64/fpu/Makefile +++ b/sysdeps/aarch64/fpu/Makefile @@ -8,6 +8,7 @@ libmvec-supported-funcs = acos \ atanh \ atanpi \ atan2 \ + atan2pi \ cbrt \ cos \ cosh \ diff --git a/sysdeps/aarch64/fpu/Versions b/sysdeps/aarch64/fpu/Versions index 2d88a0a21b..2980cb79a4 100644 --- a/sysdeps/aarch64/fpu/Versions +++ b/sysdeps/aarch64/fpu/Versions @@ -173,5 +173,10 @@ libmvec { _ZGVnN4v_atanpif; _ZGVsMxv_atanpi; _ZGVsMxv_atanpif; + _ZGVnN2vv_atan2pi; + _ZGVnN2vv_atan2pif; + _ZGVnN4vv_atan2pif; + _ZGVsMxvv_atan2pi; + _ZGVsMxvv_atan2pif; } } diff --git a/sysdeps/aarch64/fpu/advsimd_f32_protos.h b/sysdeps/aarch64/fpu/advsimd_f32_protos.h index 26c250b2be..c202bda768 100644 --- a/sysdeps/aarch64/fpu/advsimd_f32_protos.h +++ b/sysdeps/aarch64/fpu/advsimd_f32_protos.h @@ -50,3 +50,4 @@ libmvec_hidden_proto (V_NAME_F1(tan)); libmvec_hidden_proto (V_NAME_F1(tanh)); libmvec_hidden_proto (V_NAME_F1(tanpi)); libmvec_hidden_proto (V_NAME_F2(atan2)); +libmvec_hidden_proto (V_NAME_F2(atan2pi)); diff --git a/sysdeps/aarch64/fpu/atan2pi_advsimd.c b/sysdeps/aarch64/fpu/atan2pi_advsimd.c new file mode 100644 index 0000000000..3cf231b355 --- /dev/null +++ b/sysdeps/aarch64/fpu/atan2pi_advsimd.c @@ -0,0 +1,175 @@ +/* Double-Precision vector (Advanced SIMD) inverse tan2pi function + + Copyright (C) 2025 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include "v_math.h" + +static const struct data +{ + float64_t c2, c4, c6, c8, c10, c12, c14, c16, c18, c20; + float64x2_t c0; + uint64x2_t zeroinfnan; + float64x2_t c1, c3, c5, c7, c9, c11, c13, c15, c17, c19; +} data = { + /* Coefficients of polynomial P such that atan(x)~x+x*P(x^2) on + [2**-1022, 1.0]. */ + .c0 = V2 (0x1.45f306dc9c883p-2), + .c1 = V2 (-0x1.b2995e7b7ba4ap-4), + .c2 = 0x1.04c26be3d2c1p-4, + .c3 = V2 (-0x1.7483759c17ea1p-5), + .c4 = 0x1.21bb95c315d57p-5, + .c5 = V2 (-0x1.da1bdc3d453f3p-6), + .c6 = 0x1.912d20459b4bfp-6, + .c7 = V2 (-0x1.5bbd4545cad1fp-6), + .c8 = 0x1.331b83bec30a1p-6, + .c9 = V2 (-0x1.13d6457f44de3p-6), + .c10 = 0x1.f8e802974db94p-7, + .c11 = V2 (-0x1.d7e173ab04a1ap-7), + .c12 = 0x1.bdfa47d6a4f28p-7, + .c13 = V2 (-0x1.9ba78f3232ceep-7), + .c14 = 0x1.5e6044590ab4fp-7, + .c15 = V2 (-0x1.01ccfdeb9f77fp-7), + .c16 = 0x1.345cf0d4eb1c1p-8, + .c17 = V2 (-0x1.19e5f00f67e3ap-9), + .c18 = 0x1.6d3035ac7625bp-11, + .c19 = V2 (-0x1.286bb9ae4ed79p-13), + .c20 = 0x1.c37ec36da0e1ap-17, + .zeroinfnan = V2 (2 * 0x7ff0000000000000ul - 1), +}; + +#define SignMask v_u64 (0x8000000000000000) +#define OneOverPi v_f64 (0x1.45f306dc9c883p-2) + +/* Special cases i.e. 0, infinity, NaN (fall back to scalar calls). */ +static float64x2_t VPCS_ATTR NOINLINE +special_case (float64x2_t y, float64x2_t x, float64x2_t ret, + uint64x2_t sign_xy, uint64x2_t cmp) +{ + /* Account for the sign of x and y. */ + ret = vreinterpretq_f64_u64 ( + veorq_u64 (vreinterpretq_u64_f64 (ret), sign_xy)); + + /* Since we have no scalar fallback for atan2pi, + we can instead make a call to atan2f and divide by pi. */ + ret = v_call2_f64 (atan2, y, x, ret, cmp); + + /* Only divide the special cases by pi, and leave the rest unchanged. */ + return vbslq_f64 (cmp, vmulq_f64 (ret, OneOverPi), ret); +} + +/* Returns 1 if input is the bit representation of 0, infinity or nan. */ +static inline uint64x2_t +zeroinfnan (uint64x2_t i, const struct data *d) +{ + /* (2 * i - 1) >= (2 * asuint64 (INFINITY) - 1). */ + return vcgeq_u64 (vsubq_u64 (vaddq_u64 (i, i), v_u64 (1)), d->zeroinfnan); +} + +/* Fast implementation of vector atan2pi. + Maximum observed error is 3.04 ulps: + _ZGVnN2vv_atan2pi (0x1.1e0733532ce28p+5, 0x1.2d803379cca1fp+5) + got 0x1.eed60c1e89317p-3 want 0x1.eed60c1e89314p-3. */ +float64x2_t VPCS_ATTR V_NAME_D2 (atan2pi) (float64x2_t y, float64x2_t x) +{ + const struct data *d = ptr_barrier (&data); + + uint64x2_t ix = vreinterpretq_u64_f64 (x); + uint64x2_t iy = vreinterpretq_u64_f64 (y); + + uint64x2_t special_cases + = vorrq_u64 (zeroinfnan (ix, d), zeroinfnan (iy, d)); + + uint64x2_t sign_x = vandq_u64 (ix, SignMask); + uint64x2_t sign_y = vandq_u64 (iy, SignMask); + uint64x2_t sign_xy = veorq_u64 (sign_x, sign_y); + + float64x2_t ax = vabsq_f64 (x); + float64x2_t ay = vabsq_f64 (y); + + uint64x2_t pred_xlt0 = vcltzq_f64 (x); + uint64x2_t pred_aygtax = vcgtq_f64 (ay, ax); + + /* Set up z for evaluation of atanpi. */ + float64x2_t num = vbslq_f64 (pred_aygtax, vnegq_f64 (ax), ay); + float64x2_t den = vbslq_f64 (pred_aygtax, ay, ax); + float64x2_t z = vdivq_f64 (num, den); + + /* Work out the correct shift for atan2pi: + -1.0 when x < 0 and ax < ay + -0.5 when x < 0 and ax > ay + 0 when x >= 0 and ax < ay + 0.5 when x >= 0 and ax > ay. */ + float64x2_t shift = vreinterpretq_f64_u64 ( + vandq_u64 (pred_xlt0, vreinterpretq_u64_f64 (v_f64 (-1.0)))); + float64x2_t shift2 = vreinterpretq_f64_u64 ( + vandq_u64 (pred_aygtax, vreinterpretq_u64_f64 (v_f64 (0.5)))); + shift = vaddq_f64 (shift, shift2); + + /* Calculate the polynomial approximation. */ + float64x2_t z2 = vmulq_f64 (z, z); + float64x2_t z3 = vmulq_f64 (z2, z); + float64x2_t z4 = vmulq_f64 (z2, z2); + float64x2_t z8 = vmulq_f64 (z4, z4); + float64x2_t z16 = vmulq_f64 (z8, z8); + + float64x2_t c24 = vld1q_f64 (&d->c2); + float64x2_t c68 = vld1q_f64 (&d->c6); + + /* Order-7 Estrin. */ + float64x2_t p12 = vfmaq_laneq_f64 (d->c1, z2, c24, 0); + float64x2_t p34 = vfmaq_laneq_f64 (d->c3, z2, c24, 1); + float64x2_t p56 = vfmaq_laneq_f64 (d->c5, z2, c68, 0); + float64x2_t p78 = vfmaq_laneq_f64 (d->c7, z2, c68, 1); + + float64x2_t p14 = vfmaq_f64 (p12, z4, p34); + float64x2_t p58 = vfmaq_f64 (p56, z4, p78); + float64x2_t p18 = vfmaq_f64 (p14, z8, p58); + + /* Order-11 Estrin. */ + float64x2_t c1012 = vld1q_f64 (&d->c10); + float64x2_t c1416 = vld1q_f64 (&d->c14); + float64x2_t c1820 = vld1q_f64 (&d->c18); + + float64x2_t p910 = vfmaq_laneq_f64 (d->c9, z2, c1012, 0); + float64x2_t p1112 = vfmaq_laneq_f64 (d->c11, z2, c1012, 1); + float64x2_t p912 = vfmaq_f64 (p910, z4, p1112); + + float64x2_t p1314 = vfmaq_laneq_f64 (d->c13, z2, c1416, 0); + float64x2_t p1516 = vfmaq_laneq_f64 (d->c15, z2, c1416, 1); + float64x2_t p1316 = vfmaq_f64 (p1314, z4, p1516); + + float64x2_t p1718 = vfmaq_laneq_f64 (d->c17, z2, c1820, 0); + float64x2_t p1920 = vfmaq_laneq_f64 (d->c19, z2, c1820, 1); + float64x2_t p1720 = vfmaq_f64 (p1718, z4, p1920); + + float64x2_t p916 = vfmaq_f64 (p912, z8, p1316); + float64x2_t p920 = vfmaq_f64 (p916, z16, p1720); + + float64x2_t poly = vfmaq_f64 (p18, z16, p920); + + /* y = shift + z * P(z^2). */ + float64x2_t ret = vfmaq_f64 (shift, z, d->c0); + ret = vfmaq_f64 (ret, z3, poly); + + if (__glibc_unlikely (v_any_u64 (special_cases))) + return special_case (y, x, ret, sign_xy, special_cases); + + /* Account for the sign of x and y. */ + return vreinterpretq_f64_u64 ( + veorq_u64 (vreinterpretq_u64_f64 (ret), sign_xy)); +} diff --git a/sysdeps/aarch64/fpu/atan2pi_sve.c b/sysdeps/aarch64/fpu/atan2pi_sve.c new file mode 100644 index 0000000000..f1d1f1c35c --- /dev/null +++ b/sysdeps/aarch64/fpu/atan2pi_sve.c @@ -0,0 +1,159 @@ +/* Double-Precision vector (SVE) inverse tan2pi function + + Copyright (C) 2025 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include "math_config.h" +#include "sv_math.h" + +static const struct data +{ + float64_t c2, c4, c6, c8, c10, c12, c14, c16, c18, c20; + float64_t c0, c1, c3, c5, c7, c9, c11, c13, c15, c17, c19; + float64_t shift_val; +} data = { + /* Coefficients of polnomial P such that atan(x)~x+x*P(x^2) on + [2^-1022, 1.0]. */ + .c0 = 0x1.45f306dc9c883p-2, .c1 = -0x1.b2995e7b7ba4ap-4, + .c2 = 0x1.04c26be3d2c1p-4, .c3 = -0x1.7483759c17ea1p-5, + .c4 = 0x1.21bb95c315d57p-5, .c5 = -0x1.da1bdc3d453f3p-6, + .c6 = 0x1.912d20459b4bfp-6, .c7 = -0x1.5bbd4545cad1fp-6, + .c8 = 0x1.331b83bec30a1p-6, .c9 = -0x1.13d6457f44de3p-6, + .c10 = 0x1.f8e802974db94p-7, .c11 = -0x1.d7e173ab04a1ap-7, + .c12 = 0x1.bdfa47d6a4f28p-7, .c13 = -0x1.9ba78f3232ceep-7, + .c14 = 0x1.5e6044590ab4fp-7, .c15 = -0x1.01ccfdeb9f77fp-7, + .c16 = 0x1.345cf0d4eb1c1p-8, .c17 = -0x1.19e5f00f67e3ap-9, + .c18 = 0x1.6d3035ac7625bp-11, .c19 = -0x1.286bb9ae4ed79p-13, + .c20 = 0x1.c37ec36da0e1ap-17, .shift_val = 0.5, +}; + +#define OneOverPi sv_f64 (0x1.45f306dc9c883p-2) + +/* Special cases i.e. 0, infinity, nan (fall back to scalar calls). */ +static svfloat64_t NOINLINE +special_case (svfloat64_t y, svfloat64_t x, svfloat64_t ret, + const svbool_t cmp) +{ + ret = sv_call2_f64 (atan2, y, x, ret, cmp); + return svmul_f64_m (cmp, ret, OneOverPi); +} + +/* Returns a predicate indicating true if the input is the bit representation + of 0, infinity or nan. */ +static inline svbool_t +zeroinfnan (svuint64_t i, const svbool_t pg) +{ + return svcmpge (pg, svsub_x (pg, svlsl_x (pg, i, 1), 1), + sv_u64 (2 * asuint64 (INFINITY) - 1)); +} + +/* Fast implementation of SVE atan2pi. + Maximum observed error is 3.11 ulps: + _ZGVsMxvv_atan2pi (0x1.ef284a877f6b5p+6, 0x1.03fdde8242b17p+7) + got 0x1.f00f800163079p-3 want 0x1.f00f800163076p-3. */ +svfloat64_t SV_NAME_D2 (atan2pi) (svfloat64_t y, svfloat64_t x, + const svbool_t pg) +{ + const struct data *d = ptr_barrier (&data); + svbool_t ptrue = svptrue_b64 (); + + svuint64_t ix = svreinterpret_u64 (x); + svuint64_t iy = svreinterpret_u64 (y); + + svbool_t cmp_x = zeroinfnan (ix, pg); + svbool_t cmp_y = zeroinfnan (iy, pg); + svbool_t cmp_xy = svorr_z (pg, cmp_x, cmp_y); + + svfloat64_t ax = svabs_x (pg, x); + svfloat64_t ay = svabs_x (pg, y); + svuint64_t iax = svreinterpret_u64 (ax); + svuint64_t iay = svreinterpret_u64 (ay); + + svuint64_t sign_x = sveor_x (pg, ix, iax); + svuint64_t sign_y = sveor_x (pg, iy, iay); + svuint64_t sign_xy = sveor_x (pg, sign_x, sign_y); + + svbool_t pred_aygtax = svcmpgt (pg, ay, ax); + + /* Set up z for evaluation of atanpi. */ + svfloat64_t num = svsel (pred_aygtax, svneg_x (pg, ax), ay); + svfloat64_t den = svsel (pred_aygtax, ay, ax); + svfloat64_t z = svdiv_x (pg, num, den); + + /* Work out the correct shift for atan2pi: + -1.0 when x < 0 and ax < ay + -0.5 when x < 0 and ax > ay + 0 when x >= 0 and ax < ay + 0.5 when x >= 0 and ax > ay. */ + svfloat64_t shift = svreinterpret_f64 (svlsr_x (pg, sign_x, 1)); + shift = svmul_x (ptrue, shift, sv_f64 (d->shift_val)); + shift = svsel (pred_aygtax, sv_f64 (d->shift_val), shift); + shift = svreinterpret_f64 (svorr_x (pg, sign_x, svreinterpret_u64 (shift))); + + /* Use split Estrin scheme for P(z^2) with deg(P)=19. */ + svfloat64_t z2 = svmul_x (pg, z, z); + svfloat64_t z3 = svmul_x (pg, z2, z); + svfloat64_t z4 = svmul_x (pg, z2, z2); + svfloat64_t z8 = svmul_x (pg, z4, z4); + svfloat64_t z16 = svmul_x (pg, z8, z8); + + /* Order-7 Estrin. */ + svfloat64_t c24 = svld1rq (ptrue, &d->c2); + svfloat64_t c68 = svld1rq (ptrue, &d->c6); + + svfloat64_t p12 = svmla_lane (sv_f64 (d->c1), z2, c24, 0); + svfloat64_t p34 = svmla_lane (sv_f64 (d->c3), z2, c24, 1); + svfloat64_t p56 = svmla_lane (sv_f64 (d->c5), z2, c68, 0); + svfloat64_t p78 = svmla_lane (sv_f64 (d->c7), z2, c68, 1); + + svfloat64_t p14 = svmla_x (pg, p12, z4, p34); + svfloat64_t p58 = svmla_x (pg, p56, z4, p78); + svfloat64_t p18 = svmla_x (pg, p14, z8, p58); + + /* Order-11 Estrin. */ + svfloat64_t c1012 = svld1rq (ptrue, &d->c10); + svfloat64_t c1416 = svld1rq (ptrue, &d->c14); + svfloat64_t c1820 = svld1rq (ptrue, &d->c18); + + svfloat64_t p910 = svmla_lane (sv_f64 (d->c9), z2, c1012, 0); + svfloat64_t p1112 = svmla_lane (sv_f64 (d->c11), z2, c1012, 1); + svfloat64_t p912 = svmla_x (pg, p910, z4, p1112); + + svfloat64_t p1314 = svmla_lane (sv_f64 (d->c13), z2, c1416, 0); + svfloat64_t p1516 = svmla_lane (sv_f64 (d->c15), z2, c1416, 1); + svfloat64_t p1316 = svmla_x (pg, p1314, z4, p1516); + + svfloat64_t p1718 = svmla_lane (sv_f64 (d->c17), z2, c1820, 0); + svfloat64_t p1920 = svmla_lane (sv_f64 (d->c19), z2, c1820, 1); + svfloat64_t p1720 = svmla_x (pg, p1718, z4, p1920); + + svfloat64_t p916 = svmla_x (pg, p912, z8, p1316); + svfloat64_t p920 = svmla_x (pg, p916, z16, p1720); + + svfloat64_t poly = svmla_x (pg, p18, z16, p920); + + svfloat64_t ret = svmla_x (pg, shift, z, sv_f64 (d->c0)); + ret = svmla_x (pg, ret, z3, poly); + + /* Account for the sign of x and y. */ + if (__glibc_unlikely (svptest_any (pg, cmp_xy))) + return special_case ( + y, x, + svreinterpret_f64 (sveor_x (pg, svreinterpret_u64 (ret), sign_xy)), + cmp_xy); + return svreinterpret_f64 (sveor_x (pg, svreinterpret_u64 (ret), sign_xy)); +} diff --git a/sysdeps/aarch64/fpu/atan2pif_advsimd.c b/sysdeps/aarch64/fpu/atan2pif_advsimd.c new file mode 100644 index 0000000000..f1f542ba35 --- /dev/null +++ b/sysdeps/aarch64/fpu/atan2pif_advsimd.c @@ -0,0 +1,138 @@ +/* Single-Precision vector (Advanced SIMD) inverse tan2pi function + + Copyright (C) 2025 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include "v_math.h" + +static const struct data +{ + float32x4_t c1, c3, c5, c7; + float c2, c4, c6, c8; + float32x4_t c0; + uint32x4_t comp_const; +} data = { + /* Coefficients of polynomial P such that atan(x)~x+x*P(x^2) on + [2^-128, 1.0]. + Generated using fpminimax between FLT_MIN and 1. */ + .c0 = V4 (0x1.45f306p-2), .c1 = V4 (-0x1.b2975ep-4), + .c2 = 0x1.0490e4p-4, .c3 = V4 (-0x1.70c272p-5), + .c4 = 0x1.0eef52p-5, .c5 = V4 (-0x1.6abbbap-6), + .c6 = 0x1.78157p-7, .c7 = V4 (-0x1.f0b406p-9), + .c8 = 0x1.2ae7fep-11, .comp_const = V4 (2 * 0x7f800000lu - 1), +}; + +#define SignMask v_u32 (0x80000000) +#define OneOverPi v_f32 (0x1.45f307p-2) + +/* Special cases i.e. 0, infinity and nan (fall back to scalar calls). */ +static float32x4_t VPCS_ATTR NOINLINE +special_case (float32x4_t y, float32x4_t x, float32x4_t ret, + uint32x4_t sign_xy, uint32x4_t cmp) +{ + /* Account for the sign of y. */ + ret = vreinterpretq_f32_u32 ( + veorq_u32 (vreinterpretq_u32_f32 (ret), sign_xy)); + + /* Since we have no scalar fallback for atan2pif, + we can instead make a call to atan2f and divide by pi. */ + ret = v_call2_f32 (atan2f, y, x, ret, cmp); + + /* Only divide the special cases by pi, and leave the rest unchanged. */ + return vbslq_f32 (cmp, vmulq_f32 (ret, OneOverPi), ret); +} + +/* Returns 1 if input is the bit representation of 0, infinity or nan. */ +static inline uint32x4_t +zeroinfnan (uint32x4_t i, const struct data *d) +{ + /* 2 * i - 1 >= 2 * 0x7f800000lu - 1. */ + return vcgeq_u32 (vsubq_u32 (vshlq_n_u32 (i, 1), v_u32 (1)), d->comp_const); +} + +/* Fast implementation of vector atan2f. Maximum observed error is 2.89 ULP: + _ZGVnN4vv_atan2pif (0x1.bd397p+54, 0x1.e79a4ap+54) got 0x1.e2678ep-3 + want 0x1.e26794p-3. */ +float32x4_t VPCS_ATTR NOINLINE V_NAME_F2 (atan2pi) (float32x4_t y, + float32x4_t x) +{ + const struct data *d = ptr_barrier (&data); + + uint32x4_t ix = vreinterpretq_u32_f32 (x); + uint32x4_t iy = vreinterpretq_u32_f32 (y); + + uint32x4_t special_cases + = vorrq_u32 (zeroinfnan (ix, d), zeroinfnan (iy, d)); + + uint32x4_t sign_x = vandq_u32 (ix, SignMask); + uint32x4_t sign_y = vandq_u32 (iy, SignMask); + uint32x4_t sign_xy = veorq_u32 (sign_x, sign_y); + + float32x4_t ax = vabsq_f32 (x); + float32x4_t ay = vabsq_f32 (y); + + uint32x4_t pred_xlt0 = vcltzq_f32 (x); + uint32x4_t pred_aygtax = vcgtq_f32 (ay, ax); + + /* Set up z for evaluation of atanpif. */ + float32x4_t num = vbslq_f32 (pred_aygtax, vnegq_f32 (ax), ay); + float32x4_t den = vbslq_f32 (pred_aygtax, ay, ax); + float32x4_t z = vdivq_f32 (num, den); + + /* Work out the correct shift for atan2pi: + -1.0 when x < 0 and ax < ay + -0.5 when x < 0 and ax > ay + 0 when x >= 0 and ax < ay + 0.5 when x >= 0 and ax > ay. */ + float32x4_t shift = vreinterpretq_f32_u32 ( + vandq_u32 (pred_xlt0, vreinterpretq_u32_f32 (v_f32 (-1.0f)))); + float32x4_t shift2 = vreinterpretq_f32_u32 ( + vandq_u32 (pred_aygtax, vreinterpretq_u32_f32 (v_f32 (0.5f)))); + shift = vaddq_f32 (shift, shift2); + + /* Calculate the polynomial approximation. */ + float32x4_t z2 = vmulq_f32 (z, z); + float32x4_t z3 = vmulq_f32 (z2, z); + float32x4_t z4 = vmulq_f32 (z2, z2); + float32x4_t z8 = vmulq_f32 (z4, z4); + + float32x4_t c2468 = vld1q_f32 (&d->c2); + + float32x4_t p12 = vfmaq_laneq_f32 (d->c1, z2, c2468, 0); + float32x4_t p34 = vfmaq_laneq_f32 (d->c3, z2, c2468, 1); + float32x4_t p56 = vfmaq_laneq_f32 (d->c5, z2, c2468, 2); + float32x4_t p78 = vfmaq_laneq_f32 (d->c7, z2, c2468, 3); + float32x4_t p14 = vfmaq_f32 (p12, z4, p34); + float32x4_t p58 = vfmaq_f32 (p56, z4, p78); + + float32x4_t poly = vfmaq_f32 (p14, z8, p58); + + /* y = shift + z * P(z^2). */ + float32x4_t ret = vfmaq_f32 (shift, z, d->c0); + ret = vfmaq_f32 (ret, z3, poly); + + if (__glibc_unlikely (v_any_u32 (special_cases))) + { + return special_case (y, x, ret, sign_xy, special_cases); + } + + /* Account for the sign of y. */ + return vreinterpretq_f32_u32 ( + veorq_u32 (vreinterpretq_u32_f32 (ret), sign_xy)); +} +libmvec_hidden_def (V_NAME_F2 (atan2pi)) +HALF_WIDTH_ALIAS_F2 (atan2pi) diff --git a/sysdeps/aarch64/fpu/atan2pif_sve.c b/sysdeps/aarch64/fpu/atan2pif_sve.c new file mode 100644 index 0000000000..d5ac4b74b1 --- /dev/null +++ b/sysdeps/aarch64/fpu/atan2pif_sve.c @@ -0,0 +1,137 @@ +/* Single-Precision vector (SVE) inverse tan2pi function + + Copyright (C) 2025 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include "sv_math.h" + +static const struct data +{ + float32_t c0, c1, c3, c5, c7; + float32_t c2, c4, c6, c8; + float32_t shift_val; + uint32_t comp_const; +} data = { + /* Coefficients of polynomial P such that atan(x)~x+x*P(x^2) on + [2**-128, 1.0]. */ + .c0 = 0x1.45f306p-2, + .c1 = -0x1.b2975ep-4, + .c2 = 0x1.0490e4p-4, + .c3 = -0x1.70c272p-5, + .c4 = 0x1.0eef52p-5, + .c5 = -0x1.6abbbap-6, + .c6 = 0x1.78157p-7, + .c7 = -0x1.f0b406p-9, + .c8 = 0x1.2ae7fep-11, + .shift_val = 0.5f, + .comp_const = 2 * 0x7f800000lu - 1, +}; + +#define OneOverPi sv_f32 (0x1.45f307p-2) + +/* Special cases i.e. 0, infinity, nan (fall back to scalar calls). */ +static svfloat32_t NOINLINE +special_case (svfloat32_t y, svfloat32_t x, svfloat32_t ret, + const svbool_t cmp) +{ + ret = sv_call2_f32 (atan2f, y, x, ret, cmp); + return svmul_f32_x (cmp, ret, OneOverPi); +} + +/* Returns a predicate indicating true if the input is the bit representation + of 0, infinity or nan. */ +static inline svbool_t +zeroinfnan (svuint32_t i, const svbool_t pg, const struct data *d) +{ + return svcmpge (pg, svsub_x (pg, svlsl_x (pg, i, 1), 1), + sv_u32 (d->comp_const)); +} + +/* Fast implementation of SVE atan2pif based on atan(x) ~ shift + z + z^3 * + P(z^2) with reduction to [0,1] using z=1/x and shift = 1/2. Maximum + observed error is 2.90 ULP: + _ZGVsMxvv_atan2pif (0x1.a28542p+5, 0x1.adb7c6p+5) got 0x1.f76524p-3 + want 0x1.f7651ep-3. */ +svfloat32_t SV_NAME_F2 (atan2pi) (svfloat32_t y, svfloat32_t x, + const svbool_t pg) +{ + const struct data *d = ptr_barrier (&data); + svbool_t ptrue = svptrue_b32 (); + + svuint32_t ix = svreinterpret_u32 (x); + svuint32_t iy = svreinterpret_u32 (y); + + svbool_t cmp_x = zeroinfnan (ix, pg, d); + svbool_t cmp_y = zeroinfnan (iy, pg, d); + svbool_t cmp_xy = svorr_z (pg, cmp_x, cmp_y); + + svfloat32_t ax = svabs_x (pg, x); + svfloat32_t ay = svabs_x (pg, y); + svuint32_t iax = svreinterpret_u32 (ax); + svuint32_t iay = svreinterpret_u32 (ay); + + svuint32_t sign_x = sveor_x (pg, ix, iax); + svuint32_t sign_y = sveor_x (pg, iy, iay); + svuint32_t sign_xy = sveor_x (pg, sign_x, sign_y); + + svbool_t pred_aygtax = svcmpgt (pg, ay, ax); + + /* Set up z for evaluation of atanpif. */ + svfloat32_t num = svsel (pred_aygtax, svneg_x (pg, ax), ay); + svfloat32_t den = svsel (pred_aygtax, ay, ax); + svfloat32_t z = svdiv_x (ptrue, num, den); + + /* Work out the correct shift for atan2pi: + -1.0 when x < 0 and ax < ay + -0.5 when x < 0 and ax > ay + 0 when x >= 0 and ax < ay + 0.5 when x >= 0 and ax > ay. */ + svfloat32_t shift = svreinterpret_f32 (svlsr_x (pg, sign_x, 1)); + shift = svmul_x (ptrue, shift, sv_f32 (d->shift_val)); + shift = svsel (pred_aygtax, sv_f32 (d->shift_val), shift); + shift = svreinterpret_f32 (svorr_x (pg, sign_x, svreinterpret_u32 (shift))); + + /* Use pure Estrin scheme for P(z^2) with deg(P)=7. */ + svfloat32_t z2 = svmul_x (pg, z, z); + svfloat32_t z4 = svmul_x (pg, z2, z2); + svfloat32_t z8 = svmul_x (pg, z4, z4); + + svfloat32_t even_coeffs = svld1rq (ptrue, &d->c2); + + svfloat32_t p12 = svmla_lane (sv_f32 (d->c1), z2, even_coeffs, 0); + svfloat32_t p34 = svmla_lane (sv_f32 (d->c3), z2, even_coeffs, 1); + svfloat32_t p56 = svmla_lane (sv_f32 (d->c5), z2, even_coeffs, 2); + svfloat32_t p78 = svmla_lane (sv_f32 (d->c7), z2, even_coeffs, 3); + + svfloat32_t p14 = svmad_x (pg, z4, p34, p12); + svfloat32_t p58 = svmad_x (pg, z4, p78, p56); + + svfloat32_t p18 = svmad_x (pg, z8, p58, p14); + + /* ret = shift + z + z^3 * P(z^2). */ + svfloat32_t poly = svmad_x (pg, z2, p18, d->c0); + svfloat32_t ret = svmad_x (pg, poly, z, shift); + + if (__glibc_unlikely (svptest_any (pg, cmp_xy))) + return special_case ( + y, x, + svreinterpret_f32 (sveor_x (pg, svreinterpret_u32 (ret), sign_xy)), + cmp_xy); + + /* Account for the sign of x and y. */ + return svreinterpret_f32 (sveor_x (pg, svreinterpret_u32 (ret), sign_xy)); +} diff --git a/sysdeps/aarch64/fpu/bits/math-vector.h b/sysdeps/aarch64/fpu/bits/math-vector.h index 7667e0a633..eca80fb3e0 100644 --- a/sysdeps/aarch64/fpu/bits/math-vector.h +++ b/sysdeps/aarch64/fpu/bits/math-vector.h @@ -69,6 +69,10 @@ # define __DECL_SIMD_atan2 __DECL_SIMD_aarch64 # undef __DECL_SIMD_atan2f # define __DECL_SIMD_atan2f __DECL_SIMD_aarch64 +# undef __DECL_SIMD_atan2pi +# define __DECL_SIMD_atan2pi __DECL_SIMD_aarch64 +# undef __DECL_SIMD_atan2pif +# define __DECL_SIMD_atan2pif __DECL_SIMD_aarch64 # undef __DECL_SIMD_cbrt # define __DECL_SIMD_cbrt __DECL_SIMD_aarch64 # undef __DECL_SIMD_cbrtf @@ -188,6 +192,7 @@ typedef __SVBool_t __sv_bool_t; # define __vpcs __attribute__ ((__aarch64_vector_pcs__)) __vpcs __f32x4_t _ZGVnN4vv_atan2f (__f32x4_t, __f32x4_t); +__vpcs __f32x4_t _ZGVnN4vv_atan2pif (__f32x4_t, __f32x4_t); __vpcs __f32x4_t _ZGVnN4v_acosf (__f32x4_t); __vpcs __f32x4_t _ZGVnN4v_acoshf (__f32x4_t); __vpcs __f32x4_t _ZGVnN4v_acospif (__f32x4_t); @@ -222,6 +227,7 @@ __vpcs __f32x4_t _ZGVnN4v_tanhf (__f32x4_t); __vpcs __f32x4_t _ZGVnN4v_tanpif (__f32x4_t); __vpcs __f64x2_t _ZGVnN2vv_atan2 (__f64x2_t, __f64x2_t); +__vpcs __f64x2_t _ZGVnN2vv_atanpi2 (__f64x2_t, __f64x2_t); __vpcs __f64x2_t _ZGVnN2v_acos (__f64x2_t); __vpcs __f64x2_t _ZGVnN2v_acosh (__f64x2_t); __vpcs __f64x2_t _ZGVnN2v_acospi (__f64x2_t); @@ -261,6 +267,7 @@ __vpcs __f64x2_t _ZGVnN2v_tanpi (__f64x2_t); #ifdef __SVE_VEC_MATH_SUPPORTED __sv_f32_t _ZGVsMxvv_atan2f (__sv_f32_t, __sv_f32_t, __sv_bool_t); +__sv_f32_t _ZGVsMxvv_atan2pif (__sv_f32_t, __sv_f32_t, __sv_bool_t); __sv_f32_t _ZGVsMxv_acosf (__sv_f32_t, __sv_bool_t); __sv_f32_t _ZGVsMxv_acoshf (__sv_f32_t, __sv_bool_t); __sv_f32_t _ZGVsMxv_acospif (__sv_f32_t, __sv_bool_t); @@ -295,6 +302,7 @@ __sv_f32_t _ZGVsMxv_tanhf (__sv_f32_t, __sv_bool_t); __sv_f32_t _ZGVsMxv_tanpif (__sv_f32_t, __sv_bool_t); __sv_f64_t _ZGVsMxvv_atan2 (__sv_f64_t, __sv_f64_t, __sv_bool_t); +__sv_f64_t _ZGVsMxvv_atan2pi (__sv_f64_t, __sv_f64_t, __sv_bool_t); __sv_f64_t _ZGVsMxv_acos (__sv_f64_t, __sv_bool_t); __sv_f64_t _ZGVsMxv_acosh (__sv_f64_t, __sv_bool_t); __sv_f64_t _ZGVsMxv_acospi (__sv_f64_t, __sv_bool_t); diff --git a/sysdeps/aarch64/fpu/test-double-advsimd-wrappers.c b/sysdeps/aarch64/fpu/test-double-advsimd-wrappers.c index b7a0538b44..a3fef2232b 100644 --- a/sysdeps/aarch64/fpu/test-double-advsimd-wrappers.c +++ b/sysdeps/aarch64/fpu/test-double-advsimd-wrappers.c @@ -33,6 +33,7 @@ VPCS_VECTOR_WRAPPER (atan_advsimd, _ZGVnN2v_atan) VPCS_VECTOR_WRAPPER (atanh_advsimd, _ZGVnN2v_atanh) VPCS_VECTOR_WRAPPER (atanpi_advsimd, _ZGVnN2v_atanpi) VPCS_VECTOR_WRAPPER_ff (atan2_advsimd, _ZGVnN2vv_atan2) +VPCS_VECTOR_WRAPPER_ff (atan2pi_advsimd, _ZGVnN2vv_atan2pi) VPCS_VECTOR_WRAPPER (cbrt_advsimd, _ZGVnN2v_cbrt) VPCS_VECTOR_WRAPPER (cos_advsimd, _ZGVnN2v_cos) VPCS_VECTOR_WRAPPER (cosh_advsimd, _ZGVnN2v_cosh) diff --git a/sysdeps/aarch64/fpu/test-double-sve-wrappers.c b/sysdeps/aarch64/fpu/test-double-sve-wrappers.c index aec60db616..f4a5ae866d 100644 --- a/sysdeps/aarch64/fpu/test-double-sve-wrappers.c +++ b/sysdeps/aarch64/fpu/test-double-sve-wrappers.c @@ -52,6 +52,7 @@ SVE_VECTOR_WRAPPER (atan_sve, _ZGVsMxv_atan) SVE_VECTOR_WRAPPER (atanh_sve, _ZGVsMxv_atanh) SVE_VECTOR_WRAPPER (atanpi_sve, _ZGVsMxv_atanpi) SVE_VECTOR_WRAPPER_ff (atan2_sve, _ZGVsMxvv_atan2) +SVE_VECTOR_WRAPPER_ff (atan2pi_sve, _ZGVsMxvv_atan2pi) SVE_VECTOR_WRAPPER (cbrt_sve, _ZGVsMxv_cbrt) SVE_VECTOR_WRAPPER (cos_sve, _ZGVsMxv_cos) SVE_VECTOR_WRAPPER (cosh_sve, _ZGVsMxv_cosh) diff --git a/sysdeps/aarch64/fpu/test-float-advsimd-wrappers.c b/sysdeps/aarch64/fpu/test-float-advsimd-wrappers.c index 60cffd5035..bc22956994 100644 --- a/sysdeps/aarch64/fpu/test-float-advsimd-wrappers.c +++ b/sysdeps/aarch64/fpu/test-float-advsimd-wrappers.c @@ -33,6 +33,7 @@ VPCS_VECTOR_WRAPPER (atanf_advsimd, _ZGVnN4v_atanf) VPCS_VECTOR_WRAPPER (atanhf_advsimd, _ZGVnN4v_atanhf) VPCS_VECTOR_WRAPPER (atanpif_advsimd, _ZGVnN4v_atanpif) VPCS_VECTOR_WRAPPER_ff (atan2f_advsimd, _ZGVnN4vv_atan2f) +VPCS_VECTOR_WRAPPER_ff (atan2pif_advsimd, _ZGVnN4vv_atan2pif) VPCS_VECTOR_WRAPPER (cbrtf_advsimd, _ZGVnN4v_cbrtf) VPCS_VECTOR_WRAPPER (cosf_advsimd, _ZGVnN4v_cosf) VPCS_VECTOR_WRAPPER (coshf_advsimd, _ZGVnN4v_coshf) diff --git a/sysdeps/aarch64/fpu/test-float-sve-wrappers.c b/sysdeps/aarch64/fpu/test-float-sve-wrappers.c index ab50ae9ad0..ad0d6ad200 100644 --- a/sysdeps/aarch64/fpu/test-float-sve-wrappers.c +++ b/sysdeps/aarch64/fpu/test-float-sve-wrappers.c @@ -52,6 +52,7 @@ SVE_VECTOR_WRAPPER (atanf_sve, _ZGVsMxv_atanf) SVE_VECTOR_WRAPPER (atanhf_sve, _ZGVsMxv_atanhf) SVE_VECTOR_WRAPPER (atanpif_sve, _ZGVsMxv_atanpif) SVE_VECTOR_WRAPPER_ff (atan2f_sve, _ZGVsMxvv_atan2f) +SVE_VECTOR_WRAPPER_ff (atan2pif_sve, _ZGVsMxvv_atan2pif) SVE_VECTOR_WRAPPER (cbrtf_sve, _ZGVsMxv_cbrtf) SVE_VECTOR_WRAPPER (cosf_sve, _ZGVsMxv_cosf) SVE_VECTOR_WRAPPER (coshf_sve, _ZGVsMxv_coshf) diff --git a/sysdeps/unix/sysv/linux/aarch64/libmvec.abilist b/sysdeps/unix/sysv/linux/aarch64/libmvec.abilist index 2714648371..f7f72b6a58 100644 --- a/sysdeps/unix/sysv/linux/aarch64/libmvec.abilist +++ b/sysdeps/unix/sysv/linux/aarch64/libmvec.abilist @@ -154,12 +154,17 @@ GLIBC_2.42 _ZGVnN2v_asinpi F GLIBC_2.42 _ZGVnN2v_asinpif F GLIBC_2.42 _ZGVnN2v_atanpi F GLIBC_2.42 _ZGVnN2v_atanpif F +GLIBC_2.42 _ZGVnN2vv_atan2pi F +GLIBC_2.42 _ZGVnN2vv_atan2pif F GLIBC_2.42 _ZGVnN4v_acospif F GLIBC_2.42 _ZGVnN4v_asinpif F GLIBC_2.42 _ZGVnN4v_atanpif F +GLIBC_2.42 _ZGVnN4vv_atan2pif F GLIBC_2.42 _ZGVsMxv_acospi F GLIBC_2.42 _ZGVsMxv_acospif F GLIBC_2.42 _ZGVsMxv_asinpi F GLIBC_2.42 _ZGVsMxv_asinpif F GLIBC_2.42 _ZGVsMxv_atanpi F GLIBC_2.42 _ZGVsMxv_atanpif F +GLIBC_2.42 _ZGVsMxvv_atan2pi F +GLIBC_2.42 _ZGVsMxvv_atan2pif F