1425bb815Sopenharmony_ci/* Copyright JS Foundation and other contributors, http://js.foundation
2425bb815Sopenharmony_ci *
3425bb815Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4425bb815Sopenharmony_ci * you may not use this file except in compliance with the License.
5425bb815Sopenharmony_ci * You may obtain a copy of the License at
6425bb815Sopenharmony_ci *
7425bb815Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8425bb815Sopenharmony_ci *
9425bb815Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10425bb815Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS
11425bb815Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12425bb815Sopenharmony_ci * See the License for the specific language governing permissions and
13425bb815Sopenharmony_ci * limitations under the License.
14425bb815Sopenharmony_ci *
15425bb815Sopenharmony_ci * This file is based on work under the following copyright and permission
16425bb815Sopenharmony_ci * notice:
17425bb815Sopenharmony_ci *
18425bb815Sopenharmony_ci *     Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
19425bb815Sopenharmony_ci *
20425bb815Sopenharmony_ci *     Developed at SunSoft, a Sun Microsystems, Inc. business.
21425bb815Sopenharmony_ci *     Permission to use, copy, modify, and distribute this
22425bb815Sopenharmony_ci *     software is freely granted, provided that this notice
23425bb815Sopenharmony_ci *     is preserved.
24425bb815Sopenharmony_ci *
25425bb815Sopenharmony_ci *     @(#)e_acosh.c 1.3 95/01/18
26425bb815Sopenharmony_ci */
27425bb815Sopenharmony_ci
28425bb815Sopenharmony_ci#include "jerry-libm-internal.h"
29425bb815Sopenharmony_ci
30425bb815Sopenharmony_ci/* acosh(x)
31425bb815Sopenharmony_ci * Method :
32425bb815Sopenharmony_ci *  Based on
33425bb815Sopenharmony_ci *    acosh(x) = log [ x + sqrt(x * x - 1) ]
34425bb815Sopenharmony_ci *  we have
35425bb815Sopenharmony_ci *    acosh(x) := log(x) + ln2, if x is large; else
36425bb815Sopenharmony_ci *    acosh(x) := log(2x - 1 / (sqrt(x * x - 1) + x)), if x > 2; else
37425bb815Sopenharmony_ci *    acosh(x) := log1p(t + sqrt(2.0 * t + t * t)); where t = x - 1.
38425bb815Sopenharmony_ci *
39425bb815Sopenharmony_ci * Special cases:
40425bb815Sopenharmony_ci *  acosh(x) is NaN with signal if x < 1.
41425bb815Sopenharmony_ci *  acosh(NaN) is NaN without signal.
42425bb815Sopenharmony_ci */
43425bb815Sopenharmony_ci
44425bb815Sopenharmony_ci#define one 1.0
45425bb815Sopenharmony_ci#define ln2 6.93147180559945286227e-01 /* 0x3FE62E42, 0xFEFA39EF */
46425bb815Sopenharmony_ci
47425bb815Sopenharmony_cidouble
48425bb815Sopenharmony_ciacosh (double x)
49425bb815Sopenharmony_ci{
50425bb815Sopenharmony_ci  double t;
51425bb815Sopenharmony_ci  int hx;
52425bb815Sopenharmony_ci  hx = __HI (x);
53425bb815Sopenharmony_ci  if (hx < 0x3ff00000)
54425bb815Sopenharmony_ci  {
55425bb815Sopenharmony_ci    /* x < 1 */
56425bb815Sopenharmony_ci    return NAN;
57425bb815Sopenharmony_ci  }
58425bb815Sopenharmony_ci  else if (hx >= 0x41b00000)
59425bb815Sopenharmony_ci  {
60425bb815Sopenharmony_ci    /* x > 2**28 */
61425bb815Sopenharmony_ci    if (hx >= 0x7ff00000)
62425bb815Sopenharmony_ci    {
63425bb815Sopenharmony_ci      /* x is inf of NaN */
64425bb815Sopenharmony_ci      return x + x;
65425bb815Sopenharmony_ci    }
66425bb815Sopenharmony_ci    else
67425bb815Sopenharmony_ci    {
68425bb815Sopenharmony_ci      /* acosh(huge) = log(2x) */
69425bb815Sopenharmony_ci      return log (x) + ln2;
70425bb815Sopenharmony_ci    }
71425bb815Sopenharmony_ci  }
72425bb815Sopenharmony_ci  else if (((hx - 0x3ff00000) | __LO (x)) == 0)
73425bb815Sopenharmony_ci  {
74425bb815Sopenharmony_ci    /* acosh(1) = 0 */
75425bb815Sopenharmony_ci    return 0.0;
76425bb815Sopenharmony_ci  }
77425bb815Sopenharmony_ci  else if (hx > 0x40000000)
78425bb815Sopenharmony_ci  {
79425bb815Sopenharmony_ci    /* 2**28 > x > 2 */
80425bb815Sopenharmony_ci    t = x * x;
81425bb815Sopenharmony_ci    return log (2.0 * x - one / (x + sqrt (t - one)));
82425bb815Sopenharmony_ci  }
83425bb815Sopenharmony_ci  else
84425bb815Sopenharmony_ci  {
85425bb815Sopenharmony_ci    /* 1 < x < 2 */
86425bb815Sopenharmony_ci    t = x - one;
87425bb815Sopenharmony_ci    return log1p (t + sqrt (2.0 * t + t * t));
88425bb815Sopenharmony_ci  }
89425bb815Sopenharmony_ci} /* acosh */
90425bb815Sopenharmony_ci
91425bb815Sopenharmony_ci#undef one
92425bb815Sopenharmony_ci#undef ln2
93