about summary refs log tree commit diff
path: root/src/math/coshf.c
diff options
context:
space:
mode:
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;
 }