Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(186)

Side by Side Diff: src/math.js

Issue 66703005: Increase precision when finding the remainder after division by pi/2. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | src/runtime.h » ('j') | src/runtime.cc » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after
210 } 210 }
211 211
212 212
213 var InitTrigonometricFunctions; 213 var InitTrigonometricFunctions;
214 214
215 215
216 // Define constants and interpolation functions. 216 // Define constants and interpolation functions.
217 // Also define the initialization function that populates the lookup table 217 // Also define the initialization function that populates the lookup table
218 // and then wires up the function definitions. 218 // and then wires up the function definitions.
219 function SetupTrigonometricFunctions() { 219 function SetupTrigonometricFunctions() {
220 // TODO(yangguo): The following table size has been chosen to satisfy 220 var samples = 1800; // Table size. Do not change arbitrarily.
221 // Sunspider's brittle result verification. Reconsider relevance. 221 // 2 / pi
222 var samples = 4489; 222 var inverse_pi_half = %HeapNumberFromHex("83c8c96d305fe43f");
Sven Panne 2013/11/20 07:38:35 Why do we need a new %Foo? Our scanner should be a
223 var pi = 3.1415926535897932; 223 // samples / (pi / 2)
224 var pi_half = pi / 2; 224 var index_convert = %HeapNumberFromHex("3b597e90a9e79140");
225 var inverse_pi_half = 2 / pi; 225 // pi / 2 rounded up
226 var two_pi = 2 * pi; 226 var pi_half = %HeapNumberFromHex("192d4454fb21f93f");
227 var four_pi = 4 * pi; 227 // We use two parts for pi/2 to emulate a higher precision.
228 var interval = pi_half / samples; 228 // Note that pi_half > pi_half_1 + pi_half_2
229 var inverse_interval = samples / pi_half; 229 var pi_half_1 = %HeapNumberFromHex("00000054fb21f93f");
230 var pi_half_2 = %HeapNumberFromHex("3326a611460b113e");
230 var table_sin; 231 var table_sin;
231 var table_cos_interval; 232 var table_cos_interval;
232 233
233 // This implements sine using the following algorithm. 234 // This implements sine using the following algorithm.
234 // 1) Multiplication takes care of to-number conversion. 235 // 1) Multiplication takes care of to-number conversion.
235 // 2) Reduce x to the first quadrant [0, pi/2]. 236 // 2) Reduce x to the first quadrant [0, pi/2].
236 // Conveniently enough, in case of +/-Infinity, we get NaN. 237 // Conveniently enough, in case of +/-Infinity, we get NaN.
237 // 3) Replace x by (pi/2-x) if x was in the 2nd or 4th quadrant. 238 // 3) Replace x by (pi/2-x) if x was in the 2nd or 4th quadrant.
238 // 4) Do a table lookup for the closest samples to the left and right of x. 239 // 4) Do a table lookup for the closest samples to the left and right of x.
239 // 5) Find the derivatives at those sampling points by table lookup: 240 // 5) Find the derivatives at those sampling points by table lookup:
240 // dsin(x)/dx = cos(x) = sin(pi/2-x) for x in [0, pi/2]. 241 // dsin(x)/dx = cos(x) = sin(pi/2-x) for x in [0, pi/2].
241 // 6) Use cubic spline interpolation to approximate sin(x). 242 // 6) Use cubic spline interpolation to approximate sin(x).
242 // 7) Negate the result if x was in the 3rd or 4th quadrant. 243 // 7) Negate the result if x was in the 3rd or 4th quadrant.
243 // 8) Get rid of -0 by adding 0. 244 // 8) Get rid of -0 by adding 0.
244 var Interpolation = function(x) { 245 var Interpolation = function(x, phase) {
245 var double_index = x * inverse_interval; 246 var double_index = x * index_convert;
247 if (phase & 1) double_index = samples - double_index;
246 var index = double_index | 0; 248 var index = double_index | 0;
247 var t1 = double_index - index; 249 var t1 = double_index - index;
248 var t2 = 1 - t1; 250 var t2 = 1 - t1;
249 var y1 = table_sin[index]; 251 var y1 = table_sin[index];
250 var y2 = table_sin[index + 1]; 252 var y2 = table_sin[index + 1];
251 var dy = y2 - y1; 253 var dy = y2 - y1;
252 return (t2 * y1 + t1 * y2 + 254 return (t2 * y1 + t1 * y2 +
253 t1 * t2 * ((table_cos_interval[index] - dy) * t2 + 255 t1 * t2 * ((table_cos_interval[index] - dy) * t2 +
254 (dy - table_cos_interval[index + 1]) * t1)); 256 (dy - table_cos_interval[index + 1]) * t1))
257 * (1 - (phase & 2)) + 0;
255 } 258 }
256 259
257 var MathSinInterpolation = function(x) { 260 var MathSinInterpolation = function(x) {
258 // This is to make Sunspider's result verification happy. 261 var x_over_pi_half = x * inverse_pi_half;
259 if (x > four_pi) x -= four_pi; 262 if (%_IsMinusZero(x_over_pi_half)) return x_over_pi_half;
260 var multiple = MathFloor(x * inverse_pi_half); 263 var phase = 0;
261 if (%_IsMinusZero(multiple)) return multiple; 264 while (x < 0 || x > pi_half) {
262 x = (multiple & 1) * pi_half + 265 var multiple = MathFloor(x * inverse_pi_half);
263 (1 - ((multiple & 1) << 1)) * (x - multiple * pi_half); 266 x = x - multiple * pi_half_1 - multiple * pi_half_2;
264 return Interpolation(x) * (1 - (multiple & 2)) + 0; 267 phase += multiple;
268 }
269 return Interpolation(x, multiple);
265 } 270 }
266 271
267 // Cosine is sine with a phase offset of pi/2. 272 // Cosine is sine with a phase offset of pi/2.
268 var MathCosInterpolation = function(x) { 273 var MathCosInterpolation = function(x) {
269 var multiple = MathFloor(x * inverse_pi_half); 274 x = MathAbs(x);
270 var phase = multiple + 1; 275 var phase = 0;
271 x = (phase & 1) * pi_half + 276 while (x < 0 || x > pi_half) {
272 (1 - ((phase & 1) << 1)) * (x - multiple * pi_half); 277 var multiple = MathFloor(x * inverse_pi_half);
273 return Interpolation(x) * (1 - (phase & 2)) + 0; 278 x = x - multiple * pi_half_1 - multiple * pi_half_2;
279 phase += multiple;
280 }
281 return Interpolation(x, phase + 1);
274 }; 282 };
275 283
276 %SetInlineBuiltinFlag(Interpolation); 284 %SetInlineBuiltinFlag(Interpolation);
277 %SetInlineBuiltinFlag(MathSinInterpolation); 285 %SetInlineBuiltinFlag(MathSinInterpolation);
278 %SetInlineBuiltinFlag(MathCosInterpolation); 286 %SetInlineBuiltinFlag(MathCosInterpolation);
279 287
280 InitTrigonometricFunctions = function() { 288 InitTrigonometricFunctions = function() {
281 table_sin = new global.Float64Array(samples + 2); 289 table_sin = new global.Float64Array(samples + 2);
282 table_cos_interval = new global.Float64Array(samples + 2); 290 table_cos_interval = new global.Float64Array(samples + 2);
283 %PopulateTrigonometricTable(table_sin, table_cos_interval, samples); 291 %PopulateTrigonometricTable(table_sin, table_cos_interval, samples);
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
361 "min", MathMin, 369 "min", MathMin,
362 "imul", MathImul 370 "imul", MathImul
363 )); 371 ));
364 372
365 %SetInlineBuiltinFlag(MathSin); 373 %SetInlineBuiltinFlag(MathSin);
366 %SetInlineBuiltinFlag(MathCos); 374 %SetInlineBuiltinFlag(MathCos);
367 %SetInlineBuiltinFlag(MathTan); 375 %SetInlineBuiltinFlag(MathTan);
368 } 376 }
369 377
370 SetUpMath(); 378 SetUpMath();
OLDNEW
« no previous file with comments | « no previous file | src/runtime.h » ('j') | src/runtime.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698