math: rewrite inverse hyperbolic functions to be simpler/smaller
[musl] / src / math / asinh.c
1 #include "libm.h"
2
3 /* asinh(x) = sign(x)*log(|x|+sqrt(x*x+1)) ~= x - x^3/6 + o(x^5) */
4 double asinh(double x)
5 {
6         union {double f; uint64_t i;} u = {.f = x};
7         unsigned e = u.i >> 52 & 0x7ff;
8         unsigned s = u.i >> 63;
9
10         /* |x| */
11         u.i &= (uint64_t)-1/2;
12         x = u.f;
13
14         if (e >= 0x3ff + 26) {
15                 /* |x| >= 0x1p26 or inf or nan */
16                 x = log(x) + 0.693147180559945309417232121458176568;
17         } else if (e >= 0x3ff + 1) {
18                 /* |x| >= 2 */
19                 x = log(2*x + 1/(sqrt(x*x+1)+x));
20         } else if (e >= 0x3ff - 26) {
21                 /* |x| >= 0x1p-26, up to 1.6ulp error in [0.125,0.5] */
22                 x = log1p(x + x*x/(sqrt(x*x+1)+1));
23         } else {
24                 /* |x| < 0x1p-26, raise inexact if x != 0 */
25                 FORCE_EVAL(x + 0x1p1000);
26         }
27         return s ? -x : x;
28 }