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