1 /* Copyright JS Foundation and other contributors, http://js.foundation
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 *
15 * This file is based on work under the following copyright and permission
16 * notice:
17 *
18 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
19 *
20 * Developed at SunSoft, a Sun Microsystems, Inc. business.
21 * Permission to use, copy, modify, and distribute this
22 * software is freely granted, provided that this notice
23 * is preserved.
24 *
25 * @(#)s_asinh.c 1.3 95/01/18
26 */
27
28 #include "jerry-libm-internal.h"
29
30 /* asinh(x)
31 * Method :
32 * Based on
33 * asinh(x) = sign(x) * log [ |x| + sqrt(x*x+1) ]
34 * we have
35 * asinh(x) := x if 1 + x * x = 1,
36 * := sign(x) * (log(x)+ln2)) for large |x|, else
37 * := sign(x) * log(2|x| + 1 / (|x| + sqrt(x * x + 1))) if|x| > 2, else
38 * := sign(x) * log1p(|x| + x^2 / (1 + sqrt(1 + x^2)))
39 */
40
41 #define one 1.0
42 #define ln2 6.93147180559945286227e-01 /* 0x3FE62E42, 0xFEFA39EF */
43 #define huge 1.0e+300
44
45 double
asinh(double x)46 asinh (double x)
47 {
48 double t, w;
49 int hx, ix;
50 hx = __HI (x);
51 ix = hx & 0x7fffffff;
52 if (ix >= 0x7ff00000)
53 {
54 /* x is inf or NaN */
55 return x + x;
56 }
57 if (ix < 0x3e300000)
58 {
59 /* |x| < 2**-28 */
60 if (huge + x > one)
61 {
62 /* return x inexact except 0 */
63 return x;
64 }
65 }
66 if (ix > 0x41b00000)
67 {
68 /* |x| > 2**28 */
69 w = log (fabs (x)) + ln2;
70 }
71 else if (ix > 0x40000000)
72 {
73 /* 2**28 > |x| > 2.0 */
74 t = fabs (x);
75 w = log (2.0 * t + one / (sqrt (x * x + one) + t));
76 }
77 else
78 {
79 /* 2.0 > |x| > 2**-28 */
80 t = x * x;
81 w = log1p (fabs (x) + t / (one + sqrt (one + t)));
82 }
83 if (hx > 0)
84 {
85 return w;
86 }
87 else
88 {
89 return -w;
90 }
91 } /* asinh */
92
93 #undef one
94 #undef ln2
95 #undef huge
96