1#include "libm.h"
2
3/* sinh(x) = (exp(x) - 1/exp(x))/2
4 *         = (exp(x)-1 + (exp(x)-1)/exp(x))/2
5 *         = x + x^3/6 + o(x^5)
6 */
7double sinh(double x) {
8    union {
9        double f;
10        uint64_t i;
11    } u = {.f = x};
12    uint32_t w;
13    double t, h, absx;
14
15    h = 0.5;
16    if (u.i >> 63)
17        h = -h;
18    /* |x| */
19    u.i &= (uint64_t)-1 / 2;
20    absx = u.f;
21    w = u.i >> 32;
22
23    /* |x| < log(DBL_MAX) */
24    if (w < 0x40862e42) {
25        t = expm1(absx);
26        if (w < 0x3ff00000) {
27            if (w < 0x3ff00000 - (26 << 20)) /* note: inexact and underflow are raised by expm1 */
28                /* note: this branch avoids spurious underflow */
29                return x;
30            return h * (2 * t - t * t / (t + 1));
31        }
32        /* note: |x|>log(0x1p26)+eps could be just h*exp(x) */
33        return h * (t + t / (t + 1));
34    }
35
36    /* |x| > log(DBL_MAX) or nan */
37    /* note: the result is stored to handle overflow */
38    t = 2 * h * __expo2(absx);
39    return t;
40}
41