| OLD | NEW |
| 1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "src/builtins/builtins.h" | 5 #include "src/builtins/builtins.h" |
| 6 #include "src/builtins/builtins-utils.h" | 6 #include "src/builtins/builtins-utils.h" |
| 7 | 7 |
| 8 #include "src/code-factory.h" | 8 #include "src/code-factory.h" |
| 9 | 9 |
| 10 namespace v8 { | 10 namespace v8 { |
| (...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 309 Node* value32 = assembler->TruncateFloat64ToFloat32(x_value); | 309 Node* value32 = assembler->TruncateFloat64ToFloat32(x_value); |
| 310 Node* value = assembler->ChangeFloat32ToFloat64(value32); | 310 Node* value = assembler->ChangeFloat32ToFloat64(value32); |
| 311 Node* result = assembler->ChangeFloat64ToTagged(value); | 311 Node* result = assembler->ChangeFloat64ToTagged(value); |
| 312 assembler->Return(result); | 312 assembler->Return(result); |
| 313 } | 313 } |
| 314 | 314 |
| 315 // ES6 section 20.2.2.18 Math.hypot ( value1, value2, ...values ) | 315 // ES6 section 20.2.2.18 Math.hypot ( value1, value2, ...values ) |
| 316 BUILTIN(MathHypot) { | 316 BUILTIN(MathHypot) { |
| 317 HandleScope scope(isolate); | 317 HandleScope scope(isolate); |
| 318 int const length = args.length() - 1; | 318 int const length = args.length() - 1; |
| 319 if (length == 0) return Smi::FromInt(0); | 319 if (length == 0) return Smi::kZero; |
| 320 DCHECK_LT(0, length); | 320 DCHECK_LT(0, length); |
| 321 double max = 0; | 321 double max = 0; |
| 322 bool one_arg_is_nan = false; | 322 bool one_arg_is_nan = false; |
| 323 List<double> abs_values(length); | 323 List<double> abs_values(length); |
| 324 for (int i = 0; i < length; i++) { | 324 for (int i = 0; i < length; i++) { |
| 325 Handle<Object> x = args.at<Object>(i + 1); | 325 Handle<Object> x = args.at<Object>(i + 1); |
| 326 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, x, Object::ToNumber(x)); | 326 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, x, Object::ToNumber(x)); |
| 327 double abs_value = std::abs(x->Number()); | 327 double abs_value = std::abs(x->Number()); |
| 328 | 328 |
| 329 if (std::isnan(abs_value)) { | 329 if (std::isnan(abs_value)) { |
| 330 one_arg_is_nan = true; | 330 one_arg_is_nan = true; |
| 331 } else { | 331 } else { |
| 332 abs_values.Add(abs_value); | 332 abs_values.Add(abs_value); |
| 333 if (max < abs_value) { | 333 if (max < abs_value) { |
| 334 max = abs_value; | 334 max = abs_value; |
| 335 } | 335 } |
| 336 } | 336 } |
| 337 } | 337 } |
| 338 | 338 |
| 339 if (max == V8_INFINITY) { | 339 if (max == V8_INFINITY) { |
| 340 return *isolate->factory()->NewNumber(V8_INFINITY); | 340 return *isolate->factory()->NewNumber(V8_INFINITY); |
| 341 } | 341 } |
| 342 | 342 |
| 343 if (one_arg_is_nan) { | 343 if (one_arg_is_nan) { |
| 344 return *isolate->factory()->nan_value(); | 344 return *isolate->factory()->nan_value(); |
| 345 } | 345 } |
| 346 | 346 |
| 347 if (max == 0) { | 347 if (max == 0) { |
| 348 return Smi::FromInt(0); | 348 return Smi::kZero; |
| 349 } | 349 } |
| 350 DCHECK_GT(max, 0); | 350 DCHECK_GT(max, 0); |
| 351 | 351 |
| 352 // Kahan summation to avoid rounding errors. | 352 // Kahan summation to avoid rounding errors. |
| 353 // Normalize the numbers to the largest one to avoid overflow. | 353 // Normalize the numbers to the largest one to avoid overflow. |
| 354 double sum = 0; | 354 double sum = 0; |
| 355 double compensation = 0; | 355 double compensation = 0; |
| 356 for (int i = 0; i < length; i++) { | 356 for (int i = 0; i < length; i++) { |
| 357 double n = abs_values.at(i) / max; | 357 double n = abs_values.at(i) / max; |
| 358 double summand = n * n - compensation; | 358 double summand = n * n - compensation; |
| (...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 552 void Builtins::Generate_MathMax(MacroAssembler* masm) { | 552 void Builtins::Generate_MathMax(MacroAssembler* masm) { |
| 553 Generate_MathMaxMin(masm, MathMaxMinKind::kMax); | 553 Generate_MathMaxMin(masm, MathMaxMinKind::kMax); |
| 554 } | 554 } |
| 555 | 555 |
| 556 void Builtins::Generate_MathMin(MacroAssembler* masm) { | 556 void Builtins::Generate_MathMin(MacroAssembler* masm) { |
| 557 Generate_MathMaxMin(masm, MathMaxMinKind::kMin); | 557 Generate_MathMaxMin(masm, MathMaxMinKind::kMin); |
| 558 } | 558 } |
| 559 | 559 |
| 560 } // namespace internal | 560 } // namespace internal |
| 561 } // namespace v8 | 561 } // namespace v8 |
| OLD | NEW |