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