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