xref: /freebsd/contrib/arm-optimized-routines/math/aarch64/experimental/erfinvf_4u7.c (revision f3087bef11543b42e0d69b708f367097a4118d24)
1*f3087befSAndrew Turner /*
2*f3087befSAndrew Turner  * Single-precision inverse error 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 #include "poly_scalar_f32.h"
8*f3087befSAndrew Turner #include "math_config.h"
9*f3087befSAndrew Turner #include "test_sig.h"
10*f3087befSAndrew Turner #include "test_defs.h"
11*f3087befSAndrew Turner 
12*f3087befSAndrew Turner const static struct
13*f3087befSAndrew Turner {
14*f3087befSAndrew Turner   /*  We use P_N and Q_N to refer to arrays of coefficients, where P_N is the
15*f3087befSAndrew Turner       coeffs of the numerator in table N of Blair et al, and Q_N is the coeffs
16*f3087befSAndrew Turner       of the denominator.  */
17*f3087befSAndrew Turner   float P_10[3], Q_10[4], P_29[4], Q_29[4], P_50[6], Q_50[3];
18*f3087befSAndrew Turner } data = { .P_10 = { -0x1.a31268p+3, 0x1.ac9048p+4, -0x1.293ff6p+3 },
19*f3087befSAndrew Turner 	   .Q_10 = { -0x1.8265eep+3, 0x1.ef5eaep+4, -0x1.12665p+4, 0x1p+0 },
20*f3087befSAndrew Turner 	   .P_29
21*f3087befSAndrew Turner 	   = { -0x1.fc0252p-4, 0x1.119d44p+0, -0x1.f59ee2p+0, 0x1.b13626p-2 },
22*f3087befSAndrew Turner 	   .Q_29 = { -0x1.69952p-4, 0x1.c7b7d2p-1, -0x1.167d7p+1, 0x1p+0 },
23*f3087befSAndrew Turner 	   .P_50 = { 0x1.3d8948p-3, 0x1.61f9eap+0, 0x1.61c6bcp-1,
24*f3087befSAndrew Turner 		     -0x1.20c9f2p+0, 0x1.5c704cp-1, -0x1.50c6bep-3 },
25*f3087befSAndrew Turner 	   .Q_50 = { 0x1.3d7dacp-3, 0x1.629e5p+0, 0x1p+0 } };
26*f3087befSAndrew Turner 
27*f3087befSAndrew Turner /* Inverse error function approximation, based on rational approximation as
28*f3087befSAndrew Turner    described in
29*f3087befSAndrew Turner    J. M. Blair, C. A. Edwards, and J. H. Johnson,
30*f3087befSAndrew Turner    "Rational Chebyshev approximations for the inverse of the error function",
31*f3087befSAndrew Turner    Math. Comp. 30, pp. 827--830 (1976).
32*f3087befSAndrew Turner    https://doi.org/10.1090/S0025-5718-1976-0421040-7
33*f3087befSAndrew Turner    Largest error is 4.71 ULP, in the tail region:
34*f3087befSAndrew Turner    erfinvf(0x1.f84e9ap-1) got 0x1.b8326ap+0
35*f3087befSAndrew Turner 			 want 0x1.b83274p+0.  */
36*f3087befSAndrew Turner float
erfinvf(float x)37*f3087befSAndrew Turner erfinvf (float x)
38*f3087befSAndrew Turner {
39*f3087befSAndrew Turner   if (x == 1.0f)
40*f3087befSAndrew Turner     return __math_oflowf (0);
41*f3087befSAndrew Turner   if (x == -1.0f)
42*f3087befSAndrew Turner     return __math_oflowf (1);
43*f3087befSAndrew Turner 
44*f3087befSAndrew Turner   float a = fabsf (x);
45*f3087befSAndrew Turner   if (a > 1.0f)
46*f3087befSAndrew Turner     return __math_invalidf (x);
47*f3087befSAndrew Turner 
48*f3087befSAndrew Turner   if (a <= 0.75f)
49*f3087befSAndrew Turner     {
50*f3087befSAndrew Turner       /* Greatest error in this region is 4.60 ULP:
51*f3087befSAndrew Turner 	 erfinvf(0x1.0a98bap-5) got 0x1.d8a93ep-6
52*f3087befSAndrew Turner 			       want 0x1.d8a948p-6.  */
53*f3087befSAndrew Turner       float t = x * x - 0.5625f;
54*f3087befSAndrew Turner       return x * horner_2_f32 (t, data.P_10) / horner_3_f32 (t, data.Q_10);
55*f3087befSAndrew Turner     }
56*f3087befSAndrew Turner   if (a < 0.9375f)
57*f3087befSAndrew Turner     {
58*f3087befSAndrew Turner       /* Greatest error in this region is 3.79 ULP:
59*f3087befSAndrew Turner 	 erfinvf(0x1.ac82d6p-1) got 0x1.f8fc54p-1
60*f3087befSAndrew Turner 			       want 0x1.f8fc5cp-1.  */
61*f3087befSAndrew Turner       float t = x * x - 0.87890625f;
62*f3087befSAndrew Turner       return x * horner_3_f32 (t, data.P_29) / horner_3_f32 (t, data.Q_29);
63*f3087befSAndrew Turner     }
64*f3087befSAndrew Turner 
65*f3087befSAndrew Turner   /* Tail region, where error is greatest (and sensitive to sqrt and log1p
66*f3087befSAndrew Turner      implementations.  */
67*f3087befSAndrew Turner   float t = 1.0 / sqrtf (-log1pf (-a));
68*f3087befSAndrew Turner   return horner_5_f32 (t, data.P_50)
69*f3087befSAndrew Turner 	 / (copysignf (t, x) * horner_2_f32 (t, data.Q_50));
70*f3087befSAndrew Turner }
71*f3087befSAndrew Turner 
72*f3087befSAndrew Turner #if USE_MPFR
73*f3087befSAndrew Turner # warning Not generating tests for erfinvf, as MPFR has no suitable reference
74*f3087befSAndrew Turner #else
75*f3087befSAndrew Turner TEST_SIG (S, F, 1, erfinv, -0.99, 0.99)
76*f3087befSAndrew Turner TEST_ULP (erfinvf, 4.09)
77*f3087befSAndrew Turner TEST_SYM_INTERVAL (erfinvf, 0, 1, 40000)
78*f3087befSAndrew Turner #endif
79