1*412f47f9SXin Li /*
2*412f47f9SXin Li * Double-precision inverse error function (SVE variant).
3*412f47f9SXin Li *
4*412f47f9SXin Li * Copyright (c) 2024, Arm Limited.
5*412f47f9SXin Li * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6*412f47f9SXin Li */
7*412f47f9SXin Li #include "sv_math.h"
8*412f47f9SXin Li #include "pl_test.h"
9*412f47f9SXin Li #include "math_config.h"
10*412f47f9SXin Li #include "pl_sig.h"
11*412f47f9SXin Li #include "poly_sve_f64.h"
12*412f47f9SXin Li #define SV_LOG_INLINE_POLY_ORDER 4
13*412f47f9SXin Li #include "sv_log_inline.h"
14*412f47f9SXin Li
15*412f47f9SXin Li const static struct data
16*412f47f9SXin Li {
17*412f47f9SXin Li /* We use P_N and Q_N to refer to arrays of coefficients, where P_N is the
18*412f47f9SXin Li coeffs of the numerator in table N of Blair et al, and Q_N is the coeffs
19*412f47f9SXin Li of the denominator. P is interleaved P_17 and P_37, similar for Q. */
20*412f47f9SXin Li double P[7][2], Q[7][2];
21*412f47f9SXin Li double P_57[9], Q_57[9], tailshift, P37_0;
22*412f47f9SXin Li struct sv_log_inline_data log_tbl;
23*412f47f9SXin Li } data = {
24*412f47f9SXin Li .P37_0 = -0x1.f3596123109edp-7,
25*412f47f9SXin Li .tailshift = -0.87890625,
26*412f47f9SXin Li .P = { { 0x1.007ce8f01b2e8p+4, 0x1.60b8fe375999ep-2 },
27*412f47f9SXin Li { -0x1.6b23cc5c6c6d7p+6, -0x1.779bb9bef7c0fp+1 },
28*412f47f9SXin Li { 0x1.74e5f6ceb3548p+7, 0x1.786ea384470a2p+3 },
29*412f47f9SXin Li { -0x1.5200bb15cc6bbp+7, -0x1.6a7c1453c85d3p+4 },
30*412f47f9SXin Li { 0x1.05d193233a849p+6, 0x1.31f0fc5613142p+4 },
31*412f47f9SXin Li { -0x1.148c5474ee5e1p+3, -0x1.5ea6c007d4dbbp+2 },
32*412f47f9SXin Li { 0x1.689181bbafd0cp-3, 0x1.e66f265ce9e5p-3 } },
33*412f47f9SXin Li .Q = { { 0x1.d8fb0f913bd7bp+3, -0x1.636b2dcf4edbep-7 },
34*412f47f9SXin Li { -0x1.6d7f25a3f1c24p+6, 0x1.0b5411e2acf29p-2 },
35*412f47f9SXin Li { 0x1.a450d8e7f4cbbp+7, -0x1.3413109467a0bp+1 },
36*412f47f9SXin Li { -0x1.bc3480485857p+7, 0x1.563e8136c554ap+3 },
37*412f47f9SXin Li { 0x1.ae6b0c504ee02p+6, -0x1.7b77aab1dcafbp+4 },
38*412f47f9SXin Li { -0x1.499dfec1a7f5fp+4, 0x1.8a3e174e05ddcp+4 },
39*412f47f9SXin Li { 0x1p+0, -0x1.4075c56404eecp+3 } },
40*412f47f9SXin Li .P_57 = { 0x1.b874f9516f7f1p-14, 0x1.5921f2916c1c4p-7, 0x1.145ae7d5b8fa4p-2,
41*412f47f9SXin Li 0x1.29d6dcc3b2fb7p+1, 0x1.cabe2209a7985p+2, 0x1.11859f0745c4p+3,
42*412f47f9SXin Li 0x1.b7ec7bc6a2ce5p+2, 0x1.d0419e0bb42aep+1, 0x1.c5aa03eef7258p-1 },
43*412f47f9SXin Li .Q_57 = { 0x1.b8747e12691f1p-14, 0x1.59240d8ed1e0ap-7, 0x1.14aef2b181e2p-2,
44*412f47f9SXin Li 0x1.2cd181bcea52p+1, 0x1.e6e63e0b7aa4cp+2, 0x1.65cf8da94aa3ap+3,
45*412f47f9SXin Li 0x1.7e5c787b10a36p+3, 0x1.0626d68b6cea3p+3, 0x1.065c5f193abf6p+2 },
46*412f47f9SXin Li .log_tbl = SV_LOG_CONSTANTS
47*412f47f9SXin Li };
48*412f47f9SXin Li
49*412f47f9SXin Li static inline svfloat64_t
special(svbool_t pg,svfloat64_t x,const struct data * d)50*412f47f9SXin Li special (svbool_t pg, svfloat64_t x, const struct data *d)
51*412f47f9SXin Li {
52*412f47f9SXin Li /* Note erfinv(inf) should return NaN, and erfinv(1) should return Inf.
53*412f47f9SXin Li By using log here, instead of log1p, we return finite values for both
54*412f47f9SXin Li these inputs, and values outside [-1, 1]. This is non-compliant, but is an
55*412f47f9SXin Li acceptable optimisation at Ofast. To get correct behaviour for all finite
56*412f47f9SXin Li values use the log1p_inline helper on -abs(x) - note that erfinv(inf)
57*412f47f9SXin Li will still be finite. */
58*412f47f9SXin Li svfloat64_t ax = svabs_x (pg, x);
59*412f47f9SXin Li svfloat64_t t
60*412f47f9SXin Li = svneg_x (pg, sv_log_inline (pg, svsubr_x (pg, ax, 1), &d->log_tbl));
61*412f47f9SXin Li t = svdivr_x (pg, svsqrt_x (pg, t), 1);
62*412f47f9SXin Li svuint64_t sign
63*412f47f9SXin Li = sveor_x (pg, svreinterpret_u64 (ax), svreinterpret_u64 (x));
64*412f47f9SXin Li svfloat64_t ts
65*412f47f9SXin Li = svreinterpret_f64 (svorr_x (pg, sign, svreinterpret_u64 (t)));
66*412f47f9SXin Li
67*412f47f9SXin Li svfloat64_t q = svadd_x (pg, t, d->Q_57[8]);
68*412f47f9SXin Li for (int i = 7; i >= 0; i--)
69*412f47f9SXin Li q = svmad_x (pg, q, t, d->Q_57[i]);
70*412f47f9SXin Li
71*412f47f9SXin Li return svdiv_x (pg, sv_horner_8_f64_x (pg, t, d->P_57), svmul_x (pg, ts, q));
72*412f47f9SXin Li }
73*412f47f9SXin Li
74*412f47f9SXin Li static inline svfloat64_t
lookup(const double * c,svuint64_t idx)75*412f47f9SXin Li lookup (const double *c, svuint64_t idx)
76*412f47f9SXin Li {
77*412f47f9SXin Li svfloat64_t x = svld1rq_f64 (svptrue_b64 (), c);
78*412f47f9SXin Li return svtbl (x, idx);
79*412f47f9SXin Li }
80*412f47f9SXin Li
81*412f47f9SXin Li static inline svfloat64_t
notails(svbool_t pg,svfloat64_t x,const struct data * d)82*412f47f9SXin Li notails (svbool_t pg, svfloat64_t x, const struct data *d)
83*412f47f9SXin Li {
84*412f47f9SXin Li svfloat64_t t = svmad_x (pg, x, x, -0.5625);
85*412f47f9SXin Li svfloat64_t p = svmla_x (pg, sv_f64 (d->P[5][0]), t, d->P[6][0]);
86*412f47f9SXin Li svfloat64_t q = svadd_x (pg, t, d->Q[5][0]);
87*412f47f9SXin Li for (int i = 4; i >= 0; i--)
88*412f47f9SXin Li {
89*412f47f9SXin Li p = svmad_x (pg, t, p, d->P[i][0]);
90*412f47f9SXin Li q = svmad_x (pg, t, q, d->Q[i][0]);
91*412f47f9SXin Li }
92*412f47f9SXin Li p = svmul_x (pg, p, x);
93*412f47f9SXin Li return svdiv_x (pg, p, q);
94*412f47f9SXin Li }
95*412f47f9SXin Li
96*412f47f9SXin Li /* Vector implementation of Blair et al's rational approximation to inverse
97*412f47f9SXin Li error function in double precision. Largest observed error is 24.75 ULP:
98*412f47f9SXin Li _ZGVsMxv_erfinv(0x1.fc861d81c2ba8p-1) got 0x1.ea05472686625p+0
99*412f47f9SXin Li want 0x1.ea0547268660cp+0. */
SV_NAME_D1(erfinv)100*412f47f9SXin Li svfloat64_t SV_NAME_D1 (erfinv) (svfloat64_t x, svbool_t pg)
101*412f47f9SXin Li {
102*412f47f9SXin Li const struct data *d = ptr_barrier (&data);
103*412f47f9SXin Li /* Calculate inverse error using algorithm described in
104*412f47f9SXin Li J. M. Blair, C. A. Edwards, and J. H. Johnson,
105*412f47f9SXin Li "Rational Chebyshev approximations for the inverse of the error function",
106*412f47f9SXin Li Math. Comp. 30, pp. 827--830 (1976).
107*412f47f9SXin Li https://doi.org/10.1090/S0025-5718-1976-0421040-7.
108*412f47f9SXin Li
109*412f47f9SXin Li Algorithm has 3 intervals:
110*412f47f9SXin Li - 'Normal' region [-0.75, 0.75]
111*412f47f9SXin Li - Tail region [0.75, 0.9375] U [-0.9375, -0.75]
112*412f47f9SXin Li - Extreme tail [-1, -0.9375] U [0.9375, 1]
113*412f47f9SXin Li Normal and tail are both rational approximation of similar order on
114*412f47f9SXin Li shifted input - these are typically performed in parallel using gather
115*412f47f9SXin Li loads to obtain correct coefficients depending on interval. */
116*412f47f9SXin Li
117*412f47f9SXin Li svbool_t no_tail = svacle (pg, x, 0.75);
118*412f47f9SXin Li if (unlikely (!svptest_any (pg, svnot_z (pg, no_tail))))
119*412f47f9SXin Li return notails (pg, x, d);
120*412f47f9SXin Li
121*412f47f9SXin Li svbool_t is_tail = svnot_z (pg, no_tail);
122*412f47f9SXin Li svbool_t extreme_tail = svacgt (pg, x, 0.9375);
123*412f47f9SXin Li svuint64_t idx = svdup_n_u64_z (is_tail, 1);
124*412f47f9SXin Li
125*412f47f9SXin Li svfloat64_t t = svsel_f64 (is_tail, sv_f64 (d->tailshift), sv_f64 (-0.5625));
126*412f47f9SXin Li t = svmla_x (pg, t, x, x);
127*412f47f9SXin Li
128*412f47f9SXin Li svfloat64_t p = lookup (&d->P[6][0], idx);
129*412f47f9SXin Li svfloat64_t q
130*412f47f9SXin Li = svmla_x (pg, lookup (&d->Q[6][0], idx), svdup_n_f64_z (is_tail, 1), t);
131*412f47f9SXin Li for (int i = 5; i >= 0; i--)
132*412f47f9SXin Li {
133*412f47f9SXin Li p = svmla_x (pg, lookup (&d->P[i][0], idx), p, t);
134*412f47f9SXin Li q = svmla_x (pg, lookup (&d->Q[i][0], idx), q, t);
135*412f47f9SXin Li }
136*412f47f9SXin Li p = svmad_m (is_tail, p, t, d->P37_0);
137*412f47f9SXin Li p = svmul_x (pg, p, x);
138*412f47f9SXin Li
139*412f47f9SXin Li if (likely (svptest_any (pg, extreme_tail)))
140*412f47f9SXin Li return svsel (extreme_tail, special (pg, x, d), svdiv_x (pg, p, q));
141*412f47f9SXin Li return svdiv_x (pg, p, q);
142*412f47f9SXin Li }
143*412f47f9SXin Li
144*412f47f9SXin Li PL_SIG (SV, D, 1, erfinv, -0.99, 0.99)
145*412f47f9SXin Li
146*412f47f9SXin Li PL_TEST_ULP (SV_NAME_D1 (erfinv), 24.5)
147*412f47f9SXin Li /* Test with control lane in each interval. */
148*412f47f9SXin Li #define TEST_INTERVAL(lo, hi, n) \
149*412f47f9SXin Li PL_TEST_INTERVAL_C (SV_NAME_D1 (erfinv), lo, hi, n, 0.5) \
150*412f47f9SXin Li PL_TEST_INTERVAL_C (SV_NAME_D1 (erfinv), lo, hi, n, 0.8) \
151*412f47f9SXin Li PL_TEST_INTERVAL_C (SV_NAME_D1 (erfinv), lo, hi, n, 0.95)
152*412f47f9SXin Li TEST_INTERVAL (0, 1, 100000)
153*412f47f9SXin Li TEST_INTERVAL (-0, -1, 100000)
154