xref: /titanic_44/usr/src/lib/libm/common/C/erf.c (revision ddc0e0b53c661f6e439e3b7072b3ef353eadb4af)
125c28e83SPiotr Jasiukajtis /*
225c28e83SPiotr Jasiukajtis  * CDDL HEADER START
325c28e83SPiotr Jasiukajtis  *
425c28e83SPiotr Jasiukajtis  * The contents of this file are subject to the terms of the
525c28e83SPiotr Jasiukajtis  * Common Development and Distribution License (the "License").
625c28e83SPiotr Jasiukajtis  * You may not use this file except in compliance with the License.
725c28e83SPiotr Jasiukajtis  *
825c28e83SPiotr Jasiukajtis  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
925c28e83SPiotr Jasiukajtis  * or http://www.opensolaris.org/os/licensing.
1025c28e83SPiotr Jasiukajtis  * See the License for the specific language governing permissions
1125c28e83SPiotr Jasiukajtis  * and limitations under the License.
1225c28e83SPiotr Jasiukajtis  *
1325c28e83SPiotr Jasiukajtis  * When distributing Covered Code, include this CDDL HEADER in each
1425c28e83SPiotr Jasiukajtis  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1525c28e83SPiotr Jasiukajtis  * If applicable, add the following below this CDDL HEADER, with the
1625c28e83SPiotr Jasiukajtis  * fields enclosed by brackets "[]" replaced with your own identifying
1725c28e83SPiotr Jasiukajtis  * information: Portions Copyright [yyyy] [name of copyright owner]
1825c28e83SPiotr Jasiukajtis  *
1925c28e83SPiotr Jasiukajtis  * CDDL HEADER END
2025c28e83SPiotr Jasiukajtis  */
2125c28e83SPiotr Jasiukajtis 
2225c28e83SPiotr Jasiukajtis /*
2325c28e83SPiotr Jasiukajtis  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
2425c28e83SPiotr Jasiukajtis  */
2525c28e83SPiotr Jasiukajtis /*
2625c28e83SPiotr Jasiukajtis  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
2725c28e83SPiotr Jasiukajtis  * Use is subject to license terms.
2825c28e83SPiotr Jasiukajtis  */
2925c28e83SPiotr Jasiukajtis 
30*ddc0e0b5SRichard Lowe #pragma weak __erf = erf
31*ddc0e0b5SRichard Lowe #pragma weak __erfc = erfc
3225c28e83SPiotr Jasiukajtis 
3325c28e83SPiotr Jasiukajtis /* INDENT OFF */
3425c28e83SPiotr Jasiukajtis /*
3525c28e83SPiotr Jasiukajtis  * double erf(double x)
3625c28e83SPiotr Jasiukajtis  * double erfc(double x)
3725c28e83SPiotr Jasiukajtis  *			     x
3825c28e83SPiotr Jasiukajtis  *		      2      |\
3925c28e83SPiotr Jasiukajtis  *     erf(x)  =  ---------  | exp(-t*t)dt
4025c28e83SPiotr Jasiukajtis  *		   sqrt(pi) \|
4125c28e83SPiotr Jasiukajtis  *			     0
4225c28e83SPiotr Jasiukajtis  *
4325c28e83SPiotr Jasiukajtis  *     erfc(x) =  1-erf(x)
4425c28e83SPiotr Jasiukajtis  *  Note that
4525c28e83SPiotr Jasiukajtis  *		erf(-x) = -erf(x)
4625c28e83SPiotr Jasiukajtis  *		erfc(-x) = 2 - erfc(x)
4725c28e83SPiotr Jasiukajtis  *
4825c28e83SPiotr Jasiukajtis  * Method:
4925c28e83SPiotr Jasiukajtis  *	1. For |x| in [0, 0.84375]
5025c28e83SPiotr Jasiukajtis  *	    erf(x)  = x + x*R(x^2)
5125c28e83SPiotr Jasiukajtis  *          erfc(x) = 1 - erf(x)           if x in [-.84375,0.25]
5225c28e83SPiotr Jasiukajtis  *                  = 0.5 + ((0.5-x)-x*R)  if x in [0.25,0.84375]
5325c28e83SPiotr Jasiukajtis  *	   where R = P/Q where P is an odd poly of degree 8 and
5425c28e83SPiotr Jasiukajtis  *	   Q is an odd poly of degree 10.
5525c28e83SPiotr Jasiukajtis  *						 -57.90
5625c28e83SPiotr Jasiukajtis  *			| R - (erf(x)-x)/x | <= 2
5725c28e83SPiotr Jasiukajtis  *
5825c28e83SPiotr Jasiukajtis  *
5925c28e83SPiotr Jasiukajtis  *	   Remark. The formula is derived by noting
6025c28e83SPiotr Jasiukajtis  *          erf(x) = (2/sqrt(pi))*(x - x^3/3 + x^5/10 - x^7/42 + ....)
6125c28e83SPiotr Jasiukajtis  *	   and that
6225c28e83SPiotr Jasiukajtis  *          2/sqrt(pi) = 1.128379167095512573896158903121545171688
6325c28e83SPiotr Jasiukajtis  *	   is close to one. The interval is chosen because the fix
6425c28e83SPiotr Jasiukajtis  *	   point of erf(x) is near 0.6174 (i.e., erf(x)=x when x is
6525c28e83SPiotr Jasiukajtis  *	   near 0.6174), and by some experiment, 0.84375 is chosen to
6625c28e83SPiotr Jasiukajtis  *	   guarantee the error is less than one ulp for erf.
6725c28e83SPiotr Jasiukajtis  *
6825c28e83SPiotr Jasiukajtis  *      2. For |x| in [0.84375,1.25], let s = |x| - 1, and
6925c28e83SPiotr Jasiukajtis  *         c = 0.84506291151 rounded to single (24 bits)
7025c28e83SPiotr Jasiukajtis  *         	erf(x)  = sign(x) * (c  + P1(s)/Q1(s))
7125c28e83SPiotr Jasiukajtis  *         	erfc(x) = (1-c)  - P1(s)/Q1(s) if x > 0
7225c28e83SPiotr Jasiukajtis  *			  1+(c+P1(s)/Q1(s))    if x < 0
7325c28e83SPiotr Jasiukajtis  *         	|P1/Q1 - (erf(|x|)-c)| <= 2**-59.06
7425c28e83SPiotr Jasiukajtis  *	   Remark: here we use the taylor series expansion at x=1.
7525c28e83SPiotr Jasiukajtis  *		erf(1+s) = erf(1) + s*Poly(s)
7625c28e83SPiotr Jasiukajtis  *			 = 0.845.. + P1(s)/Q1(s)
7725c28e83SPiotr Jasiukajtis  *	   That is, we use rational approximation to approximate
7825c28e83SPiotr Jasiukajtis  *			erf(1+s) - (c = (single)0.84506291151)
7925c28e83SPiotr Jasiukajtis  *	   Note that |P1/Q1|< 0.078 for x in [0.84375,1.25]
8025c28e83SPiotr Jasiukajtis  *	   where
8125c28e83SPiotr Jasiukajtis  *		P1(s) = degree 6 poly in s
8225c28e83SPiotr Jasiukajtis  *		Q1(s) = degree 6 poly in s
8325c28e83SPiotr Jasiukajtis  *
8425c28e83SPiotr Jasiukajtis  *      3. For x in [1.25,1/0.35(~2.857143)],
8525c28e83SPiotr Jasiukajtis  *         	erfc(x) = (1/x)*exp(-x*x-0.5625+R1/S1)
8625c28e83SPiotr Jasiukajtis  *         	erf(x)  = 1 - erfc(x)
8725c28e83SPiotr Jasiukajtis  *	   where
8825c28e83SPiotr Jasiukajtis  *		R1(z) = degree 7 poly in z, (z=1/x^2)
8925c28e83SPiotr Jasiukajtis  *		S1(z) = degree 8 poly in z
9025c28e83SPiotr Jasiukajtis  *
9125c28e83SPiotr Jasiukajtis  *      4. For x in [1/0.35,28]
9225c28e83SPiotr Jasiukajtis  *         	erfc(x) = (1/x)*exp(-x*x-0.5625+R2/S2) if x > 0
9325c28e83SPiotr Jasiukajtis  *			= 2.0 - (1/x)*exp(-x*x-0.5625+R2/S2) if -6<x<0
9425c28e83SPiotr Jasiukajtis  *			= 2.0 - tiny		(if x <= -6)
9525c28e83SPiotr Jasiukajtis  *         	erf(x)  = sign(x)*(1.0 - erfc(x)) if x < 6, else
9625c28e83SPiotr Jasiukajtis  *         	erf(x)  = sign(x)*(1.0 - tiny)
9725c28e83SPiotr Jasiukajtis  *	   where
9825c28e83SPiotr Jasiukajtis  *		R2(z) = degree 6 poly in z, (z=1/x^2)
9925c28e83SPiotr Jasiukajtis  *		S2(z) = degree 7 poly in z
10025c28e83SPiotr Jasiukajtis  *
10125c28e83SPiotr Jasiukajtis  *      Note1:
10225c28e83SPiotr Jasiukajtis  *	   To compute exp(-x*x-0.5625+R/S), let s be a single
10325c28e83SPiotr Jasiukajtis  *	   precision number and s := x; then
10425c28e83SPiotr Jasiukajtis  *		-x*x = -s*s + (s-x)*(s+x)
10525c28e83SPiotr Jasiukajtis  *	        exp(-x*x-0.5626+R/S) =
10625c28e83SPiotr Jasiukajtis  *			exp(-s*s-0.5625)*exp((s-x)*(s+x)+R/S);
10725c28e83SPiotr Jasiukajtis  *      Note2:
10825c28e83SPiotr Jasiukajtis  *	   Here 4 and 5 make use of the asymptotic series
10925c28e83SPiotr Jasiukajtis  *			  exp(-x*x)
11025c28e83SPiotr Jasiukajtis  *		erfc(x) ~ ---------- * ( 1 + Poly(1/x^2) )
11125c28e83SPiotr Jasiukajtis  *			  x*sqrt(pi)
11225c28e83SPiotr Jasiukajtis  *	   We use rational approximation to approximate
11325c28e83SPiotr Jasiukajtis  *      	g(s)=f(1/x^2) = log(erfc(x)*x) - x*x + 0.5625
11425c28e83SPiotr Jasiukajtis  *	   Here is the error bound for R1/S1 and R2/S2
11525c28e83SPiotr Jasiukajtis  *      	|R1/S1 - f(x)|  < 2**(-62.57)
11625c28e83SPiotr Jasiukajtis  *      	|R2/S2 - f(x)|  < 2**(-61.52)
11725c28e83SPiotr Jasiukajtis  *
11825c28e83SPiotr Jasiukajtis  *      5. For inf > x >= 28
11925c28e83SPiotr Jasiukajtis  *         	erf(x)  = sign(x) *(1 - tiny)  (raise inexact)
12025c28e83SPiotr Jasiukajtis  *         	erfc(x) = tiny*tiny (raise underflow) if x > 0
12125c28e83SPiotr Jasiukajtis  *			= 2 - tiny if x<0
12225c28e83SPiotr Jasiukajtis  *
12325c28e83SPiotr Jasiukajtis  *      7. Special case:
12425c28e83SPiotr Jasiukajtis  *         	erf(0)  = 0, erf(inf)  = 1, erf(-inf) = -1,
12525c28e83SPiotr Jasiukajtis  *         	erfc(0) = 1, erfc(inf) = 0, erfc(-inf) = 2,
12625c28e83SPiotr Jasiukajtis  *   	erfc/erf(NaN) is NaN
12725c28e83SPiotr Jasiukajtis  */
12825c28e83SPiotr Jasiukajtis /* INDENT ON */
12925c28e83SPiotr Jasiukajtis 
13025c28e83SPiotr Jasiukajtis #include "libm_macros.h"
13125c28e83SPiotr Jasiukajtis #include <math.h>
13225c28e83SPiotr Jasiukajtis 
13325c28e83SPiotr Jasiukajtis static const double xxx[] = {
13425c28e83SPiotr Jasiukajtis /* tiny */	1e-300,
13525c28e83SPiotr Jasiukajtis /* half */	5.00000000000000000000e-01,	/* 3FE00000, 00000000 */
13625c28e83SPiotr Jasiukajtis /* one */	1.00000000000000000000e+00,	/* 3FF00000, 00000000 */
13725c28e83SPiotr Jasiukajtis /* two */	2.00000000000000000000e+00,	/* 40000000, 00000000 */
13825c28e83SPiotr Jasiukajtis /* erx */	8.45062911510467529297e-01,	/* 3FEB0AC1, 60000000 */
13925c28e83SPiotr Jasiukajtis /*
14025c28e83SPiotr Jasiukajtis  * Coefficients for approximation to  erf on [0,0.84375]
14125c28e83SPiotr Jasiukajtis  */
14225c28e83SPiotr Jasiukajtis /* efx */	 1.28379167095512586316e-01,	/* 3FC06EBA, 8214DB69 */
14325c28e83SPiotr Jasiukajtis /* efx8 */	 1.02703333676410069053e+00,	/* 3FF06EBA, 8214DB69 */
14425c28e83SPiotr Jasiukajtis /* pp0 */	 1.28379167095512558561e-01,	/* 3FC06EBA, 8214DB68 */
14525c28e83SPiotr Jasiukajtis /* pp1 */	-3.25042107247001499370e-01,	/* BFD4CD7D, 691CB913 */
14625c28e83SPiotr Jasiukajtis /* pp2 */	-2.84817495755985104766e-02,	/* BF9D2A51, DBD7194F */
14725c28e83SPiotr Jasiukajtis /* pp3 */	-5.77027029648944159157e-03,	/* BF77A291, 236668E4 */
14825c28e83SPiotr Jasiukajtis /* pp4 */	-2.37630166566501626084e-05,	/* BEF8EAD6, 120016AC */
14925c28e83SPiotr Jasiukajtis /* qq1 */	 3.97917223959155352819e-01,	/* 3FD97779, CDDADC09 */
15025c28e83SPiotr Jasiukajtis /* qq2 */	 6.50222499887672944485e-02,	/* 3FB0A54C, 5536CEBA */
15125c28e83SPiotr Jasiukajtis /* qq3 */	 5.08130628187576562776e-03,	/* 3F74D022, C4D36B0F */
15225c28e83SPiotr Jasiukajtis /* qq4 */	 1.32494738004321644526e-04,	/* 3F215DC9, 221C1A10 */
15325c28e83SPiotr Jasiukajtis /* qq5 */	-3.96022827877536812320e-06,	/* BED09C43, 42A26120 */
15425c28e83SPiotr Jasiukajtis /*
15525c28e83SPiotr Jasiukajtis  * Coefficients for approximation to  erf  in [0.84375,1.25]
15625c28e83SPiotr Jasiukajtis  */
15725c28e83SPiotr Jasiukajtis /* pa0 */	-2.36211856075265944077e-03,	/* BF6359B8, BEF77538 */
15825c28e83SPiotr Jasiukajtis /* pa1 */	 4.14856118683748331666e-01,	/* 3FDA8D00, AD92B34D */
15925c28e83SPiotr Jasiukajtis /* pa2 */	-3.72207876035701323847e-01,	/* BFD7D240, FBB8C3F1 */
16025c28e83SPiotr Jasiukajtis /* pa3 */	 3.18346619901161753674e-01,	/* 3FD45FCA, 805120E4 */
16125c28e83SPiotr Jasiukajtis /* pa4 */	-1.10894694282396677476e-01,	/* BFBC6398, 3D3E28EC */
16225c28e83SPiotr Jasiukajtis /* pa5 */	 3.54783043256182359371e-02,	/* 3FA22A36, 599795EB */
16325c28e83SPiotr Jasiukajtis /* pa6 */	-2.16637559486879084300e-03,	/* BF61BF38, 0A96073F */
16425c28e83SPiotr Jasiukajtis /* qa1 */	 1.06420880400844228286e-01,	/* 3FBB3E66, 18EEE323 */
16525c28e83SPiotr Jasiukajtis /* qa2 */	 5.40397917702171048937e-01,	/* 3FE14AF0, 92EB6F33 */
16625c28e83SPiotr Jasiukajtis /* qa3 */	 7.18286544141962662868e-02,	/* 3FB2635C, D99FE9A7 */
16725c28e83SPiotr Jasiukajtis /* qa4 */	 1.26171219808761642112e-01,	/* 3FC02660, E763351F */
16825c28e83SPiotr Jasiukajtis /* qa5 */	 1.36370839120290507362e-02,	/* 3F8BEDC2, 6B51DD1C */
16925c28e83SPiotr Jasiukajtis /* qa6 */	 1.19844998467991074170e-02,	/* 3F888B54, 5735151D */
17025c28e83SPiotr Jasiukajtis /*
17125c28e83SPiotr Jasiukajtis  * Coefficients for approximation to  erfc in [1.25,1/0.35]
17225c28e83SPiotr Jasiukajtis  */
17325c28e83SPiotr Jasiukajtis /* ra0 */	-9.86494403484714822705e-03,	/* BF843412, 600D6435 */
17425c28e83SPiotr Jasiukajtis /* ra1 */	-6.93858572707181764372e-01,	/* BFE63416, E4BA7360 */
17525c28e83SPiotr Jasiukajtis /* ra2 */	-1.05586262253232909814e+01,	/* C0251E04, 41B0E726 */
17625c28e83SPiotr Jasiukajtis /* ra3 */	-6.23753324503260060396e+01,	/* C04F300A, E4CBA38D */
17725c28e83SPiotr Jasiukajtis /* ra4 */	-1.62396669462573470355e+02,	/* C0644CB1, 84282266 */
17825c28e83SPiotr Jasiukajtis /* ra5 */	-1.84605092906711035994e+02,	/* C067135C, EBCCABB2 */
17925c28e83SPiotr Jasiukajtis /* ra6 */	-8.12874355063065934246e+01,	/* C0545265, 57E4D2F2 */
18025c28e83SPiotr Jasiukajtis /* ra7 */	-9.81432934416914548592e+00,	/* C023A0EF, C69AC25C */
18125c28e83SPiotr Jasiukajtis /* sa1 */	 1.96512716674392571292e+01,	/* 4033A6B9, BD707687 */
18225c28e83SPiotr Jasiukajtis /* sa2 */	 1.37657754143519042600e+02,	/* 4061350C, 526AE721 */
18325c28e83SPiotr Jasiukajtis /* sa3 */	 4.34565877475229228821e+02,	/* 407B290D, D58A1A71 */
18425c28e83SPiotr Jasiukajtis /* sa4 */	 6.45387271733267880336e+02,	/* 40842B19, 21EC2868 */
18525c28e83SPiotr Jasiukajtis /* sa5 */	 4.29008140027567833386e+02,	/* 407AD021, 57700314 */
18625c28e83SPiotr Jasiukajtis /* sa6 */	 1.08635005541779435134e+02,	/* 405B28A3, EE48AE2C */
18725c28e83SPiotr Jasiukajtis /* sa7 */	 6.57024977031928170135e+00,	/* 401A47EF, 8E484A93 */
18825c28e83SPiotr Jasiukajtis /* sa8 */	-6.04244152148580987438e-02,	/* BFAEEFF2, EE749A62 */
18925c28e83SPiotr Jasiukajtis /*
19025c28e83SPiotr Jasiukajtis  * Coefficients for approximation to  erfc in [1/.35,28]
19125c28e83SPiotr Jasiukajtis  */
19225c28e83SPiotr Jasiukajtis /* rb0 */	-9.86494292470009928597e-03,	/* BF843412, 39E86F4A */
19325c28e83SPiotr Jasiukajtis /* rb1 */	-7.99283237680523006574e-01,	/* BFE993BA, 70C285DE */
19425c28e83SPiotr Jasiukajtis /* rb2 */	-1.77579549177547519889e+01,	/* C031C209, 555F995A */
19525c28e83SPiotr Jasiukajtis /* rb3 */	-1.60636384855821916062e+02,	/* C064145D, 43C5ED98 */
19625c28e83SPiotr Jasiukajtis /* rb4 */	-6.37566443368389627722e+02,	/* C083EC88, 1375F228 */
19725c28e83SPiotr Jasiukajtis /* rb5 */	-1.02509513161107724954e+03,	/* C0900461, 6A2E5992 */
19825c28e83SPiotr Jasiukajtis /* rb6 */	-4.83519191608651397019e+02,	/* C07E384E, 9BDC383F */
19925c28e83SPiotr Jasiukajtis /* sb1 */	 3.03380607434824582924e+01,	/* 403E568B, 261D5190 */
20025c28e83SPiotr Jasiukajtis /* sb2 */	 3.25792512996573918826e+02,	/* 40745CAE, 221B9F0A */
20125c28e83SPiotr Jasiukajtis /* sb3 */	 1.53672958608443695994e+03,	/* 409802EB, 189D5118 */
20225c28e83SPiotr Jasiukajtis /* sb4 */	 3.19985821950859553908e+03,	/* 40A8FFB7, 688C246A */
20325c28e83SPiotr Jasiukajtis /* sb5 */	 2.55305040643316442583e+03,	/* 40A3F219, CEDF3BE6 */
20425c28e83SPiotr Jasiukajtis /* sb6 */	 4.74528541206955367215e+02,	/* 407DA874, E79FE763 */
20525c28e83SPiotr Jasiukajtis /* sb7 */	-2.24409524465858183362e+01	/* C03670E2, 42712D62 */
20625c28e83SPiotr Jasiukajtis };
20725c28e83SPiotr Jasiukajtis 
20825c28e83SPiotr Jasiukajtis #define	tiny	xxx[0]
20925c28e83SPiotr Jasiukajtis #define	half	xxx[1]
21025c28e83SPiotr Jasiukajtis #define	one	xxx[2]
21125c28e83SPiotr Jasiukajtis #define	two	xxx[3]
21225c28e83SPiotr Jasiukajtis #define	erx	xxx[4]
21325c28e83SPiotr Jasiukajtis /*
21425c28e83SPiotr Jasiukajtis  * Coefficients for approximation to  erf on [0,0.84375]
21525c28e83SPiotr Jasiukajtis  */
21625c28e83SPiotr Jasiukajtis #define	efx	xxx[5]
21725c28e83SPiotr Jasiukajtis #define	efx8	xxx[6]
21825c28e83SPiotr Jasiukajtis #define	pp0	xxx[7]
21925c28e83SPiotr Jasiukajtis #define	pp1	xxx[8]
22025c28e83SPiotr Jasiukajtis #define	pp2	xxx[9]
22125c28e83SPiotr Jasiukajtis #define	pp3	xxx[10]
22225c28e83SPiotr Jasiukajtis #define	pp4	xxx[11]
22325c28e83SPiotr Jasiukajtis #define	qq1	xxx[12]
22425c28e83SPiotr Jasiukajtis #define	qq2	xxx[13]
22525c28e83SPiotr Jasiukajtis #define	qq3	xxx[14]
22625c28e83SPiotr Jasiukajtis #define	qq4	xxx[15]
22725c28e83SPiotr Jasiukajtis #define	qq5	xxx[16]
22825c28e83SPiotr Jasiukajtis /*
22925c28e83SPiotr Jasiukajtis  * Coefficients for approximation to  erf  in [0.84375,1.25]
23025c28e83SPiotr Jasiukajtis  */
23125c28e83SPiotr Jasiukajtis #define	pa0	xxx[17]
23225c28e83SPiotr Jasiukajtis #define	pa1	xxx[18]
23325c28e83SPiotr Jasiukajtis #define	pa2	xxx[19]
23425c28e83SPiotr Jasiukajtis #define	pa3	xxx[20]
23525c28e83SPiotr Jasiukajtis #define	pa4	xxx[21]
23625c28e83SPiotr Jasiukajtis #define	pa5	xxx[22]
23725c28e83SPiotr Jasiukajtis #define	pa6	xxx[23]
23825c28e83SPiotr Jasiukajtis #define	qa1	xxx[24]
23925c28e83SPiotr Jasiukajtis #define	qa2	xxx[25]
24025c28e83SPiotr Jasiukajtis #define	qa3	xxx[26]
24125c28e83SPiotr Jasiukajtis #define	qa4	xxx[27]
24225c28e83SPiotr Jasiukajtis #define	qa5	xxx[28]
24325c28e83SPiotr Jasiukajtis #define	qa6	xxx[29]
24425c28e83SPiotr Jasiukajtis /*
24525c28e83SPiotr Jasiukajtis  * Coefficients for approximation to  erfc in [1.25,1/0.35]
24625c28e83SPiotr Jasiukajtis  */
24725c28e83SPiotr Jasiukajtis #define	ra0	xxx[30]
24825c28e83SPiotr Jasiukajtis #define	ra1	xxx[31]
24925c28e83SPiotr Jasiukajtis #define	ra2	xxx[32]
25025c28e83SPiotr Jasiukajtis #define	ra3	xxx[33]
25125c28e83SPiotr Jasiukajtis #define	ra4	xxx[34]
25225c28e83SPiotr Jasiukajtis #define	ra5	xxx[35]
25325c28e83SPiotr Jasiukajtis #define	ra6	xxx[36]
25425c28e83SPiotr Jasiukajtis #define	ra7	xxx[37]
25525c28e83SPiotr Jasiukajtis #define	sa1	xxx[38]
25625c28e83SPiotr Jasiukajtis #define	sa2	xxx[39]
25725c28e83SPiotr Jasiukajtis #define	sa3	xxx[40]
25825c28e83SPiotr Jasiukajtis #define	sa4	xxx[41]
25925c28e83SPiotr Jasiukajtis #define	sa5	xxx[42]
26025c28e83SPiotr Jasiukajtis #define	sa6	xxx[43]
26125c28e83SPiotr Jasiukajtis #define	sa7	xxx[44]
26225c28e83SPiotr Jasiukajtis #define	sa8	xxx[45]
26325c28e83SPiotr Jasiukajtis /*
26425c28e83SPiotr Jasiukajtis  * Coefficients for approximation to  erfc in [1/.35,28]
26525c28e83SPiotr Jasiukajtis  */
26625c28e83SPiotr Jasiukajtis #define	rb0	xxx[46]
26725c28e83SPiotr Jasiukajtis #define	rb1	xxx[47]
26825c28e83SPiotr Jasiukajtis #define	rb2	xxx[48]
26925c28e83SPiotr Jasiukajtis #define	rb3	xxx[49]
27025c28e83SPiotr Jasiukajtis #define	rb4	xxx[50]
27125c28e83SPiotr Jasiukajtis #define	rb5	xxx[51]
27225c28e83SPiotr Jasiukajtis #define	rb6	xxx[52]
27325c28e83SPiotr Jasiukajtis #define	sb1	xxx[53]
27425c28e83SPiotr Jasiukajtis #define	sb2	xxx[54]
27525c28e83SPiotr Jasiukajtis #define	sb3	xxx[55]
27625c28e83SPiotr Jasiukajtis #define	sb4	xxx[56]
27725c28e83SPiotr Jasiukajtis #define	sb5	xxx[57]
27825c28e83SPiotr Jasiukajtis #define	sb6	xxx[58]
27925c28e83SPiotr Jasiukajtis #define	sb7	xxx[59]
28025c28e83SPiotr Jasiukajtis 
28125c28e83SPiotr Jasiukajtis double
erf(double x)28225c28e83SPiotr Jasiukajtis erf(double x) {
28325c28e83SPiotr Jasiukajtis 	int hx, ix, i;
28425c28e83SPiotr Jasiukajtis 	double R, S, P, Q, s, y, z, r;
28525c28e83SPiotr Jasiukajtis 
28625c28e83SPiotr Jasiukajtis 	hx = ((int *) &x)[HIWORD];
28725c28e83SPiotr Jasiukajtis 	ix = hx & 0x7fffffff;
28825c28e83SPiotr Jasiukajtis 	if (ix >= 0x7ff00000) {	/* erf(nan)=nan */
28925c28e83SPiotr Jasiukajtis #if defined(FPADD_TRAPS_INCOMPLETE_ON_NAN)
29025c28e83SPiotr Jasiukajtis 		if (ix >= 0x7ff80000)		/* assumes sparc-like QNaN */
29125c28e83SPiotr Jasiukajtis 			return (x);
29225c28e83SPiotr Jasiukajtis #endif
29325c28e83SPiotr Jasiukajtis 		i = ((unsigned) hx >> 31) << 1;
29425c28e83SPiotr Jasiukajtis 		return ((double) (1 - i) + one / x);	/* erf(+-inf)=+-1 */
29525c28e83SPiotr Jasiukajtis 	}
29625c28e83SPiotr Jasiukajtis 
29725c28e83SPiotr Jasiukajtis 	if (ix < 0x3feb0000) {	/* |x|<0.84375 */
29825c28e83SPiotr Jasiukajtis 		if (ix < 0x3e300000) {	/* |x|<2**-28 */
29925c28e83SPiotr Jasiukajtis 			if (ix < 0x00800000)	/* avoid underflow */
30025c28e83SPiotr Jasiukajtis 				return (0.125 * (8.0 * x + efx8 * x));
30125c28e83SPiotr Jasiukajtis 			return (x + efx * x);
30225c28e83SPiotr Jasiukajtis 		}
30325c28e83SPiotr Jasiukajtis 		z = x * x;
30425c28e83SPiotr Jasiukajtis 		r = pp0 + z * (pp1 + z * (pp2 + z * (pp3 + z * pp4)));
30525c28e83SPiotr Jasiukajtis 		s = one +
30625c28e83SPiotr Jasiukajtis 			z *(qq1 + z * (qq2 + z * (qq3 + z * (qq4 + z * qq5))));
30725c28e83SPiotr Jasiukajtis 		y = r / s;
30825c28e83SPiotr Jasiukajtis 		return (x + x * y);
30925c28e83SPiotr Jasiukajtis 	}
31025c28e83SPiotr Jasiukajtis 	if (ix < 0x3ff40000) {	/* 0.84375 <= |x| < 1.25 */
31125c28e83SPiotr Jasiukajtis 		s = fabs(x) - one;
31225c28e83SPiotr Jasiukajtis 		P = pa0 + s * (pa1 + s * (pa2 + s * (pa3 + s * (pa4 +
31325c28e83SPiotr Jasiukajtis 			s * (pa5 + s * pa6)))));
31425c28e83SPiotr Jasiukajtis 		Q = one + s * (qa1 + s * (qa2 + s * (qa3 + s * (qa4 +
31525c28e83SPiotr Jasiukajtis 			s * (qa5 + s * qa6)))));
31625c28e83SPiotr Jasiukajtis 		if (hx >= 0)
31725c28e83SPiotr Jasiukajtis 			return (erx + P / Q);
31825c28e83SPiotr Jasiukajtis 		else
31925c28e83SPiotr Jasiukajtis 			return (-erx - P / Q);
32025c28e83SPiotr Jasiukajtis 	}
32125c28e83SPiotr Jasiukajtis 	if (ix >= 0x40180000) {	/* inf > |x| >= 6 */
32225c28e83SPiotr Jasiukajtis 		if (hx >= 0)
32325c28e83SPiotr Jasiukajtis 			return (one - tiny);
32425c28e83SPiotr Jasiukajtis 		else
32525c28e83SPiotr Jasiukajtis 			return (tiny - one);
32625c28e83SPiotr Jasiukajtis 	}
32725c28e83SPiotr Jasiukajtis 	x = fabs(x);
32825c28e83SPiotr Jasiukajtis 	s = one / (x * x);
32925c28e83SPiotr Jasiukajtis 	if (ix < 0x4006DB6E) {	/* |x| < 1/0.35 */
33025c28e83SPiotr Jasiukajtis 		R = ra0 + s * (ra1 + s * (ra2 + s * (ra3 + s * (ra4 +
33125c28e83SPiotr Jasiukajtis 			s * (ra5 + s * (ra6 + s * ra7))))));
33225c28e83SPiotr Jasiukajtis 		S = one + s * (sa1 + s * (sa2 + s * (sa3 + s * (sa4 +
33325c28e83SPiotr Jasiukajtis 			s * (sa5 + s * (sa6 + s * (sa7 + s * sa8)))))));
33425c28e83SPiotr Jasiukajtis 	} else {			/* |x| >= 1/0.35 */
33525c28e83SPiotr Jasiukajtis 		R = rb0 + s * (rb1 + s * (rb2 + s * (rb3 + s * (rb4 +
33625c28e83SPiotr Jasiukajtis 			s * (rb5 + s * rb6)))));
33725c28e83SPiotr Jasiukajtis 		S = one + s * (sb1 + s * (sb2 + s * (sb3 + s * (sb4 +
33825c28e83SPiotr Jasiukajtis 			s * (sb5 + s * (sb6 + s * sb7))))));
33925c28e83SPiotr Jasiukajtis 	}
34025c28e83SPiotr Jasiukajtis 	z = x;
34125c28e83SPiotr Jasiukajtis 	((int *) &z)[LOWORD] = 0;
34225c28e83SPiotr Jasiukajtis 	r = exp(-z * z - 0.5625) * exp((z - x) * (z + x) + R / S);
34325c28e83SPiotr Jasiukajtis 	if (hx >= 0)
34425c28e83SPiotr Jasiukajtis 		return (one - r / x);
34525c28e83SPiotr Jasiukajtis 	else
34625c28e83SPiotr Jasiukajtis 		return (r / x - one);
34725c28e83SPiotr Jasiukajtis }
34825c28e83SPiotr Jasiukajtis 
34925c28e83SPiotr Jasiukajtis double
erfc(double x)35025c28e83SPiotr Jasiukajtis erfc(double x) {
35125c28e83SPiotr Jasiukajtis 	int hx, ix;
35225c28e83SPiotr Jasiukajtis 	double R, S, P, Q, s, y, z, r;
35325c28e83SPiotr Jasiukajtis 
35425c28e83SPiotr Jasiukajtis 	hx = ((int *) &x)[HIWORD];
35525c28e83SPiotr Jasiukajtis 	ix = hx & 0x7fffffff;
35625c28e83SPiotr Jasiukajtis 	if (ix >= 0x7ff00000) {	/* erfc(nan)=nan */
35725c28e83SPiotr Jasiukajtis #if defined(FPADD_TRAPS_INCOMPLETE_ON_NAN)
35825c28e83SPiotr Jasiukajtis 		if (ix >= 0x7ff80000)		/* assumes sparc-like QNaN */
35925c28e83SPiotr Jasiukajtis 			return (x);
36025c28e83SPiotr Jasiukajtis #endif
36125c28e83SPiotr Jasiukajtis 		/* erfc(+-inf)=0,2 */
36225c28e83SPiotr Jasiukajtis 		return ((double) (((unsigned) hx >> 31) << 1) + one / x);
36325c28e83SPiotr Jasiukajtis 	}
36425c28e83SPiotr Jasiukajtis 
36525c28e83SPiotr Jasiukajtis 	if (ix < 0x3feb0000) {	/* |x| < 0.84375 */
36625c28e83SPiotr Jasiukajtis 		if (ix < 0x3c700000)	/* |x| < 2**-56 */
36725c28e83SPiotr Jasiukajtis 			return (one - x);
36825c28e83SPiotr Jasiukajtis 		z = x * x;
36925c28e83SPiotr Jasiukajtis 		r = pp0 + z * (pp1 + z * (pp2 + z * (pp3 + z * pp4)));
37025c28e83SPiotr Jasiukajtis 		s = one +
37125c28e83SPiotr Jasiukajtis 			z * (qq1 + z * (qq2 + z * (qq3 + z * (qq4 + z * qq5))));
37225c28e83SPiotr Jasiukajtis 		y = r / s;
37325c28e83SPiotr Jasiukajtis 		if (hx < 0x3fd00000) {	/* x < 1/4 */
37425c28e83SPiotr Jasiukajtis 			return (one - (x + x * y));
37525c28e83SPiotr Jasiukajtis 		} else {
37625c28e83SPiotr Jasiukajtis 			r = x * y;
37725c28e83SPiotr Jasiukajtis 			r += (x - half);
37825c28e83SPiotr Jasiukajtis 			return (half - r);
37925c28e83SPiotr Jasiukajtis 		}
38025c28e83SPiotr Jasiukajtis 	}
38125c28e83SPiotr Jasiukajtis 	if (ix < 0x3ff40000) {	/* 0.84375 <= |x| < 1.25 */
38225c28e83SPiotr Jasiukajtis 		s = fabs(x) - one;
38325c28e83SPiotr Jasiukajtis 		P = pa0 + s * (pa1 + s * (pa2 + s * (pa3 + s * (pa4 +
38425c28e83SPiotr Jasiukajtis 			s * (pa5 + s * pa6)))));
38525c28e83SPiotr Jasiukajtis 		Q = one + s * (qa1 + s * (qa2 + s * (qa3 + s * (qa4 +
38625c28e83SPiotr Jasiukajtis 			s * (qa5 + s * qa6)))));
38725c28e83SPiotr Jasiukajtis 		if (hx >= 0) {
38825c28e83SPiotr Jasiukajtis 			z = one - erx;
38925c28e83SPiotr Jasiukajtis 			return (z - P / Q);
39025c28e83SPiotr Jasiukajtis 		} else {
39125c28e83SPiotr Jasiukajtis 			z = erx + P / Q;
39225c28e83SPiotr Jasiukajtis 			return (one + z);
39325c28e83SPiotr Jasiukajtis 		}
39425c28e83SPiotr Jasiukajtis 	}
39525c28e83SPiotr Jasiukajtis 	if (ix < 0x403c0000) {	/* |x|<28 */
39625c28e83SPiotr Jasiukajtis 		x = fabs(x);
39725c28e83SPiotr Jasiukajtis 		s = one / (x * x);
39825c28e83SPiotr Jasiukajtis 		if (ix < 0x4006DB6D) {	/* |x| < 1/.35 ~ 2.857143 */
39925c28e83SPiotr Jasiukajtis 			R = ra0 + s * (ra1 + s * (ra2 + s * (ra3 + s * (ra4 +
40025c28e83SPiotr Jasiukajtis 				s * (ra5 + s * (ra6 + s * ra7))))));
40125c28e83SPiotr Jasiukajtis 			S = one + s * (sa1 + s * (sa2 + s * (sa3 + s * (sa4 +
40225c28e83SPiotr Jasiukajtis 				s * (sa5 + s * (sa6 + s * (sa7 + s * sa8)))))));
40325c28e83SPiotr Jasiukajtis 		} else {
40425c28e83SPiotr Jasiukajtis 			/* |x| >= 1/.35 ~ 2.857143 */
40525c28e83SPiotr Jasiukajtis 			if (hx < 0 && ix >= 0x40180000)
40625c28e83SPiotr Jasiukajtis 				return (two - tiny);	/* x < -6 */
40725c28e83SPiotr Jasiukajtis 
40825c28e83SPiotr Jasiukajtis 			R = rb0 + s * (rb1 + s * (rb2 + s * (rb3 + s * (rb4 +
40925c28e83SPiotr Jasiukajtis 				s * (rb5 + s * rb6)))));
41025c28e83SPiotr Jasiukajtis 			S = one + s * (sb1 + s * (sb2 + s * (sb3 + s * (sb4 +
41125c28e83SPiotr Jasiukajtis 				s * (sb5 + s * (sb6 + s * sb7))))));
41225c28e83SPiotr Jasiukajtis 		}
41325c28e83SPiotr Jasiukajtis 		z = x;
41425c28e83SPiotr Jasiukajtis 		((int *) &z)[LOWORD] = 0;
41525c28e83SPiotr Jasiukajtis 		r = exp(-z * z - 0.5625) * exp((z - x) * (z + x) + R / S);
41625c28e83SPiotr Jasiukajtis 		if (hx > 0)
41725c28e83SPiotr Jasiukajtis 			return (r / x);
41825c28e83SPiotr Jasiukajtis 		else
41925c28e83SPiotr Jasiukajtis 			return (two - r / x);
42025c28e83SPiotr Jasiukajtis 	} else {
42125c28e83SPiotr Jasiukajtis 		if (hx > 0)
42225c28e83SPiotr Jasiukajtis 			return (tiny * tiny);
42325c28e83SPiotr Jasiukajtis 		else
42425c28e83SPiotr Jasiukajtis 			return (two - tiny);
42525c28e83SPiotr Jasiukajtis 	}
42625c28e83SPiotr Jasiukajtis }
427