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