xref: /freebsd/contrib/arm-optimized-routines/math/aarch64/sve/erf.c (revision dd21556857e8d40f66bf5ad54754d9d52669ebf7)
1 /*
2  * Double-precision vector erf(x) function.
3  *
4  * Copyright (c) 2023-2024, Arm Limited.
5  * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6  */
7 
8 #include "sv_math.h"
9 #include "test_sig.h"
10 #include "test_defs.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.  */
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 = svand_x (pg, svreinterpret_u64 (z), 0xfff);
61   i = svadd_x (pg, i, i);
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
67       = svld1_gather_index (a_lt_max, &__v_erf_data.tab[0].erf, i);
68   svfloat64_t scale
69       = svld1_gather_index (a_lt_max, &__v_erf_data.tab[0].scale, i);
70 
71   /* erf(x) ~ erf(r) + scale * d * poly (r, d).  */
72   svfloat64_t d = svsub_x (pg, a, r);
73   svfloat64_t d2 = svmul_x (pg, d, d);
74   svfloat64_t r2 = svmul_x (pg, r, r);
75 
76   /* poly (d, r) = 1 + p1(r) * d + p2(r) * d^2 + ... + p5(r) * d^5.  */
77   svfloat64_t p1 = r;
78   svfloat64_t third = sv_f64 (dat->third);
79   svfloat64_t twothird = svmul_x (pg, third, 2.0);
80   svfloat64_t sixth = svmul_x (pg, third, 0.5);
81   svfloat64_t p2 = svmls_x (pg, third, r2, twothird);
82   svfloat64_t p3 = svmad_x (pg, r2, third, -0.5);
83   p3 = svmul_x (pg, r, p3);
84   svfloat64_t p4
85       = svmla_x (pg, sv_f64 (dat->two_over_five), r2, dat->two_over_fifteen);
86   p4 = svmls_x (pg, sv_f64 (dat->tenth), r2, p4);
87   svfloat64_t p5
88       = svmla_x (pg, sv_f64 (dat->two_over_nine), r2, dat->two_over_fortyfive);
89   p5 = svmla_x (pg, sixth, r2, p5);
90   p5 = svmul_x (pg, r, p5);
91 
92   svfloat64_t p34 = svmla_x (pg, p3, d, p4);
93   svfloat64_t p12 = svmla_x (pg, p1, d, p2);
94   svfloat64_t y = svmla_x (pg, p34, d2, p5);
95   y = svmla_x (pg, p12, d2, y);
96 
97   y = svmla_x (pg, erfr, scale, svmls_x (pg, d, d2, y));
98 
99   /* Solves the |x| = inf and NaN cases.  */
100   y = svsel (a_ge_max, sv_f64 (1.0), y);
101 
102   /* Copy sign.  */
103   svuint64_t ix = svreinterpret_u64 (x);
104   svuint64_t iy = svreinterpret_u64 (y);
105   svuint64_t sign = svand_x (pg, ix, SignMask);
106   return svreinterpret_f64 (svorr_x (pg, sign, iy));
107 }
108 
109 TEST_SIG (SV, D, 1, erf, -6.0, 6.0)
110 TEST_ULP (SV_NAME_D1 (erf), 1.79)
111 TEST_DISABLE_FENV (SV_NAME_D1 (erf))
112 TEST_SYM_INTERVAL (SV_NAME_D1 (erf), 0, 5.9921875, 40000)
113 TEST_SYM_INTERVAL (SV_NAME_D1 (erf), 5.9921875, inf, 40000)
114 TEST_SYM_INTERVAL (SV_NAME_D1 (erf), 0, inf, 4000)
115 CLOSE_SVE_ATTR
116