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 * @(#)e_asin.c 1.3 95/01/18 26 */ 27 28#include "jerry-libm-internal.h" 29 30/* asin(x) 31 * 32 * Method: 33 * Since asin(x) = x + x^3/6 + x^5*3/40 + x^7*15/336 + ... 34 * we approximate asin(x) on [0,0.5] by 35 * asin(x) = x + x*x^2*R(x^2) 36 * where 37 * R(x^2) is a rational approximation of (asin(x)-x)/x^3 38 * and its remez error is bounded by 39 * |(asin(x)-x)/x^3 - R(x^2)| < 2^(-58.75) 40 * 41 * For x in [0.5,1] 42 * asin(x) = pi/2-2*asin(sqrt((1-x)/2)) 43 * Let y = (1-x), z = y/2, s := sqrt(z), and pio2_hi+pio2_lo=pi/2; 44 * then for x>0.98 45 * asin(x) = pi/2 - 2*(s+s*z*R(z)) 46 * = pio2_hi - (2*(s+s*z*R(z)) - pio2_lo) 47 * For x<=0.98, let pio4_hi = pio2_hi/2, then 48 * f = hi part of s; 49 * c = sqrt(z) - f = (z-f*f)/(s+f) ...f+c=sqrt(z) 50 * and 51 * asin(x) = pi/2 - 2*(s+s*z*R(z)) 52 * = pio4_hi+(pio4-2s)-(2s*z*R(z)-pio2_lo) 53 * = pio4_hi+(pio4-2f)-(2s*z*R(z)-(pio2_lo+2c)) 54 * 55 * Special cases: 56 * if x is NaN, return x itself; 57 * if |x|>1, return NaN with invalid signal. 58 */ 59 60#define one 1.00000000000000000000e+00 /* 0x3FF00000, 0x00000000 */ 61#define huge 1.000e+300 62#define pio2_hi 1.57079632679489655800e+00 /* 0x3FF921FB, 0x54442D18 */ 63#define pio2_lo 6.12323399573676603587e-17 /* 0x3C91A626, 0x33145C07 */ 64#define pio4_hi 7.85398163397448278999e-01 /* 0x3FE921FB, 0x54442D18 */ 65/* coefficient for R(x^2) */ 66#define pS0 1.66666666666666657415e-01 /* 0x3FC55555, 0x55555555 */ 67#define pS1 -3.25565818622400915405e-01 /* 0xBFD4D612, 0x03EB6F7D */ 68#define pS2 2.01212532134862925881e-01 /* 0x3FC9C155, 0x0E884455 */ 69#define pS3 -4.00555345006794114027e-02 /* 0xBFA48228, 0xB5688F3B */ 70#define pS4 7.91534994289814532176e-04 /* 0x3F49EFE0, 0x7501B288 */ 71#define pS5 3.47933107596021167570e-05 /* 0x3F023DE1, 0x0DFDF709 */ 72#define qS1 -2.40339491173441421878e+00 /* 0xC0033A27, 0x1C8A2D4B */ 73#define qS2 2.02094576023350569471e+00 /* 0x40002AE5, 0x9C598AC8 */ 74#define qS3 -6.88283971605453293030e-01 /* 0xBFE6066C, 0x1B8D0159 */ 75#define qS4 7.70381505559019352791e-02 /* 0x3FB3B8C5, 0xB12E9282 */ 76 77double 78asin (double x) 79{ 80 double t, p, q, c, r, s; 81 double_accessor w; 82 int hx, ix; 83 84 hx = __HI (x); 85 ix = hx & 0x7fffffff; 86 if (ix >= 0x3ff00000) /* |x| >= 1 */ 87 { 88 if (((ix - 0x3ff00000) | __LO (x)) == 0) /* asin(1) = +-pi/2 with inexact */ 89 { 90 return x * pio2_hi + x * pio2_lo; 91 } 92 return NAN; /* asin(|x|>1) is NaN */ 93 } 94 else if (ix < 0x3fe00000) /* |x| < 0.5 */ 95 { 96 if (ix < 0x3e400000) /* if |x| < 2**-27 */ 97 { 98 if (huge + x > one) /* return x with inexact if x != 0 */ 99 { 100 return x; 101 } 102 } 103 t = x * x; 104 p = t * (pS0 + t * (pS1 + t * (pS2 + t * (pS3 + t * (pS4 + t * pS5))))); 105 q = one + t * (qS1 + t * (qS2 + t * (qS3 + t * qS4))); 106 w.dbl = p / q; 107 return x + x * w.dbl; 108 } 109 /* 1 > |x| >= 0.5 */ 110 w.dbl = one - fabs (x); 111 t = w.dbl * 0.5; 112 p = t * (pS0 + t * (pS1 + t * (pS2 + t * (pS3 + t * (pS4 + t * pS5))))); 113 q = one + t * (qS1 + t * (qS2 + t * (qS3 + t * qS4))); 114 s = sqrt (t); 115 if (ix >= 0x3FEF3333) /* if |x| > 0.975 */ 116 { 117 w.dbl = p / q; 118 t = pio2_hi - (2.0 * (s + s * w.dbl) - pio2_lo); 119 } 120 else 121 { 122 w.dbl = s; 123 w.as_int.lo = 0; 124 c = (t - w.dbl * w.dbl) / (s + w.dbl); 125 r = p / q; 126 p = 2.0 * s * r - (pio2_lo - 2.0 * c); 127 q = pio4_hi - 2.0 * w.dbl; 128 t = pio4_hi - (p - q); 129 } 130 if (hx > 0) 131 { 132 return t; 133 } 134 else 135 { 136 return -t; 137 } 138} /* asin */ 139 140#undef one 141#undef huge 142#undef pio2_hi 143#undef pio2_lo 144#undef pio4_hi 145#undef pS0 146#undef pS1 147#undef pS2 148#undef pS3 149#undef pS4 150#undef pS5 151#undef qS1 152#undef qS2 153#undef qS3 154#undef qS4 155