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_atanh.c 1.3 95/01/18
26425bb815Sopenharmony_ci */
27425bb815Sopenharmony_ci
28425bb815Sopenharmony_ci#include "jerry-libm-internal.h"
29425bb815Sopenharmony_ci
30425bb815Sopenharmony_ci/* atanh(x)
31425bb815Sopenharmony_ci * Method :
32425bb815Sopenharmony_ci *    1.Reduced x to positive by atanh(-x) = -atanh(x)
33425bb815Sopenharmony_ci *    2.For x >= 0.5
34425bb815Sopenharmony_ci *              1              2x                          x
35425bb815Sopenharmony_ci *  atanh(x) = --- * log(1 + -------) = 0.5 * log1p(2 * --------)
36425bb815Sopenharmony_ci *              2             1 - x                      1 - x
37425bb815Sopenharmony_ci *
38425bb815Sopenharmony_ci *   For x < 0.5
39425bb815Sopenharmony_ci*    atanh(x) = 0.5 * log1p(2x + 2x * x / (1 - x))
40425bb815Sopenharmony_ci *
41425bb815Sopenharmony_ci * Special cases:
42425bb815Sopenharmony_ci *  atanh(x) is NaN if |x| > 1 with signal;
43425bb815Sopenharmony_ci *  atanh(NaN) is that NaN with no signal;
44425bb815Sopenharmony_ci *  atanh(+-1) is +-INF with signal.
45425bb815Sopenharmony_ci *
46425bb815Sopenharmony_ci */
47425bb815Sopenharmony_ci
48425bb815Sopenharmony_ci#define zero 0.0
49425bb815Sopenharmony_ci#define one 1.0
50425bb815Sopenharmony_ci#define huge 1.0e+300
51425bb815Sopenharmony_ci
52425bb815Sopenharmony_cidouble
53425bb815Sopenharmony_ciatanh (double x)
54425bb815Sopenharmony_ci{
55425bb815Sopenharmony_ci  double t;
56425bb815Sopenharmony_ci  int hx, ix;
57425bb815Sopenharmony_ci  double_accessor temp;
58425bb815Sopenharmony_ci  temp.dbl = x;
59425bb815Sopenharmony_ci  hx = temp.as_int.hi;
60425bb815Sopenharmony_ci  ix = hx & 0x7fffffff;
61425bb815Sopenharmony_ci
62425bb815Sopenharmony_ci  /* |x| > 1 */
63425bb815Sopenharmony_ci  if ((ix | ((unsigned int) (temp.as_int.lo | (-temp.as_int.lo)) >> 31)) > 0x3ff00000)
64425bb815Sopenharmony_ci  {
65425bb815Sopenharmony_ci    return NAN;
66425bb815Sopenharmony_ci  }
67425bb815Sopenharmony_ci  if (ix == 0x3ff00000)
68425bb815Sopenharmony_ci  {
69425bb815Sopenharmony_ci    return x / zero;
70425bb815Sopenharmony_ci  }
71425bb815Sopenharmony_ci  if (ix < 0x3e300000 && (huge + x) > zero)
72425bb815Sopenharmony_ci  {
73425bb815Sopenharmony_ci    return x; /* x<2**-28 */
74425bb815Sopenharmony_ci  }
75425bb815Sopenharmony_ci
76425bb815Sopenharmony_ci  /* x <- |x| */
77425bb815Sopenharmony_ci  temp.as_int.hi = ix;
78425bb815Sopenharmony_ci  if (ix < 0x3fe00000)
79425bb815Sopenharmony_ci  {
80425bb815Sopenharmony_ci    /* x < 0.5 */
81425bb815Sopenharmony_ci    t = temp.dbl + temp.dbl;
82425bb815Sopenharmony_ci    t = 0.5 * log1p (t + t * temp.dbl / (one - temp.dbl));
83425bb815Sopenharmony_ci  }
84425bb815Sopenharmony_ci  else
85425bb815Sopenharmony_ci  {
86425bb815Sopenharmony_ci    t = 0.5 * log1p ((temp.dbl + temp.dbl) / (one - temp.dbl));
87425bb815Sopenharmony_ci  }
88425bb815Sopenharmony_ci  if (hx >= 0)
89425bb815Sopenharmony_ci  {
90425bb815Sopenharmony_ci    return t;
91425bb815Sopenharmony_ci  }
92425bb815Sopenharmony_ci  else
93425bb815Sopenharmony_ci  {
94425bb815Sopenharmony_ci    return -t;
95425bb815Sopenharmony_ci  }
96425bb815Sopenharmony_ci} /* atanh */
97425bb815Sopenharmony_ci
98425bb815Sopenharmony_ci#undef zero
99425bb815Sopenharmony_ci#undef one
100425bb815Sopenharmony_ci#undef huge
101