aarch64/fpu: Add vector variants of erf
[glibc.git] / sysdeps / aarch64 / fpu / erf_sve.c
blob7d51417406e9d9d3aa5ecb66b5dfc56b7b432bfc
1 /* Double-precision vector (SVE) erf function
3 Copyright (C) 2024 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
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.
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.
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/>. */
20 #include "sv_math.h"
22 static const struct data
24 double third;
25 double tenth, two_over_five, two_over_fifteen;
26 double two_over_nine, two_over_fortyfive;
27 double max, shift;
28 } data = {
29 .third = 0x1.5555555555556p-2, /* used to compute 2/3 and 1/6 too. */
30 .two_over_fifteen = 0x1.1111111111111p-3,
31 .tenth = -0x1.999999999999ap-4,
32 .two_over_five = -0x1.999999999999ap-2,
33 .two_over_nine = -0x1.c71c71c71c71cp-3,
34 .two_over_fortyfive = 0x1.6c16c16c16c17p-5,
35 .max = 5.9921875, /* 6 - 1/128. */
36 .shift = 0x1p45,
39 #define SignMask (0x8000000000000000)
41 /* Double-precision implementation of vector erf(x).
42 Approximation based on series expansion near x rounded to
43 nearest multiple of 1/128.
44 Let d = x - r, and scale = 2 / sqrt(pi) * exp(-r^2). For x near r,
45 erf(x) ~ erf(r) + scale * d * [
46 + 1
47 - r d
48 + 1/3 (2 r^2 - 1) d^2
49 - 1/6 (r (2 r^2 - 3)) d^3
50 + 1/30 (4 r^4 - 12 r^2 + 3) d^4
51 - 1/90 (4 r^4 - 20 r^2 + 15) d^5
54 Maximum measure error: 2.29 ULP
55 _ZGVsMxv_erf(-0x1.00003c924e5d1p-8) got -0x1.20dd59132ebadp-8
56 want -0x1.20dd59132ebafp-8. */
57 svfloat64_t SV_NAME_D1 (erf) (svfloat64_t x, const svbool_t pg)
59 const struct data *dat = ptr_barrier (&data);
61 /* |x| >= 6.0 - 1/128. Opposite conditions except none of them catch NaNs so
62 they can be used in lookup and BSLs to yield the expected results. */
63 svbool_t a_ge_max = svacge (pg, x, dat->max);
64 svbool_t a_lt_max = svaclt (pg, x, dat->max);
66 /* Set r to multiple of 1/128 nearest to |x|. */
67 svfloat64_t a = svabs_x (pg, x);
68 svfloat64_t shift = sv_f64 (dat->shift);
69 svfloat64_t z = svadd_x (pg, a, shift);
70 svuint64_t i
71 = svsub_x (pg, svreinterpret_u64 (z), svreinterpret_u64 (shift));
73 /* Lookup without shortcut for small values but with predicate to avoid
74 segfault for large values and NaNs. */
75 svfloat64_t r = svsub_x (pg, z, shift);
76 svfloat64_t erfr = svld1_gather_index (a_lt_max, __sv_erf_data.erf, i);
77 svfloat64_t scale = svld1_gather_index (a_lt_max, __sv_erf_data.scale, i);
79 /* erf(x) ~ erf(r) + scale * d * poly (r, d). */
80 svfloat64_t d = svsub_x (pg, a, r);
81 svfloat64_t d2 = svmul_x (pg, d, d);
82 svfloat64_t r2 = svmul_x (pg, r, r);
84 /* poly (d, r) = 1 + p1(r) * d + p2(r) * d^2 + ... + p5(r) * d^5. */
85 svfloat64_t p1 = r;
86 svfloat64_t third = sv_f64 (dat->third);
87 svfloat64_t twothird = svmul_x (pg, third, 2.0);
88 svfloat64_t sixth = svmul_x (pg, third, 0.5);
89 svfloat64_t p2 = svmls_x (pg, third, r2, twothird);
90 svfloat64_t p3 = svmad_x (pg, r2, third, -0.5);
91 p3 = svmul_x (pg, r, p3);
92 svfloat64_t p4
93 = svmla_x (pg, sv_f64 (dat->two_over_five), r2, dat->two_over_fifteen);
94 p4 = svmls_x (pg, sv_f64 (dat->tenth), r2, p4);
95 svfloat64_t p5
96 = svmla_x (pg, sv_f64 (dat->two_over_nine), r2, dat->two_over_fortyfive);
97 p5 = svmla_x (pg, sixth, r2, p5);
98 p5 = svmul_x (pg, r, p5);
100 svfloat64_t p34 = svmla_x (pg, p3, d, p4);
101 svfloat64_t p12 = svmla_x (pg, p1, d, p2);
102 svfloat64_t y = svmla_x (pg, p34, d2, p5);
103 y = svmla_x (pg, p12, d2, y);
105 y = svmla_x (pg, erfr, scale, svmls_x (pg, d, d2, y));
107 /* Solves the |x| = inf and NaN cases. */
108 y = svsel (a_ge_max, sv_f64 (1.0), y);
110 /* Copy sign. */
111 svuint64_t ix = svreinterpret_u64 (x);
112 svuint64_t iy = svreinterpret_u64 (y);
113 svuint64_t sign = svand_x (pg, ix, SignMask);
114 return svreinterpret_f64 (svorr_x (pg, sign, iy));