about summary refs log tree commit diff
path: root/src/math/coshf.c
diff options
context:
space:
mode:
authorSzabolcs Nagy <nsz@port70.net>2012-12-12 01:39:23 +0100
committerSzabolcs Nagy <nsz@port70.net>2012-12-12 01:39:23 +0100
commit14cc9c7f38c80094c05353fcb11fe9e441340583 (patch)
treed7670bf9fd274ff699746e28d49c0e70f6f93659 /src/math/coshf.c
parent9c6b1de0fb11d6e2927a19bbaf6345aedcc84297 (diff)
downloadmusl-14cc9c7f38c80094c05353fcb11fe9e441340583.tar.gz
musl-14cc9c7f38c80094c05353fcb11fe9e441340583.tar.xz
musl-14cc9c7f38c80094c05353fcb11fe9e441340583.zip
math: cosh cleanup
do fabs by hand, don't check for nan and inf separately
Diffstat (limited to 'src/math/coshf.c')
-rw-r--r--src/math/coshf.c37
1 files changed, 17 insertions, 20 deletions
diff --git a/src/math/coshf.c b/src/math/coshf.c
index 97318f10..2bed1258 100644
--- a/src/math/coshf.c
+++ b/src/math/coshf.c
@@ -15,43 +15,40 @@
 
 #include "libm.h"
 
-static const float huge = 1.0e30;
-
 float coshf(float x)
 {
-	float t, w;
-	int32_t ix;
-
-	GET_FLOAT_WORD(ix, x);
-	ix &= 0x7fffffff;
+	union {float f; uint32_t i;} u = {.f = x};
+	uint32_t ix;
+	float t;
 
-	/* x is INF or NaN */
-	if (ix >= 0x7f800000)
-		return x*x;
+	/* |x| */
+	u.i &= 0x7fffffff;
+	x = u.f;
+	ix = u.i;
 
 	/* |x| in [0,0.5*ln2], return 1+expm1(|x|)^2/(2*exp(|x|)) */
 	if (ix < 0x3eb17218) {
-		t = expm1f(fabsf(x));
-		w = 1.0f+t;
-		if (ix<0x39800000)
-			return 1.0f;  /* cosh(tiny) = 1 */
-		return 1.0f + (t*t)/(w+w);
+		t = expm1f(x);
+		if (ix < 0x39800000)
+			return 1;
+		return 1 + t*t/(2*(1+t));
 	}
 
 	/* |x| in [0.5*ln2,9], return (exp(|x|)+1/exp(|x|))/2; */
 	if (ix < 0x41100000) {
-		t = expf(fabsf(x));
+		t = expf(x);
 		return 0.5f*t + 0.5f/t;
 	}
 
 	/* |x| in [9, log(maxfloat)] return 0.5f*exp(|x|) */
 	if (ix < 0x42b17217)
-		return 0.5f*expf(fabsf(x));
+		return 0.5f*expf(x);
 
 	/* |x| in [log(maxfloat), overflowthresold] */
 	if (ix <= 0x42b2d4fc)
-		return __expo2f(fabsf(x));
+		return __expo2f(x);
 
-	/* |x| > overflowthresold, cosh(x) overflow */
-	return huge*huge;
+	/* |x| > overflowthresold or nan */
+	x *= 0x1p127f;
+	return x;
 }