xref: /freebsd/lib/msun/src/e_acoshl.c (revision 0dd5a5603e7a33d976f8e6015620bbc79839c609)
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 SunSoft, 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 /*
16*998b640bSDavid Schultz  * See e_acosh.c for complete comments.
17*998b640bSDavid Schultz  *
18*998b640bSDavid Schultz  * Converted to long double by David Schultz <das@FreeBSD.ORG> and
19*998b640bSDavid Schultz  * Bruce D. Evans.
20*998b640bSDavid Schultz  */
21*998b640bSDavid Schultz 
22*998b640bSDavid Schultz #include <float.h>
23*998b640bSDavid Schultz #ifdef __i386__
24*998b640bSDavid Schultz #include <ieeefp.h>
25*998b640bSDavid Schultz #endif
26*998b640bSDavid Schultz 
27*998b640bSDavid Schultz #include "fpmath.h"
28*998b640bSDavid Schultz #include "math.h"
29*998b640bSDavid Schultz #include "math_private.h"
30*998b640bSDavid Schultz 
31*998b640bSDavid Schultz /* EXP_LARGE is the threshold above which we use acosh(x) ~= log(2x). */
32*998b640bSDavid Schultz #if LDBL_MANT_DIG == 64
33*998b640bSDavid Schultz #define	EXP_LARGE	34
34*998b640bSDavid Schultz #elif LDBL_MANT_DIG == 113
35*998b640bSDavid Schultz #define	EXP_LARGE	58
36*998b640bSDavid Schultz #else
37*998b640bSDavid Schultz #error "Unsupported long double format"
38*998b640bSDavid Schultz #endif
39*998b640bSDavid Schultz 
40*998b640bSDavid Schultz #if LDBL_MAX_EXP != 0x4000
41*998b640bSDavid Schultz /* We also require the usual expsign encoding. */
42*998b640bSDavid Schultz #error "Unsupported long double format"
43*998b640bSDavid Schultz #endif
44*998b640bSDavid Schultz 
45*998b640bSDavid Schultz #define	BIAS	(LDBL_MAX_EXP - 1)
46*998b640bSDavid Schultz 
47*998b640bSDavid Schultz static const double
48*998b640bSDavid Schultz one	= 1.0;
49*998b640bSDavid Schultz 
50*998b640bSDavid Schultz #if LDBL_MANT_DIG == 64
51*998b640bSDavid Schultz static const union IEEEl2bits
52*998b640bSDavid Schultz u_ln2 =  LD80C(0xb17217f7d1cf79ac, -1, 6.93147180559945309417e-1L);
53*998b640bSDavid Schultz #define	ln2	u_ln2.e
54*998b640bSDavid Schultz #elif LDBL_MANT_DIG == 113
55*998b640bSDavid Schultz static const long double
56*998b640bSDavid Schultz ln2 =  6.93147180559945309417232121458176568e-1L;	/* 0x162e42fefa39ef35793c7673007e6.0p-113 */
57*998b640bSDavid Schultz #else
58*998b640bSDavid Schultz #error "Unsupported long double format"
59*998b640bSDavid Schultz #endif
60*998b640bSDavid Schultz 
61*998b640bSDavid Schultz long double
acoshl(long double x)62*998b640bSDavid Schultz acoshl(long double x)
63*998b640bSDavid Schultz {
64*998b640bSDavid Schultz 	long double t;
65*998b640bSDavid Schultz 	int16_t hx;
66*998b640bSDavid Schultz 
67*998b640bSDavid Schultz 	ENTERI();
68*998b640bSDavid Schultz 	GET_LDBL_EXPSIGN(hx, x);
69*998b640bSDavid Schultz 	if (hx < 0x3fff) {		/* x < 1, or misnormal */
70*998b640bSDavid Schultz 	    RETURNI((x-x)/(x-x));
71*998b640bSDavid Schultz 	} else if (hx >= BIAS + EXP_LARGE) { /* x >= LARGE */
72*998b640bSDavid Schultz 	    if (hx >= 0x7fff) {		/* x is inf, NaN or misnormal */
73*998b640bSDavid Schultz 	        RETURNI(x+x);
74*998b640bSDavid Schultz 	    } else
75*998b640bSDavid Schultz 		RETURNI(logl(x)+ln2);	/* acosh(huge)=log(2x), or misnormal */
76*998b640bSDavid Schultz 	} else if (hx == 0x3fff && x == 1) {
77*998b640bSDavid Schultz 	    RETURNI(0.0);		/* acosh(1) = 0 */
78*998b640bSDavid Schultz 	} else if (hx >= 0x4000) {	/* LARGE > x >= 2, or misnormal */
79*998b640bSDavid Schultz 	    t=x*x;
80*998b640bSDavid Schultz 	    RETURNI(logl(2.0*x-one/(x+sqrtl(t-one))));
81*998b640bSDavid Schultz 	} else {			/* 1<x<2 */
82*998b640bSDavid Schultz 	    t = x-one;
83*998b640bSDavid Schultz 	    RETURNI(log1pl(t+sqrtl(2.0*t+t*t)));
84*998b640bSDavid Schultz 	}
85*998b640bSDavid Schultz }
86