xref: /freebsd/lib/msun/src/e_acoshl.c (revision fd45b686)
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 SunSoft, 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 
15 /*
16  * See e_acosh.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 acosh(x) ~= log(2x). */
32 #if LDBL_MANT_DIG == 64
33 #define	EXP_LARGE	34
34 #elif LDBL_MANT_DIG == 113
35 #define	EXP_LARGE	58
36 #else
37 #error "Unsupported long double format"
38 #endif
39 
40 #if LDBL_MAX_EXP != 0x4000
41 /* We also require the usual expsign encoding. */
42 #error "Unsupported long double format"
43 #endif
44 
45 #define	BIAS	(LDBL_MAX_EXP - 1)
46 
47 static const double
48 one	= 1.0;
49 
50 #if LDBL_MANT_DIG == 64
51 static const union IEEEl2bits
52 u_ln2 =  LD80C(0xb17217f7d1cf79ac, -1, 6.93147180559945309417e-1L);
53 #define	ln2	u_ln2.e
54 #elif LDBL_MANT_DIG == 113
55 static const long double
56 ln2 =  6.93147180559945309417232121458176568e-1L;	/* 0x162e42fefa39ef35793c7673007e6.0p-113 */
57 #else
58 #error "Unsupported long double format"
59 #endif
60 
61 long double
62 acoshl(long double x)
63 {
64 	long double t;
65 	int16_t hx;
66 
67 	ENTERI();
68 	GET_LDBL_EXPSIGN(hx, x);
69 	if (hx < 0x3fff) {		/* x < 1, or misnormal */
70 	    RETURNI((x-x)/(x-x));
71 	} else if (hx >= BIAS + EXP_LARGE) { /* x >= LARGE */
72 	    if (hx >= 0x7fff) {		/* x is inf, NaN or misnormal */
73 	        RETURNI(x+x);
74 	    } else
75 		RETURNI(logl(x)+ln2);	/* acosh(huge)=log(2x), or misnormal */
76 	} else if (hx == 0x3fff && x == 1) {
77 	    RETURNI(0.0);		/* acosh(1) = 0 */
78 	} else if (hx >= 0x4000) {	/* LARGE > x >= 2, or misnormal */
79 	    t=x*x;
80 	    RETURNI(logl(2.0*x-one/(x+sqrtl(t-one))));
81 	} else {			/* 1<x<2 */
82 	    t = x-one;
83 	    RETURNI(log1pl(t+sqrtl(2.0*t+t*t)));
84 	}
85 }
86