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