OLD | NEW |
1 // The following is adapted from fdlibm (http://www.netlib.org/fdlibm). | 1 // The following is adapted from fdlibm (http://www.netlib.org/fdlibm). |
2 // | 2 // |
3 // ==================================================== | 3 // ==================================================== |
4 // Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. | 4 // Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. |
5 // | 5 // |
6 // Developed at SunSoft, a Sun Microsystems, Inc. business. | 6 // Developed at SunSoft, a Sun Microsystems, Inc. business. |
7 // Permission to use, copy, modify, and distribute this | 7 // Permission to use, copy, modify, and distribute this |
8 // software is freely granted, provided that this notice | 8 // software is freely granted, provided that this notice |
9 // is preserved. | 9 // is preserved. |
10 // ==================================================== | 10 // ==================================================== |
(...skipping 873 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
884 } | 884 } |
885 | 885 |
886 #undef one | 886 #undef one |
887 #undef pio4 | 887 #undef pio4 |
888 #undef pio4lo | 888 #undef pio4lo |
889 #undef T | 889 #undef T |
890 } | 890 } |
891 | 891 |
892 } // namespace | 892 } // namespace |
893 | 893 |
| 894 /* acos(x) |
| 895 * Method : |
| 896 * acos(x) = pi/2 - asin(x) |
| 897 * acos(-x) = pi/2 + asin(x) |
| 898 * For |x|<=0.5 |
| 899 * acos(x) = pi/2 - (x + x*x^2*R(x^2)) (see asin.c) |
| 900 * For x>0.5 |
| 901 * acos(x) = pi/2 - (pi/2 - 2asin(sqrt((1-x)/2))) |
| 902 * = 2asin(sqrt((1-x)/2)) |
| 903 * = 2s + 2s*z*R(z) ...z=(1-x)/2, s=sqrt(z) |
| 904 * = 2f + (2c + 2s*z*R(z)) |
| 905 * where f=hi part of s, and c = (z-f*f)/(s+f) is the correction term |
| 906 * for f so that f+c ~ sqrt(z). |
| 907 * For x<-0.5 |
| 908 * acos(x) = pi - 2asin(sqrt((1-|x|)/2)) |
| 909 * = pi - 0.5*(s+s*z*R(z)), where z=(1-|x|)/2,s=sqrt(z) |
| 910 * |
| 911 * Special cases: |
| 912 * if x is NaN, return x itself; |
| 913 * if |x|>1, return NaN with invalid signal. |
| 914 * |
| 915 * Function needed: sqrt |
| 916 */ |
| 917 double acos(double x) { |
| 918 static const double |
| 919 one = 1.00000000000000000000e+00, /* 0x3FF00000, 0x00000000 */ |
| 920 pi = 3.14159265358979311600e+00, /* 0x400921FB, 0x54442D18 */ |
| 921 pio2_hi = 1.57079632679489655800e+00, /* 0x3FF921FB, 0x54442D18 */ |
| 922 pio2_lo = 6.12323399573676603587e-17, /* 0x3C91A626, 0x33145C07 */ |
| 923 pS0 = 1.66666666666666657415e-01, /* 0x3FC55555, 0x55555555 */ |
| 924 pS1 = -3.25565818622400915405e-01, /* 0xBFD4D612, 0x03EB6F7D */ |
| 925 pS2 = 2.01212532134862925881e-01, /* 0x3FC9C155, 0x0E884455 */ |
| 926 pS3 = -4.00555345006794114027e-02, /* 0xBFA48228, 0xB5688F3B */ |
| 927 pS4 = 7.91534994289814532176e-04, /* 0x3F49EFE0, 0x7501B288 */ |
| 928 pS5 = 3.47933107596021167570e-05, /* 0x3F023DE1, 0x0DFDF709 */ |
| 929 qS1 = -2.40339491173441421878e+00, /* 0xC0033A27, 0x1C8A2D4B */ |
| 930 qS2 = 2.02094576023350569471e+00, /* 0x40002AE5, 0x9C598AC8 */ |
| 931 qS3 = -6.88283971605453293030e-01, /* 0xBFE6066C, 0x1B8D0159 */ |
| 932 qS4 = 7.70381505559019352791e-02; /* 0x3FB3B8C5, 0xB12E9282 */ |
| 933 |
| 934 double z, p, q, r, w, s, c, df; |
| 935 int32_t hx, ix; |
| 936 GET_HIGH_WORD(hx, x); |
| 937 ix = hx & 0x7fffffff; |
| 938 if (ix >= 0x3ff00000) { /* |x| >= 1 */ |
| 939 uint32_t lx; |
| 940 GET_LOW_WORD(lx, x); |
| 941 if (((ix - 0x3ff00000) | lx) == 0) { /* |x|==1 */ |
| 942 if (hx > 0) |
| 943 return 0.0; /* acos(1) = 0 */ |
| 944 else |
| 945 return pi + 2.0 * pio2_lo; /* acos(-1)= pi */ |
| 946 } |
| 947 return (x - x) / (x - x); /* acos(|x|>1) is NaN */ |
| 948 } |
| 949 if (ix < 0x3fe00000) { /* |x| < 0.5 */ |
| 950 if (ix <= 0x3c600000) return pio2_hi + pio2_lo; /*if|x|<2**-57*/ |
| 951 z = x * x; |
| 952 p = z * (pS0 + z * (pS1 + z * (pS2 + z * (pS3 + z * (pS4 + z * pS5))))); |
| 953 q = one + z * (qS1 + z * (qS2 + z * (qS3 + z * qS4))); |
| 954 r = p / q; |
| 955 return pio2_hi - (x - (pio2_lo - x * r)); |
| 956 } else if (hx < 0) { /* x < -0.5 */ |
| 957 z = (one + x) * 0.5; |
| 958 p = z * (pS0 + z * (pS1 + z * (pS2 + z * (pS3 + z * (pS4 + z * pS5))))); |
| 959 q = one + z * (qS1 + z * (qS2 + z * (qS3 + z * qS4))); |
| 960 s = sqrt(z); |
| 961 r = p / q; |
| 962 w = r * s - pio2_lo; |
| 963 return pi - 2.0 * (s + w); |
| 964 } else { /* x > 0.5 */ |
| 965 z = (one - x) * 0.5; |
| 966 s = sqrt(z); |
| 967 df = s; |
| 968 SET_LOW_WORD(df, 0); |
| 969 c = (z - df * df) / (s + df); |
| 970 p = z * (pS0 + z * (pS1 + z * (pS2 + z * (pS3 + z * (pS4 + z * pS5))))); |
| 971 q = one + z * (qS1 + z * (qS2 + z * (qS3 + z * qS4))); |
| 972 r = p / q; |
| 973 w = r * s + c; |
| 974 return 2.0 * (df + w); |
| 975 } |
| 976 } |
| 977 |
| 978 /* acosh(x) |
| 979 * Method : |
| 980 * Based on |
| 981 * acosh(x) = log [ x + sqrt(x*x-1) ] |
| 982 * we have |
| 983 * acosh(x) := log(x)+ln2, if x is large; else |
| 984 * acosh(x) := log(2x-1/(sqrt(x*x-1)+x)) if x>2; else |
| 985 * acosh(x) := log1p(t+sqrt(2.0*t+t*t)); where t=x-1. |
| 986 * |
| 987 * Special cases: |
| 988 * acosh(x) is NaN with signal if x<1. |
| 989 * acosh(NaN) is NaN without signal. |
| 990 */ |
| 991 double acosh(double x) { |
| 992 static const double |
| 993 one = 1.0, |
| 994 ln2 = 6.93147180559945286227e-01; /* 0x3FE62E42, 0xFEFA39EF */ |
| 995 double t; |
| 996 int32_t hx; |
| 997 uint32_t lx; |
| 998 EXTRACT_WORDS(hx, lx, x); |
| 999 if (hx < 0x3ff00000) { /* x < 1 */ |
| 1000 return (x - x) / (x - x); |
| 1001 } else if (hx >= 0x41b00000) { /* x > 2**28 */ |
| 1002 if (hx >= 0x7ff00000) { /* x is inf of NaN */ |
| 1003 return x + x; |
| 1004 } else { |
| 1005 return log(x) + ln2; /* acosh(huge)=log(2x) */ |
| 1006 } |
| 1007 } else if (((hx - 0x3ff00000) | lx) == 0) { |
| 1008 return 0.0; /* acosh(1) = 0 */ |
| 1009 } else if (hx > 0x40000000) { /* 2**28 > x > 2 */ |
| 1010 t = x * x; |
| 1011 return log(2.0 * x - one / (x + sqrt(t - one))); |
| 1012 } else { /* 1<x<2 */ |
| 1013 t = x - one; |
| 1014 return log1p(t + sqrt(2.0 * t + t * t)); |
| 1015 } |
| 1016 } |
| 1017 |
| 1018 /* asin(x) |
| 1019 * Method : |
| 1020 * Since asin(x) = x + x^3/6 + x^5*3/40 + x^7*15/336 + ... |
| 1021 * we approximate asin(x) on [0,0.5] by |
| 1022 * asin(x) = x + x*x^2*R(x^2) |
| 1023 * where |
| 1024 * R(x^2) is a rational approximation of (asin(x)-x)/x^3 |
| 1025 * and its remez error is bounded by |
| 1026 * |(asin(x)-x)/x^3 - R(x^2)| < 2^(-58.75) |
| 1027 * |
| 1028 * For x in [0.5,1] |
| 1029 * asin(x) = pi/2-2*asin(sqrt((1-x)/2)) |
| 1030 * Let y = (1-x), z = y/2, s := sqrt(z), and pio2_hi+pio2_lo=pi/2; |
| 1031 * then for x>0.98 |
| 1032 * asin(x) = pi/2 - 2*(s+s*z*R(z)) |
| 1033 * = pio2_hi - (2*(s+s*z*R(z)) - pio2_lo) |
| 1034 * For x<=0.98, let pio4_hi = pio2_hi/2, then |
| 1035 * f = hi part of s; |
| 1036 * c = sqrt(z) - f = (z-f*f)/(s+f) ...f+c=sqrt(z) |
| 1037 * and |
| 1038 * asin(x) = pi/2 - 2*(s+s*z*R(z)) |
| 1039 * = pio4_hi+(pio4-2s)-(2s*z*R(z)-pio2_lo) |
| 1040 * = pio4_hi+(pio4-2f)-(2s*z*R(z)-(pio2_lo+2c)) |
| 1041 * |
| 1042 * Special cases: |
| 1043 * if x is NaN, return x itself; |
| 1044 * if |x|>1, return NaN with invalid signal. |
| 1045 */ |
| 1046 double asin(double x) { |
| 1047 static const double |
| 1048 one = 1.00000000000000000000e+00, /* 0x3FF00000, 0x00000000 */ |
| 1049 huge = 1.000e+300, |
| 1050 pio2_hi = 1.57079632679489655800e+00, /* 0x3FF921FB, 0x54442D18 */ |
| 1051 pio2_lo = 6.12323399573676603587e-17, /* 0x3C91A626, 0x33145C07 */ |
| 1052 pio4_hi = 7.85398163397448278999e-01, /* 0x3FE921FB, 0x54442D18 */ |
| 1053 /* coefficient for R(x^2) */ |
| 1054 pS0 = 1.66666666666666657415e-01, /* 0x3FC55555, 0x55555555 */ |
| 1055 pS1 = -3.25565818622400915405e-01, /* 0xBFD4D612, 0x03EB6F7D */ |
| 1056 pS2 = 2.01212532134862925881e-01, /* 0x3FC9C155, 0x0E884455 */ |
| 1057 pS3 = -4.00555345006794114027e-02, /* 0xBFA48228, 0xB5688F3B */ |
| 1058 pS4 = 7.91534994289814532176e-04, /* 0x3F49EFE0, 0x7501B288 */ |
| 1059 pS5 = 3.47933107596021167570e-05, /* 0x3F023DE1, 0x0DFDF709 */ |
| 1060 qS1 = -2.40339491173441421878e+00, /* 0xC0033A27, 0x1C8A2D4B */ |
| 1061 qS2 = 2.02094576023350569471e+00, /* 0x40002AE5, 0x9C598AC8 */ |
| 1062 qS3 = -6.88283971605453293030e-01, /* 0xBFE6066C, 0x1B8D0159 */ |
| 1063 qS4 = 7.70381505559019352791e-02; /* 0x3FB3B8C5, 0xB12E9282 */ |
| 1064 |
| 1065 double t, w, p, q, c, r, s; |
| 1066 int32_t hx, ix; |
| 1067 |
| 1068 t = 0; |
| 1069 GET_HIGH_WORD(hx, x); |
| 1070 ix = hx & 0x7fffffff; |
| 1071 if (ix >= 0x3ff00000) { /* |x|>= 1 */ |
| 1072 uint32_t lx; |
| 1073 GET_LOW_WORD(lx, x); |
| 1074 if (((ix - 0x3ff00000) | lx) == 0) /* asin(1)=+-pi/2 with inexact */ |
| 1075 return x * pio2_hi + x * pio2_lo; |
| 1076 return (x - x) / (x - x); /* asin(|x|>1) is NaN */ |
| 1077 } else if (ix < 0x3fe00000) { /* |x|<0.5 */ |
| 1078 if (ix < 0x3e400000) { /* if |x| < 2**-27 */ |
| 1079 if (huge + x > one) return x; /* return x with inexact if x!=0*/ |
| 1080 } else { |
| 1081 t = x * x; |
| 1082 } |
| 1083 p = t * (pS0 + t * (pS1 + t * (pS2 + t * (pS3 + t * (pS4 + t * pS5))))); |
| 1084 q = one + t * (qS1 + t * (qS2 + t * (qS3 + t * qS4))); |
| 1085 w = p / q; |
| 1086 return x + x * w; |
| 1087 } |
| 1088 /* 1> |x|>= 0.5 */ |
| 1089 w = one - fabs(x); |
| 1090 t = w * 0.5; |
| 1091 p = t * (pS0 + t * (pS1 + t * (pS2 + t * (pS3 + t * (pS4 + t * pS5))))); |
| 1092 q = one + t * (qS1 + t * (qS2 + t * (qS3 + t * qS4))); |
| 1093 s = sqrt(t); |
| 1094 if (ix >= 0x3FEF3333) { /* if |x| > 0.975 */ |
| 1095 w = p / q; |
| 1096 t = pio2_hi - (2.0 * (s + s * w) - pio2_lo); |
| 1097 } else { |
| 1098 w = s; |
| 1099 SET_LOW_WORD(w, 0); |
| 1100 c = (t - w * w) / (s + w); |
| 1101 r = p / q; |
| 1102 p = 2.0 * s * r - (pio2_lo - 2.0 * c); |
| 1103 q = pio4_hi - 2.0 * w; |
| 1104 t = pio4_hi - (p - q); |
| 1105 } |
| 1106 if (hx > 0) |
| 1107 return t; |
| 1108 else |
| 1109 return -t; |
| 1110 } |
| 1111 /* asinh(x) |
| 1112 * Method : |
| 1113 * Based on |
| 1114 * asinh(x) = sign(x) * log [ |x| + sqrt(x*x+1) ] |
| 1115 * we have |
| 1116 * asinh(x) := x if 1+x*x=1, |
| 1117 * := sign(x)*(log(x)+ln2)) for large |x|, else |
| 1118 * := sign(x)*log(2|x|+1/(|x|+sqrt(x*x+1))) if|x|>2, else |
| 1119 * := sign(x)*log1p(|x| + x^2/(1 + sqrt(1+x^2))) |
| 1120 */ |
| 1121 double asinh(double x) { |
| 1122 static const double |
| 1123 one = 1.00000000000000000000e+00, /* 0x3FF00000, 0x00000000 */ |
| 1124 ln2 = 6.93147180559945286227e-01, /* 0x3FE62E42, 0xFEFA39EF */ |
| 1125 huge = 1.00000000000000000000e+300; |
| 1126 |
| 1127 double t, w; |
| 1128 int32_t hx, ix; |
| 1129 GET_HIGH_WORD(hx, x); |
| 1130 ix = hx & 0x7fffffff; |
| 1131 if (ix >= 0x7ff00000) return x + x; /* x is inf or NaN */ |
| 1132 if (ix < 0x3e300000) { /* |x|<2**-28 */ |
| 1133 if (huge + x > one) return x; /* return x inexact except 0 */ |
| 1134 } |
| 1135 if (ix > 0x41b00000) { /* |x| > 2**28 */ |
| 1136 w = log(fabs(x)) + ln2; |
| 1137 } else if (ix > 0x40000000) { /* 2**28 > |x| > 2.0 */ |
| 1138 t = fabs(x); |
| 1139 w = log(2.0 * t + one / (sqrt(x * x + one) + t)); |
| 1140 } else { /* 2.0 > |x| > 2**-28 */ |
| 1141 t = x * x; |
| 1142 w = log1p(fabs(x) + t / (one + sqrt(one + t))); |
| 1143 } |
| 1144 if (hx > 0) { |
| 1145 return w; |
| 1146 } else { |
| 1147 return -w; |
| 1148 } |
| 1149 } |
| 1150 |
894 /* atan(x) | 1151 /* atan(x) |
895 * Method | 1152 * Method |
896 * 1. Reduce x to positive by atan(x) = -atan(-x). | 1153 * 1. Reduce x to positive by atan(x) = -atan(-x). |
897 * 2. According to the integer k=4t+0.25 chopped, t=x, the argument | 1154 * 2. According to the integer k=4t+0.25 chopped, t=x, the argument |
898 * is further reduced to one of the following intervals and the | 1155 * is further reduced to one of the following intervals and the |
899 * arctangent of t is evaluated by the corresponding formula: | 1156 * arctangent of t is evaluated by the corresponding formula: |
900 * | 1157 * |
901 * [0,7/16] atan(x) = t-t^3*(a1+t^2*(a2+...(a10+t^2*a11)...) | 1158 * [0,7/16] atan(x) = t-t^3*(a1+t^2*(a2+...(a10+t^2*a11)...) |
902 * [7/16,11/16] atan(x) = atan(1/2) + atan( (t-0.5)/(1+t/2) ) | 1159 * [7/16,11/16] atan(x) = atan(1/2) + atan( (t-0.5)/(1+t/2) ) |
903 * [11/16.19/16] atan(x) = atan( 1 ) + atan( (t-1)/(1+t) ) | 1160 * [11/16.19/16] atan(x) = atan( 1 ) + atan( (t-1)/(1+t) ) |
(...skipping 1576 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2480 /* |x| >= 22, return +-1 */ | 2737 /* |x| >= 22, return +-1 */ |
2481 } else { | 2738 } else { |
2482 z = one - tiny; /* raise inexact flag */ | 2739 z = one - tiny; /* raise inexact flag */ |
2483 } | 2740 } |
2484 return (jx >= 0) ? z : -z; | 2741 return (jx >= 0) ? z : -z; |
2485 } | 2742 } |
2486 | 2743 |
2487 } // namespace ieee754 | 2744 } // namespace ieee754 |
2488 } // namespace base | 2745 } // namespace base |
2489 } // namespace v8 | 2746 } // namespace v8 |
OLD | NEW |