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