1 /* s_erff.c -- float version of s_erf.c. 2 * Conversion to float by Ian Lance Taylor, Cygnus Support, ian (at) 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 <sys/cdefs.h> 17 __FBSDID("$FreeBSD$"); 18 19 #include "math.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 float 82 erff(float x) 83 { 84 int32_t hx,ix,i; 85 float R,S,P,Q,s,y,z,r; 86 GET_FLOAT_WORD(hx,x); 87 ix = hx&0x7fffffff; 88 if(ix>=0x7f800000) { /* erf(nan)=nan */ 89 i = ((u_int32_t)hx>>31)<<1; 90 return (float)(1-i)+one/x; /* erf(+-inf)=+-1 */ 91 } 92 93 if(ix < 0x3f580000) { /* |x|<0.84375 */ 94 if(ix < 0x38800000) { /* |x|<2**-14 */ 95 if (ix < 0x04000000) /* |x|<0x1p-119 */ 96 return (8*x+efx8*x)/8; /* avoid spurious underflow */ 97 return x + efx*x; 98 } 99 z = x*x; 100 r = pp0+z*(pp1+z*pp2); 101 s = one+z*(qq1+z*(qq2+z*qq3)); 102 y = r/s; 103 return x + x*y; 104 } 105 if(ix < 0x3fa00000) { /* 0.84375 <= |x| < 1.25 */ 106 s = fabsf(x)-one; 107 P = pa0+s*(pa1+s*(pa2+s*pa3)); 108 Q = one+s*(qa1+s*(qa2+s*(qa3+s*qa4))); 109 if(hx>=0) return erx + P/Q; else return -erx - P/Q; 110 } 111 if (ix >= 0x40800000) { /* inf>|x|>=4 */ 112 if(hx>=0) return one-tiny; else return tiny-one; 113 } 114 x = fabsf(x); 115 s = one/(x*x); 116 if(ix< 0x4036DB6E) { /* |x| < 1/0.35 */ 117 R=ra0+s*(ra1+s*(ra2+s*ra3)); 118 S=one+s*(sa1+s*(sa2+s*(sa3+s*sa4))); 119 } else { /* |x| >= 1/0.35 */ 120 R=rb0+s*(rb1+s*(rb2+s*(rb3+s*rb4))); 121 S=one+s*(sb1+s*(sb2+s*(sb3+s*sb4))); 122 } 123 SET_FLOAT_WORD(z,hx&0xffffe000); 124 r = expf(-z*z-0.5625F)*expf((z-x)*(z+x)+R/S); 125 if(hx>=0) return one-r/x; else return r/x-one; 126 } 127 128 float 129 erfcf(float x) 130 { 131 int32_t hx,ix; 132 float R,S,P,Q,s,y,z,r; 133 GET_FLOAT_WORD(hx,x); 134 ix = hx&0x7fffffff; 135 if(ix>=0x7f800000) { /* erfc(nan)=nan */ 136 /* erfc(+-inf)=0,2 */ 137 return (float)(((u_int32_t)hx>>31)<<1)+one/x; 138 } 139 140 if(ix < 0x3f580000) { /* |x|<0.84375 */ 141 if(ix < 0x33800000) /* |x|<2**-24 */ 142 return one-x; 143 z = x*x; 144 r = pp0+z*(pp1+z*pp2); 145 s = one+z*(qq1+z*(qq2+z*qq3)); 146 y = r/s; 147 if(hx < 0x3e800000) { /* x<1/4 */ 148 return one-(x+x*y); 149 } else { 150 r = x*y; 151 r += (x-half); 152 return half - r ; 153 } 154 } 155 if(ix < 0x3fa00000) { /* 0.84375 <= |x| < 1.25 */ 156 s = fabsf(x)-one; 157 P = pa0+s*(pa1+s*(pa2+s*pa3)); 158 Q = one+s*(qa1+s*(qa2+s*(qa3+s*qa4))); 159 if(hx>=0) { 160 z = one-erx; return z - P/Q; 161 } else { 162 z = erx+P/Q; return one+z; 163 } 164 } 165 if (ix < 0x41300000) { /* |x|<11 */ 166 x = fabsf(x); 167 s = one/(x*x); 168 if(ix< 0x4036DB6D) { /* |x| < 1/.35 ~ 2.857143*/ 169 R=ra0+s*(ra1+s*(ra2+s*ra3)); 170 S=one+s*(sa1+s*(sa2+s*(sa3+s*sa4))); 171 } else { /* |x| >= 1/.35 ~ 2.857143 */ 172 if(hx<0&&ix>=0x40a00000) return two-tiny;/* x < -5 */ 173 R=rb0+s*(rb1+s*(rb2+s*(rb3+s*rb4))); 174 S=one+s*(sb1+s*(sb2+s*(sb3+s*sb4))); 175 } 176 SET_FLOAT_WORD(z,hx&0xffffe000); 177 r = expf(-z*z-0.5625F)*expf((z-x)*(z+x)+R/S); 178 if(hx>0) return r/x; else return two-r/x; 179 } else { 180 if(hx>0) return tiny*tiny; else return two-tiny; 181 } 182 } 183