xref: /aosp_15_r20/external/arm-optimized-routines/pl/math/sv_erff_2u.c (revision 412f47f9e737e10ed5cc46ec6a8d7fa2264f8a14)
1*412f47f9SXin Li /*
2*412f47f9SXin Li  * Single-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   float min, max, scale, shift, third;
15*412f47f9SXin Li } data = {
16*412f47f9SXin Li   .min = 0x1.cp-7f,	   /* 1/64 - 1/512.  */
17*412f47f9SXin Li   .max = 3.9375,	   /* 4 - 8/128.  */
18*412f47f9SXin Li   .scale = 0x1.20dd76p+0f, /* 2/sqrt(pi).  */
19*412f47f9SXin Li   .shift = 0x1p16f,
20*412f47f9SXin Li   .third = 0x1.555556p-2f, /* 1/3.  */
21*412f47f9SXin Li };
22*412f47f9SXin Li 
23*412f47f9SXin Li #define SignMask (0x80000000)
24*412f47f9SXin Li 
25*412f47f9SXin Li /* Single-precision implementation of vector erf(x).
26*412f47f9SXin Li    Approximation based on series expansion near x rounded to
27*412f47f9SXin Li    nearest multiple of 1/128.
28*412f47f9SXin Li    Let d = x - r, and scale = 2 / sqrt(pi) * exp(-r^2). For x near r,
29*412f47f9SXin Li 
30*412f47f9SXin Li    erf(x) ~ erf(r) + scale * d * [1 - r * d - 1/3 * d^2]
31*412f47f9SXin Li 
32*412f47f9SXin Li    Values of erf(r) and scale are read from lookup tables.
33*412f47f9SXin Li    For |x| < 0x1.cp-7, the algorithm sets r = 0, erf(r) = 0, and scale = 2 /
34*412f47f9SXin Li    sqrt(pi), so it simply boils down to a Taylor series expansion near 0. For
35*412f47f9SXin Li    |x| > 3.9375, erf(|x|) rounds to 1.0f.
36*412f47f9SXin Li 
37*412f47f9SXin Li    Maximum error on each interval:
38*412f47f9SXin Li    - [0, 0x1.cp-7]: 1.93 ULP
39*412f47f9SXin Li      _ZGVsMxv_erff(0x1.c373e6p-9) got 0x1.fd686cp-9 want 0x1.fd6868p-9
40*412f47f9SXin Li    - [0x1.cp-7, 4.0]: 1.26 ULP
41*412f47f9SXin Li      _ZGVsMxv_erff(0x1.1d002ep+0) got 0x1.c4eb9ap-1 want 0x1.c4eb98p-1.  */
SV_NAME_F1(erf)42*412f47f9SXin Li svfloat32_t SV_NAME_F1 (erf) (svfloat32_t x, const svbool_t pg)
43*412f47f9SXin Li {
44*412f47f9SXin Li   const struct data *dat = ptr_barrier (&data);
45*412f47f9SXin Li 
46*412f47f9SXin Li   /* |x| > 1/64 - 1/512.  */
47*412f47f9SXin Li   svbool_t a_gt_min = svacgt (pg, x, dat->min);
48*412f47f9SXin Li 
49*412f47f9SXin Li   /* |x| >= 4.0 - 8/128.  */
50*412f47f9SXin Li   svbool_t a_ge_max = svacge (pg, x, dat->max);
51*412f47f9SXin Li   svfloat32_t a = svabs_x (pg, x);
52*412f47f9SXin Li 
53*412f47f9SXin Li   svfloat32_t shift = sv_f32 (dat->shift);
54*412f47f9SXin Li   svfloat32_t z = svadd_x (pg, a, shift);
55*412f47f9SXin Li   svuint32_t i
56*412f47f9SXin Li       = svsub_x (pg, svreinterpret_u32 (z), svreinterpret_u32 (shift));
57*412f47f9SXin Li 
58*412f47f9SXin Li   /* Saturate lookup index.  */
59*412f47f9SXin Li   i = svsel (a_ge_max, sv_u32 (512), i);
60*412f47f9SXin Li 
61*412f47f9SXin Li   /* r and erf(r) set to 0 for |x| below min.  */
62*412f47f9SXin Li   svfloat32_t r = svsub_z (a_gt_min, z, shift);
63*412f47f9SXin Li   svfloat32_t erfr = svld1_gather_index (a_gt_min, __sv_erff_data.erf, i);
64*412f47f9SXin Li 
65*412f47f9SXin Li   /* scale set to 2/sqrt(pi) for |x| below min.  */
66*412f47f9SXin Li   svfloat32_t scale = svld1_gather_index (a_gt_min, __sv_erff_data.scale, i);
67*412f47f9SXin Li   scale = svsel (a_gt_min, scale, sv_f32 (dat->scale));
68*412f47f9SXin Li 
69*412f47f9SXin Li   /* erf(x) ~ erf(r) + scale * d * (1 - r * d + 1/3 * d^2).  */
70*412f47f9SXin Li   svfloat32_t d = svsub_x (pg, a, r);
71*412f47f9SXin Li   svfloat32_t d2 = svmul_x (pg, d, d);
72*412f47f9SXin Li   svfloat32_t y = svmla_x (pg, r, d, dat->third);
73*412f47f9SXin Li   y = svmla_x (pg, erfr, scale, svmls_x (pg, d, d2, y));
74*412f47f9SXin Li 
75*412f47f9SXin Li   /* Solves the |x| = inf case.  */
76*412f47f9SXin Li   y = svsel (a_ge_max, sv_f32 (1.0f), y);
77*412f47f9SXin Li 
78*412f47f9SXin Li   /* Copy sign.  */
79*412f47f9SXin Li   svuint32_t ix = svreinterpret_u32 (x);
80*412f47f9SXin Li   svuint32_t iy = svreinterpret_u32 (y);
81*412f47f9SXin Li   svuint32_t sign = svand_x (pg, ix, SignMask);
82*412f47f9SXin Li   return svreinterpret_f32 (svorr_x (pg, sign, iy));
83*412f47f9SXin Li }
84*412f47f9SXin Li 
85*412f47f9SXin Li PL_SIG (SV, F, 1, erf, -4.0, 4.0)
86*412f47f9SXin Li PL_TEST_ULP (SV_NAME_F1 (erf), 1.43)
87*412f47f9SXin Li PL_TEST_SYM_INTERVAL (SV_NAME_F1 (erf), 0, 0x1.cp-7, 40000)
88*412f47f9SXin Li PL_TEST_SYM_INTERVAL (SV_NAME_F1 (erf), 0x1.cp-7, 3.9375, 40000)
89*412f47f9SXin Li PL_TEST_SYM_INTERVAL (SV_NAME_F1 (erf), 3.9375, inf, 40000)
90*412f47f9SXin Li PL_TEST_SYM_INTERVAL (SV_NAME_F1 (erf), 0, inf, 4000)
91