about summary refs log tree commit diff
path: root/src/math/exp.c
diff options
context:
space:
mode:
authorSzabolcs Nagy <nsz@port70.net>2012-11-18 03:42:09 +0100
committerSzabolcs Nagy <nsz@port70.net>2012-11-18 03:42:09 +0100
commitab1772c597ba8fe0c26400256b12d7a4df23880e (patch)
treeaa9e9df0cb237e2fdf3847300b9fc6b9547fc240 /src/math/exp.c
parent159c7655d06f04aa56a57385d633699c4c63d72c (diff)
downloadmusl-ab1772c597ba8fe0c26400256b12d7a4df23880e.tar.gz
musl-ab1772c597ba8fe0c26400256b12d7a4df23880e.tar.xz
musl-ab1772c597ba8fe0c26400256b12d7a4df23880e.zip
math: expf.c cleanup
similar to exp.c cleanup: use scalbnf, don't return excess precision,
drop some optimizatoins.
exp.c was changed to be more consistent with expf.c code.
Diffstat (limited to 'src/math/exp.c')
-rw-r--r--src/math/exp.c20
1 files changed, 11 insertions, 9 deletions
diff --git a/src/math/exp.c b/src/math/exp.c
index 5c0edee4..5ec8f8a7 100644
--- a/src/math/exp.c
+++ b/src/math/exp.c
@@ -80,7 +80,7 @@ P5   =  4.13813679705723846039e-08; /* 0x3E663769, 0x72BEA4D0 */
 
 double exp(double x)
 {
-	double hi, lo, c, z;
+	double hi, lo, c, xx;
 	int k, sign;
 	uint32_t hx;
 
@@ -92,24 +92,26 @@ double exp(double x)
 	if (hx >= 0x40862e42) {  /* if |x| >= 709.78... */
 		if (isnan(x))
 			return x;
+		if (hx == 0x7ff00000 && sign) /* -inf */
+			return 0;
 		if (x > 709.782712893383973096) {
 			/* overflow if x!=inf */
 			STRICT_ASSIGN(double, x, 0x1p1023 * x);
 			return x;
 		}
 		if (x < -745.13321910194110842) {
-			/* underflow if x!=-inf */
-			STRICT_ASSIGN(double, x, 0x1p-1000 / -x * 0x1p-1000);
+			/* underflow */
+			STRICT_ASSIGN(double, x, 0x1p-1000 * 0x1p-1000);
 			return x;
 		}
 	}
 
 	/* argument reduction */
 	if (hx > 0x3fd62e42) {  /* if |x| > 0.5 ln2 */
-		if (hx < 0x3ff0a2b2)  /* if |x| < 1.5 ln2 */
-			k = 1 - sign - sign; /* optimization */
-		else
+		if (hx >= 0x3ff0a2b2)  /* if |x| >= 1.5 ln2 */
 			k = (int)(invln2*x + half[sign]);
+		else
+			k = 1 - sign - sign;
 		hi = x - k*ln2hi;  /* k*ln2hi is exact here */
 		lo = k*ln2lo;
 		STRICT_ASSIGN(double, x, hi - lo);
@@ -124,9 +126,9 @@ double exp(double x)
 	}
 
 	/* x is now in primary range */
-	z = x*x;
-	c = x - z*(P1+z*(P2+z*(P3+z*(P4+z*P5))));
-	x = 1 + ((x*c/(2-c) - lo) + hi);
+	xx = x*x;
+	c = x - xx*(P1+xx*(P2+xx*(P3+xx*(P4+xx*P5))));
+	x = 1 + (x*c/(2-c) - lo + hi);
 	if (k == 0)
 		return x;
 	return scalbn(x, k);