xref: /freebsd/contrib/arm-optimized-routines/math/aarch64/advsimd/erfc.c (revision f3087bef11543b42e0d69b708f367097a4118d24)
1*f3087befSAndrew Turner /*
2*f3087befSAndrew Turner  * Double-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 "v_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   uint64x2_t offset, table_scale;
15*f3087befSAndrew Turner   float64x2_t max, shift;
16*f3087befSAndrew Turner   float64x2_t p20, p40, p41, p51;
17*f3087befSAndrew Turner   double p42, p52;
18*f3087befSAndrew Turner   double qr5[2], qr6[2], qr7[2], qr8[2], qr9[2];
19*f3087befSAndrew Turner #if WANT_SIMD_EXCEPT
20*f3087befSAndrew Turner   float64x2_t uflow_bound;
21*f3087befSAndrew Turner #endif
22*f3087befSAndrew Turner } data = {
23*f3087befSAndrew Turner   /* Set an offset so the range of the index used for lookup is 3487, and it
24*f3087befSAndrew Turner      can be clamped using a saturated add on an offset index.
25*f3087befSAndrew Turner      Index offset is 0xffffffffffffffff - asuint64(shift) - 3487.  */
26*f3087befSAndrew Turner   .offset = V2 (0xbd3ffffffffff260),
27*f3087befSAndrew Turner   .table_scale = V2 (0x37f0000000000000 << 1), /* asuint64 (2^-128) << 1.  */
28*f3087befSAndrew Turner   .max = V2 (0x1.b3ep+4),		       /* 3487/128.  */
29*f3087befSAndrew Turner   .shift = V2 (0x1p45),
30*f3087befSAndrew Turner   .p20 = V2 (0x1.5555555555555p-2),  /* 1/3, used to compute 2/3 and 1/6.  */
31*f3087befSAndrew Turner   .p40 = V2 (-0x1.999999999999ap-4), /* 1/10.  */
32*f3087befSAndrew Turner   .p41 = V2 (-0x1.999999999999ap-2), /* 2/5.  */
33*f3087befSAndrew Turner   .p42 = 0x1.1111111111111p-3,	     /* 2/15.  */
34*f3087befSAndrew Turner   .p51 = V2 (-0x1.c71c71c71c71cp-3), /* 2/9.  */
35*f3087befSAndrew Turner   .p52 = 0x1.6c16c16c16c17p-5,	     /* 2/45.  */
36*f3087befSAndrew Turner   /* Qi = (i+1) / i, Ri = -2 * i / ((i+1)*(i+2)), for i = 5, ..., 9.  */
37*f3087befSAndrew Turner   .qr5 = { 0x1.3333333333333p0, -0x1.e79e79e79e79ep-3 },
38*f3087befSAndrew Turner   .qr6 = { 0x1.2aaaaaaaaaaabp0, -0x1.b6db6db6db6dbp-3 },
39*f3087befSAndrew Turner   .qr7 = { 0x1.2492492492492p0, -0x1.8e38e38e38e39p-3 },
40*f3087befSAndrew Turner   .qr8 = { 0x1.2p0, -0x1.6c16c16c16c17p-3 },
41*f3087befSAndrew Turner   .qr9 = { 0x1.1c71c71c71c72p0, -0x1.4f2094f2094f2p-3 },
42*f3087befSAndrew Turner #if WANT_SIMD_EXCEPT
43*f3087befSAndrew Turner   .uflow_bound = V2 (0x1.a8b12fc6e4892p+4),
44*f3087befSAndrew Turner #endif
45*f3087befSAndrew Turner };
46*f3087befSAndrew Turner 
47*f3087befSAndrew Turner #define TinyBound 0x4000000000000000 /* 0x1p-511 << 1.  */
48*f3087befSAndrew Turner #define Off 0xfffffffffffff260	     /* 0xffffffffffffffff - 3487.  */
49*f3087befSAndrew Turner 
50*f3087befSAndrew Turner struct entry
51*f3087befSAndrew Turner {
52*f3087befSAndrew Turner   float64x2_t erfc;
53*f3087befSAndrew Turner   float64x2_t scale;
54*f3087befSAndrew Turner };
55*f3087befSAndrew Turner 
56*f3087befSAndrew Turner static inline struct entry
lookup(uint64x2_t i)57*f3087befSAndrew Turner lookup (uint64x2_t i)
58*f3087befSAndrew Turner {
59*f3087befSAndrew Turner   struct entry e;
60*f3087befSAndrew Turner   float64x2_t e1
61*f3087befSAndrew Turner       = vld1q_f64 (&__v_erfc_data.tab[vgetq_lane_u64 (i, 0) - Off].erfc);
62*f3087befSAndrew Turner   float64x2_t e2
63*f3087befSAndrew Turner       = vld1q_f64 (&__v_erfc_data.tab[vgetq_lane_u64 (i, 1) - Off].erfc);
64*f3087befSAndrew Turner   e.erfc = vuzp1q_f64 (e1, e2);
65*f3087befSAndrew Turner   e.scale = vuzp2q_f64 (e1, e2);
66*f3087befSAndrew Turner   return e;
67*f3087befSAndrew Turner }
68*f3087befSAndrew Turner 
69*f3087befSAndrew Turner #if WANT_SIMD_EXCEPT
70*f3087befSAndrew Turner static float64x2_t VPCS_ATTR NOINLINE
special_case(float64x2_t x,float64x2_t y,uint64x2_t cmp)71*f3087befSAndrew Turner special_case (float64x2_t x, float64x2_t y, uint64x2_t cmp)
72*f3087befSAndrew Turner {
73*f3087befSAndrew Turner   return v_call_f64 (erfc, x, y, cmp);
74*f3087befSAndrew Turner }
75*f3087befSAndrew Turner #endif
76*f3087befSAndrew Turner 
77*f3087befSAndrew Turner /* Optimized double-precision vector erfc(x).
78*f3087befSAndrew Turner    Approximation based on series expansion near x rounded to
79*f3087befSAndrew Turner    nearest multiple of 1/128.
80*f3087befSAndrew Turner 
81*f3087befSAndrew Turner    Let d = x - r, and scale = 2 / sqrt(pi) * exp(-r^2). For x near r,
82*f3087befSAndrew Turner 
83*f3087befSAndrew Turner    erfc(x) ~ erfc(r) - scale * d * poly(r, d), with
84*f3087befSAndrew Turner 
85*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
86*f3087befSAndrew Turner 		+ (2/15 r^4 - 2/5 r^2 + 1/10) d^4
87*f3087befSAndrew Turner 		- r * (2/45 r^4 - 2/9 r^2 + 1/6) d^5
88*f3087befSAndrew Turner 		+ p6(r) d^6 + ... + p10(r) d^10
89*f3087befSAndrew Turner 
90*f3087befSAndrew Turner    Polynomials p6(r) to p10(r) are computed using recurrence relation
91*f3087befSAndrew Turner 
92*f3087befSAndrew Turner    2(i+1)p_i + 2r(i+2)p_{i+1} + (i+2)(i+3)p_{i+2} = 0,
93*f3087befSAndrew Turner    with p0 = 1, and p1(r) = -r.
94*f3087befSAndrew Turner 
95*f3087befSAndrew Turner    Values of erfc(r) and scale are read from lookup tables. Stored values
96*f3087befSAndrew Turner    are scaled to avoid hitting the subnormal range.
97*f3087befSAndrew Turner 
98*f3087befSAndrew Turner    Note that for x < 0, erfc(x) = 2.0 - erfc(-x).
99*f3087befSAndrew Turner 
100*f3087befSAndrew Turner    Maximum measured error: 1.71 ULP
101*f3087befSAndrew Turner    V_NAME_D1 (erfc)(0x1.46cfe976733p+4) got 0x1.e15fcbea3e7afp-608
102*f3087befSAndrew Turner 				       want 0x1.e15fcbea3e7adp-608.  */
103*f3087befSAndrew Turner VPCS_ATTR
V_NAME_D1(erfc)104*f3087befSAndrew Turner float64x2_t V_NAME_D1 (erfc) (float64x2_t x)
105*f3087befSAndrew Turner {
106*f3087befSAndrew Turner   const struct data *dat = ptr_barrier (&data);
107*f3087befSAndrew Turner 
108*f3087befSAndrew Turner #if WANT_SIMD_EXCEPT
109*f3087befSAndrew Turner   /* |x| < 2^-511. Avoid fabs by left-shifting by 1.  */
110*f3087befSAndrew Turner   uint64x2_t ix = vreinterpretq_u64_f64 (x);
111*f3087befSAndrew Turner   uint64x2_t cmp = vcltq_u64 (vaddq_u64 (ix, ix), v_u64 (TinyBound));
112*f3087befSAndrew Turner   /* x >= ~26.54 (into subnormal case and uflow case). Comparison is done in
113*f3087befSAndrew Turner      integer domain to avoid raising exceptions in presence of nans.  */
114*f3087befSAndrew Turner   uint64x2_t uflow = vcgeq_s64 (vreinterpretq_s64_f64 (x),
115*f3087befSAndrew Turner 				vreinterpretq_s64_f64 (dat->uflow_bound));
116*f3087befSAndrew Turner   cmp = vorrq_u64 (cmp, uflow);
117*f3087befSAndrew Turner   float64x2_t xm = x;
118*f3087befSAndrew Turner   /* If any lanes are special, mask them with 0 and retain a copy of x to allow
119*f3087befSAndrew Turner      special case handler to fix special lanes later. This is only necessary if
120*f3087befSAndrew Turner      fenv exceptions are to be triggered correctly.  */
121*f3087befSAndrew Turner   if (unlikely (v_any_u64 (cmp)))
122*f3087befSAndrew Turner     x = v_zerofy_f64 (x, cmp);
123*f3087befSAndrew Turner #endif
124*f3087befSAndrew Turner 
125*f3087befSAndrew Turner   float64x2_t a = vabsq_f64 (x);
126*f3087befSAndrew Turner   a = vminq_f64 (a, dat->max);
127*f3087befSAndrew Turner 
128*f3087befSAndrew Turner   /* Lookup erfc(r) and scale(r) in tables, e.g. set erfc(r) to 0 and scale to
129*f3087befSAndrew Turner      2/sqrt(pi), when x reduced to r = 0.  */
130*f3087befSAndrew Turner   float64x2_t shift = dat->shift;
131*f3087befSAndrew Turner   float64x2_t z = vaddq_f64 (a, shift);
132*f3087befSAndrew Turner 
133*f3087befSAndrew Turner   /* Clamp index to a range of 3487. A naive approach would use a subtract and
134*f3087befSAndrew Turner      min. Instead we offset the table address and the index, then use a
135*f3087befSAndrew Turner      saturating add.  */
136*f3087befSAndrew Turner   uint64x2_t i = vqaddq_u64 (vreinterpretq_u64_f64 (z), dat->offset);
137*f3087befSAndrew Turner 
138*f3087befSAndrew Turner   struct entry e = lookup (i);
139*f3087befSAndrew Turner 
140*f3087befSAndrew Turner   /* erfc(x) ~ erfc(r) - scale * d * poly(r, d).  */
141*f3087befSAndrew Turner   float64x2_t r = vsubq_f64 (z, shift);
142*f3087befSAndrew Turner   float64x2_t d = vsubq_f64 (a, r);
143*f3087befSAndrew Turner   float64x2_t d2 = vmulq_f64 (d, d);
144*f3087befSAndrew Turner   float64x2_t r2 = vmulq_f64 (r, r);
145*f3087befSAndrew Turner 
146*f3087befSAndrew Turner   float64x2_t p1 = r;
147*f3087befSAndrew Turner   float64x2_t p2 = vfmsq_f64 (dat->p20, r2, vaddq_f64 (dat->p20, dat->p20));
148*f3087befSAndrew Turner   float64x2_t p3 = vmulq_f64 (r, vfmaq_f64 (v_f64 (-0.5), r2, dat->p20));
149*f3087befSAndrew Turner   float64x2_t p42_p52 = vld1q_f64 (&dat->p42);
150*f3087befSAndrew Turner   float64x2_t p4 = vfmaq_laneq_f64 (dat->p41, r2, p42_p52, 0);
151*f3087befSAndrew Turner   p4 = vfmsq_f64 (dat->p40, r2, p4);
152*f3087befSAndrew Turner   float64x2_t p5 = vfmaq_laneq_f64 (dat->p51, r2, p42_p52, 1);
153*f3087befSAndrew Turner   p5 = vmulq_f64 (r, vfmaq_f64 (vmulq_f64 (v_f64 (0.5), dat->p20), r2, p5));
154*f3087befSAndrew Turner   /* Compute p_i using recurrence relation:
155*f3087befSAndrew Turner      p_{i+2} = (p_i + r * Q_{i+1} * p_{i+1}) * R_{i+1}.  */
156*f3087befSAndrew Turner   float64x2_t qr5 = vld1q_f64 (dat->qr5), qr6 = vld1q_f64 (dat->qr6),
157*f3087befSAndrew Turner 	      qr7 = vld1q_f64 (dat->qr7), qr8 = vld1q_f64 (dat->qr8),
158*f3087befSAndrew Turner 	      qr9 = vld1q_f64 (dat->qr9);
159*f3087befSAndrew Turner   float64x2_t p6 = vfmaq_f64 (p4, p5, vmulq_laneq_f64 (r, qr5, 0));
160*f3087befSAndrew Turner   p6 = vmulq_laneq_f64 (p6, qr5, 1);
161*f3087befSAndrew Turner   float64x2_t p7 = vfmaq_f64 (p5, p6, vmulq_laneq_f64 (r, qr6, 0));
162*f3087befSAndrew Turner   p7 = vmulq_laneq_f64 (p7, qr6, 1);
163*f3087befSAndrew Turner   float64x2_t p8 = vfmaq_f64 (p6, p7, vmulq_laneq_f64 (r, qr7, 0));
164*f3087befSAndrew Turner   p8 = vmulq_laneq_f64 (p8, qr7, 1);
165*f3087befSAndrew Turner   float64x2_t p9 = vfmaq_f64 (p7, p8, vmulq_laneq_f64 (r, qr8, 0));
166*f3087befSAndrew Turner   p9 = vmulq_laneq_f64 (p9, qr8, 1);
167*f3087befSAndrew Turner   float64x2_t p10 = vfmaq_f64 (p8, p9, vmulq_laneq_f64 (r, qr9, 0));
168*f3087befSAndrew Turner   p10 = vmulq_laneq_f64 (p10, qr9, 1);
169*f3087befSAndrew Turner   /* Compute polynomial in d using pairwise Horner scheme.  */
170*f3087befSAndrew Turner   float64x2_t p90 = vfmaq_f64 (p9, d, p10);
171*f3087befSAndrew Turner   float64x2_t p78 = vfmaq_f64 (p7, d, p8);
172*f3087befSAndrew Turner   float64x2_t p56 = vfmaq_f64 (p5, d, p6);
173*f3087befSAndrew Turner   float64x2_t p34 = vfmaq_f64 (p3, d, p4);
174*f3087befSAndrew Turner   float64x2_t p12 = vfmaq_f64 (p1, d, p2);
175*f3087befSAndrew Turner   float64x2_t y = vfmaq_f64 (p78, d2, p90);
176*f3087befSAndrew Turner   y = vfmaq_f64 (p56, d2, y);
177*f3087befSAndrew Turner   y = vfmaq_f64 (p34, d2, y);
178*f3087befSAndrew Turner   y = vfmaq_f64 (p12, d2, y);
179*f3087befSAndrew Turner 
180*f3087befSAndrew Turner   y = vfmsq_f64 (e.erfc, e.scale, vfmsq_f64 (d, d2, y));
181*f3087befSAndrew Turner 
182*f3087befSAndrew Turner   /* Offset equals 2.0 if sign, else 0.0.  */
183*f3087befSAndrew Turner   uint64x2_t sign = vshrq_n_u64 (vreinterpretq_u64_f64 (x), 63);
184*f3087befSAndrew Turner   float64x2_t off = vreinterpretq_f64_u64 (vshlq_n_u64 (sign, 62));
185*f3087befSAndrew Turner   /* Copy sign and scale back in a single fma. Since the bit patterns do not
186*f3087befSAndrew Turner      overlap, then logical or and addition are equivalent here.  */
187*f3087befSAndrew Turner   float64x2_t fac = vreinterpretq_f64_u64 (
188*f3087befSAndrew Turner       vsraq_n_u64 (vshlq_n_u64 (sign, 63), dat->table_scale, 1));
189*f3087befSAndrew Turner 
190*f3087befSAndrew Turner #if WANT_SIMD_EXCEPT
191*f3087befSAndrew Turner   if (unlikely (v_any_u64 (cmp)))
192*f3087befSAndrew Turner     return special_case (xm, vfmaq_f64 (off, fac, y), cmp);
193*f3087befSAndrew Turner #endif
194*f3087befSAndrew Turner 
195*f3087befSAndrew Turner   return vfmaq_f64 (off, fac, y);
196*f3087befSAndrew Turner }
197*f3087befSAndrew Turner 
198*f3087befSAndrew Turner TEST_SIG (V, D, 1, erfc, -6.0, 28.0)
199*f3087befSAndrew Turner TEST_ULP (V_NAME_D1 (erfc), 1.21)
200*f3087befSAndrew Turner TEST_DISABLE_FENV_IF_NOT (V_NAME_D1 (erfc), WANT_SIMD_EXCEPT)
201*f3087befSAndrew Turner TEST_SYM_INTERVAL (V_NAME_D1 (erfc), 0, 0x1p-26, 40000)
202*f3087befSAndrew Turner TEST_INTERVAL (V_NAME_D1 (erfc), 0x1p-26, 28.0, 40000)
203*f3087befSAndrew Turner TEST_INTERVAL (V_NAME_D1 (erfc), -0x1p-26, -6.0, 40000)
204*f3087befSAndrew Turner TEST_INTERVAL (V_NAME_D1 (erfc), 28.0, inf, 40000)
205*f3087befSAndrew Turner TEST_INTERVAL (V_NAME_D1 (erfc), -6.0, -inf, 40000)
206