xref: /openbsd/lib/libm/src/ld128/s_erfl.c (revision 2f2c0062)
149393c00Smartynas /*
249393c00Smartynas  * ====================================================
349393c00Smartynas  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
449393c00Smartynas  *
549393c00Smartynas  * Developed at SunPro, a Sun Microsystems, Inc. business.
649393c00Smartynas  * Permission to use, copy, modify, and distribute this
749393c00Smartynas  * software is freely granted, provided that this notice
849393c00Smartynas  * is preserved.
949393c00Smartynas  * ====================================================
1049393c00Smartynas  */
1149393c00Smartynas 
1249393c00Smartynas /*
1349393c00Smartynas  * Copyright (c) 2008 Stephen L. Moshier <steve@moshier.net>
1449393c00Smartynas  *
1549393c00Smartynas  * Permission to use, copy, modify, and distribute this software for any
1649393c00Smartynas  * purpose with or without fee is hereby granted, provided that the above
1749393c00Smartynas  * copyright notice and this permission notice appear in all copies.
1849393c00Smartynas  *
1949393c00Smartynas  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
2049393c00Smartynas  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
2149393c00Smartynas  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
2249393c00Smartynas  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
2349393c00Smartynas  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
2449393c00Smartynas  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
2549393c00Smartynas  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2649393c00Smartynas  */
2749393c00Smartynas 
2849393c00Smartynas /* double erf(double x)
2949393c00Smartynas  * double erfc(double x)
3049393c00Smartynas  *			     x
3149393c00Smartynas  *		      2      |\
3249393c00Smartynas  *     erf(x)  =  ---------  | exp(-t*t)dt
3349393c00Smartynas  *		   sqrt(pi) \|
3449393c00Smartynas  *			     0
3549393c00Smartynas  *
3649393c00Smartynas  *     erfc(x) =  1-erf(x)
3749393c00Smartynas  *  Note that
3849393c00Smartynas  *		erf(-x) = -erf(x)
3949393c00Smartynas  *		erfc(-x) = 2 - erfc(x)
4049393c00Smartynas  *
4149393c00Smartynas  * Method:
4249393c00Smartynas  *	1.  erf(x)  = x + x*R(x^2) for |x| in [0, 7/8]
4349393c00Smartynas  *	   Remark. The formula is derived by noting
4449393c00Smartynas  *          erf(x) = (2/sqrt(pi))*(x - x^3/3 + x^5/10 - x^7/42 + ....)
4549393c00Smartynas  *	   and that
4649393c00Smartynas  *          2/sqrt(pi) = 1.128379167095512573896158903121545171688
4749393c00Smartynas  *	   is close to one.
4849393c00Smartynas  *
4949393c00Smartynas  *      1a. erf(x)  = 1 - erfc(x), for |x| > 1.0
5049393c00Smartynas  *          erfc(x) = 1 - erf(x)  if |x| < 1/4
5149393c00Smartynas  *
5249393c00Smartynas  *      2. For |x| in [7/8, 1], let s = |x| - 1, and
5349393c00Smartynas  *         c = 0.84506291151 rounded to single (24 bits)
5449393c00Smartynas  *	erf(s + c)  = sign(x) * (c  + P1(s)/Q1(s))
5549393c00Smartynas  *	   Remark: here we use the taylor series expansion at x=1.
5649393c00Smartynas  *		erf(1+s) = erf(1) + s*Poly(s)
5749393c00Smartynas  *			 = 0.845.. + P1(s)/Q1(s)
5849393c00Smartynas  *	   Note that |P1/Q1|< 0.078 for x in [0.84375,1.25]
5949393c00Smartynas  *
6049393c00Smartynas  *      3. For x in [1/4, 5/4],
6149393c00Smartynas  *	erfc(s + const)  = erfc(const)  + s P1(s)/Q1(s)
6249393c00Smartynas  *              for const = 1/4, 3/8, ..., 9/8
6349393c00Smartynas  *              and 0 <= s <= 1/8 .
6449393c00Smartynas  *
6549393c00Smartynas  *      4. For x in [5/4, 107],
6649393c00Smartynas  *	erfc(x) = (1/x)*exp(-x*x-0.5625 + R(z))
6749393c00Smartynas  *              z=1/x^2
6849393c00Smartynas  *         The interval is partitioned into several segments
6949393c00Smartynas  *         of width 1/8 in 1/x.
7049393c00Smartynas  *
7149393c00Smartynas  *      Note1:
7249393c00Smartynas  *	   To compute exp(-x*x-0.5625+R/S), let s be a single
7349393c00Smartynas  *	   precision number and s := x; then
7449393c00Smartynas  *		-x*x = -s*s + (s-x)*(s+x)
7549393c00Smartynas  *	        exp(-x*x-0.5626+R/S) =
7649393c00Smartynas  *			exp(-s*s-0.5625)*exp((s-x)*(s+x)+R/S);
7749393c00Smartynas  *      Note2:
7849393c00Smartynas  *	   Here 4 and 5 make use of the asymptotic series
7949393c00Smartynas  *			  exp(-x*x)
8049393c00Smartynas  *		erfc(x) ~ ---------- * ( 1 + Poly(1/x^2) )
8149393c00Smartynas  *			  x*sqrt(pi)
8249393c00Smartynas  *
8349393c00Smartynas  *      5. For inf > x >= 107
8449393c00Smartynas  *	erf(x)  = sign(x) *(1 - tiny)  (raise inexact)
8549393c00Smartynas  *	erfc(x) = tiny*tiny (raise underflow) if x > 0
8649393c00Smartynas  *			= 2 - tiny if x<0
8749393c00Smartynas  *
8849393c00Smartynas  *      7. Special case:
8949393c00Smartynas  *	erf(0)  = 0, erf(inf)  = 1, erf(-inf) = -1,
9049393c00Smartynas  *	erfc(0) = 1, erfc(inf) = 0, erfc(-inf) = 2,
9149393c00Smartynas  *		erfc/erf(NaN) is NaN
9249393c00Smartynas  */
9349393c00Smartynas 
9449393c00Smartynas #include <math.h>
9549393c00Smartynas 
9649393c00Smartynas #include "math_private.h"
9749393c00Smartynas 
9849393c00Smartynas /* Evaluate P[n] x^n  +  P[n-1] x^(n-1)  +  ...  +  P[0] */
9949393c00Smartynas 
10049393c00Smartynas static long double
neval(long double x,const long double * p,int n)10149393c00Smartynas neval (long double x, const long double *p, int n)
10249393c00Smartynas {
10349393c00Smartynas   long double y;
10449393c00Smartynas 
10549393c00Smartynas   p += n;
10649393c00Smartynas   y = *p--;
10749393c00Smartynas   do
10849393c00Smartynas     {
10949393c00Smartynas       y = y * x + *p--;
11049393c00Smartynas     }
11149393c00Smartynas   while (--n > 0);
11249393c00Smartynas   return y;
11349393c00Smartynas }
11449393c00Smartynas 
11549393c00Smartynas 
11649393c00Smartynas /* Evaluate x^n+1  +  P[n] x^(n)  +  P[n-1] x^(n-1)  +  ...  +  P[0] */
11749393c00Smartynas 
11849393c00Smartynas static long double
deval(long double x,const long double * p,int n)11949393c00Smartynas deval (long double x, const long double *p, int n)
12049393c00Smartynas {
12149393c00Smartynas   long double y;
12249393c00Smartynas 
12349393c00Smartynas   p += n;
12449393c00Smartynas   y = x + *p--;
12549393c00Smartynas   do
12649393c00Smartynas     {
12749393c00Smartynas       y = y * x + *p--;
12849393c00Smartynas     }
12949393c00Smartynas   while (--n > 0);
13049393c00Smartynas   return y;
13149393c00Smartynas }
13249393c00Smartynas 
13349393c00Smartynas 
13449393c00Smartynas 
13549393c00Smartynas static const long double
13649393c00Smartynas tiny = 1e-4931L,
13749393c00Smartynas   one = 1.0L,
13849393c00Smartynas   two = 2.0L,
13949393c00Smartynas   /* 2/sqrt(pi) - 1 */
14049393c00Smartynas   efx = 1.2837916709551257389615890312154517168810E-1L,
14149393c00Smartynas   /* 8 * (2/sqrt(pi) - 1) */
14249393c00Smartynas   efx8 = 1.0270333367641005911692712249723613735048E0L;
14349393c00Smartynas 
14449393c00Smartynas 
14549393c00Smartynas /* erf(x)  = x  + x R(x^2)
14649393c00Smartynas    0 <= x <= 7/8
14749393c00Smartynas    Peak relative error 1.8e-35  */
14849393c00Smartynas #define NTN1 8
14949393c00Smartynas static const long double TN1[NTN1 + 1] =
15049393c00Smartynas {
15149393c00Smartynas  -3.858252324254637124543172907442106422373E10L,
15249393c00Smartynas   9.580319248590464682316366876952214879858E10L,
15349393c00Smartynas   1.302170519734879977595901236693040544854E10L,
15449393c00Smartynas   2.922956950426397417800321486727032845006E9L,
15549393c00Smartynas   1.764317520783319397868923218385468729799E8L,
15649393c00Smartynas   1.573436014601118630105796794840834145120E7L,
15749393c00Smartynas   4.028077380105721388745632295157816229289E5L,
15849393c00Smartynas   1.644056806467289066852135096352853491530E4L,
15949393c00Smartynas   3.390868480059991640235675479463287886081E1L
16049393c00Smartynas };
16149393c00Smartynas #define NTD1 8
16249393c00Smartynas static const long double TD1[NTD1 + 1] =
16349393c00Smartynas {
16449393c00Smartynas   -3.005357030696532927149885530689529032152E11L,
16549393c00Smartynas   -1.342602283126282827411658673839982164042E11L,
16649393c00Smartynas   -2.777153893355340961288511024443668743399E10L,
16749393c00Smartynas   -3.483826391033531996955620074072768276974E9L,
16849393c00Smartynas   -2.906321047071299585682722511260895227921E8L,
16949393c00Smartynas   -1.653347985722154162439387878512427542691E7L,
17049393c00Smartynas   -6.245520581562848778466500301865173123136E5L,
17149393c00Smartynas   -1.402124304177498828590239373389110545142E4L,
17249393c00Smartynas   -1.209368072473510674493129989468348633579E2L
17349393c00Smartynas /* 1.0E0 */
17449393c00Smartynas };
17549393c00Smartynas 
17649393c00Smartynas 
17749393c00Smartynas /* erf(z+1)  = erf_const + P(z)/Q(z)
17849393c00Smartynas    -.125 <= z <= 0
17949393c00Smartynas    Peak relative error 7.3e-36  */
18049393c00Smartynas static const long double erf_const = 0.845062911510467529296875L;
18149393c00Smartynas #define NTN2 8
18249393c00Smartynas static const long double TN2[NTN2 + 1] =
18349393c00Smartynas {
18449393c00Smartynas  -4.088889697077485301010486931817357000235E1L,
18549393c00Smartynas   7.157046430681808553842307502826960051036E3L,
18649393c00Smartynas  -2.191561912574409865550015485451373731780E3L,
18749393c00Smartynas   2.180174916555316874988981177654057337219E3L,
18849393c00Smartynas   2.848578658049670668231333682379720943455E2L,
18949393c00Smartynas   1.630362490952512836762810462174798925274E2L,
19049393c00Smartynas   6.317712353961866974143739396865293596895E0L,
19149393c00Smartynas   2.450441034183492434655586496522857578066E1L,
19249393c00Smartynas   5.127662277706787664956025545897050896203E-1L
19349393c00Smartynas };
19449393c00Smartynas #define NTD2 8
19549393c00Smartynas static const long double TD2[NTD2 + 1] =
19649393c00Smartynas {
19749393c00Smartynas   1.731026445926834008273768924015161048885E4L,
19849393c00Smartynas   1.209682239007990370796112604286048173750E4L,
19949393c00Smartynas   1.160950290217993641320602282462976163857E4L,
20049393c00Smartynas   5.394294645127126577825507169061355698157E3L,
20149393c00Smartynas   2.791239340533632669442158497532521776093E3L,
20249393c00Smartynas   8.989365571337319032943005387378993827684E2L,
20349393c00Smartynas   2.974016493766349409725385710897298069677E2L,
20449393c00Smartynas   6.148192754590376378740261072533527271947E1L,
20549393c00Smartynas   1.178502892490738445655468927408440847480E1L
20649393c00Smartynas  /* 1.0E0 */
20749393c00Smartynas };
20849393c00Smartynas 
20949393c00Smartynas 
21049393c00Smartynas /* erfc(x + 0.25) = erfc(0.25) + x R(x)
21149393c00Smartynas    0 <= x < 0.125
21249393c00Smartynas    Peak relative error 1.4e-35  */
21349393c00Smartynas #define NRNr13 8
21449393c00Smartynas static const long double RNr13[NRNr13 + 1] =
21549393c00Smartynas {
21649393c00Smartynas  -2.353707097641280550282633036456457014829E3L,
21749393c00Smartynas   3.871159656228743599994116143079870279866E2L,
21849393c00Smartynas  -3.888105134258266192210485617504098426679E2L,
21949393c00Smartynas  -2.129998539120061668038806696199343094971E1L,
22049393c00Smartynas  -8.125462263594034672468446317145384108734E1L,
22149393c00Smartynas   8.151549093983505810118308635926270319660E0L,
22249393c00Smartynas  -5.033362032729207310462422357772568553670E0L,
22349393c00Smartynas  -4.253956621135136090295893547735851168471E-2L,
22449393c00Smartynas  -8.098602878463854789780108161581050357814E-2L
22549393c00Smartynas };
22649393c00Smartynas #define NRDr13 7
22749393c00Smartynas static const long double RDr13[NRDr13 + 1] =
22849393c00Smartynas {
22949393c00Smartynas   2.220448796306693503549505450626652881752E3L,
23049393c00Smartynas   1.899133258779578688791041599040951431383E2L,
23149393c00Smartynas   1.061906712284961110196427571557149268454E3L,
23249393c00Smartynas   7.497086072306967965180978101974566760042E1L,
23349393c00Smartynas   2.146796115662672795876463568170441327274E2L,
23449393c00Smartynas   1.120156008362573736664338015952284925592E1L,
23549393c00Smartynas   2.211014952075052616409845051695042741074E1L,
23649393c00Smartynas   6.469655675326150785692908453094054988938E-1L
23749393c00Smartynas  /* 1.0E0 */
23849393c00Smartynas };
23949393c00Smartynas /* erfc(0.25) = C13a + C13b to extra precision.  */
24049393c00Smartynas static const long double C13a = 0.723663330078125L;
24149393c00Smartynas static const long double C13b = 1.0279753638067014931732235184287934646022E-5L;
24249393c00Smartynas 
24349393c00Smartynas 
24449393c00Smartynas /* erfc(x + 0.375) = erfc(0.375) + x R(x)
24549393c00Smartynas    0 <= x < 0.125
24649393c00Smartynas    Peak relative error 1.2e-35  */
24749393c00Smartynas #define NRNr14 8
24849393c00Smartynas static const long double RNr14[NRNr14 + 1] =
24949393c00Smartynas {
25049393c00Smartynas  -2.446164016404426277577283038988918202456E3L,
25149393c00Smartynas   6.718753324496563913392217011618096698140E2L,
25249393c00Smartynas  -4.581631138049836157425391886957389240794E2L,
25349393c00Smartynas  -2.382844088987092233033215402335026078208E1L,
25449393c00Smartynas  -7.119237852400600507927038680970936336458E1L,
25549393c00Smartynas   1.313609646108420136332418282286454287146E1L,
25649393c00Smartynas  -6.188608702082264389155862490056401365834E0L,
25749393c00Smartynas  -2.787116601106678287277373011101132659279E-2L,
25849393c00Smartynas  -2.230395570574153963203348263549700967918E-2L
25949393c00Smartynas };
26049393c00Smartynas #define NRDr14 7
26149393c00Smartynas static const long double RDr14[NRDr14 + 1] =
26249393c00Smartynas {
26349393c00Smartynas   2.495187439241869732696223349840963702875E3L,
26449393c00Smartynas   2.503549449872925580011284635695738412162E2L,
26549393c00Smartynas   1.159033560988895481698051531263861842461E3L,
26649393c00Smartynas   9.493751466542304491261487998684383688622E1L,
26749393c00Smartynas   2.276214929562354328261422263078480321204E2L,
26849393c00Smartynas   1.367697521219069280358984081407807931847E1L,
26949393c00Smartynas   2.276988395995528495055594829206582732682E1L,
27049393c00Smartynas   7.647745753648996559837591812375456641163E-1L
27149393c00Smartynas  /* 1.0E0 */
27249393c00Smartynas };
27349393c00Smartynas /* erfc(0.375) = C14a + C14b to extra precision.  */
27449393c00Smartynas static const long double C14a = 0.5958709716796875L;
27549393c00Smartynas static const long double C14b = 1.2118885490201676174914080878232469565953E-5L;
27649393c00Smartynas 
27749393c00Smartynas /* erfc(x + 0.5) = erfc(0.5) + x R(x)
27849393c00Smartynas    0 <= x < 0.125
27949393c00Smartynas    Peak relative error 4.7e-36  */
28049393c00Smartynas #define NRNr15 8
28149393c00Smartynas static const long double RNr15[NRNr15 + 1] =
28249393c00Smartynas {
28349393c00Smartynas  -2.624212418011181487924855581955853461925E3L,
28449393c00Smartynas   8.473828904647825181073831556439301342756E2L,
28549393c00Smartynas  -5.286207458628380765099405359607331669027E2L,
28649393c00Smartynas  -3.895781234155315729088407259045269652318E1L,
28749393c00Smartynas  -6.200857908065163618041240848728398496256E1L,
28849393c00Smartynas   1.469324610346924001393137895116129204737E1L,
28949393c00Smartynas  -6.961356525370658572800674953305625578903E0L,
29049393c00Smartynas   5.145724386641163809595512876629030548495E-3L,
29149393c00Smartynas   1.990253655948179713415957791776180406812E-2L
29249393c00Smartynas };
29349393c00Smartynas #define NRDr15 7
29449393c00Smartynas static const long double RDr15[NRDr15 + 1] =
29549393c00Smartynas {
29649393c00Smartynas   2.986190760847974943034021764693341524962E3L,
29749393c00Smartynas   5.288262758961073066335410218650047725985E2L,
29849393c00Smartynas   1.363649178071006978355113026427856008978E3L,
29949393c00Smartynas   1.921707975649915894241864988942255320833E2L,
30049393c00Smartynas   2.588651100651029023069013885900085533226E2L,
30149393c00Smartynas   2.628752920321455606558942309396855629459E1L,
30249393c00Smartynas   2.455649035885114308978333741080991380610E1L,
30349393c00Smartynas   1.378826653595128464383127836412100939126E0L
30449393c00Smartynas   /* 1.0E0 */
30549393c00Smartynas };
30649393c00Smartynas /* erfc(0.5) = C15a + C15b to extra precision.  */
30749393c00Smartynas static const long double C15a = 0.4794921875L;
30849393c00Smartynas static const long double C15b = 7.9346869534623172533461080354712635484242E-6L;
30949393c00Smartynas 
31049393c00Smartynas /* erfc(x + 0.625) = erfc(0.625) + x R(x)
31149393c00Smartynas    0 <= x < 0.125
31249393c00Smartynas    Peak relative error 5.1e-36  */
31349393c00Smartynas #define NRNr16 8
31449393c00Smartynas static const long double RNr16[NRNr16 + 1] =
31549393c00Smartynas {
31649393c00Smartynas  -2.347887943200680563784690094002722906820E3L,
31749393c00Smartynas   8.008590660692105004780722726421020136482E2L,
31849393c00Smartynas  -5.257363310384119728760181252132311447963E2L,
31949393c00Smartynas  -4.471737717857801230450290232600243795637E1L,
32049393c00Smartynas  -4.849540386452573306708795324759300320304E1L,
32149393c00Smartynas   1.140885264677134679275986782978655952843E1L,
32249393c00Smartynas  -6.731591085460269447926746876983786152300E0L,
32349393c00Smartynas   1.370831653033047440345050025876085121231E-1L,
32449393c00Smartynas   2.022958279982138755020825717073966576670E-2L,
32549393c00Smartynas };
32649393c00Smartynas #define NRDr16 7
32749393c00Smartynas static const long double RDr16[NRDr16 + 1] =
32849393c00Smartynas {
32949393c00Smartynas   3.075166170024837215399323264868308087281E3L,
33049393c00Smartynas   8.730468942160798031608053127270430036627E2L,
33149393c00Smartynas   1.458472799166340479742581949088453244767E3L,
33249393c00Smartynas   3.230423687568019709453130785873540386217E2L,
33349393c00Smartynas   2.804009872719893612081109617983169474655E2L,
33449393c00Smartynas   4.465334221323222943418085830026979293091E1L,
33549393c00Smartynas   2.612723259683205928103787842214809134746E1L,
33649393c00Smartynas   2.341526751185244109722204018543276124997E0L,
33749393c00Smartynas   /* 1.0E0 */
33849393c00Smartynas };
33949393c00Smartynas /* erfc(0.625) = C16a + C16b to extra precision.  */
34049393c00Smartynas static const long double C16a = 0.3767547607421875L;
34149393c00Smartynas static const long double C16b = 4.3570693945275513594941232097252997287766E-6L;
34249393c00Smartynas 
34349393c00Smartynas /* erfc(x + 0.75) = erfc(0.75) + x R(x)
34449393c00Smartynas    0 <= x < 0.125
34549393c00Smartynas    Peak relative error 1.7e-35  */
34649393c00Smartynas #define NRNr17 8
34749393c00Smartynas static const long double RNr17[NRNr17 + 1] =
34849393c00Smartynas {
34949393c00Smartynas   -1.767068734220277728233364375724380366826E3L,
35049393c00Smartynas   6.693746645665242832426891888805363898707E2L,
35149393c00Smartynas   -4.746224241837275958126060307406616817753E2L,
35249393c00Smartynas   -2.274160637728782675145666064841883803196E1L,
35349393c00Smartynas   -3.541232266140939050094370552538987982637E1L,
35449393c00Smartynas   6.988950514747052676394491563585179503865E0L,
35549393c00Smartynas   -5.807687216836540830881352383529281215100E0L,
35649393c00Smartynas   3.631915988567346438830283503729569443642E-1L,
35749393c00Smartynas   -1.488945487149634820537348176770282391202E-2L
35849393c00Smartynas };
35949393c00Smartynas #define NRDr17 7
36049393c00Smartynas static const long double RDr17[NRDr17 + 1] =
36149393c00Smartynas {
36249393c00Smartynas   2.748457523498150741964464942246913394647E3L,
36349393c00Smartynas   1.020213390713477686776037331757871252652E3L,
36449393c00Smartynas   1.388857635935432621972601695296561952738E3L,
36549393c00Smartynas   3.903363681143817750895999579637315491087E2L,
36649393c00Smartynas   2.784568344378139499217928969529219886578E2L,
36749393c00Smartynas   5.555800830216764702779238020065345401144E1L,
36849393c00Smartynas   2.646215470959050279430447295801291168941E1L,
36949393c00Smartynas   2.984905282103517497081766758550112011265E0L,
37049393c00Smartynas   /* 1.0E0 */
37149393c00Smartynas };
37249393c00Smartynas /* erfc(0.75) = C17a + C17b to extra precision.  */
37349393c00Smartynas static const long double C17a = 0.2888336181640625L;
37449393c00Smartynas static const long double C17b = 1.0748182422368401062165408589222625794046E-5L;
37549393c00Smartynas 
37649393c00Smartynas 
37749393c00Smartynas /* erfc(x + 0.875) = erfc(0.875) + x R(x)
37849393c00Smartynas    0 <= x < 0.125
37949393c00Smartynas    Peak relative error 2.2e-35  */
38049393c00Smartynas #define NRNr18 8
38149393c00Smartynas static const long double RNr18[NRNr18 + 1] =
38249393c00Smartynas {
38349393c00Smartynas  -1.342044899087593397419622771847219619588E3L,
38449393c00Smartynas   6.127221294229172997509252330961641850598E2L,
38549393c00Smartynas  -4.519821356522291185621206350470820610727E2L,
38649393c00Smartynas   1.223275177825128732497510264197915160235E1L,
38749393c00Smartynas  -2.730789571382971355625020710543532867692E1L,
38849393c00Smartynas   4.045181204921538886880171727755445395862E0L,
38949393c00Smartynas  -4.925146477876592723401384464691452700539E0L,
39049393c00Smartynas   5.933878036611279244654299924101068088582E-1L,
39149393c00Smartynas  -5.557645435858916025452563379795159124753E-2L
39249393c00Smartynas };
39349393c00Smartynas #define NRDr18 7
39449393c00Smartynas static const long double RDr18[NRDr18 + 1] =
39549393c00Smartynas {
39649393c00Smartynas   2.557518000661700588758505116291983092951E3L,
39749393c00Smartynas   1.070171433382888994954602511991940418588E3L,
39849393c00Smartynas   1.344842834423493081054489613250688918709E3L,
39949393c00Smartynas   4.161144478449381901208660598266288188426E2L,
40049393c00Smartynas   2.763670252219855198052378138756906980422E2L,
40149393c00Smartynas   5.998153487868943708236273854747564557632E1L,
40249393c00Smartynas   2.657695108438628847733050476209037025318E1L,
40349393c00Smartynas   3.252140524394421868923289114410336976512E0L,
40449393c00Smartynas   /* 1.0E0 */
40549393c00Smartynas };
40649393c00Smartynas /* erfc(0.875) = C18a + C18b to extra precision.  */
40749393c00Smartynas static const long double C18a = 0.215911865234375L;
40849393c00Smartynas static const long double C18b = 1.3073705765341685464282101150637224028267E-5L;
40949393c00Smartynas 
41049393c00Smartynas /* erfc(x + 1.0) = erfc(1.0) + x R(x)
41149393c00Smartynas    0 <= x < 0.125
41249393c00Smartynas    Peak relative error 1.6e-35  */
41349393c00Smartynas #define NRNr19 8
41449393c00Smartynas static const long double RNr19[NRNr19 + 1] =
41549393c00Smartynas {
41649393c00Smartynas  -1.139180936454157193495882956565663294826E3L,
41749393c00Smartynas   6.134903129086899737514712477207945973616E2L,
41849393c00Smartynas  -4.628909024715329562325555164720732868263E2L,
41949393c00Smartynas   4.165702387210732352564932347500364010833E1L,
42049393c00Smartynas  -2.286979913515229747204101330405771801610E1L,
42149393c00Smartynas   1.870695256449872743066783202326943667722E0L,
42249393c00Smartynas  -4.177486601273105752879868187237000032364E0L,
42349393c00Smartynas   7.533980372789646140112424811291782526263E-1L,
42449393c00Smartynas  -8.629945436917752003058064731308767664446E-2L
42549393c00Smartynas };
42649393c00Smartynas #define NRDr19 7
42749393c00Smartynas static const long double RDr19[NRDr19 + 1] =
42849393c00Smartynas {
42949393c00Smartynas   2.744303447981132701432716278363418643778E3L,
43049393c00Smartynas   1.266396359526187065222528050591302171471E3L,
43149393c00Smartynas   1.466739461422073351497972255511919814273E3L,
43249393c00Smartynas   4.868710570759693955597496520298058147162E2L,
43349393c00Smartynas   2.993694301559756046478189634131722579643E2L,
43449393c00Smartynas   6.868976819510254139741559102693828237440E1L,
43549393c00Smartynas   2.801505816247677193480190483913753613630E1L,
43649393c00Smartynas   3.604439909194350263552750347742663954481E0L,
43749393c00Smartynas   /* 1.0E0 */
43849393c00Smartynas };
43949393c00Smartynas /* erfc(1.0) = C19a + C19b to extra precision.  */
44049393c00Smartynas static const long double C19a = 0.15728759765625L;
44149393c00Smartynas static const long double C19b = 1.1609394035130658779364917390740703933002E-5L;
44249393c00Smartynas 
44349393c00Smartynas /* erfc(x + 1.125) = erfc(1.125) + x R(x)
44449393c00Smartynas    0 <= x < 0.125
44549393c00Smartynas    Peak relative error 3.6e-36  */
44649393c00Smartynas #define NRNr20 8
44749393c00Smartynas static const long double RNr20[NRNr20 + 1] =
44849393c00Smartynas {
44949393c00Smartynas  -9.652706916457973956366721379612508047640E2L,
45049393c00Smartynas   5.577066396050932776683469951773643880634E2L,
45149393c00Smartynas  -4.406335508848496713572223098693575485978E2L,
45249393c00Smartynas   5.202893466490242733570232680736966655434E1L,
45349393c00Smartynas  -1.931311847665757913322495948705563937159E1L,
45449393c00Smartynas  -9.364318268748287664267341457164918090611E-2L,
45549393c00Smartynas  -3.306390351286352764891355375882586201069E0L,
45649393c00Smartynas   7.573806045289044647727613003096916516475E-1L,
45749393c00Smartynas  -9.611744011489092894027478899545635991213E-2L
45849393c00Smartynas };
45949393c00Smartynas #define NRDr20 7
46049393c00Smartynas static const long double RDr20[NRDr20 + 1] =
46149393c00Smartynas {
46249393c00Smartynas   3.032829629520142564106649167182428189014E3L,
46349393c00Smartynas   1.659648470721967719961167083684972196891E3L,
46449393c00Smartynas   1.703545128657284619402511356932569292535E3L,
46549393c00Smartynas   6.393465677731598872500200253155257708763E2L,
46649393c00Smartynas   3.489131397281030947405287112726059221934E2L,
46749393c00Smartynas   8.848641738570783406484348434387611713070E1L,
46849393c00Smartynas   3.132269062552392974833215844236160958502E1L,
46949393c00Smartynas   4.430131663290563523933419966185230513168E0L
47049393c00Smartynas  /* 1.0E0 */
47149393c00Smartynas };
47249393c00Smartynas /* erfc(1.125) = C20a + C20b to extra precision.  */
47349393c00Smartynas static const long double C20a = 0.111602783203125L;
47449393c00Smartynas static const long double C20b = 8.9850951672359304215530728365232161564636E-6L;
47549393c00Smartynas 
47649393c00Smartynas /* erfc(1/x) = 1/x exp (-1/x^2 - 0.5625 + R(1/x^2))
47749393c00Smartynas    7/8 <= 1/x < 1
47849393c00Smartynas    Peak relative error 1.4e-35  */
47949393c00Smartynas #define NRNr8 9
48049393c00Smartynas static const long double RNr8[NRNr8 + 1] =
48149393c00Smartynas {
48249393c00Smartynas   3.587451489255356250759834295199296936784E1L,
48349393c00Smartynas   5.406249749087340431871378009874875889602E2L,
48449393c00Smartynas   2.931301290625250886238822286506381194157E3L,
48549393c00Smartynas   7.359254185241795584113047248898753470923E3L,
48649393c00Smartynas   9.201031849810636104112101947312492532314E3L,
48749393c00Smartynas   5.749697096193191467751650366613289284777E3L,
48849393c00Smartynas   1.710415234419860825710780802678697889231E3L,
48949393c00Smartynas   2.150753982543378580859546706243022719599E2L,
49049393c00Smartynas   8.740953582272147335100537849981160931197E0L,
49149393c00Smartynas   4.876422978828717219629814794707963640913E-2L
49249393c00Smartynas };
49349393c00Smartynas #define NRDr8 8
49449393c00Smartynas static const long double RDr8[NRDr8 + 1] =
49549393c00Smartynas {
49649393c00Smartynas   6.358593134096908350929496535931630140282E1L,
49749393c00Smartynas   9.900253816552450073757174323424051765523E2L,
49849393c00Smartynas   5.642928777856801020545245437089490805186E3L,
49949393c00Smartynas   1.524195375199570868195152698617273739609E4L,
50049393c00Smartynas   2.113829644500006749947332935305800887345E4L,
50149393c00Smartynas   1.526438562626465706267943737310282977138E4L,
50249393c00Smartynas   5.561370922149241457131421914140039411782E3L,
50349393c00Smartynas   9.394035530179705051609070428036834496942E2L,
50449393c00Smartynas   6.147019596150394577984175188032707343615E1L
50549393c00Smartynas   /* 1.0E0 */
50649393c00Smartynas };
50749393c00Smartynas 
50849393c00Smartynas /* erfc(1/x) = 1/x exp (-1/x^2 - 0.5625 + R(1/x^2))
50949393c00Smartynas    0.75 <= 1/x <= 0.875
51049393c00Smartynas    Peak relative error 2.0e-36  */
51149393c00Smartynas #define NRNr7 9
51249393c00Smartynas static const long double RNr7[NRNr7 + 1] =
51349393c00Smartynas {
51449393c00Smartynas  1.686222193385987690785945787708644476545E1L,
51549393c00Smartynas  1.178224543567604215602418571310612066594E3L,
51649393c00Smartynas  1.764550584290149466653899886088166091093E4L,
51749393c00Smartynas  1.073758321890334822002849369898232811561E5L,
51849393c00Smartynas  3.132840749205943137619839114451290324371E5L,
51949393c00Smartynas  4.607864939974100224615527007793867585915E5L,
52049393c00Smartynas  3.389781820105852303125270837910972384510E5L,
52149393c00Smartynas  1.174042187110565202875011358512564753399E5L,
52249393c00Smartynas  1.660013606011167144046604892622504338313E4L,
52349393c00Smartynas  6.700393957480661937695573729183733234400E2L
52449393c00Smartynas };
52549393c00Smartynas #define NRDr7 9
52649393c00Smartynas static const long double RDr7[NRDr7 + 1] =
52749393c00Smartynas {
52849393c00Smartynas -1.709305024718358874701575813642933561169E3L,
52949393c00Smartynas -3.280033887481333199580464617020514788369E4L,
53049393c00Smartynas -2.345284228022521885093072363418750835214E5L,
53149393c00Smartynas -8.086758123097763971926711729242327554917E5L,
53249393c00Smartynas -1.456900414510108718402423999575992450138E6L,
53349393c00Smartynas -1.391654264881255068392389037292702041855E6L,
53449393c00Smartynas -6.842360801869939983674527468509852583855E5L,
53549393c00Smartynas -1.597430214446573566179675395199807533371E5L,
53649393c00Smartynas -1.488876130609876681421645314851760773480E4L,
53749393c00Smartynas -3.511762950935060301403599443436465645703E2L
53849393c00Smartynas  /* 1.0E0 */
53949393c00Smartynas };
54049393c00Smartynas 
54149393c00Smartynas /* erfc(1/x) = 1/x exp(-1/x^2 - 0.5625 + R(1/x^2))
54249393c00Smartynas    5/8 <= 1/x < 3/4
54349393c00Smartynas    Peak relative error 1.9e-35  */
54449393c00Smartynas #define NRNr6 9
54549393c00Smartynas static const long double RNr6[NRNr6 + 1] =
54649393c00Smartynas {
54749393c00Smartynas  1.642076876176834390623842732352935761108E0L,
54849393c00Smartynas  1.207150003611117689000664385596211076662E2L,
54949393c00Smartynas  2.119260779316389904742873816462800103939E3L,
55049393c00Smartynas  1.562942227734663441801452930916044224174E4L,
55149393c00Smartynas  5.656779189549710079988084081145693580479E4L,
55249393c00Smartynas  1.052166241021481691922831746350942786299E5L,
55349393c00Smartynas  9.949798524786000595621602790068349165758E4L,
55449393c00Smartynas  4.491790734080265043407035220188849562856E4L,
55549393c00Smartynas  8.377074098301530326270432059434791287601E3L,
55649393c00Smartynas  4.506934806567986810091824791963991057083E2L
55749393c00Smartynas };
55849393c00Smartynas #define NRDr6 9
55949393c00Smartynas static const long double RDr6[NRDr6 + 1] =
56049393c00Smartynas {
56149393c00Smartynas -1.664557643928263091879301304019826629067E2L,
56249393c00Smartynas -3.800035902507656624590531122291160668452E3L,
56349393c00Smartynas -3.277028191591734928360050685359277076056E4L,
56449393c00Smartynas -1.381359471502885446400589109566587443987E5L,
56549393c00Smartynas -3.082204287382581873532528989283748656546E5L,
56649393c00Smartynas -3.691071488256738343008271448234631037095E5L,
56749393c00Smartynas -2.300482443038349815750714219117566715043E5L,
56849393c00Smartynas -6.873955300927636236692803579555752171530E4L,
56949393c00Smartynas -8.262158817978334142081581542749986845399E3L,
57049393c00Smartynas -2.517122254384430859629423488157361983661E2L
57149393c00Smartynas  /* 1.00 */
57249393c00Smartynas };
57349393c00Smartynas 
57449393c00Smartynas /* erfc(1/x) = 1/x exp(-1/x^2 - 0.5625 + R(1/x^2))
57549393c00Smartynas    1/2 <= 1/x < 5/8
57649393c00Smartynas    Peak relative error 4.6e-36  */
57749393c00Smartynas #define NRNr5 10
57849393c00Smartynas static const long double RNr5[NRNr5 + 1] =
57949393c00Smartynas {
58049393c00Smartynas -3.332258927455285458355550878136506961608E-3L,
58149393c00Smartynas -2.697100758900280402659586595884478660721E-1L,
58249393c00Smartynas -6.083328551139621521416618424949137195536E0L,
58349393c00Smartynas -6.119863528983308012970821226810162441263E1L,
58449393c00Smartynas -3.176535282475593173248810678636522589861E2L,
58549393c00Smartynas -8.933395175080560925809992467187963260693E2L,
58649393c00Smartynas -1.360019508488475978060917477620199499560E3L,
58749393c00Smartynas -1.075075579828188621541398761300910213280E3L,
58849393c00Smartynas -4.017346561586014822824459436695197089916E2L,
58949393c00Smartynas -5.857581368145266249509589726077645791341E1L,
59049393c00Smartynas -2.077715925587834606379119585995758954399E0L
59149393c00Smartynas };
59249393c00Smartynas #define NRDr5 9
59349393c00Smartynas static const long double RDr5[NRDr5 + 1] =
59449393c00Smartynas {
59549393c00Smartynas  3.377879570417399341550710467744693125385E-1L,
59649393c00Smartynas  1.021963322742390735430008860602594456187E1L,
59749393c00Smartynas  1.200847646592942095192766255154827011939E2L,
59849393c00Smartynas  7.118915528142927104078182863387116942836E2L,
59949393c00Smartynas  2.318159380062066469386544552429625026238E3L,
60049393c00Smartynas  4.238729853534009221025582008928765281620E3L,
60149393c00Smartynas  4.279114907284825886266493994833515580782E3L,
60249393c00Smartynas  2.257277186663261531053293222591851737504E3L,
60349393c00Smartynas  5.570475501285054293371908382916063822957E2L,
60449393c00Smartynas  5.142189243856288981145786492585432443560E1L
60549393c00Smartynas  /* 1.0E0 */
60649393c00Smartynas };
60749393c00Smartynas 
60849393c00Smartynas /* erfc(1/x) = 1/x exp(-1/x^2 - 0.5625 + R(1/x^2))
60949393c00Smartynas    3/8 <= 1/x < 1/2
61049393c00Smartynas    Peak relative error 2.0e-36  */
61149393c00Smartynas #define NRNr4 10
61249393c00Smartynas static const long double RNr4[NRNr4 + 1] =
61349393c00Smartynas {
61449393c00Smartynas  3.258530712024527835089319075288494524465E-3L,
61549393c00Smartynas  2.987056016877277929720231688689431056567E-1L,
61649393c00Smartynas  8.738729089340199750734409156830371528862E0L,
61749393c00Smartynas  1.207211160148647782396337792426311125923E2L,
61849393c00Smartynas  8.997558632489032902250523945248208224445E2L,
61949393c00Smartynas  3.798025197699757225978410230530640879762E3L,
62049393c00Smartynas  9.113203668683080975637043118209210146846E3L,
62149393c00Smartynas  1.203285891339933238608683715194034900149E4L,
62249393c00Smartynas  8.100647057919140328536743641735339740855E3L,
62349393c00Smartynas  2.383888249907144945837976899822927411769E3L,
62449393c00Smartynas  2.127493573166454249221983582495245662319E2L
62549393c00Smartynas };
62649393c00Smartynas #define NRDr4 10
62749393c00Smartynas static const long double RDr4[NRDr4 + 1] =
62849393c00Smartynas {
62949393c00Smartynas -3.303141981514540274165450687270180479586E-1L,
63049393c00Smartynas -1.353768629363605300707949368917687066724E1L,
63149393c00Smartynas -2.206127630303621521950193783894598987033E2L,
63249393c00Smartynas -1.861800338758066696514480386180875607204E3L,
63349393c00Smartynas -8.889048775872605708249140016201753255599E3L,
63449393c00Smartynas -2.465888106627948210478692168261494857089E4L,
63549393c00Smartynas -3.934642211710774494879042116768390014289E4L,
63649393c00Smartynas -3.455077258242252974937480623730228841003E4L,
63749393c00Smartynas -1.524083977439690284820586063729912653196E4L,
63849393c00Smartynas -2.810541887397984804237552337349093953857E3L,
63949393c00Smartynas -1.343929553541159933824901621702567066156E2L
64049393c00Smartynas  /* 1.0E0 */
64149393c00Smartynas };
64249393c00Smartynas 
64349393c00Smartynas /* erfc(1/x) = 1/x exp(-1/x^2 - 0.5625 + R(1/x^2))
64449393c00Smartynas    1/4 <= 1/x < 3/8
64549393c00Smartynas    Peak relative error 8.4e-37  */
64649393c00Smartynas #define NRNr3 11
64749393c00Smartynas static const long double RNr3[NRNr3 + 1] =
64849393c00Smartynas {
64949393c00Smartynas -1.952401126551202208698629992497306292987E-6L,
65049393c00Smartynas -2.130881743066372952515162564941682716125E-4L,
65149393c00Smartynas -8.376493958090190943737529486107282224387E-3L,
65249393c00Smartynas -1.650592646560987700661598877522831234791E-1L,
65349393c00Smartynas -1.839290818933317338111364667708678163199E0L,
65449393c00Smartynas -1.216278715570882422410442318517814388470E1L,
65549393c00Smartynas -4.818759344462360427612133632533779091386E1L,
65649393c00Smartynas -1.120994661297476876804405329172164436784E2L,
65749393c00Smartynas -1.452850765662319264191141091859300126931E2L,
65849393c00Smartynas -9.485207851128957108648038238656777241333E1L,
65949393c00Smartynas -2.563663855025796641216191848818620020073E1L,
66049393c00Smartynas -1.787995944187565676837847610706317833247E0L
66149393c00Smartynas };
66249393c00Smartynas #define NRDr3 10
66349393c00Smartynas static const long double RDr3[NRDr3 + 1] =
66449393c00Smartynas {
66549393c00Smartynas  1.979130686770349481460559711878399476903E-4L,
66649393c00Smartynas  1.156941716128488266238105813374635099057E-2L,
66749393c00Smartynas  2.752657634309886336431266395637285974292E-1L,
66849393c00Smartynas  3.482245457248318787349778336603569327521E0L,
66949393c00Smartynas  2.569347069372696358578399521203959253162E1L,
67049393c00Smartynas  1.142279000180457419740314694631879921561E2L,
67149393c00Smartynas  3.056503977190564294341422623108332700840E2L,
67249393c00Smartynas  4.780844020923794821656358157128719184422E2L,
67349393c00Smartynas  4.105972727212554277496256802312730410518E2L,
67449393c00Smartynas  1.724072188063746970865027817017067646246E2L,
67549393c00Smartynas  2.815939183464818198705278118326590370435E1L
67649393c00Smartynas  /* 1.0E0 */
67749393c00Smartynas };
67849393c00Smartynas 
67949393c00Smartynas /* erfc(1/x) = 1/x exp(-1/x^2 - 0.5625 + R(1/x^2))
68049393c00Smartynas    1/8 <= 1/x < 1/4
68149393c00Smartynas    Peak relative error 1.5e-36  */
68249393c00Smartynas #define NRNr2 11
68349393c00Smartynas static const long double RNr2[NRNr2 + 1] =
68449393c00Smartynas {
68549393c00Smartynas -2.638914383420287212401687401284326363787E-8L,
68649393c00Smartynas -3.479198370260633977258201271399116766619E-6L,
68749393c00Smartynas -1.783985295335697686382487087502222519983E-4L,
68849393c00Smartynas -4.777876933122576014266349277217559356276E-3L,
68949393c00Smartynas -7.450634738987325004070761301045014986520E-2L,
69049393c00Smartynas -7.068318854874733315971973707247467326619E-1L,
69149393c00Smartynas -4.113919921935944795764071670806867038732E0L,
69249393c00Smartynas -1.440447573226906222417767283691888875082E1L,
69349393c00Smartynas -2.883484031530718428417168042141288943905E1L,
69449393c00Smartynas -2.990886974328476387277797361464279931446E1L,
69549393c00Smartynas -1.325283914915104866248279787536128997331E1L,
69649393c00Smartynas -1.572436106228070195510230310658206154374E0L
69749393c00Smartynas };
69849393c00Smartynas #define NRDr2 10
69949393c00Smartynas static const long double RDr2[NRDr2 + 1] =
70049393c00Smartynas {
70149393c00Smartynas  2.675042728136731923554119302571867799673E-6L,
70249393c00Smartynas  2.170997868451812708585443282998329996268E-4L,
70349393c00Smartynas  7.249969752687540289422684951196241427445E-3L,
70449393c00Smartynas  1.302040375859768674620410563307838448508E-1L,
70549393c00Smartynas  1.380202483082910888897654537144485285549E0L,
70649393c00Smartynas  8.926594113174165352623847870299170069350E0L,
70749393c00Smartynas  3.521089584782616472372909095331572607185E1L,
70849393c00Smartynas  8.233547427533181375185259050330809105570E1L,
70949393c00Smartynas  1.072971579885803033079469639073292840135E2L,
71049393c00Smartynas  6.943803113337964469736022094105143158033E1L,
71149393c00Smartynas  1.775695341031607738233608307835017282662E1L
71249393c00Smartynas  /* 1.0E0 */
71349393c00Smartynas };
71449393c00Smartynas 
71549393c00Smartynas /* erfc(1/x) = 1/x exp(-1/x^2 - 0.5625 + R(1/x^2))
71649393c00Smartynas    1/128 <= 1/x < 1/8
71749393c00Smartynas    Peak relative error 2.2e-36  */
71849393c00Smartynas #define NRNr1 9
71949393c00Smartynas static const long double RNr1[NRNr1 + 1] =
72049393c00Smartynas {
72149393c00Smartynas -4.250780883202361946697751475473042685782E-8L,
72249393c00Smartynas -5.375777053288612282487696975623206383019E-6L,
72349393c00Smartynas -2.573645949220896816208565944117382460452E-4L,
72449393c00Smartynas -6.199032928113542080263152610799113086319E-3L,
72549393c00Smartynas -8.262721198693404060380104048479916247786E-2L,
72649393c00Smartynas -6.242615227257324746371284637695778043982E-1L,
72749393c00Smartynas -2.609874739199595400225113299437099626386E0L,
72849393c00Smartynas -5.581967563336676737146358534602770006970E0L,
72949393c00Smartynas -5.124398923356022609707490956634280573882E0L,
73049393c00Smartynas -1.290865243944292370661544030414667556649E0L
73149393c00Smartynas };
73249393c00Smartynas #define NRDr1 8
73349393c00Smartynas static const long double RDr1[NRDr1 + 1] =
73449393c00Smartynas {
73549393c00Smartynas  4.308976661749509034845251315983612976224E-6L,
73649393c00Smartynas  3.265390126432780184125233455960049294580E-4L,
73749393c00Smartynas  9.811328839187040701901866531796570418691E-3L,
73849393c00Smartynas  1.511222515036021033410078631914783519649E-1L,
73949393c00Smartynas  1.289264341917429958858379585970225092274E0L,
74049393c00Smartynas  6.147640356182230769548007536914983522270E0L,
74149393c00Smartynas  1.573966871337739784518246317003956180750E1L,
74249393c00Smartynas  1.955534123435095067199574045529218238263E1L,
74349393c00Smartynas  9.472613121363135472247929109615785855865E0L
74449393c00Smartynas   /* 1.0E0 */
74549393c00Smartynas };
74649393c00Smartynas 
74749393c00Smartynas 
74849393c00Smartynas long double
erfl(long double x)74949393c00Smartynas erfl(long double x)
75049393c00Smartynas {
75149393c00Smartynas   long double a, y, z;
75249393c00Smartynas   int32_t i, ix, sign;
75349393c00Smartynas   ieee_quad_shape_type u;
75449393c00Smartynas 
75549393c00Smartynas   u.value = x;
75649393c00Smartynas   sign = u.parts32.mswhi;
75749393c00Smartynas   ix = sign & 0x7fffffff;
75849393c00Smartynas 
75949393c00Smartynas   if (ix >= 0x7fff0000)
76049393c00Smartynas     {				/* erf(nan)=nan */
76149393c00Smartynas       i = ((sign & 0xffff0000) >> 31) << 1;
76249393c00Smartynas       return (long double) (1 - i) + one / x;	/* erf(+-inf)=+-1 */
76349393c00Smartynas     }
76449393c00Smartynas 
76549393c00Smartynas   if (ix >= 0x3fff0000) /* |x| >= 1.0 */
76649393c00Smartynas     {
76749393c00Smartynas       y = erfcl (x);
76849393c00Smartynas       return (one - y);
76949393c00Smartynas       /*    return (one - erfcl (x)); */
77049393c00Smartynas     }
77149393c00Smartynas   u.parts32.mswhi = ix;
77249393c00Smartynas   a = u.value;
77349393c00Smartynas   z = x * x;
77449393c00Smartynas   if (ix < 0x3ffec000)  /* a < 0.875 */
77549393c00Smartynas     {
77649393c00Smartynas       if (ix < 0x3fc60000) /* |x|<2**-57 */
77749393c00Smartynas 	{
77849393c00Smartynas 	  if (ix < 0x00080000)
77949393c00Smartynas 	    return 0.125 * (8.0 * x + efx8 * x);	/*avoid underflow */
78049393c00Smartynas 	  return x + efx * x;
78149393c00Smartynas 	}
78249393c00Smartynas       y = a + a * neval (z, TN1, NTN1) / deval (z, TD1, NTD1);
78349393c00Smartynas     }
78449393c00Smartynas   else
78549393c00Smartynas     {
78649393c00Smartynas       a = a - one;
78749393c00Smartynas       y = erf_const + neval (a, TN2, NTN2) / deval (a, TD2, NTD2);
78849393c00Smartynas     }
78949393c00Smartynas 
79049393c00Smartynas   if (sign & 0x80000000) /* x < 0 */
79149393c00Smartynas     y = -y;
79249393c00Smartynas   return( y );
79349393c00Smartynas }
794*2f2c0062Sguenther DEF_STD(erfl);
79549393c00Smartynas 
79649393c00Smartynas long double
erfcl(long double x)79749393c00Smartynas erfcl(long double x)
79849393c00Smartynas {
79949393c00Smartynas   long double y, z, p, r;
80049393c00Smartynas   int32_t i, ix, sign;
80149393c00Smartynas   ieee_quad_shape_type u;
80249393c00Smartynas 
80349393c00Smartynas   u.value = x;
80449393c00Smartynas   sign = u.parts32.mswhi;
80549393c00Smartynas   ix = sign & 0x7fffffff;
80649393c00Smartynas   u.parts32.mswhi = ix;
80749393c00Smartynas 
80849393c00Smartynas   if (ix >= 0x7fff0000)
80949393c00Smartynas     {				/* erfc(nan)=nan */
81049393c00Smartynas       /* erfc(+-inf)=0,2 */
81149393c00Smartynas       return (long double) (((u_int32_t) sign >> 31) << 1) + one / x;
81249393c00Smartynas     }
81349393c00Smartynas 
81449393c00Smartynas   if (ix < 0x3ffd0000) /* |x| <1/4 */
81549393c00Smartynas     {
81649393c00Smartynas       if (ix < 0x3f8d0000) /* |x|<2**-114 */
81749393c00Smartynas 	return one - x;
81849393c00Smartynas       return one - erfl (x);
81949393c00Smartynas     }
82049393c00Smartynas   if (ix < 0x3fff4000) /* 1.25 */
82149393c00Smartynas     {
82249393c00Smartynas       x = u.value;
82349393c00Smartynas       i = 8.0 * x;
82449393c00Smartynas       switch (i)
82549393c00Smartynas 	{
82649393c00Smartynas 	case 2:
82749393c00Smartynas 	  z = x - 0.25L;
82849393c00Smartynas 	  y = C13b + z * neval (z, RNr13, NRNr13) / deval (z, RDr13, NRDr13);
82949393c00Smartynas 	  y += C13a;
83049393c00Smartynas 	  break;
83149393c00Smartynas 	case 3:
83249393c00Smartynas 	  z = x - 0.375L;
83349393c00Smartynas 	  y = C14b + z * neval (z, RNr14, NRNr14) / deval (z, RDr14, NRDr14);
83449393c00Smartynas 	  y += C14a;
83549393c00Smartynas 	  break;
83649393c00Smartynas 	case 4:
83749393c00Smartynas 	  z = x - 0.5L;
83849393c00Smartynas 	  y = C15b + z * neval (z, RNr15, NRNr15) / deval (z, RDr15, NRDr15);
83949393c00Smartynas 	  y += C15a;
84049393c00Smartynas 	  break;
84149393c00Smartynas 	case 5:
84249393c00Smartynas 	  z = x - 0.625L;
84349393c00Smartynas 	  y = C16b + z * neval (z, RNr16, NRNr16) / deval (z, RDr16, NRDr16);
84449393c00Smartynas 	  y += C16a;
84549393c00Smartynas 	  break;
84649393c00Smartynas 	case 6:
84749393c00Smartynas 	  z = x - 0.75L;
84849393c00Smartynas 	  y = C17b + z * neval (z, RNr17, NRNr17) / deval (z, RDr17, NRDr17);
84949393c00Smartynas 	  y += C17a;
85049393c00Smartynas 	  break;
85149393c00Smartynas 	case 7:
85249393c00Smartynas 	  z = x - 0.875L;
85349393c00Smartynas 	  y = C18b + z * neval (z, RNr18, NRNr18) / deval (z, RDr18, NRDr18);
85449393c00Smartynas 	  y += C18a;
85549393c00Smartynas 	  break;
85649393c00Smartynas 	case 8:
85749393c00Smartynas 	  z = x - 1.0L;
85849393c00Smartynas 	  y = C19b + z * neval (z, RNr19, NRNr19) / deval (z, RDr19, NRDr19);
85949393c00Smartynas 	  y += C19a;
86049393c00Smartynas 	  break;
86149393c00Smartynas 	case 9:
86249393c00Smartynas 	  z = x - 1.125L;
86349393c00Smartynas 	  y = C20b + z * neval (z, RNr20, NRNr20) / deval (z, RDr20, NRDr20);
86449393c00Smartynas 	  y += C20a;
86549393c00Smartynas 	  break;
86649393c00Smartynas 	}
86749393c00Smartynas       if (sign & 0x80000000)
86849393c00Smartynas 	y = 2.0L - y;
86949393c00Smartynas       return y;
87049393c00Smartynas     }
87149393c00Smartynas   /* 1.25 < |x| < 107 */
87249393c00Smartynas   if (ix < 0x4005ac00)
87349393c00Smartynas     {
87449393c00Smartynas       /* x < -9 */
87549393c00Smartynas       if ((ix >= 0x40022000) && (sign & 0x80000000))
87649393c00Smartynas 	return two - tiny;
87749393c00Smartynas 
87849393c00Smartynas       x = fabsl (x);
87949393c00Smartynas       z = one / (x * x);
88049393c00Smartynas       i = 8.0 / x;
88149393c00Smartynas       switch (i)
88249393c00Smartynas 	{
88349393c00Smartynas 	default:
88449393c00Smartynas 	case 0:
88549393c00Smartynas 	  p = neval (z, RNr1, NRNr1) / deval (z, RDr1, NRDr1);
88649393c00Smartynas 	  break;
88749393c00Smartynas 	case 1:
88849393c00Smartynas 	  p = neval (z, RNr2, NRNr2) / deval (z, RDr2, NRDr2);
88949393c00Smartynas 	  break;
89049393c00Smartynas 	case 2:
89149393c00Smartynas 	  p = neval (z, RNr3, NRNr3) / deval (z, RDr3, NRDr3);
89249393c00Smartynas 	  break;
89349393c00Smartynas 	case 3:
89449393c00Smartynas 	  p = neval (z, RNr4, NRNr4) / deval (z, RDr4, NRDr4);
89549393c00Smartynas 	  break;
89649393c00Smartynas 	case 4:
89749393c00Smartynas 	  p = neval (z, RNr5, NRNr5) / deval (z, RDr5, NRDr5);
89849393c00Smartynas 	  break;
89949393c00Smartynas 	case 5:
90049393c00Smartynas 	  p = neval (z, RNr6, NRNr6) / deval (z, RDr6, NRDr6);
90149393c00Smartynas 	  break;
90249393c00Smartynas 	case 6:
90349393c00Smartynas 	  p = neval (z, RNr7, NRNr7) / deval (z, RDr7, NRDr7);
90449393c00Smartynas 	  break;
90549393c00Smartynas 	case 7:
90649393c00Smartynas 	  p = neval (z, RNr8, NRNr8) / deval (z, RDr8, NRDr8);
90749393c00Smartynas 	  break;
90849393c00Smartynas 	}
90949393c00Smartynas       u.value = x;
91049393c00Smartynas       u.parts32.lswlo = 0;
91149393c00Smartynas       u.parts32.lswhi &= 0xfe000000;
91249393c00Smartynas       z = u.value;
91349393c00Smartynas       r = expl (-z * z - 0.5625) *
91449393c00Smartynas 	expl ((z - x) * (z + x) + p);
91549393c00Smartynas       if ((sign & 0x80000000) == 0)
91649393c00Smartynas 	return r / x;
91749393c00Smartynas       else
91849393c00Smartynas 	return two - r / x;
91949393c00Smartynas     }
92049393c00Smartynas   else
92149393c00Smartynas     {
92249393c00Smartynas       if ((sign & 0x80000000) == 0)
92349393c00Smartynas 	return tiny * tiny;
92449393c00Smartynas       else
92549393c00Smartynas 	return two - tiny;
92649393c00Smartynas     }
92749393c00Smartynas }
928*2f2c0062Sguenther DEF_STD(erfcl);
929