1570af302Sopenharmony_ci#include "libm.h"
2570af302Sopenharmony_ci
3570af302Sopenharmony_ci/* asinh(x) = sign(x)*log(|x|+sqrt(x*x+1)) ~= x - x^3/6 + o(x^5) */
4570af302Sopenharmony_cidouble asinh(double x)
5570af302Sopenharmony_ci{
6570af302Sopenharmony_ci	union {double f; uint64_t i;} u = {.f = x};
7570af302Sopenharmony_ci	unsigned e = u.i >> 52 & 0x7ff;
8570af302Sopenharmony_ci	unsigned s = u.i >> 63;
9570af302Sopenharmony_ci
10570af302Sopenharmony_ci	/* |x| */
11570af302Sopenharmony_ci	u.i &= (uint64_t)-1/2;
12570af302Sopenharmony_ci	x = u.f;
13570af302Sopenharmony_ci
14570af302Sopenharmony_ci	if (e >= 0x3ff + 26) {
15570af302Sopenharmony_ci		/* |x| >= 0x1p26 or inf or nan */
16570af302Sopenharmony_ci		x = log(x) + 0.693147180559945309417232121458176568;
17570af302Sopenharmony_ci	} else if (e >= 0x3ff + 1) {
18570af302Sopenharmony_ci		/* |x| >= 2 */
19570af302Sopenharmony_ci		x = log(2*x + 1/(sqrt(x*x+1)+x));
20570af302Sopenharmony_ci	} else if (e >= 0x3ff - 26) {
21570af302Sopenharmony_ci		/* |x| >= 0x1p-26, up to 1.6ulp error in [0.125,0.5] */
22570af302Sopenharmony_ci		x = log1p(x + x*x/(sqrt(x*x+1)+1));
23570af302Sopenharmony_ci	} else {
24570af302Sopenharmony_ci		/* |x| < 0x1p-26, raise inexact if x != 0 */
25570af302Sopenharmony_ci		FORCE_EVAL(x + 0x1p120f);
26570af302Sopenharmony_ci	}
27570af302Sopenharmony_ci	return s ? -x : x;
28570af302Sopenharmony_ci}
29