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