1*412f47f9SXin Li /*
2*412f47f9SXin Li * Single-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 uint32x4_t offset, table_scale;
15*412f47f9SXin Li float32x4_t max, shift;
16*412f47f9SXin Li float coeffs[4];
17*412f47f9SXin Li float32x4_t third, two_over_five, tenth;
18*412f47f9SXin Li #if WANT_SIMD_EXCEPT
19*412f47f9SXin Li float32x4_t uflow_bound;
20*412f47f9SXin Li #endif
21*412f47f9SXin Li
22*412f47f9SXin Li } data = {
23*412f47f9SXin Li /* Set an offset so the range of the index used for lookup is 644, and it can
24*412f47f9SXin Li be clamped using a saturated add. */
25*412f47f9SXin Li .offset = V4 (0xb7fffd7b), /* 0xffffffff - asuint(shift) - 644. */
26*412f47f9SXin Li .table_scale = V4 (0x28000000 << 1), /* asuint (2^-47) << 1. */
27*412f47f9SXin Li .max = V4 (10.0625f), /* 10 + 1/16 = 644/64. */
28*412f47f9SXin Li .shift = V4 (0x1p17f),
29*412f47f9SXin Li /* Store 1/3, 2/3 and 2/15 in a single register for use with indexed muls and
30*412f47f9SXin Li fmas. */
31*412f47f9SXin Li .coeffs = { 0x1.555556p-2f, 0x1.555556p-1f, 0x1.111112p-3f, 0 },
32*412f47f9SXin Li .third = V4 (0x1.555556p-2f),
33*412f47f9SXin Li .two_over_five = V4 (-0x1.99999ap-2f),
34*412f47f9SXin Li .tenth = V4 (-0x1.99999ap-4f),
35*412f47f9SXin Li #if WANT_SIMD_EXCEPT
36*412f47f9SXin Li .uflow_bound = V4 (0x1.2639cp+3f),
37*412f47f9SXin Li #endif
38*412f47f9SXin Li };
39*412f47f9SXin Li
40*412f47f9SXin Li #define TinyBound 0x41000000 /* 0x1p-62f << 1. */
41*412f47f9SXin Li #define Thres 0xbe000000 /* asuint(infinity) << 1 - TinyBound. */
42*412f47f9SXin Li #define Off 0xfffffd7b /* 0xffffffff - 644. */
43*412f47f9SXin Li
44*412f47f9SXin Li struct entry
45*412f47f9SXin Li {
46*412f47f9SXin Li float32x4_t erfc;
47*412f47f9SXin Li float32x4_t scale;
48*412f47f9SXin Li };
49*412f47f9SXin Li
50*412f47f9SXin Li static inline struct entry
lookup(uint32x4_t i)51*412f47f9SXin Li lookup (uint32x4_t i)
52*412f47f9SXin Li {
53*412f47f9SXin Li struct entry e;
54*412f47f9SXin Li float32x2_t t0
55*412f47f9SXin Li = vld1_f32 (&__erfcf_data.tab[vgetq_lane_u32 (i, 0) - Off].erfc);
56*412f47f9SXin Li float32x2_t t1
57*412f47f9SXin Li = vld1_f32 (&__erfcf_data.tab[vgetq_lane_u32 (i, 1) - Off].erfc);
58*412f47f9SXin Li float32x2_t t2
59*412f47f9SXin Li = vld1_f32 (&__erfcf_data.tab[vgetq_lane_u32 (i, 2) - Off].erfc);
60*412f47f9SXin Li float32x2_t t3
61*412f47f9SXin Li = vld1_f32 (&__erfcf_data.tab[vgetq_lane_u32 (i, 3) - Off].erfc);
62*412f47f9SXin Li float32x4_t e1 = vcombine_f32 (t0, t1);
63*412f47f9SXin Li float32x4_t e2 = vcombine_f32 (t2, t3);
64*412f47f9SXin Li e.erfc = vuzp1q_f32 (e1, e2);
65*412f47f9SXin Li e.scale = vuzp2q_f32 (e1, e2);
66*412f47f9SXin Li return e;
67*412f47f9SXin Li }
68*412f47f9SXin Li
69*412f47f9SXin Li #if WANT_SIMD_EXCEPT
70*412f47f9SXin Li static float32x4_t VPCS_ATTR NOINLINE
special_case(float32x4_t x,float32x4_t y,uint32x4_t cmp)71*412f47f9SXin Li special_case (float32x4_t x, float32x4_t y, uint32x4_t cmp)
72*412f47f9SXin Li {
73*412f47f9SXin Li return v_call_f32 (erfcf, x, y, cmp);
74*412f47f9SXin Li }
75*412f47f9SXin Li #endif
76*412f47f9SXin Li
77*412f47f9SXin Li /* Optimized single-precision vector erfcf(x).
78*412f47f9SXin Li Approximation based on series expansion near x rounded to
79*412f47f9SXin Li nearest multiple of 1/64.
80*412f47f9SXin Li Let d = x - r, and scale = 2 / sqrt(pi) * exp(-r^2). For x near r,
81*412f47f9SXin Li
82*412f47f9SXin Li erfc(x) ~ erfc(r) - scale * d * poly(r, d), with
83*412f47f9SXin Li
84*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
85*412f47f9SXin Li + (2/15 r^4 - 2/5 r^2 + 1/10) d^4
86*412f47f9SXin Li
87*412f47f9SXin Li Values of erfc(r) and scale are read from lookup tables. Stored values
88*412f47f9SXin Li are scaled to avoid hitting the subnormal range.
89*412f47f9SXin Li
90*412f47f9SXin Li Note that for x < 0, erfc(x) = 2.0 - erfc(-x).
91*412f47f9SXin Li Maximum error: 1.63 ULP (~1.0 ULP for x < 0.0).
92*412f47f9SXin Li _ZGVnN4v_erfcf(0x1.1dbf7ap+3) got 0x1.f51212p-120
93*412f47f9SXin Li want 0x1.f51216p-120. */
94*412f47f9SXin Li VPCS_ATTR
V_NAME_F1(erfc)95*412f47f9SXin Li float32x4_t V_NAME_F1 (erfc) (float32x4_t x)
96*412f47f9SXin Li {
97*412f47f9SXin Li const struct data *dat = ptr_barrier (&data);
98*412f47f9SXin Li
99*412f47f9SXin Li #if WANT_SIMD_EXCEPT
100*412f47f9SXin Li /* |x| < 2^-62. Avoid fabs by left-shifting by 1. */
101*412f47f9SXin Li uint32x4_t ix = vreinterpretq_u32_f32 (x);
102*412f47f9SXin Li uint32x4_t cmp = vcltq_u32 (vaddq_u32 (ix, ix), v_u32 (TinyBound));
103*412f47f9SXin Li /* x >= ~9.19 (into subnormal case and uflow case). Comparison is done in
104*412f47f9SXin Li integer domain to avoid raising exceptions in presence of nans. */
105*412f47f9SXin Li uint32x4_t uflow = vcgeq_s32 (vreinterpretq_s32_f32 (x),
106*412f47f9SXin Li vreinterpretq_s32_f32 (dat->uflow_bound));
107*412f47f9SXin Li cmp = vorrq_u32 (cmp, uflow);
108*412f47f9SXin Li float32x4_t xm = x;
109*412f47f9SXin Li /* If any lanes are special, mask them with 0 and retain a copy of x to allow
110*412f47f9SXin Li special case handler to fix special lanes later. This is only necessary if
111*412f47f9SXin Li fenv exceptions are to be triggered correctly. */
112*412f47f9SXin Li if (unlikely (v_any_u32 (cmp)))
113*412f47f9SXin Li x = v_zerofy_f32 (x, cmp);
114*412f47f9SXin Li #endif
115*412f47f9SXin Li
116*412f47f9SXin Li float32x4_t a = vabsq_f32 (x);
117*412f47f9SXin Li a = vminq_f32 (a, dat->max);
118*412f47f9SXin Li
119*412f47f9SXin Li /* Lookup erfc(r) and scale(r) in tables, e.g. set erfc(r) to 0 and scale to
120*412f47f9SXin Li 2/sqrt(pi), when x reduced to r = 0. */
121*412f47f9SXin Li float32x4_t shift = dat->shift;
122*412f47f9SXin Li float32x4_t z = vaddq_f32 (a, shift);
123*412f47f9SXin Li
124*412f47f9SXin Li /* Clamp index to a range of 644. A naive approach would use a subtract and
125*412f47f9SXin Li min. Instead we offset the table address and the index, then use a
126*412f47f9SXin Li saturating add. */
127*412f47f9SXin Li uint32x4_t i = vqaddq_u32 (vreinterpretq_u32_f32 (z), dat->offset);
128*412f47f9SXin Li
129*412f47f9SXin Li struct entry e = lookup (i);
130*412f47f9SXin Li
131*412f47f9SXin Li /* erfc(x) ~ erfc(r) - scale * d * poly(r, d). */
132*412f47f9SXin Li float32x4_t r = vsubq_f32 (z, shift);
133*412f47f9SXin Li float32x4_t d = vsubq_f32 (a, r);
134*412f47f9SXin Li float32x4_t d2 = vmulq_f32 (d, d);
135*412f47f9SXin Li float32x4_t r2 = vmulq_f32 (r, r);
136*412f47f9SXin Li
137*412f47f9SXin Li float32x4_t p1 = r;
138*412f47f9SXin Li float32x4_t coeffs = vld1q_f32 (dat->coeffs);
139*412f47f9SXin Li float32x4_t p2 = vfmsq_laneq_f32 (dat->third, r2, coeffs, 1);
140*412f47f9SXin Li float32x4_t p3
141*412f47f9SXin Li = vmulq_f32 (r, vfmaq_laneq_f32 (v_f32 (-0.5), r2, coeffs, 0));
142*412f47f9SXin Li float32x4_t p4 = vfmaq_laneq_f32 (dat->two_over_five, r2, coeffs, 2);
143*412f47f9SXin Li p4 = vfmsq_f32 (dat->tenth, r2, p4);
144*412f47f9SXin Li
145*412f47f9SXin Li float32x4_t y = vfmaq_f32 (p3, d, p4);
146*412f47f9SXin Li y = vfmaq_f32 (p2, d, y);
147*412f47f9SXin Li y = vfmaq_f32 (p1, d, y);
148*412f47f9SXin Li y = vfmsq_f32 (e.erfc, e.scale, vfmsq_f32 (d, d2, y));
149*412f47f9SXin Li
150*412f47f9SXin Li /* Offset equals 2.0f if sign, else 0.0f. */
151*412f47f9SXin Li uint32x4_t sign = vshrq_n_u32 (vreinterpretq_u32_f32 (x), 31);
152*412f47f9SXin Li float32x4_t off = vreinterpretq_f32_u32 (vshlq_n_u32 (sign, 30));
153*412f47f9SXin Li /* Copy sign and scale back in a single fma. Since the bit patterns do not
154*412f47f9SXin Li overlap, then logical or and addition are equivalent here. */
155*412f47f9SXin Li float32x4_t fac = vreinterpretq_f32_u32 (
156*412f47f9SXin Li vsraq_n_u32 (vshlq_n_u32 (sign, 31), dat->table_scale, 1));
157*412f47f9SXin Li
158*412f47f9SXin Li #if WANT_SIMD_EXCEPT
159*412f47f9SXin Li if (unlikely (v_any_u32 (cmp)))
160*412f47f9SXin Li return special_case (xm, vfmaq_f32 (off, fac, y), cmp);
161*412f47f9SXin Li #endif
162*412f47f9SXin Li
163*412f47f9SXin Li return vfmaq_f32 (off, fac, y);
164*412f47f9SXin Li }
165*412f47f9SXin Li
166*412f47f9SXin Li PL_SIG (V, F, 1, erfc, -4.0, 10.0)
167*412f47f9SXin Li PL_TEST_ULP (V_NAME_F1 (erfc), 1.14)
168*412f47f9SXin Li PL_TEST_SYM_INTERVAL (V_NAME_F1 (erfc), 0, 0x1p-26, 40000)
169*412f47f9SXin Li PL_TEST_INTERVAL (V_NAME_F1 (erfc), 0x1p-26, 10.0625, 40000)
170*412f47f9SXin Li PL_TEST_INTERVAL (V_NAME_F1 (erfc), -0x1p-26, -4.0, 40000)
171*412f47f9SXin Li PL_TEST_INTERVAL (V_NAME_F1 (erfc), 10.0625, inf, 40000)
172*412f47f9SXin Li PL_TEST_INTERVAL (V_NAME_F1 (erfc), -4.0, -inf, 40000)
173