xref: /aosp_15_r20/external/arm-optimized-routines/pl/math/sv_erfcf_1u7.c (revision 412f47f9e737e10ed5cc46ec6a8d7fa2264f8a14)
1*412f47f9SXin Li /*
2*412f47f9SXin Li  * Single-precision vector erfc(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   uint32_t off_idx, off_arr;
15*412f47f9SXin Li   float max, shift;
16*412f47f9SXin Li   float third, two_thirds, two_over_fifteen, two_over_five, tenth;
17*412f47f9SXin Li } data = {
18*412f47f9SXin Li   /* Set an offset so the range of the index used for lookup is 644, and it can
19*412f47f9SXin Li      be clamped using a saturated add.  */
20*412f47f9SXin Li   .off_idx = 0xb7fffd7b, /* 0xffffffff - asuint(shift) - 644.  */
21*412f47f9SXin Li   .off_arr = 0xfffffd7b, /* 0xffffffff - 644.  */
22*412f47f9SXin Li   .max = 10.0625f,	 /* 644/64.  */
23*412f47f9SXin Li   .shift = 0x1p17f,
24*412f47f9SXin Li   .third = 0x1.555556p-2f,
25*412f47f9SXin Li   .two_thirds = 0x1.555556p-1f,
26*412f47f9SXin Li   .two_over_fifteen = 0x1.111112p-3f,
27*412f47f9SXin Li   .two_over_five = -0x1.99999ap-2f,
28*412f47f9SXin Li   .tenth = -0x1.99999ap-4f,
29*412f47f9SXin Li };
30*412f47f9SXin Li 
31*412f47f9SXin Li #define SignMask 0x80000000
32*412f47f9SXin Li #define TableScale 0x28000000 /* 0x1p-47.  */
33*412f47f9SXin Li 
34*412f47f9SXin Li /* Optimized single-precision vector erfcf(x).
35*412f47f9SXin Li    Approximation based on series expansion near x rounded to
36*412f47f9SXin Li    nearest multiple of 1/64.
37*412f47f9SXin Li    Let d = x - r, and scale = 2 / sqrt(pi) * exp(-r^2). For x near r,
38*412f47f9SXin Li 
39*412f47f9SXin Li    erfc(x) ~ erfc(r) - scale * d * poly(r, d), with
40*412f47f9SXin Li 
41*412f47f9SXin Li    poly(r, d) = 1 - r d + (2/3 r^2 - 1/3) d^2 - r (1/3 r^2 - 1/2) d^3
42*412f47f9SXin Li 		+ (2/15 r^4 - 2/5 r^2 + 1/10) d^4
43*412f47f9SXin Li 
44*412f47f9SXin Li    Values of erfc(r) and scale are read from lookup tables. Stored values
45*412f47f9SXin Li    are scaled to avoid hitting the subnormal range.
46*412f47f9SXin Li 
47*412f47f9SXin Li    Note that for x < 0, erfc(x) = 2.0 - erfc(-x).
48*412f47f9SXin Li 
49*412f47f9SXin Li    Maximum error: 1.63 ULP (~1.0 ULP for x < 0.0).
50*412f47f9SXin Li    _ZGVsMxv_erfcf(0x1.1dbf7ap+3) got 0x1.f51212p-120
51*412f47f9SXin Li 				want 0x1.f51216p-120.  */
SV_NAME_F1(erfc)52*412f47f9SXin Li svfloat32_t SV_NAME_F1 (erfc) (svfloat32_t x, const svbool_t pg)
53*412f47f9SXin Li {
54*412f47f9SXin Li   const struct data *dat = ptr_barrier (&data);
55*412f47f9SXin Li 
56*412f47f9SXin Li   svfloat32_t a = svabs_x (pg, x);
57*412f47f9SXin Li 
58*412f47f9SXin Li   /* Clamp input at |x| <= 10.0 + 4/64.  */
59*412f47f9SXin Li   a = svmin_x (pg, a, dat->max);
60*412f47f9SXin Li 
61*412f47f9SXin Li   /* Reduce x to the nearest multiple of 1/64.  */
62*412f47f9SXin Li   svfloat32_t shift = sv_f32 (dat->shift);
63*412f47f9SXin Li   svfloat32_t z = svadd_x (pg, a, shift);
64*412f47f9SXin Li 
65*412f47f9SXin Li   /* Saturate index for the NaN case.  */
66*412f47f9SXin Li   svuint32_t i = svqadd (svreinterpret_u32 (z), dat->off_idx);
67*412f47f9SXin Li 
68*412f47f9SXin Li   /* Lookup erfc(r) and 2/sqrt(pi)*exp(-r^2) in tables.  */
69*412f47f9SXin Li   i = svmul_x (pg, i, 2);
70*412f47f9SXin Li   const float32_t *p = &__erfcf_data.tab[0].erfc - 2 * dat->off_arr;
71*412f47f9SXin Li   svfloat32_t erfcr = svld1_gather_index (pg, p, i);
72*412f47f9SXin Li   svfloat32_t scale = svld1_gather_index (pg, p + 1, i);
73*412f47f9SXin Li 
74*412f47f9SXin Li   /* erfc(x) ~ erfc(r) - scale * d * poly(r, d).  */
75*412f47f9SXin Li   svfloat32_t r = svsub_x (pg, z, shift);
76*412f47f9SXin Li   svfloat32_t d = svsub_x (pg, a, r);
77*412f47f9SXin Li   svfloat32_t d2 = svmul_x (pg, d, d);
78*412f47f9SXin Li   svfloat32_t r2 = svmul_x (pg, r, r);
79*412f47f9SXin Li 
80*412f47f9SXin Li   svfloat32_t coeffs = svld1rq (svptrue_b32 (), &dat->third);
81*412f47f9SXin Li   svfloat32_t third = svdup_lane (coeffs, 0);
82*412f47f9SXin Li 
83*412f47f9SXin Li   svfloat32_t p1 = r;
84*412f47f9SXin Li   svfloat32_t p2 = svmls_lane (third, r2, coeffs, 1);
85*412f47f9SXin Li   svfloat32_t p3 = svmul_x (pg, r, svmla_lane (sv_f32 (-0.5), r2, coeffs, 0));
86*412f47f9SXin Li   svfloat32_t p4 = svmla_lane (sv_f32 (dat->two_over_five), r2, coeffs, 2);
87*412f47f9SXin Li   p4 = svmls_x (pg, sv_f32 (dat->tenth), r2, p4);
88*412f47f9SXin Li 
89*412f47f9SXin Li   svfloat32_t y = svmla_x (pg, p3, d, p4);
90*412f47f9SXin Li   y = svmla_x (pg, p2, d, y);
91*412f47f9SXin Li   y = svmla_x (pg, p1, d, y);
92*412f47f9SXin Li 
93*412f47f9SXin Li   /* Solves the |x| = inf/nan case.  */
94*412f47f9SXin Li   y = svmls_x (pg, erfcr, scale, svmls_x (pg, d, d2, y));
95*412f47f9SXin Li 
96*412f47f9SXin Li   /* Offset equals 2.0f if sign, else 0.0f.  */
97*412f47f9SXin Li   svuint32_t sign = svand_x (pg, svreinterpret_u32 (x), SignMask);
98*412f47f9SXin Li   svfloat32_t off = svreinterpret_f32 (svlsr_x (pg, sign, 1));
99*412f47f9SXin Li   /* Handle sign and scale back in a single fma.  */
100*412f47f9SXin Li   svfloat32_t fac = svreinterpret_f32 (svorr_x (pg, sign, TableScale));
101*412f47f9SXin Li 
102*412f47f9SXin Li   return svmla_x (pg, off, fac, y);
103*412f47f9SXin Li }
104*412f47f9SXin Li 
105*412f47f9SXin Li PL_SIG (SV, F, 1, erfc, -4.0, 10.0)
106*412f47f9SXin Li PL_TEST_ULP (SV_NAME_F1 (erfc), 1.14)
107*412f47f9SXin Li PL_TEST_SYM_INTERVAL (SV_NAME_F1 (erfc), 0.0, 0x1p-26, 40000)
108*412f47f9SXin Li PL_TEST_INTERVAL (SV_NAME_F1 (erfc), 0x1p-26, 10.0625, 40000)
109*412f47f9SXin Li PL_TEST_INTERVAL (SV_NAME_F1 (erfc), -0x1p-26, -4.0, 40000)
110*412f47f9SXin Li PL_TEST_INTERVAL (SV_NAME_F1 (erfc), 10.0625, inf, 40000)
111*412f47f9SXin Li PL_TEST_INTERVAL (SV_NAME_F1 (erfc), -4.0, -inf, 40000)
112