X-Git-Url: http://nsz.repo.hu/git/?p=musl;a=blobdiff_plain;f=src%2Fmath%2Fcoshf.c;h=2bed1258a2ca9a87c88d4973c75dbb65c92499af;hp=97318f1098203109986f61483ff3ac211d1590c8;hb=14cc9c7f38c80094c05353fcb11fe9e441340583;hpb=9c6b1de0fb11d6e2927a19bbaf6345aedcc84297 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; }