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_acos.c 1.3 95/01/18
26425bb815Sopenharmony_ci */
27425bb815Sopenharmony_ci
28425bb815Sopenharmony_ci#include "jerry-libm-internal.h"
29425bb815Sopenharmony_ci
30425bb815Sopenharmony_ci/* acos(x)
31425bb815Sopenharmony_ci *
32425bb815Sopenharmony_ci * Method:
33425bb815Sopenharmony_ci *      acos(x)  = pi/2 - asin(x)
34425bb815Sopenharmony_ci *      acos(-x) = pi/2 + asin(x)
35425bb815Sopenharmony_ci * For |x|<=0.5
36425bb815Sopenharmony_ci *      acos(x) = pi/2 - (x + x*x^2*R(x^2))     (see asin.c)
37425bb815Sopenharmony_ci * For x>0.5
38425bb815Sopenharmony_ci *      acos(x) = pi/2 - (pi/2 - 2asin(sqrt((1-x)/2)))
39425bb815Sopenharmony_ci *              = 2asin(sqrt((1-x)/2))
40425bb815Sopenharmony_ci *              = 2s + 2s*z*R(z)        ...z=(1-x)/2, s=sqrt(z)
41425bb815Sopenharmony_ci *              = 2f + (2c + 2s*z*R(z))
42425bb815Sopenharmony_ci *     where f=hi part of s, and c = (z-f*f)/(s+f) is the correction term
43425bb815Sopenharmony_ci *     for f so that f+c ~ sqrt(z).
44425bb815Sopenharmony_ci * For x<-0.5
45425bb815Sopenharmony_ci *      acos(x) = pi - 2asin(sqrt((1-|x|)/2))
46425bb815Sopenharmony_ci *              = pi - 0.5*(s+s*z*R(z)), where z=(1-|x|)/2,s=sqrt(z)
47425bb815Sopenharmony_ci *
48425bb815Sopenharmony_ci * Special cases:
49425bb815Sopenharmony_ci *      if x is NaN, return x itself;
50425bb815Sopenharmony_ci *      if |x|>1, return NaN with invalid signal.
51425bb815Sopenharmony_ci *
52425bb815Sopenharmony_ci * Function needed: sqrt
53425bb815Sopenharmony_ci */
54425bb815Sopenharmony_ci
55425bb815Sopenharmony_ci#define one      1.00000000000000000000e+00 /* 0x3FF00000, 0x00000000 */
56425bb815Sopenharmony_ci#define pi       3.14159265358979311600e+00 /* 0x400921FB, 0x54442D18 */
57425bb815Sopenharmony_ci#define pio2_hi  1.57079632679489655800e+00 /* 0x3FF921FB, 0x54442D18 */
58425bb815Sopenharmony_ci#define pio2_lo  6.12323399573676603587e-17 /* 0x3C91A626, 0x33145C07 */
59425bb815Sopenharmony_ci#define pS0      1.66666666666666657415e-01 /* 0x3FC55555, 0x55555555 */
60425bb815Sopenharmony_ci#define pS1     -3.25565818622400915405e-01 /* 0xBFD4D612, 0x03EB6F7D */
61425bb815Sopenharmony_ci#define pS2      2.01212532134862925881e-01 /* 0x3FC9C155, 0x0E884455 */
62425bb815Sopenharmony_ci#define pS3     -4.00555345006794114027e-02 /* 0xBFA48228, 0xB5688F3B */
63425bb815Sopenharmony_ci#define pS4      7.91534994289814532176e-04 /* 0x3F49EFE0, 0x7501B288 */
64425bb815Sopenharmony_ci#define pS5      3.47933107596021167570e-05 /* 0x3F023DE1, 0x0DFDF709 */
65425bb815Sopenharmony_ci#define qS1     -2.40339491173441421878e+00 /* 0xC0033A27, 0x1C8A2D4B */
66425bb815Sopenharmony_ci#define qS2      2.02094576023350569471e+00 /* 0x40002AE5, 0x9C598AC8 */
67425bb815Sopenharmony_ci#define qS3     -6.88283971605453293030e-01 /* 0xBFE6066C, 0x1B8D0159 */
68425bb815Sopenharmony_ci#define qS4      7.70381505559019352791e-02 /* 0x3FB3B8C5, 0xB12E9282 */
69425bb815Sopenharmony_ci
70425bb815Sopenharmony_cidouble
71425bb815Sopenharmony_ciacos (double x)
72425bb815Sopenharmony_ci{
73425bb815Sopenharmony_ci  double z, p, q, r, w, s, c;
74425bb815Sopenharmony_ci  int hx, ix;
75425bb815Sopenharmony_ci
76425bb815Sopenharmony_ci  hx = __HI (x);
77425bb815Sopenharmony_ci  ix = hx & 0x7fffffff;
78425bb815Sopenharmony_ci  if (ix >= 0x3ff00000) /* |x| >= 1 */
79425bb815Sopenharmony_ci  {
80425bb815Sopenharmony_ci    if (((ix - 0x3ff00000) | __LO (x)) == 0) /* |x| == 1 */
81425bb815Sopenharmony_ci    {
82425bb815Sopenharmony_ci      if (hx > 0) /* acos(1) = 0  */
83425bb815Sopenharmony_ci      {
84425bb815Sopenharmony_ci        return 0.0;
85425bb815Sopenharmony_ci      }
86425bb815Sopenharmony_ci      else /* acos(-1) = pi */
87425bb815Sopenharmony_ci      {
88425bb815Sopenharmony_ci        return pi + 2.0 * pio2_lo;
89425bb815Sopenharmony_ci      }
90425bb815Sopenharmony_ci    }
91425bb815Sopenharmony_ci    return NAN; /* acos(|x|>1) is NaN */
92425bb815Sopenharmony_ci  }
93425bb815Sopenharmony_ci  if (ix < 0x3fe00000) /* |x| < 0.5 */
94425bb815Sopenharmony_ci  {
95425bb815Sopenharmony_ci    if (ix <= 0x3c600000) /* if |x| < 2**-57 */
96425bb815Sopenharmony_ci    {
97425bb815Sopenharmony_ci      return pio2_hi + pio2_lo;
98425bb815Sopenharmony_ci    }
99425bb815Sopenharmony_ci    z = x * x;
100425bb815Sopenharmony_ci    p = z * (pS0 + z * (pS1 + z * (pS2 + z * (pS3 + z * (pS4 + z * pS5)))));
101425bb815Sopenharmony_ci    q = one + z * (qS1 + z * (qS2 + z * (qS3 + z * qS4)));
102425bb815Sopenharmony_ci    r = p / q;
103425bb815Sopenharmony_ci    return pio2_hi - (x - (pio2_lo - x * r));
104425bb815Sopenharmony_ci  }
105425bb815Sopenharmony_ci  else if (hx < 0) /* x < -0.5 */
106425bb815Sopenharmony_ci  {
107425bb815Sopenharmony_ci    z = (one + x) * 0.5;
108425bb815Sopenharmony_ci    p = z * (pS0 + z * (pS1 + z * (pS2 + z * (pS3 + z * (pS4 + z * pS5)))));
109425bb815Sopenharmony_ci    q = one + z * (qS1 + z * (qS2 + z * (qS3 + z * qS4)));
110425bb815Sopenharmony_ci    s = sqrt (z);
111425bb815Sopenharmony_ci    r = p / q;
112425bb815Sopenharmony_ci    w = r * s - pio2_lo;
113425bb815Sopenharmony_ci    return pi - 2.0 * (s + w);
114425bb815Sopenharmony_ci  }
115425bb815Sopenharmony_ci  else /* x > 0.5 */
116425bb815Sopenharmony_ci  {
117425bb815Sopenharmony_ci    double_accessor df;
118425bb815Sopenharmony_ci    z = (one - x) * 0.5;
119425bb815Sopenharmony_ci    s = sqrt (z);
120425bb815Sopenharmony_ci    df.dbl = s;
121425bb815Sopenharmony_ci    df.as_int.lo = 0;
122425bb815Sopenharmony_ci    c = (z - df.dbl * df.dbl) / (s + df.dbl);
123425bb815Sopenharmony_ci    p = z * (pS0 + z * (pS1 + z * (pS2 + z * (pS3 + z * (pS4 + z * pS5)))));
124425bb815Sopenharmony_ci    q = one + z * (qS1 + z * (qS2 + z * (qS3 + z * qS4)));
125425bb815Sopenharmony_ci    r = p / q;
126425bb815Sopenharmony_ci    w = r * s + c;
127425bb815Sopenharmony_ci    return 2.0 * (df.dbl + w);
128425bb815Sopenharmony_ci  }
129425bb815Sopenharmony_ci} /* acos */
130425bb815Sopenharmony_ci
131425bb815Sopenharmony_ci#undef one
132425bb815Sopenharmony_ci#undef pi
133425bb815Sopenharmony_ci#undef pio2_hi
134425bb815Sopenharmony_ci#undef pio2_lo
135425bb815Sopenharmony_ci#undef pS0
136425bb815Sopenharmony_ci#undef pS1
137425bb815Sopenharmony_ci#undef pS2
138425bb815Sopenharmony_ci#undef pS3
139425bb815Sopenharmony_ci#undef pS4
140425bb815Sopenharmony_ci#undef pS5
141425bb815Sopenharmony_ci#undef qS1
142425bb815Sopenharmony_ci#undef qS2
143425bb815Sopenharmony_ci#undef qS3
144425bb815Sopenharmony_ci#undef qS4
145