1*412f47f9SXin Li /*
2*412f47f9SXin Li * Double-precision inverse error function (AdvSIMD variant).
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 #include "v_math.h"
8*412f47f9SXin Li #include "pl_test.h"
9*412f47f9SXin Li #include "mathlib.h"
10*412f47f9SXin Li #include "math_config.h"
11*412f47f9SXin Li #include "pl_sig.h"
12*412f47f9SXin Li #include "poly_advsimd_f64.h"
13*412f47f9SXin Li #define V_LOG_INLINE_POLY_ORDER 4
14*412f47f9SXin Li #include "v_log_inline.h"
15*412f47f9SXin Li
16*412f47f9SXin Li const static struct data
17*412f47f9SXin Li {
18*412f47f9SXin Li /* We use P_N and Q_N to refer to arrays of coefficients, where P_N is the
19*412f47f9SXin Li coeffs of the numerator in table N of Blair et al, and Q_N is the coeffs
20*412f47f9SXin Li of the denominator. P is interleaved P_17 and P_37, similar for Q. P17
21*412f47f9SXin Li and Q17 are provided as homogenous vectors as well for when the shortcut
22*412f47f9SXin Li can be taken. */
23*412f47f9SXin Li double P[8][2], Q[7][2];
24*412f47f9SXin Li float64x2_t tailshift;
25*412f47f9SXin Li uint8_t idx[16];
26*412f47f9SXin Li struct v_log_inline_data log_tbl;
27*412f47f9SXin Li float64x2_t P_57[9], Q_57[10], P_17[7], Q_17[6];
28*412f47f9SXin Li } data = { .P = { { 0x1.007ce8f01b2e8p+4, -0x1.f3596123109edp-7 },
29*412f47f9SXin Li { -0x1.6b23cc5c6c6d7p+6, 0x1.60b8fe375999ep-2 },
30*412f47f9SXin Li { 0x1.74e5f6ceb3548p+7, -0x1.779bb9bef7c0fp+1 },
31*412f47f9SXin Li { -0x1.5200bb15cc6bbp+7, 0x1.786ea384470a2p+3 },
32*412f47f9SXin Li { 0x1.05d193233a849p+6, -0x1.6a7c1453c85d3p+4 },
33*412f47f9SXin Li { -0x1.148c5474ee5e1p+3, 0x1.31f0fc5613142p+4 },
34*412f47f9SXin Li { 0x1.689181bbafd0cp-3, -0x1.5ea6c007d4dbbp+2 },
35*412f47f9SXin Li { 0, 0x1.e66f265ce9e5p-3 } },
36*412f47f9SXin Li .Q = { { 0x1.d8fb0f913bd7bp+3, -0x1.636b2dcf4edbep-7 },
37*412f47f9SXin Li { -0x1.6d7f25a3f1c24p+6, 0x1.0b5411e2acf29p-2 },
38*412f47f9SXin Li { 0x1.a450d8e7f4cbbp+7, -0x1.3413109467a0bp+1 },
39*412f47f9SXin Li { -0x1.bc3480485857p+7, 0x1.563e8136c554ap+3 },
40*412f47f9SXin Li { 0x1.ae6b0c504ee02p+6, -0x1.7b77aab1dcafbp+4 },
41*412f47f9SXin Li { -0x1.499dfec1a7f5fp+4, 0x1.8a3e174e05ddcp+4 },
42*412f47f9SXin Li { 0x1p+0, -0x1.4075c56404eecp+3 } },
43*412f47f9SXin Li .P_57 = { V2 (0x1.b874f9516f7f1p-14), V2 (0x1.5921f2916c1c4p-7),
44*412f47f9SXin Li V2 (0x1.145ae7d5b8fa4p-2), V2 (0x1.29d6dcc3b2fb7p+1),
45*412f47f9SXin Li V2 (0x1.cabe2209a7985p+2), V2 (0x1.11859f0745c4p+3),
46*412f47f9SXin Li V2 (0x1.b7ec7bc6a2ce5p+2), V2 (0x1.d0419e0bb42aep+1),
47*412f47f9SXin Li V2 (0x1.c5aa03eef7258p-1) },
48*412f47f9SXin Li .Q_57 = { V2 (0x1.b8747e12691f1p-14), V2 (0x1.59240d8ed1e0ap-7),
49*412f47f9SXin Li V2 (0x1.14aef2b181e2p-2), V2 (0x1.2cd181bcea52p+1),
50*412f47f9SXin Li V2 (0x1.e6e63e0b7aa4cp+2), V2 (0x1.65cf8da94aa3ap+3),
51*412f47f9SXin Li V2 (0x1.7e5c787b10a36p+3), V2 (0x1.0626d68b6cea3p+3),
52*412f47f9SXin Li V2 (0x1.065c5f193abf6p+2), V2 (0x1p+0) },
53*412f47f9SXin Li .P_17 = { V2 (0x1.007ce8f01b2e8p+4), V2 (-0x1.6b23cc5c6c6d7p+6),
54*412f47f9SXin Li V2 (0x1.74e5f6ceb3548p+7), V2 (-0x1.5200bb15cc6bbp+7),
55*412f47f9SXin Li V2 (0x1.05d193233a849p+6), V2 (-0x1.148c5474ee5e1p+3),
56*412f47f9SXin Li V2 (0x1.689181bbafd0cp-3) },
57*412f47f9SXin Li .Q_17 = { V2 (0x1.d8fb0f913bd7bp+3), V2 (-0x1.6d7f25a3f1c24p+6),
58*412f47f9SXin Li V2 (0x1.a450d8e7f4cbbp+7), V2 (-0x1.bc3480485857p+7),
59*412f47f9SXin Li V2 (0x1.ae6b0c504ee02p+6), V2 (-0x1.499dfec1a7f5fp+4) },
60*412f47f9SXin Li .tailshift = V2 (-0.87890625),
61*412f47f9SXin Li .idx = { 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7 },
62*412f47f9SXin Li .log_tbl = V_LOG_CONSTANTS };
63*412f47f9SXin Li
64*412f47f9SXin Li static inline float64x2_t
special(float64x2_t x,const struct data * d)65*412f47f9SXin Li special (float64x2_t x, const struct data *d)
66*412f47f9SXin Li {
67*412f47f9SXin Li /* Note erfinv(inf) should return NaN, and erfinv(1) should return Inf.
68*412f47f9SXin Li By using log here, instead of log1p, we return finite values for both
69*412f47f9SXin Li these inputs, and values outside [-1, 1]. This is non-compliant, but is an
70*412f47f9SXin Li acceptable optimisation at Ofast. To get correct behaviour for all finite
71*412f47f9SXin Li values use the log1p_inline helper on -abs(x) - note that erfinv(inf)
72*412f47f9SXin Li will still be finite. */
73*412f47f9SXin Li float64x2_t t = vnegq_f64 (
74*412f47f9SXin Li v_log_inline (vsubq_f64 (v_f64 (1), vabsq_f64 (x)), &d->log_tbl));
75*412f47f9SXin Li t = vdivq_f64 (v_f64 (1), vsqrtq_f64 (t));
76*412f47f9SXin Li float64x2_t ts = vbslq_f64 (v_u64 (0x7fffffffffffffff), t, x);
77*412f47f9SXin Li return vdivq_f64 (v_horner_8_f64 (t, d->P_57),
78*412f47f9SXin Li vmulq_f64 (ts, v_horner_9_f64 (t, d->Q_57)));
79*412f47f9SXin Li }
80*412f47f9SXin Li
81*412f47f9SXin Li static inline float64x2_t
lookup(const double * c,uint8x16_t idx)82*412f47f9SXin Li lookup (const double *c, uint8x16_t idx)
83*412f47f9SXin Li {
84*412f47f9SXin Li float64x2_t x = vld1q_f64 (c);
85*412f47f9SXin Li return vreinterpretq_f64_u8 (vqtbl1q_u8 (vreinterpretq_u8_f64 (x), idx));
86*412f47f9SXin Li }
87*412f47f9SXin Li
88*412f47f9SXin Li static inline float64x2_t VPCS_ATTR
notails(float64x2_t x,const struct data * d)89*412f47f9SXin Li notails (float64x2_t x, const struct data *d)
90*412f47f9SXin Li {
91*412f47f9SXin Li /* Shortcut when no input is in a tail region - no need to gather shift or
92*412f47f9SXin Li coefficients. */
93*412f47f9SXin Li float64x2_t t = vfmaq_f64 (v_f64 (-0.5625), x, x);
94*412f47f9SXin Li float64x2_t p = vmulq_f64 (v_horner_6_f64 (t, d->P_17), x);
95*412f47f9SXin Li float64x2_t q = vaddq_f64 (d->Q_17[5], t);
96*412f47f9SXin Li for (int i = 4; i >= 0; i--)
97*412f47f9SXin Li q = vfmaq_f64 (d->Q_17[i], q, t);
98*412f47f9SXin Li return vdivq_f64 (p, q);
99*412f47f9SXin Li }
100*412f47f9SXin Li
101*412f47f9SXin Li /* Vector implementation of Blair et al's rational approximation to inverse
102*412f47f9SXin Li error function in single-precision. Largest observed error is 24.75 ULP:
103*412f47f9SXin Li _ZGVnN2v_erfinv(0x1.fc861d81c2ba8p-1) got 0x1.ea05472686625p+0
104*412f47f9SXin Li want 0x1.ea0547268660cp+0. */
V_NAME_D1(erfinv)105*412f47f9SXin Li float64x2_t VPCS_ATTR V_NAME_D1 (erfinv) (float64x2_t x)
106*412f47f9SXin Li {
107*412f47f9SXin Li const struct data *d = ptr_barrier (&data);
108*412f47f9SXin Li /* Calculate inverse error using algorithm described in
109*412f47f9SXin Li J. M. Blair, C. A. Edwards, and J. H. Johnson,
110*412f47f9SXin Li "Rational Chebyshev approximations for the inverse of the error function",
111*412f47f9SXin Li Math. Comp. 30, pp. 827--830 (1976).
112*412f47f9SXin Li https://doi.org/10.1090/S0025-5718-1976-0421040-7.
113*412f47f9SXin Li
114*412f47f9SXin Li Algorithm has 3 intervals:
115*412f47f9SXin Li - 'Normal' region [-0.75, 0.75]
116*412f47f9SXin Li - Tail region [0.75, 0.9375] U [-0.9375, -0.75]
117*412f47f9SXin Li - Extreme tail [-1, -0.9375] U [0.9375, 1]
118*412f47f9SXin Li Normal and tail are both rational approximation of similar order on
119*412f47f9SXin Li shifted input - these are typically performed in parallel using gather
120*412f47f9SXin Li loads to obtain correct coefficients depending on interval. */
121*412f47f9SXin Li uint64x2_t is_tail = vcagtq_f64 (x, v_f64 (0.75));
122*412f47f9SXin Li
123*412f47f9SXin Li if (unlikely (!v_any_u64 (is_tail)))
124*412f47f9SXin Li /* If input is normally distributed in [-1, 1] then likelihood of this is
125*412f47f9SXin Li 0.75^2 ~= 0.56. */
126*412f47f9SXin Li return notails (x, d);
127*412f47f9SXin Li
128*412f47f9SXin Li uint64x2_t extreme_tail = vcagtq_f64 (x, v_f64 (0.9375));
129*412f47f9SXin Li
130*412f47f9SXin Li uint8x16_t off = vandq_u8 (vreinterpretq_u8_u64 (is_tail), vdupq_n_u8 (8));
131*412f47f9SXin Li uint8x16_t idx = vaddq_u8 (vld1q_u8 (d->idx), off);
132*412f47f9SXin Li
133*412f47f9SXin Li float64x2_t t = vbslq_f64 (is_tail, d->tailshift, v_f64 (-0.5625));
134*412f47f9SXin Li t = vfmaq_f64 (t, x, x);
135*412f47f9SXin Li
136*412f47f9SXin Li float64x2_t p = lookup (&d->P[7][0], idx);
137*412f47f9SXin Li /* Last coeff of q is either 0 or 1 - use mask instead of load. */
138*412f47f9SXin Li float64x2_t q = vreinterpretq_f64_u64 (
139*412f47f9SXin Li vandq_u64 (is_tail, vreinterpretq_u64_f64 (v_f64 (1))));
140*412f47f9SXin Li for (int i = 6; i >= 0; i--)
141*412f47f9SXin Li {
142*412f47f9SXin Li p = vfmaq_f64 (lookup (&d->P[i][0], idx), p, t);
143*412f47f9SXin Li q = vfmaq_f64 (lookup (&d->Q[i][0], idx), q, t);
144*412f47f9SXin Li }
145*412f47f9SXin Li p = vmulq_f64 (p, x);
146*412f47f9SXin Li
147*412f47f9SXin Li if (unlikely (v_any_u64 (extreme_tail)))
148*412f47f9SXin Li return vbslq_f64 (extreme_tail, special (x, d), vdivq_f64 (p, q));
149*412f47f9SXin Li
150*412f47f9SXin Li return vdivq_f64 (p, q);
151*412f47f9SXin Li }
152*412f47f9SXin Li
153*412f47f9SXin Li PL_SIG (V, D, 1, erfinv, -0.99, 0.99)
154*412f47f9SXin Li PL_TEST_ULP (V_NAME_D1 (erfinv), 24.8)
155*412f47f9SXin Li /* Test with control lane in each interval. */
156*412f47f9SXin Li PL_TEST_SYM_INTERVAL_C (V_NAME_D1 (erfinv), 0, 0x1.fffffffffffffp-1, 100000,
157*412f47f9SXin Li 0.5)
158*412f47f9SXin Li PL_TEST_SYM_INTERVAL_C (V_NAME_D1 (erfinv), 0, 0x1.fffffffffffffp-1, 100000,
159*412f47f9SXin Li 0.8)
160*412f47f9SXin Li PL_TEST_SYM_INTERVAL_C (V_NAME_D1 (erfinv), 0, 0x1.fffffffffffffp-1, 100000,
161*412f47f9SXin Li 0.95)
162