11dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* s_erff.c -- float version of s_erf.c. 21dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. 31dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */ 41dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 51dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* 61dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * ==================================================== 71dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 81dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 91dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Developed at SunPro, a Sun Microsystems, Inc. business. 101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Permission to use, copy, modify, and distribute this 111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * software is freely granted, provided that this notice 121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * is preserved. 131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * ==================================================== 141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */ 151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 16a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes#include <sys/cdefs.h> 17a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes__FBSDID("$FreeBSD$"); 181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include "math.h" 201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include "math_private.h" 211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 22460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughes/* XXX Prevent compilers from erroneously constant folding: */ 23460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughesstatic const volatile float tiny = 1e-30; 24460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughes 251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic const float 26460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hugheshalf= 0.5, 27460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughesone = 1, 28460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughestwo = 2, 29460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hugheserx = 8.42697144e-01, /* 0x3f57bb00 */ 301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* 31460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughes * In the domain [0, 2**-14], only the first term in the power series 32460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughes * expansion of erf(x) is used. The magnitude of the first neglected 33460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughes * terms is less than 2**-42. 341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */ 35460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughesefx = 1.28379166e-01, /* 0x3e0375d4 */ 36460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughesefx8= 1.02703333e+00, /* 0x3f8375d4 */ 371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* 38460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughes * Domain [0, 0.84375], range ~[-5.4419e-10, 5.5179e-10]: 39460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughes * |(erf(x) - x)/x - pp(x)/qq(x)| < 2**-31 401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */ 41460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughespp0 = 1.28379166e-01, /* 0x3e0375d4 */ 42460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughespp1 = -3.36030394e-01, /* 0xbeac0c2d */ 43460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughespp2 = -1.86261395e-03, /* 0xbaf422f4 */ 44460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughesqq1 = 3.12324315e-01, /* 0x3e9fe8f9 */ 45460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughesqq2 = 2.16070414e-02, /* 0x3cb10140 */ 46460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughesqq3 = -1.98859372e-03, /* 0xbb025311 */ 471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* 48460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughes * Domain [0.84375, 1.25], range ~[-1.023e-9, 1.023e-9]: 49460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughes * |(erf(x) - erx) - pa(x)/qa(x)| < 2**-31 501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */ 51460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughespa0 = 3.65041046e-06, /* 0x3674f993 */ 52460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughespa1 = 4.15109307e-01, /* 0x3ed48935 */ 53460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughespa2 = -2.09395722e-01, /* 0xbe566bd5 */ 54460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughespa3 = 8.67677554e-02, /* 0x3db1b34b */ 55460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughesqa1 = 4.95560974e-01, /* 0x3efdba2b */ 56460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughesqa2 = 3.71248513e-01, /* 0x3ebe1449 */ 57460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughesqa3 = 3.92478965e-02, /* 0x3d20c267 */ 581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* 59460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughes * Domain [1.25,1/0.35], range ~[-4.821e-9, 4.927e-9]: 60460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughes * |log(x*erfc(x)) + x**2 + 0.5625 - ra(x)/sa(x)| < 2**-28 611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */ 62460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughesra0 = -9.88156721e-03, /* 0xbc21e64c */ 63460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughesra1 = -5.43658376e-01, /* 0xbf0b2d32 */ 64460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughesra2 = -1.66828310e+00, /* 0xbfd58a4d */ 65460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughesra3 = -6.91554189e-01, /* 0xbf3109b2 */ 66460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughessa1 = 4.48581553e+00, /* 0x408f8bcd */ 67460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughessa2 = 4.10799170e+00, /* 0x408374ab */ 68460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughessa3 = 5.53855181e-01, /* 0x3f0dc974 */ 69bd3155dc5ddf09647388ad7f8fdbe6df123fbd6aCalin Juravle/* 70460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughes * Domain [2.85715, 11], range ~[-1.484e-9, 1.505e-9]: 71460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughes * |log(x*erfc(x)) + x**2 + 0.5625 - rb(x)/sb(x)| < 2**-30 72bd3155dc5ddf09647388ad7f8fdbe6df123fbd6aCalin Juravle */ 73460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughesrb0 = -9.86496918e-03, /* 0xbc21a0ae */ 74460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughesrb1 = -5.48049808e-01, /* 0xbf0c4cfe */ 75460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughesrb2 = -1.84115684e+00, /* 0xbfebab07 */ 76460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughessb1 = 4.87132740e+00, /* 0x409be1ea */ 77460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughessb2 = 3.04982710e+00, /* 0x4043305e */ 78460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughessb3 = -7.61900663e-01; /* 0xbf430bec */ 791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectfloat 811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projecterff(float x) 821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int32_t hx,ix,i; 841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project float R,S,P,Q,s,y,z,r; 851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project GET_FLOAT_WORD(hx,x); 861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project ix = hx&0x7fffffff; 87460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughes if(ix>=0x7f800000) { /* erff(nan)=nan */ 881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project i = ((u_int32_t)hx>>31)<<1; 89460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughes return (float)(1-i)+one/x; /* erff(+-inf)=+-1 */ 901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if(ix < 0x3f580000) { /* |x|<0.84375 */ 93bd3155dc5ddf09647388ad7f8fdbe6df123fbd6aCalin Juravle if(ix < 0x38800000) { /* |x|<2**-14 */ 94bd3155dc5ddf09647388ad7f8fdbe6df123fbd6aCalin Juravle if (ix < 0x04000000) /* |x|<0x1p-119 */ 95bd3155dc5ddf09647388ad7f8fdbe6df123fbd6aCalin Juravle return (8*x+efx8*x)/8; /* avoid spurious underflow */ 961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return x + efx*x; 971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project z = x*x; 99bd3155dc5ddf09647388ad7f8fdbe6df123fbd6aCalin Juravle r = pp0+z*(pp1+z*pp2); 100bd3155dc5ddf09647388ad7f8fdbe6df123fbd6aCalin Juravle s = one+z*(qq1+z*(qq2+z*qq3)); 1011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project y = r/s; 1021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return x + x*y; 1031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 1041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if(ix < 0x3fa00000) { /* 0.84375 <= |x| < 1.25 */ 1051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project s = fabsf(x)-one; 106bd3155dc5ddf09647388ad7f8fdbe6df123fbd6aCalin Juravle P = pa0+s*(pa1+s*(pa2+s*pa3)); 107460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughes Q = one+s*(qa1+s*(qa2+s*qa3)); 1081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if(hx>=0) return erx + P/Q; else return -erx - P/Q; 1091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 110bd3155dc5ddf09647388ad7f8fdbe6df123fbd6aCalin Juravle if (ix >= 0x40800000) { /* inf>|x|>=4 */ 1111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if(hx>=0) return one-tiny; else return tiny-one; 1121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 1131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project x = fabsf(x); 1141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project s = one/(x*x); 115460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughes if(ix< 0x4036db8c) { /* |x| < 2.85715 ~ 1/0.35 */ 116bd3155dc5ddf09647388ad7f8fdbe6df123fbd6aCalin Juravle R=ra0+s*(ra1+s*(ra2+s*ra3)); 117460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughes S=one+s*(sa1+s*(sa2+s*sa3)); 118460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughes } else { /* |x| >= 2.85715 ~ 1/0.35 */ 119460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughes R=rb0+s*(rb1+s*rb2); 120460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughes S=one+s*(sb1+s*(sb2+s*sb3)); 1211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 122bd3155dc5ddf09647388ad7f8fdbe6df123fbd6aCalin Juravle SET_FLOAT_WORD(z,hx&0xffffe000); 123bd3155dc5ddf09647388ad7f8fdbe6df123fbd6aCalin Juravle r = expf(-z*z-0.5625F)*expf((z-x)*(z+x)+R/S); 1241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if(hx>=0) return one-r/x; else return r/x-one; 1251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 1261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectfloat 1281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projecterfcf(float x) 1291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 1301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int32_t hx,ix; 1311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project float R,S,P,Q,s,y,z,r; 1321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project GET_FLOAT_WORD(hx,x); 1331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project ix = hx&0x7fffffff; 134460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughes if(ix>=0x7f800000) { /* erfcf(nan)=nan */ 135460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughes /* erfcf(+-inf)=0,2 */ 1361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return (float)(((u_int32_t)hx>>31)<<1)+one/x; 1371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 1381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if(ix < 0x3f580000) { /* |x|<0.84375 */ 140bd3155dc5ddf09647388ad7f8fdbe6df123fbd6aCalin Juravle if(ix < 0x33800000) /* |x|<2**-24 */ 1411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return one-x; 1421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project z = x*x; 143bd3155dc5ddf09647388ad7f8fdbe6df123fbd6aCalin Juravle r = pp0+z*(pp1+z*pp2); 144bd3155dc5ddf09647388ad7f8fdbe6df123fbd6aCalin Juravle s = one+z*(qq1+z*(qq2+z*qq3)); 1451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project y = r/s; 1461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if(hx < 0x3e800000) { /* x<1/4 */ 1471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return one-(x+x*y); 1481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } else { 1491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project r = x*y; 1501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project r += (x-half); 1511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return half - r ; 1521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 1531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 1541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if(ix < 0x3fa00000) { /* 0.84375 <= |x| < 1.25 */ 1551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project s = fabsf(x)-one; 156bd3155dc5ddf09647388ad7f8fdbe6df123fbd6aCalin Juravle P = pa0+s*(pa1+s*(pa2+s*pa3)); 157460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughes Q = one+s*(qa1+s*(qa2+s*qa3)); 1581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if(hx>=0) { 1591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project z = one-erx; return z - P/Q; 1601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } else { 1611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project z = erx+P/Q; return one+z; 1621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 1631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 164bd3155dc5ddf09647388ad7f8fdbe6df123fbd6aCalin Juravle if (ix < 0x41300000) { /* |x|<11 */ 1651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project x = fabsf(x); 1661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project s = one/(x*x); 167460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughes if(ix< 0x4036db8c) { /* |x| < 2.85715 ~ 1/.35 */ 168460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughes R=ra0+s*(ra1+s*(ra2+s*ra3)); 169460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughes S=one+s*(sa1+s*(sa2+s*sa3)); 170460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughes } else { /* |x| >= 2.85715 ~ 1/.35 */ 171bd3155dc5ddf09647388ad7f8fdbe6df123fbd6aCalin Juravle if(hx<0&&ix>=0x40a00000) return two-tiny;/* x < -5 */ 172460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughes R=rb0+s*(rb1+s*rb2); 173460ad7454a281470a1938103f099a1e3192fb3d6Elliott Hughes S=one+s*(sb1+s*(sb2+s*sb3)); 1741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 175bd3155dc5ddf09647388ad7f8fdbe6df123fbd6aCalin Juravle SET_FLOAT_WORD(z,hx&0xffffe000); 176bd3155dc5ddf09647388ad7f8fdbe6df123fbd6aCalin Juravle r = expf(-z*z-0.5625F)*expf((z-x)*(z+x)+R/S); 1771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if(hx>0) return r/x; else return two-r/x; 1781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } else { 1791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if(hx>0) return tiny*tiny; else return two-tiny; 1801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 1811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 182