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