1 /*
2 * Double-precision vector erf(x) function.
3 *
4 * Copyright (c) 2023, Arm Limited.
5 * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6 */
7
8 #include "sv_math.h"
9 #include "pl_sig.h"
10 #include "pl_test.h"
11
12 static const struct data
13 {
14 double third;
15 double tenth, two_over_five, two_over_fifteen;
16 double two_over_nine, two_over_fortyfive;
17 double max, shift;
18 } data = {
19 .third = 0x1.5555555555556p-2, /* used to compute 2/3 and 1/6 too. */
20 .two_over_fifteen = 0x1.1111111111111p-3,
21 .tenth = -0x1.999999999999ap-4,
22 .two_over_five = -0x1.999999999999ap-2,
23 .two_over_nine = -0x1.c71c71c71c71cp-3,
24 .two_over_fortyfive = 0x1.6c16c16c16c17p-5,
25 .max = 5.9921875, /* 6 - 1/128. */
26 .shift = 0x1p45,
27 };
28
29 #define SignMask (0x8000000000000000)
30
31 /* Double-precision implementation of vector erf(x).
32 Approximation based on series expansion near x rounded to
33 nearest multiple of 1/128.
34 Let d = x - r, and scale = 2 / sqrt(pi) * exp(-r^2). For x near r,
35 erf(x) ~ erf(r) + scale * d * [
36 + 1
37 - r d
38 + 1/3 (2 r^2 - 1) d^2
39 - 1/6 (r (2 r^2 - 3)) d^3
40 + 1/30 (4 r^4 - 12 r^2 + 3) d^4
41 - 1/90 (4 r^4 - 20 r^2 + 15) d^5
42 ]
43
44 Maximum measure error: 2.29 ULP
45 _ZGVsMxv_erf(-0x1.00003c924e5d1p-8) got -0x1.20dd59132ebadp-8
46 want -0x1.20dd59132ebafp-8. */
SV_NAME_D1(erf)47 svfloat64_t SV_NAME_D1 (erf) (svfloat64_t x, const svbool_t pg)
48 {
49 const struct data *dat = ptr_barrier (&data);
50
51 /* |x| >= 6.0 - 1/128. Opposite conditions except none of them catch NaNs so
52 they can be used in lookup and BSLs to yield the expected results. */
53 svbool_t a_ge_max = svacge (pg, x, dat->max);
54 svbool_t a_lt_max = svaclt (pg, x, dat->max);
55
56 /* Set r to multiple of 1/128 nearest to |x|. */
57 svfloat64_t a = svabs_x (pg, x);
58 svfloat64_t shift = sv_f64 (dat->shift);
59 svfloat64_t z = svadd_x (pg, a, shift);
60 svuint64_t i
61 = svsub_x (pg, svreinterpret_u64 (z), svreinterpret_u64 (shift));
62
63 /* Lookup without shortcut for small values but with predicate to avoid
64 segfault for large values and NaNs. */
65 svfloat64_t r = svsub_x (pg, z, shift);
66 svfloat64_t erfr = svld1_gather_index (a_lt_max, __sv_erf_data.erf, i);
67 svfloat64_t scale = svld1_gather_index (a_lt_max, __sv_erf_data.scale, i);
68
69 /* erf(x) ~ erf(r) + scale * d * poly (r, d). */
70 svfloat64_t d = svsub_x (pg, a, r);
71 svfloat64_t d2 = svmul_x (pg, d, d);
72 svfloat64_t r2 = svmul_x (pg, r, r);
73
74 /* poly (d, r) = 1 + p1(r) * d + p2(r) * d^2 + ... + p5(r) * d^5. */
75 svfloat64_t p1 = r;
76 svfloat64_t third = sv_f64 (dat->third);
77 svfloat64_t twothird = svmul_x (pg, third, 2.0);
78 svfloat64_t sixth = svmul_x (pg, third, 0.5);
79 svfloat64_t p2 = svmls_x (pg, third, r2, twothird);
80 svfloat64_t p3 = svmad_x (pg, r2, third, -0.5);
81 p3 = svmul_x (pg, r, p3);
82 svfloat64_t p4
83 = svmla_x (pg, sv_f64 (dat->two_over_five), r2, dat->two_over_fifteen);
84 p4 = svmls_x (pg, sv_f64 (dat->tenth), r2, p4);
85 svfloat64_t p5
86 = svmla_x (pg, sv_f64 (dat->two_over_nine), r2, dat->two_over_fortyfive);
87 p5 = svmla_x (pg, sixth, r2, p5);
88 p5 = svmul_x (pg, r, p5);
89
90 svfloat64_t p34 = svmla_x (pg, p3, d, p4);
91 svfloat64_t p12 = svmla_x (pg, p1, d, p2);
92 svfloat64_t y = svmla_x (pg, p34, d2, p5);
93 y = svmla_x (pg, p12, d2, y);
94
95 y = svmla_x (pg, erfr, scale, svmls_x (pg, d, d2, y));
96
97 /* Solves the |x| = inf and NaN cases. */
98 y = svsel (a_ge_max, sv_f64 (1.0), y);
99
100 /* Copy sign. */
101 svuint64_t ix = svreinterpret_u64 (x);
102 svuint64_t iy = svreinterpret_u64 (y);
103 svuint64_t sign = svand_x (pg, ix, SignMask);
104 return svreinterpret_f64 (svorr_x (pg, sign, iy));
105 }
106
107 PL_SIG (SV, D, 1, erf, -6.0, 6.0)
108 PL_TEST_ULP (SV_NAME_D1 (erf), 1.79)
109 PL_TEST_SYM_INTERVAL (SV_NAME_D1 (erf), 0, 5.9921875, 40000)
110 PL_TEST_SYM_INTERVAL (SV_NAME_D1 (erf), 5.9921875, inf, 40000)
111 PL_TEST_SYM_INTERVAL (SV_NAME_D1 (erf), 0, inf, 4000)
112