xref: /relibc/openlibm/src/s_erff.c (revision 71f60ec6321f6a3ef50d9834255ef506135d4005)
1 /* s_erff.c -- float version of s_erf.c.
2  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
3  */
4 
5 /*
6  * ====================================================
7  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
8  *
9  * Developed at SunPro, a Sun Microsystems, Inc. business.
10  * Permission to use, copy, modify, and distribute this
11  * software is freely granted, provided that this notice
12  * is preserved.
13  * ====================================================
14  */
15 
16 #include "cdefs-compat.h"
17 //__FBSDID("$FreeBSD: src/lib/msun/src/s_erff.c,v 1.8 2008/02/22 02:30:35 das Exp $");
18 
19 #include "openlibm.h"
20 #include "math_private.h"
21 
22 static const float
23 tiny	    = 1e-30,
24 half=  5.0000000000e-01, /* 0x3F000000 */
25 one =  1.0000000000e+00, /* 0x3F800000 */
26 two =  2.0000000000e+00, /* 0x40000000 */
27 /*
28  * Coefficients for approximation to erf on [0,0.84375]
29  */
30 efx =  1.2837916613e-01, /* 0x3e0375d4 */
31 efx8=  1.0270333290e+00, /* 0x3f8375d4 */
32 /*
33  *  Domain [0, 0.84375], range ~[-5.4446e-10,5.5197e-10]:
34  *  |(erf(x) - x)/x - p(x)/q(x)| < 2**-31.
35  */
36 pp0  =  1.28379166e-01F, /*  0x1.06eba8p-3 */
37 pp1  = -3.36030394e-01F, /* -0x1.58185ap-2 */
38 pp2  = -1.86260219e-03F, /* -0x1.e8451ep-10 */
39 qq1  =  3.12324286e-01F, /*  0x1.3fd1f0p-2 */
40 qq2  =  2.16070302e-02F, /*  0x1.620274p-6 */
41 qq3  = -1.98859419e-03F, /* -0x1.04a626p-9 */
42 /*
43  * Domain [0.84375, 1.25], range ~[-1.953e-11,1.940e-11]:
44  * |(erf(x) - erx) - p(x)/q(x)| < 2**-36.
45  */
46 erx  =  8.42697144e-01F, /*  0x1.af7600p-1.  erf(1) rounded to 16 bits. */
47 pa0  =  3.64939137e-06F, /*  0x1.e9d022p-19 */
48 pa1  =  4.15109694e-01F, /*  0x1.a91284p-2 */
49 pa2  = -1.65179938e-01F, /* -0x1.5249dcp-3 */
50 pa3  =  1.10914491e-01F, /*  0x1.c64e46p-4 */
51 qa1  =  6.02074385e-01F, /*  0x1.344318p-1 */
52 qa2  =  5.35934687e-01F, /*  0x1.126608p-1 */
53 qa3  =  1.68576106e-01F, /*  0x1.593e6ep-3 */
54 qa4  =  5.62181212e-02F, /*  0x1.cc89f2p-5 */
55 /*
56  * Domain [1.25,1/0.35], range ~[-7.043e-10,7.457e-10]:
57  * |log(x*erfc(x)) + x**2 + 0.5625 - r(x)/s(x)| < 2**-30
58  */
59 ra0  = -9.87132732e-03F, /* -0x1.4376b2p-7 */
60 ra1  = -5.53605914e-01F, /* -0x1.1b723cp-1 */
61 ra2  = -2.17589188e+00F, /* -0x1.1683a0p+1 */
62 ra3  = -1.43268085e+00F, /* -0x1.6ec42cp+0 */
63 sa1  =  5.45995426e+00F, /*  0x1.5d6fe4p+2 */
64 sa2  =  6.69798088e+00F, /*  0x1.acabb8p+2 */
65 sa3  =  1.43113089e+00F, /*  0x1.6e5e98p+0 */
66 sa4  = -5.77397496e-02F, /* -0x1.d90108p-5 */
67 /*
68  * Domain [1/0.35, 11], range ~[-2.264e-13,2.336e-13]:
69  * |log(x*erfc(x)) + x**2 + 0.5625 - r(x)/s(x)| < 2**-42
70  */
71 rb0  = -9.86494310e-03F, /* -0x1.434124p-7 */
72 rb1  = -6.25171244e-01F, /* -0x1.401672p-1 */
73 rb2  = -6.16498327e+00F, /* -0x1.8a8f16p+2 */
74 rb3  = -1.66696873e+01F, /* -0x1.0ab70ap+4 */
75 rb4  = -9.53764343e+00F, /* -0x1.313460p+3 */
76 sb1  =  1.26884899e+01F, /*  0x1.96081cp+3 */
77 sb2  =  4.51839523e+01F, /*  0x1.6978bcp+5 */
78 sb3  =  4.72810211e+01F, /*  0x1.7a3f88p+5 */
79 sb4  =  8.93033314e+00F; /*  0x1.1dc54ap+3 */
80 
81 
82 DLLEXPORT float
83 erff(float x)
84 {
85 	int32_t hx,ix,i;
86 	float R,S,P,Q,s,y,z,r;
87 	GET_FLOAT_WORD(hx,x);
88 	ix = hx&0x7fffffff;
89 	if(ix>=0x7f800000) {		/* erf(nan)=nan */
90 	    i = ((u_int32_t)hx>>31)<<1;
91 	    return (float)(1-i)+one/x;	/* erf(+-inf)=+-1 */
92 	}
93 
94 	if(ix < 0x3f580000) {		/* |x|<0.84375 */
95             if(ix < 0x38800000) {       /* |x|<2**-14 */
96                 if (ix < 0x04000000)    /* |x|<0x1p-119 */
97                     return (8*x+efx8*x)/8;      /* avoid spurious underflow */
98 		return x + efx*x;
99 	    }
100 	    z = x*x;
101             r = pp0+z*(pp1+z*pp2);
102             s = one+z*(qq1+z*(qq2+z*qq3));
103 	    y = r/s;
104 	    return x + x*y;
105 	}
106 	if(ix < 0x3fa00000) {		/* 0.84375 <= |x| < 1.25 */
107 	    s = fabsf(x)-one;
108             P = pa0+s*(pa1+s*(pa2+s*pa3));
109             Q = one+s*(qa1+s*(qa2+s*(qa3+s*qa4)));
110 	    if(hx>=0) return erx + P/Q; else return -erx - P/Q;
111 	}
112 	if (ix >= 0x40800000) {		/* inf>|x|>=4 */
113 	    if(hx>=0) return one-tiny; else return tiny-one;
114 	}
115 	x = fabsf(x);
116  	s = one/(x*x);
117 	if(ix< 0x4036DB6E) {	/* |x| < 1/0.35 */
118             R=ra0+s*(ra1+s*(ra2+s*ra3));
119             S=one+s*(sa1+s*(sa2+s*(sa3+s*sa4)));
120 	} else {	/* |x| >= 1/0.35 */
121             R=rb0+s*(rb1+s*(rb2+s*(rb3+s*rb4)));
122             S=one+s*(sb1+s*(sb2+s*(sb3+s*sb4)));
123         }
124         SET_FLOAT_WORD(z,hx&0xffffe000);
125         r  = expf(-z*z-0.5625F)*expf((z-x)*(z+x)+R/S);
126 	if(hx>=0) return one-r/x; else return  r/x-one;
127 }
128 
129 DLLEXPORT float
130 erfcf(float x)
131 {
132 	int32_t hx,ix;
133 	float R,S,P,Q,s,y,z,r;
134 	GET_FLOAT_WORD(hx,x);
135 	ix = hx&0x7fffffff;
136 	if(ix>=0x7f800000) {			/* erfc(nan)=nan */
137 						/* erfc(+-inf)=0,2 */
138 	    return (float)(((u_int32_t)hx>>31)<<1)+one/x;
139 	}
140 
141 	if(ix < 0x3f580000) {		/* |x|<0.84375 */
142 	    if(ix < 0x33800000)  	/* |x|<2**-56 */
143 		return one-x;
144 	    z = x*x;
145             r = pp0+z*(pp1+z*pp2);
146             s = one+z*(qq1+z*(qq2+z*qq3));
147 	    y = r/s;
148 	    if(hx < 0x3e800000) {  	/* x<1/4 */
149 		return one-(x+x*y);
150 	    } else {
151 		r = x*y;
152 		r += (x-half);
153 	        return half - r ;
154 	    }
155 	}
156 	if(ix < 0x3fa00000) {		/* 0.84375 <= |x| < 1.25 */
157 	    s = fabsf(x)-one;
158             P = pa0+s*(pa1+s*(pa2+s*pa3));
159             Q = one+s*(qa1+s*(qa2+s*(qa3+s*qa4)));
160 	    if(hx>=0) {
161 	        z  = one-erx; return z - P/Q;
162 	    } else {
163 		z = erx+P/Q; return one+z;
164 	    }
165 	}
166 	if (ix < 0x41300000) {		/* |x|<28 */
167 	    x = fabsf(x);
168  	    s = one/(x*x);
169 	    if(ix< 0x4036DB6D) {	/* |x| < 1/.35 ~ 2.857143*/
170                 R=ra0+s*(ra1+s*(ra2+s*ra3));
171                 S=one+s*(sa1+s*(sa2+s*(sa3+s*sa4)));
172 	    } else {			/* |x| >= 1/.35 ~ 2.857143 */
173                 if(hx<0&&ix>=0x40a00000) return two-tiny;/* x < -5 */
174                 R=rb0+s*(rb1+s*(rb2+s*(rb3+s*rb4)));
175                 S=one+s*(sb1+s*(sb2+s*(sb3+s*sb4)));
176 	    }
177             SET_FLOAT_WORD(z,hx&0xffffe000);
178             r  = expf(-z*z-0.5625F)*expf((z-x)*(z+x)+R/S);
179 	    if(hx>0) return r/x; else return two-r/x;
180 	} else {
181 	    if(hx>0) return tiny*tiny; else return two-tiny;
182 	}
183 }
184