OLD | NEW |
| (Empty) |
1 // The following is adapted from fdlibm (http://www.netlib.org/fdlibm), | |
2 // | |
3 // ==================================================== | |
4 // Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. | |
5 // | |
6 // Developed at SunSoft, a Sun Microsystems, Inc. business. | |
7 // Permission to use, copy, modify, and distribute this | |
8 // software is freely granted, provided that this notice | |
9 // is preserved. | |
10 // ==================================================== | |
11 // | |
12 // The original source code covered by the above license above has been | |
13 // modified significantly by Google Inc. | |
14 // Copyright 2014 the V8 project authors. All rights reserved. | |
15 // | |
16 // The following is a straightforward translation of fdlibm routines for | |
17 // sin, cos, and tan, by Raymond Toy (rtoy@google.com). | |
18 | |
19 | |
20 var kTrig; // Initialized to a Float64Array during genesis and is not writable. | |
21 | |
22 const INVPIO2 = kTrig[0]; | |
23 const PIO2_1 = kTrig[1]; | |
24 const PIO2_1T = kTrig[2]; | |
25 const PIO2_2 = kTrig[3]; | |
26 const PIO2_2T = kTrig[4]; | |
27 const PIO2_3 = kTrig[5]; | |
28 const PIO2_3T = kTrig[6]; | |
29 const PIO4 = kTrig[32]; | |
30 const PIO4LO = kTrig[33]; | |
31 | |
32 // Compute k and r such that x - k*pi/2 = r where |r| < pi/4. For | |
33 // precision, r is returned as two values y0 and y1 such that r = y0 + y1 | |
34 // to more than double precision. | |
35 macro REMPIO2(X) | |
36 var n, y0, y1; | |
37 var hx = %_DoubleHi(X); | |
38 var ix = hx & 0x7fffffff; | |
39 | |
40 if (ix < 0x4002d97c) { | |
41 // |X| ~< 3*pi/4, special case with n = +/- 1 | |
42 if (hx > 0) { | |
43 var z = X - PIO2_1; | |
44 if (ix != 0x3ff921fb) { | |
45 // 33+53 bit pi is good enough | |
46 y0 = z - PIO2_1T; | |
47 y1 = (z - y0) - PIO2_1T; | |
48 } else { | |
49 // near pi/2, use 33+33+53 bit pi | |
50 z -= PIO2_2; | |
51 y0 = z - PIO2_2T; | |
52 y1 = (z - y0) - PIO2_2T; | |
53 } | |
54 n = 1; | |
55 } else { | |
56 // Negative X | |
57 var z = X + PIO2_1; | |
58 if (ix != 0x3ff921fb) { | |
59 // 33+53 bit pi is good enough | |
60 y0 = z + PIO2_1T; | |
61 y1 = (z - y0) + PIO2_1T; | |
62 } else { | |
63 // near pi/2, use 33+33+53 bit pi | |
64 z += PIO2_2; | |
65 y0 = z + PIO2_2T; | |
66 y1 = (z - y0) + PIO2_2T; | |
67 } | |
68 n = -1; | |
69 } | |
70 } else if (ix <= 0x413921fb) { | |
71 // |X| ~<= 2^19*(pi/2), medium size | |
72 var t = MathAbs(X); | |
73 n = (t * INVPIO2 + 0.5) | 0; | |
74 var r = t - n * PIO2_1; | |
75 var w = n * PIO2_1T; | |
76 // First round good to 85 bit | |
77 y0 = r - w; | |
78 if (ix - (%_DoubleHi(y0) & 0x7ff00000) > 0x1000000) { | |
79 // 2nd iteration needed, good to 118 | |
80 t = r; | |
81 w = n * PIO2_2; | |
82 r = t - w; | |
83 w = n * PIO2_2T - ((t - r) - w); | |
84 y0 = r - w; | |
85 if (ix - (%_DoubleHi(y0) & 0x7ff00000) > 0x3100000) { | |
86 // 3rd iteration needed. 151 bits accuracy | |
87 t = r; | |
88 w = n * PIO2_3; | |
89 r = t - w; | |
90 w = n * PIO2_3T - ((t - r) - w); | |
91 y0 = r - w; | |
92 } | |
93 } | |
94 y1 = (r - y0) - w; | |
95 if (hx < 0) { | |
96 n = -n; | |
97 y0 = -y0; | |
98 y1 = -y1; | |
99 } | |
100 } else { | |
101 // Need to do full Payne-Hanek reduction here. | |
102 var r = %RemPiO2(X); | |
103 n = r[0]; | |
104 y0 = r[1]; | |
105 y1 = r[2]; | |
106 } | |
107 endmacro | |
108 | |
109 | |
110 // __kernel_sin(X, Y, IY) | |
111 // kernel sin function on [-pi/4, pi/4], pi/4 ~ 0.7854 | |
112 // Input X is assumed to be bounded by ~pi/4 in magnitude. | |
113 // Input Y is the tail of X so that x = X + Y. | |
114 // | |
115 // Algorithm | |
116 // 1. Since ieee_sin(-x) = -ieee_sin(x), we need only to consider positive x. | |
117 // 2. ieee_sin(x) is approximated by a polynomial of degree 13 on | |
118 // [0,pi/4] | |
119 // 3 13 | |
120 // sin(x) ~ x + S1*x + ... + S6*x | |
121 // where | |
122 // | |
123 // |ieee_sin(x) 2 4 6 8 10 12 | -58 | |
124 // |----- - (1+S1*x +S2*x +S3*x +S4*x +S5*x +S6*x )| <= 2 | |
125 // | x | | |
126 // | |
127 // 3. ieee_sin(X+Y) = ieee_sin(X) + sin'(X')*Y | |
128 // ~ ieee_sin(X) + (1-X*X/2)*Y | |
129 // For better accuracy, let | |
130 // 3 2 2 2 2 | |
131 // r = X *(S2+X *(S3+X *(S4+X *(S5+X *S6)))) | |
132 // then 3 2 | |
133 // sin(x) = X + (S1*X + (X *(r-Y/2)+Y)) | |
134 // | |
135 macro KSIN(x) | |
136 kTrig[7+x] | |
137 endmacro | |
138 | |
139 macro RETURN_KERNELSIN(X, Y, SIGN) | |
140 var z = X * X; | |
141 var v = z * X; | |
142 var r = KSIN(1) + z * (KSIN(2) + z * (KSIN(3) + | |
143 z * (KSIN(4) + z * KSIN(5)))); | |
144 return (X - ((z * (0.5 * Y - v * r) - Y) - v * KSIN(0))) SIGN; | |
145 endmacro | |
146 | |
147 // __kernel_cos(X, Y) | |
148 // kernel cos function on [-pi/4, pi/4], pi/4 ~ 0.785398164 | |
149 // Input X is assumed to be bounded by ~pi/4 in magnitude. | |
150 // Input Y is the tail of X so that x = X + Y. | |
151 // | |
152 // Algorithm | |
153 // 1. Since ieee_cos(-x) = ieee_cos(x), we need only to consider positive x. | |
154 // 2. ieee_cos(x) is approximated by a polynomial of degree 14 on | |
155 // [0,pi/4] | |
156 // 4 14 | |
157 // cos(x) ~ 1 - x*x/2 + C1*x + ... + C6*x | |
158 // where the remez error is | |
159 // | |
160 // | 2 4 6 8 10 12 14 | -58 | |
161 // |ieee_cos(x)-(1-.5*x +C1*x +C2*x +C3*x +C4*x +C5*x +C6*x )| <= 2 | |
162 // | | | |
163 // | |
164 // 4 6 8 10 12 14 | |
165 // 3. let r = C1*x +C2*x +C3*x +C4*x +C5*x +C6*x , then | |
166 // ieee_cos(x) = 1 - x*x/2 + r | |
167 // since ieee_cos(X+Y) ~ ieee_cos(X) - ieee_sin(X)*Y | |
168 // ~ ieee_cos(X) - X*Y, | |
169 // a correction term is necessary in ieee_cos(x) and hence | |
170 // cos(X+Y) = 1 - (X*X/2 - (r - X*Y)) | |
171 // For better accuracy when x > 0.3, let qx = |x|/4 with | |
172 // the last 32 bits mask off, and if x > 0.78125, let qx = 0.28125. | |
173 // Then | |
174 // cos(X+Y) = (1-qx) - ((X*X/2-qx) - (r-X*Y)). | |
175 // Note that 1-qx and (X*X/2-qx) is EXACT here, and the | |
176 // magnitude of the latter is at least a quarter of X*X/2, | |
177 // thus, reducing the rounding error in the subtraction. | |
178 // | |
179 macro KCOS(x) | |
180 kTrig[13+x] | |
181 endmacro | |
182 | |
183 macro RETURN_KERNELCOS(X, Y, SIGN) | |
184 var ix = %_DoubleHi(X) & 0x7fffffff; | |
185 var z = X * X; | |
186 var r = z * (KCOS(0) + z * (KCOS(1) + z * (KCOS(2)+ | |
187 z * (KCOS(3) + z * (KCOS(4) + z * KCOS(5)))))); | |
188 if (ix < 0x3fd33333) { // |x| ~< 0.3 | |
189 return (1 - (0.5 * z - (z * r - X * Y))) SIGN; | |
190 } else { | |
191 var qx; | |
192 if (ix > 0x3fe90000) { // |x| > 0.78125 | |
193 qx = 0.28125; | |
194 } else { | |
195 qx = %_ConstructDouble(%_DoubleHi(0.25 * X), 0); | |
196 } | |
197 var hz = 0.5 * z - qx; | |
198 return (1 - qx - (hz - (z * r - X * Y))) SIGN; | |
199 } | |
200 endmacro | |
201 | |
202 // kernel tan function on [-pi/4, pi/4], pi/4 ~ 0.7854 | |
203 // Input x is assumed to be bounded by ~pi/4 in magnitude. | |
204 // Input y is the tail of x. | |
205 // Input k indicates whether ieee_tan (if k = 1) or -1/tan (if k = -1) | |
206 // is returned. | |
207 // | |
208 // Algorithm | |
209 // 1. Since ieee_tan(-x) = -ieee_tan(x), we need only to consider positive x. | |
210 // 2. if x < 2^-28 (hx<0x3e300000 0), return x with inexact if x!=0. | |
211 // 3. ieee_tan(x) is approximated by a odd polynomial of degree 27 on | |
212 // [0,0.67434] | |
213 // 3 27 | |
214 // tan(x) ~ x + T1*x + ... + T13*x | |
215 // where | |
216 // | |
217 // |ieee_tan(x) 2 4 26 | -59.2 | |
218 // |----- - (1+T1*x +T2*x +.... +T13*x )| <= 2 | |
219 // | x | | |
220 // | |
221 // Note: ieee_tan(x+y) = ieee_tan(x) + tan'(x)*y | |
222 // ~ ieee_tan(x) + (1+x*x)*y | |
223 // Therefore, for better accuracy in computing ieee_tan(x+y), let | |
224 // 3 2 2 2 2 | |
225 // r = x *(T2+x *(T3+x *(...+x *(T12+x *T13)))) | |
226 // then | |
227 // 3 2 | |
228 // tan(x+y) = x + (T1*x + (x *(r+y)+y)) | |
229 // | |
230 // 4. For x in [0.67434,pi/4], let y = pi/4 - x, then | |
231 // tan(x) = ieee_tan(pi/4-y) = (1-ieee_tan(y))/(1+ieee_tan(y)) | |
232 // = 1 - 2*(ieee_tan(y) - (ieee_tan(y)^2)/(1+ieee_tan(y))) | |
233 // | |
234 // Set returnTan to 1 for tan; -1 for cot. Anything else is illegal | |
235 // and will cause incorrect results. | |
236 // | |
237 macro KTAN(x) | |
238 kTrig[19+x] | |
239 endmacro | |
240 | |
241 function KernelTan(x, y, returnTan) { | |
242 var z; | |
243 var w; | |
244 var hx = %_DoubleHi(x); | |
245 var ix = hx & 0x7fffffff; | |
246 | |
247 if (ix < 0x3e300000) { // |x| < 2^-28 | |
248 if (((ix | %_DoubleLo(x)) | (returnTan + 1)) == 0) { | |
249 // x == 0 && returnTan = -1 | |
250 return 1 / MathAbs(x); | |
251 } else { | |
252 if (returnTan == 1) { | |
253 return x; | |
254 } else { | |
255 // Compute -1/(x + y) carefully | |
256 var w = x + y; | |
257 var z = %_ConstructDouble(%_DoubleHi(w), 0); | |
258 var v = y - (z - x); | |
259 var a = -1 / w; | |
260 var t = %_ConstructDouble(%_DoubleHi(a), 0); | |
261 var s = 1 + t * z; | |
262 return t + a * (s + t * v); | |
263 } | |
264 } | |
265 } | |
266 if (ix >= 0x3fe59429) { // |x| > .6744 | |
267 if (x < 0) { | |
268 x = -x; | |
269 y = -y; | |
270 } | |
271 z = PIO4 - x; | |
272 w = PIO4LO - y; | |
273 x = z + w; | |
274 y = 0; | |
275 } | |
276 z = x * x; | |
277 w = z * z; | |
278 | |
279 // Break x^5 * (T1 + x^2*T2 + ...) into | |
280 // x^5 * (T1 + x^4*T3 + ... + x^20*T11) + | |
281 // x^5 * (x^2 * (T2 + x^4*T4 + ... + x^22*T12)) | |
282 var r = KTAN(1) + w * (KTAN(3) + w * (KTAN(5) + | |
283 w * (KTAN(7) + w * (KTAN(9) + w * KTAN(11))))); | |
284 var v = z * (KTAN(2) + w * (KTAN(4) + w * (KTAN(6) + | |
285 w * (KTAN(8) + w * (KTAN(10) + w * KTAN(12)))))); | |
286 var s = z * x; | |
287 r = y + z * (s * (r + v) + y); | |
288 r = r + KTAN(0) * s; | |
289 w = x + r; | |
290 if (ix >= 0x3fe59428) { | |
291 return (1 - ((hx >> 30) & 2)) * | |
292 (returnTan - 2.0 * (x - (w * w / (w + returnTan) - r))); | |
293 } | |
294 if (returnTan == 1) { | |
295 return w; | |
296 } else { | |
297 z = %_ConstructDouble(%_DoubleHi(w), 0); | |
298 v = r - (z - x); | |
299 var a = -1 / w; | |
300 var t = %_ConstructDouble(%_DoubleHi(a), 0); | |
301 s = 1 + t * z; | |
302 return t + a * (s + t * v); | |
303 } | |
304 } | |
305 | |
306 function MathSinSlow(x) { | |
307 REMPIO2(x); | |
308 var sign = 1 - (n & 2); | |
309 if (n & 1) { | |
310 RETURN_KERNELCOS(y0, y1, * sign); | |
311 } else { | |
312 RETURN_KERNELSIN(y0, y1, * sign); | |
313 } | |
314 } | |
315 | |
316 function MathCosSlow(x) { | |
317 REMPIO2(x); | |
318 if (n & 1) { | |
319 var sign = (n & 2) - 1; | |
320 RETURN_KERNELSIN(y0, y1, * sign); | |
321 } else { | |
322 var sign = 1 - (n & 2); | |
323 RETURN_KERNELCOS(y0, y1, * sign); | |
324 } | |
325 } | |
326 | |
327 // ECMA 262 - 15.8.2.16 | |
328 function MathSin(x) { | |
329 x = x * 1; // Convert to number. | |
330 if ((%_DoubleHi(x) & 0x7fffffff) <= 0x3fe921fb) { | |
331 // |x| < pi/4, approximately. No reduction needed. | |
332 RETURN_KERNELSIN(x, 0, /* empty */); | |
333 } | |
334 return MathSinSlow(x); | |
335 } | |
336 | |
337 // ECMA 262 - 15.8.2.7 | |
338 function MathCos(x) { | |
339 x = x * 1; // Convert to number. | |
340 if ((%_DoubleHi(x) & 0x7fffffff) <= 0x3fe921fb) { | |
341 // |x| < pi/4, approximately. No reduction needed. | |
342 RETURN_KERNELCOS(x, 0, /* empty */); | |
343 } | |
344 return MathCosSlow(x); | |
345 } | |
346 | |
347 // ECMA 262 - 15.8.2.18 | |
348 function MathTan(x) { | |
349 x = x * 1; // Convert to number. | |
350 if ((%_DoubleHi(x) & 0x7fffffff) <= 0x3fe921fb) { | |
351 // |x| < pi/4, approximately. No reduction needed. | |
352 return KernelTan(x, 0, 1); | |
353 } | |
354 REMPIO2(x); | |
355 return KernelTan(y0, y1, (n & 1) ? -1 : 1); | |
356 } | |
OLD | NEW |