blob: d6cfbd227678582c804f41208cdee31eb3e66096 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/* 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
Elliott Hughesa0ee0782013-01-30 19:06:37 -080016#include <sys/cdefs.h>
17__FBSDID("$FreeBSD$");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080018
19#include "math.h"
20#include "math_private.h"
21
Elliott Hughes460ad742014-09-12 14:00:02 -070022/* XXX Prevent compilers from erroneously constant folding: */
23static const volatile float tiny = 1e-30;
24
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080025static const float
Elliott Hughes460ad742014-09-12 14:00:02 -070026half= 0.5,
27one = 1,
28two = 2,
29erx = 8.42697144e-01, /* 0x3f57bb00 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080030/*
Elliott Hughes460ad742014-09-12 14:00:02 -070031 * In the domain [0, 2**-14], only the first term in the power series
32 * expansion of erf(x) is used. The magnitude of the first neglected
33 * terms is less than 2**-42.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080034 */
Elliott Hughes460ad742014-09-12 14:00:02 -070035efx = 1.28379166e-01, /* 0x3e0375d4 */
36efx8= 1.02703333e+00, /* 0x3f8375d4 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080037/*
Elliott Hughes460ad742014-09-12 14:00:02 -070038 * Domain [0, 0.84375], range ~[-5.4419e-10, 5.5179e-10]:
39 * |(erf(x) - x)/x - pp(x)/qq(x)| < 2**-31
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080040 */
Elliott Hughes460ad742014-09-12 14:00:02 -070041pp0 = 1.28379166e-01, /* 0x3e0375d4 */
42pp1 = -3.36030394e-01, /* 0xbeac0c2d */
43pp2 = -1.86261395e-03, /* 0xbaf422f4 */
44qq1 = 3.12324315e-01, /* 0x3e9fe8f9 */
45qq2 = 2.16070414e-02, /* 0x3cb10140 */
46qq3 = -1.98859372e-03, /* 0xbb025311 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080047/*
Elliott Hughes460ad742014-09-12 14:00:02 -070048 * Domain [0.84375, 1.25], range ~[-1.023e-9, 1.023e-9]:
49 * |(erf(x) - erx) - pa(x)/qa(x)| < 2**-31
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080050 */
Elliott Hughes460ad742014-09-12 14:00:02 -070051pa0 = 3.65041046e-06, /* 0x3674f993 */
52pa1 = 4.15109307e-01, /* 0x3ed48935 */
53pa2 = -2.09395722e-01, /* 0xbe566bd5 */
54pa3 = 8.67677554e-02, /* 0x3db1b34b */
55qa1 = 4.95560974e-01, /* 0x3efdba2b */
56qa2 = 3.71248513e-01, /* 0x3ebe1449 */
57qa3 = 3.92478965e-02, /* 0x3d20c267 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080058/*
Elliott Hughes460ad742014-09-12 14:00:02 -070059 * Domain [1.25,1/0.35], range ~[-4.821e-9, 4.927e-9]:
60 * |log(x*erfc(x)) + x**2 + 0.5625 - ra(x)/sa(x)| < 2**-28
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080061 */
Elliott Hughes460ad742014-09-12 14:00:02 -070062ra0 = -9.88156721e-03, /* 0xbc21e64c */
63ra1 = -5.43658376e-01, /* 0xbf0b2d32 */
64ra2 = -1.66828310e+00, /* 0xbfd58a4d */
65ra3 = -6.91554189e-01, /* 0xbf3109b2 */
66sa1 = 4.48581553e+00, /* 0x408f8bcd */
67sa2 = 4.10799170e+00, /* 0x408374ab */
68sa3 = 5.53855181e-01, /* 0x3f0dc974 */
Calin Juravlebd3155d2014-03-13 16:20:36 +000069/*
Elliott Hughes460ad742014-09-12 14:00:02 -070070 * Domain [2.85715, 11], range ~[-1.484e-9, 1.505e-9]:
71 * |log(x*erfc(x)) + x**2 + 0.5625 - rb(x)/sb(x)| < 2**-30
Calin Juravlebd3155d2014-03-13 16:20:36 +000072 */
Elliott Hughes460ad742014-09-12 14:00:02 -070073rb0 = -9.86496918e-03, /* 0xbc21a0ae */
74rb1 = -5.48049808e-01, /* 0xbf0c4cfe */
75rb2 = -1.84115684e+00, /* 0xbfebab07 */
76sb1 = 4.87132740e+00, /* 0x409be1ea */
77sb2 = 3.04982710e+00, /* 0x4043305e */
78sb3 = -7.61900663e-01; /* 0xbf430bec */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080079
80float
81erff(float x)
82{
83 int32_t hx,ix,i;
84 float R,S,P,Q,s,y,z,r;
85 GET_FLOAT_WORD(hx,x);
86 ix = hx&0x7fffffff;
Elliott Hughes460ad742014-09-12 14:00:02 -070087 if(ix>=0x7f800000) { /* erff(nan)=nan */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080088 i = ((u_int32_t)hx>>31)<<1;
Elliott Hughes460ad742014-09-12 14:00:02 -070089 return (float)(1-i)+one/x; /* erff(+-inf)=+-1 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080090 }
91
92 if(ix < 0x3f580000) { /* |x|<0.84375 */
Calin Juravlebd3155d2014-03-13 16:20:36 +000093 if(ix < 0x38800000) { /* |x|<2**-14 */
94 if (ix < 0x04000000) /* |x|<0x1p-119 */
95 return (8*x+efx8*x)/8; /* avoid spurious underflow */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080096 return x + efx*x;
97 }
98 z = x*x;
Calin Juravlebd3155d2014-03-13 16:20:36 +000099 r = pp0+z*(pp1+z*pp2);
100 s = one+z*(qq1+z*(qq2+z*qq3));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800101 y = r/s;
102 return x + x*y;
103 }
104 if(ix < 0x3fa00000) { /* 0.84375 <= |x| < 1.25 */
105 s = fabsf(x)-one;
Calin Juravlebd3155d2014-03-13 16:20:36 +0000106 P = pa0+s*(pa1+s*(pa2+s*pa3));
Elliott Hughes460ad742014-09-12 14:00:02 -0700107 Q = one+s*(qa1+s*(qa2+s*qa3));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800108 if(hx>=0) return erx + P/Q; else return -erx - P/Q;
109 }
Calin Juravlebd3155d2014-03-13 16:20:36 +0000110 if (ix >= 0x40800000) { /* inf>|x|>=4 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800111 if(hx>=0) return one-tiny; else return tiny-one;
112 }
113 x = fabsf(x);
114 s = one/(x*x);
Elliott Hughes460ad742014-09-12 14:00:02 -0700115 if(ix< 0x4036db8c) { /* |x| < 2.85715 ~ 1/0.35 */
Calin Juravlebd3155d2014-03-13 16:20:36 +0000116 R=ra0+s*(ra1+s*(ra2+s*ra3));
Elliott Hughes460ad742014-09-12 14:00:02 -0700117 S=one+s*(sa1+s*(sa2+s*sa3));
118 } else { /* |x| >= 2.85715 ~ 1/0.35 */
119 R=rb0+s*(rb1+s*rb2);
120 S=one+s*(sb1+s*(sb2+s*sb3));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800121 }
Calin Juravlebd3155d2014-03-13 16:20:36 +0000122 SET_FLOAT_WORD(z,hx&0xffffe000);
123 r = expf(-z*z-0.5625F)*expf((z-x)*(z+x)+R/S);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800124 if(hx>=0) return one-r/x; else return r/x-one;
125}
126
127float
128erfcf(float x)
129{
130 int32_t hx,ix;
131 float R,S,P,Q,s,y,z,r;
132 GET_FLOAT_WORD(hx,x);
133 ix = hx&0x7fffffff;
Elliott Hughes460ad742014-09-12 14:00:02 -0700134 if(ix>=0x7f800000) { /* erfcf(nan)=nan */
135 /* erfcf(+-inf)=0,2 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800136 return (float)(((u_int32_t)hx>>31)<<1)+one/x;
137 }
138
139 if(ix < 0x3f580000) { /* |x|<0.84375 */
Calin Juravlebd3155d2014-03-13 16:20:36 +0000140 if(ix < 0x33800000) /* |x|<2**-24 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800141 return one-x;
142 z = x*x;
Calin Juravlebd3155d2014-03-13 16:20:36 +0000143 r = pp0+z*(pp1+z*pp2);
144 s = one+z*(qq1+z*(qq2+z*qq3));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800145 y = r/s;
146 if(hx < 0x3e800000) { /* x<1/4 */
147 return one-(x+x*y);
148 } else {
149 r = x*y;
150 r += (x-half);
151 return half - r ;
152 }
153 }
154 if(ix < 0x3fa00000) { /* 0.84375 <= |x| < 1.25 */
155 s = fabsf(x)-one;
Calin Juravlebd3155d2014-03-13 16:20:36 +0000156 P = pa0+s*(pa1+s*(pa2+s*pa3));
Elliott Hughes460ad742014-09-12 14:00:02 -0700157 Q = one+s*(qa1+s*(qa2+s*qa3));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800158 if(hx>=0) {
159 z = one-erx; return z - P/Q;
160 } else {
161 z = erx+P/Q; return one+z;
162 }
163 }
Calin Juravlebd3155d2014-03-13 16:20:36 +0000164 if (ix < 0x41300000) { /* |x|<11 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800165 x = fabsf(x);
166 s = one/(x*x);
Elliott Hughes460ad742014-09-12 14:00:02 -0700167 if(ix< 0x4036db8c) { /* |x| < 2.85715 ~ 1/.35 */
168 R=ra0+s*(ra1+s*(ra2+s*ra3));
169 S=one+s*(sa1+s*(sa2+s*sa3));
170 } else { /* |x| >= 2.85715 ~ 1/.35 */
Calin Juravlebd3155d2014-03-13 16:20:36 +0000171 if(hx<0&&ix>=0x40a00000) return two-tiny;/* x < -5 */
Elliott Hughes460ad742014-09-12 14:00:02 -0700172 R=rb0+s*(rb1+s*rb2);
173 S=one+s*(sb1+s*(sb2+s*sb3));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800174 }
Calin Juravlebd3155d2014-03-13 16:20:36 +0000175 SET_FLOAT_WORD(z,hx&0xffffe000);
176 r = expf(-z*z-0.5625F)*expf((z-x)*(z+x)+R/S);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800177 if(hx>0) return r/x; else return two-r/x;
178 } else {
179 if(hx>0) return tiny*tiny; else return two-tiny;
180 }
181}