1 /*
2 * Single-precision vector erf(x) function.
3 *
4 * Copyright (c) 2020-2023, Arm Limited.
5 * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6 */
7
8 #include "v_math.h"
9 #include "include/mathlib.h"
10 #include "math_config.h"
11 #include "pl_sig.h"
12 #include "pl_test.h"
13
14 #if V_SUPPORTED
15
16 VPCS_ATTR v_f32_t V_NAME (expf) (v_f32_t);
17
18 #define AbsMask v_u32 (0x7fffffff)
19
20 /* Special cases (fall back to scalar calls). */
21 VPCS_ATTR
22 NOINLINE static v_f32_t
specialcase(v_f32_t x,v_f32_t y,v_u32_t cmp)23 specialcase (v_f32_t x, v_f32_t y, v_u32_t cmp)
24 {
25 return v_call_f32 (erff, x, y, cmp);
26 }
27
28 /* A structure to perform look-up in coeffs and other parameter tables. */
29 struct entry
30 {
31 v_f32_t P[V_ERFF_NCOEFFS];
32 };
33
34 static inline struct entry
lookup(v_u32_t i)35 lookup (v_u32_t i)
36 {
37 struct entry e;
38 #ifdef SCALAR
39 for (int j = 0; j < V_ERFF_NCOEFFS; ++j)
40 e.P[j] = __v_erff_data.coeffs[j][i];
41 #else
42 for (int j = 0; j < V_ERFF_NCOEFFS; ++j)
43 {
44 e.P[j][0] = __v_erff_data.coeffs[j][i[0]];
45 e.P[j][1] = __v_erff_data.coeffs[j][i[1]];
46 e.P[j][2] = __v_erff_data.coeffs[j][i[2]];
47 e.P[j][3] = __v_erff_data.coeffs[j][i[3]];
48 }
49 #endif
50 return e;
51 }
52
53 /* Optimized single precision vector error function erf.
54 Maximum measured at +/- 0.931, 1.25ULP:
55 v_erff(-0x1.dc59fap-1) got -0x1.9f9c88p-1
56 want -0x1.9f9c8ap-1. */
57 VPCS_ATTR
V_NAME(erff)58 v_f32_t V_NAME (erff) (v_f32_t x)
59 {
60 /* Handle both inf/nan as well as small values (|x|<2^-28). If any condition
61 in the lane is true then a loop over scalar calls will be performed. */
62 v_u32_t ix = v_as_u32_f32 (x);
63 v_u32_t atop = (ix >> 16) & v_u32 (0x7fff);
64 v_u32_t cmp = v_cond_u32 (atop - v_u32 (0x3180) >= v_u32 (0x7ff0 - 0x3180));
65
66 /* Get sign and absolute value. */
67 v_u32_t sign = ix & ~AbsMask;
68 /* |x| < 0.921875. */
69 v_u32_t red = v_calt_f32 (x, v_f32 (0.921875f));
70 /* |x| > 4.0. */
71 v_u32_t bor = v_cagt_f32 (x, v_f32 (4.0f));
72 /* Avoid dependency in abs(x) in division (and comparison). */
73 v_u32_t i = v_sel_u32 (red, v_u32 (0), v_u32 (1));
74
75 /* Get polynomial coefficients. */
76 struct entry dat = lookup (i);
77
78 v_f32_t a = v_abs_f32 (x);
79 v_f32_t z = v_sel_f32 (red, x * x, a);
80
81 /* Evaluate Polynomial of |x| or x^2. */
82 v_f32_t r = dat.P[6];
83 r = v_fma_f32 (z, r, dat.P[5]);
84 r = v_fma_f32 (z, r, dat.P[4]);
85 r = v_fma_f32 (z, r, dat.P[3]);
86 r = v_fma_f32 (z, r, dat.P[2]);
87 r = v_fma_f32 (z, r, dat.P[1]);
88 r = v_sel_f32 (red, r, v_fma_f32 (z, r, dat.P[0]));
89 r = v_fma_f32 (a, r, a);
90
91 /* y = |x| + |x|*P(|x|) if |x| < 0.921875
92 1 - exp (-(|x|+|x|*P(x^2))) otherwise. */
93 v_f32_t y = v_sel_f32 (red, r, v_f32 (1.0f) - V_NAME (expf) (-r));
94
95 /* Boring domain (absolute value is required to get the sign of erf(-nan)
96 right). */
97 y = v_sel_f32 (bor, v_f32 (1.0f), v_abs_f32 (y));
98
99 /* y=erf(x) if x>0, -erf(-x) otherwise. */
100 y = v_as_f32_u32 (v_as_u32_f32 (y) ^ sign);
101
102 if (unlikely (v_any_u32 (cmp)))
103 return specialcase (x, y, cmp);
104 return y;
105 }
106 VPCS_ALIAS
107
108 PL_SIG (V, F, 1, erf, -4.0, 4.0)
109 PL_TEST_ULP (V_NAME (erff), 0.76)
110 PL_TEST_INTERVAL (V_NAME (erff), 0, 0xffff0000, 10000)
111 PL_TEST_INTERVAL (V_NAME (erff), 0x1p-127, 0x1p-26, 40000)
112 PL_TEST_INTERVAL (V_NAME (erff), -0x1p-127, -0x1p-26, 40000)
113 PL_TEST_INTERVAL (V_NAME (erff), 0x1p-26, 0x1p3, 40000)
114 PL_TEST_INTERVAL (V_NAME (erff), -0x1p-26, -0x1p3, 40000)
115 PL_TEST_INTERVAL (V_NAME (erff), 0, inf, 40000)
116 #endif
117