From 14cc9c7f38c80094c05353fcb11fe9e441340583 Mon Sep 17 00:00:00 2001 From: Szabolcs Nagy Date: Wed, 12 Dec 2012 01:39:23 +0100 Subject: math: cosh cleanup do fabs by hand, don't check for nan and inf separately --- src/math/coshf.c | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) (limited to 'src/math/coshf.c') 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; } -- cgit v1.2.1