1*998b640bSDavid Schultz /* from: FreeBSD: head/lib/msun/src/e_acosh.c 176451 2008-02-22 02:30:36Z das */
2*998b640bSDavid Schultz
3*998b640bSDavid Schultz /*
4*998b640bSDavid Schultz * ====================================================
5*998b640bSDavid Schultz * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
6*998b640bSDavid Schultz *
7*998b640bSDavid Schultz * Developed at SunPro, a Sun Microsystems, Inc. business.
8*998b640bSDavid Schultz * Permission to use, copy, modify, and distribute this
9*998b640bSDavid Schultz * software is freely granted, provided that this notice
10*998b640bSDavid Schultz * is preserved.
11*998b640bSDavid Schultz * ====================================================
12*998b640bSDavid Schultz */
13*998b640bSDavid Schultz
14*998b640bSDavid Schultz /*
15*998b640bSDavid Schultz * See s_asinh.c for complete comments.
16*998b640bSDavid Schultz *
17*998b640bSDavid Schultz * Converted to long double by David Schultz <das@FreeBSD.ORG> and
18*998b640bSDavid Schultz * Bruce D. Evans.
19*998b640bSDavid Schultz */
20*998b640bSDavid Schultz
21*998b640bSDavid Schultz #include <float.h>
22*998b640bSDavid Schultz #ifdef __i386__
23*998b640bSDavid Schultz #include <ieeefp.h>
24*998b640bSDavid Schultz #endif
25*998b640bSDavid Schultz
26*998b640bSDavid Schultz #include "fpmath.h"
27*998b640bSDavid Schultz #include "math.h"
28*998b640bSDavid Schultz #include "math_private.h"
29*998b640bSDavid Schultz
30*998b640bSDavid Schultz /* EXP_LARGE is the threshold above which we use asinh(x) ~= log(2x). */
31*998b640bSDavid Schultz /* EXP_TINY is the threshold below which we use asinh(x) ~= x. */
32*998b640bSDavid Schultz #if LDBL_MANT_DIG == 64
33*998b640bSDavid Schultz #define EXP_LARGE 34
34*998b640bSDavid Schultz #define EXP_TINY -34
35*998b640bSDavid Schultz #elif LDBL_MANT_DIG == 113
36*998b640bSDavid Schultz #define EXP_LARGE 58
37*998b640bSDavid Schultz #define EXP_TINY -58
38*998b640bSDavid Schultz #else
39*998b640bSDavid Schultz #error "Unsupported long double format"
40*998b640bSDavid Schultz #endif
41*998b640bSDavid Schultz
42*998b640bSDavid Schultz #if LDBL_MAX_EXP != 0x4000
43*998b640bSDavid Schultz /* We also require the usual expsign encoding. */
44*998b640bSDavid Schultz #error "Unsupported long double format"
45*998b640bSDavid Schultz #endif
46*998b640bSDavid Schultz
47*998b640bSDavid Schultz #define BIAS (LDBL_MAX_EXP - 1)
48*998b640bSDavid Schultz
49*998b640bSDavid Schultz static const double
50*998b640bSDavid Schultz one = 1.00000000000000000000e+00, /* 0x3FF00000, 0x00000000 */
51*998b640bSDavid Schultz huge= 1.00000000000000000000e+300;
52*998b640bSDavid Schultz
53*998b640bSDavid Schultz #if LDBL_MANT_DIG == 64
54*998b640bSDavid Schultz static const union IEEEl2bits
55*998b640bSDavid Schultz u_ln2 = LD80C(0xb17217f7d1cf79ac, -1, 6.93147180559945309417e-1L);
56*998b640bSDavid Schultz #define ln2 u_ln2.e
57*998b640bSDavid Schultz #elif LDBL_MANT_DIG == 113
58*998b640bSDavid Schultz static const long double
59*998b640bSDavid Schultz ln2 = 6.93147180559945309417232121458176568e-1L; /* 0x162e42fefa39ef35793c7673007e6.0p-113 */
60*998b640bSDavid Schultz #else
61*998b640bSDavid Schultz #error "Unsupported long double format"
62*998b640bSDavid Schultz #endif
63*998b640bSDavid Schultz
64*998b640bSDavid Schultz long double
asinhl(long double x)65*998b640bSDavid Schultz asinhl(long double x)
66*998b640bSDavid Schultz {
67*998b640bSDavid Schultz long double t, w;
68*998b640bSDavid Schultz uint16_t hx, ix;
69*998b640bSDavid Schultz
70*998b640bSDavid Schultz ENTERI();
71*998b640bSDavid Schultz GET_LDBL_EXPSIGN(hx, x);
72*998b640bSDavid Schultz ix = hx & 0x7fff;
73*998b640bSDavid Schultz if (ix >= 0x7fff) RETURNI(x+x); /* x is inf, NaN or misnormal */
74*998b640bSDavid Schultz if (ix < BIAS + EXP_TINY) { /* |x| < TINY, or misnormal */
75*998b640bSDavid Schultz if (huge + x > one) RETURNI(x); /* return x inexact except 0 */
76*998b640bSDavid Schultz }
77*998b640bSDavid Schultz if (ix >= BIAS + EXP_LARGE) { /* |x| >= LARGE, or misnormal */
78*998b640bSDavid Schultz w = logl(fabsl(x))+ln2;
79*998b640bSDavid Schultz } else if (ix >= 0x4000) { /* LARGE > |x| >= 2.0, or misnormal */
80*998b640bSDavid Schultz t = fabsl(x);
81*998b640bSDavid Schultz w = logl(2.0*t+one/(sqrtl(x*x+one)+t));
82*998b640bSDavid Schultz } else { /* 2.0 > |x| >= TINY, or misnormal */
83*998b640bSDavid Schultz t = x*x;
84*998b640bSDavid Schultz w =log1pl(fabsl(x)+t/(one+sqrtl(one+t)));
85*998b640bSDavid Schultz }
86*998b640bSDavid Schultz RETURNI((hx & 0x8000) == 0 ? w : -w);
87*998b640bSDavid Schultz }
88