initial cmath code and minor libm.h update
[libm] / src / math / acosh.c
1 /* origin: FreeBSD /usr/src/lib/msun/src/e_acosh.c */
2 /*
3  * ====================================================
4  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5  *
6  * Developed at SunSoft, a Sun Microsystems, Inc. business.
7  * Permission to use, copy, modify, and distribute this
8  * software is freely granted, provided that this notice
9  * is preserved.
10  * ====================================================
11  *
12  */
13 /* acosh(x)
14  * Method :
15  *      Based on
16  *              acosh(x) = log [ x + sqrt(x*x-1) ]
17  *      we have
18  *              acosh(x) := log(x)+ln2, if x is large; else
19  *              acosh(x) := log(2x-1/(sqrt(x*x-1)+x)) if x>2; else
20  *              acosh(x) := log1p(t+sqrt(2.0*t+t*t)); where t=x-1.
21  *
22  * Special cases:
23  *      acosh(x) is NaN with signal if x<1.
24  *      acosh(NaN) is NaN without signal.
25  */
26
27 #include "libm.h"
28
29 static const double
30 one = 1.0,
31 ln2 = 6.93147180559945286227e-01; /* 0x3FE62E42, 0xFEFA39EF */
32
33 double acosh(double x)
34 {
35         double t;
36         int32_t hx;
37         uint32_t lx;
38
39         EXTRACT_WORDS(hx, lx, x);
40         if (hx < 0x3ff00000) {  /* x < 1 */
41                 return (x-x)/(x-x);
42         } else if (hx >= 0x41b00000) {  /* x > 2**28 */
43                 if (hx >= 0x7ff00000)  /* x is inf of NaN */
44                         return x+x;
45                 return log(x) + ln2;   /* acosh(huge) = log(2x) */
46         } else if ((hx-0x3ff00000 | lx) == 0) {
47                 return 0.0;            /* acosh(1) = 0 */
48         } else if (hx > 0x40000000) {  /* 2**28 > x > 2 */
49                 t = x*x;
50                 return log(2.0*x - one/(x+sqrt(t-one)));
51         } else {                /* 1 < x < 2 */
52                 t = x-one;
53                 return log1p(t + sqrt(2.0*t+t*t));
54         }
55 }