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