1/* from: FreeBSD: head/lib/msun/src/e_acosh.c 176451 2008-02-22 02:30:36Z das */
2
3/* @(#)e_acosh.c 1.3 95/01/18 */
4/*
5 * ====================================================
6 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
7 *
8 * Developed at SunSoft, 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
16#include <sys/cdefs.h>
17__FBSDID("$FreeBSD$");
18
19/*
20 * See e_acosh.c for complete comments.
21 *
22 * Converted to long double by David Schultz <das@FreeBSD.ORG> and
23 * Bruce D. Evans.
24 */
25
26#include <float.h>
27#ifdef __i386__
28#include <ieeefp.h>
29#endif
30
31#include "fpmath.h"
32#include "math.h"
33#include "math_private.h"
34
35/* EXP_LARGE is the threshold above which we use acosh(x) ~= log(2x). */
36#if LDBL_MANT_DIG == 64
37#define	EXP_LARGE	34
38#elif LDBL_MANT_DIG == 113
39#define	EXP_LARGE	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
51static const double
52one	= 1.0;
53
54#if LDBL_MANT_DIG == 64
55static const union IEEEl2bits
56u_ln2 =  LD80C(0xb17217f7d1cf79ac, -1, 6.93147180559945309417e-1L);
57#define	ln2	u_ln2.e
58#elif LDBL_MANT_DIG == 113
59static const long double
60ln2 =  6.93147180559945309417232121458176568e-1L;	/* 0x162e42fefa39ef35793c7673007e6.0p-113 */
61#else
62#error "Unsupported long double format"
63#endif
64
65long double
66acoshl(long double x)
67{
68	long double t;
69	int16_t hx;
70
71	ENTERI();
72	GET_LDBL_EXPSIGN(hx, x);
73	if (hx < 0x3fff) {		/* x < 1, or misnormal */
74	    RETURNI((x-x)/(x-x));
75	} else if (hx >= BIAS + EXP_LARGE) { /* x >= LARGE */
76	    if (hx >= 0x7fff) {		/* x is inf, NaN or misnormal */
77	        RETURNI(x+x);
78	    } else
79		RETURNI(logl(x)+ln2);	/* acosh(huge)=log(2x), or misnormal */
80	} else if (hx == 0x3fff && x == 1) {
81	    RETURNI(0.0);		/* acosh(1) = 0 */
82	} else if (hx >= 0x4000) {	/* LARGE > x >= 2, or misnormal */
83	    t=x*x;
84	    RETURNI(logl(2.0*x-one/(x+sqrtl(t-one))));
85	} else {			/* 1<x<2 */
86	    t = x-one;
87	    RETURNI(log1pl(t+sqrtl(2.0*t+t*t)));
88	}
89}
90