1#include "libm.h"
2
3/* asinh(x) = sign(x)*log(|x|+sqrt(x*x+1)) ~= x - x^3/6 + o(x^5) */
4float asinhf(float x) {
5    union {
6        float f;
7        uint32_t i;
8    } u = {.f = x};
9    uint32_t i = u.i & 0x7fffffff;
10    unsigned s = u.i >> 31;
11
12    /* |x| */
13    u.i = i;
14    x = u.f;
15
16    if (i >= 0x3f800000 + (12 << 23)) {
17        /* |x| >= 0x1p12 or inf or nan */
18        x = logf(x) + 0.693147180559945309417232121458176568f;
19    } else if (i >= 0x3f800000 + (1 << 23)) {
20        /* |x| >= 2 */
21        x = logf(2 * x + 1 / (sqrtf(x * x + 1) + x));
22    } else if (i >= 0x3f800000 - (12 << 23)) {
23        /* |x| >= 0x1p-12, up to 1.6ulp error in [0.125,0.5] */
24        x = log1pf(x + x * x / (sqrtf(x * x + 1) + 1));
25    } else {
26        /* |x| < 0x1p-12, raise inexact if x!=0 */
27        FORCE_EVAL(x + 0x1p120f);
28    }
29    return s ? -x : x;
30}
31