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

Side by Side Diff: src/builtins.cc

Issue 2102223005: [builtins] Migrate Math.hypot() to C++ builtins. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@MathAbs
Patch Set: Do not add Math.hypot() to list of optimized functions. Created 4 years, 5 months 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
« no previous file with comments | « src/builtins.h ('k') | src/js/math.js » ('j') | no next file with comments »
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 // 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.h" 5 #include "src/builtins.h"
6 6
7 #include "src/api-arguments.h" 7 #include "src/api-arguments.h"
8 #include "src/api-natives.h" 8 #include "src/api-natives.h"
9 #include "src/api.h" 9 #include "src/api.h"
10 #include "src/base/ieee754.h" 10 #include "src/base/ieee754.h"
(...skipping 2250 matching lines...) Expand 10 before | Expand all | Expand 10 after
2261 2261
2262 // ES6 section 20.2.2.4 Math.asin ( x ) 2262 // ES6 section 20.2.2.4 Math.asin ( x )
2263 BUILTIN(MathAsin) { 2263 BUILTIN(MathAsin) {
2264 HandleScope scope(isolate); 2264 HandleScope scope(isolate);
2265 DCHECK_EQ(2, args.length()); 2265 DCHECK_EQ(2, args.length());
2266 Handle<Object> x = args.at<Object>(1); 2266 Handle<Object> x = args.at<Object>(1);
2267 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, x, Object::ToNumber(x)); 2267 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, x, Object::ToNumber(x));
2268 return *isolate->factory()->NewHeapNumber(std::asin(x->Number())); 2268 return *isolate->factory()->NewHeapNumber(std::asin(x->Number()));
2269 } 2269 }
2270 2270
2271 // ES6 section 20.2.2.18 Math.hypot ( value1, value2, ...values )
2272 BUILTIN(MathHypot) {
2273 HandleScope scope(isolate);
2274 int const length = args.length() - 1;
2275 if (length == 0) return Smi::FromInt(0);
2276 DCHECK_LT(0, length);
2277 double max = 0;
2278 bool one_arg_is_nan = false;
2279 List<double> abs_values(length);
2280 for (int i = 0; i < length; i++) {
2281 Handle<Object> x = args.at<Object>(i + 1);
2282 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, x, Object::ToNumber(x));
2283 double abs_value = std::abs(x->Number());
2284
2285 if (std::isnan(abs_value)) {
2286 one_arg_is_nan = true;
2287 } else {
2288 abs_values.Add(abs_value);
2289 if (max < abs_value) {
2290 max = abs_value;
2291 }
2292 }
2293 }
2294
2295 if (max == V8_INFINITY) {
2296 return *isolate->factory()->NewNumber(V8_INFINITY);
2297 }
2298
2299 if (one_arg_is_nan) {
2300 return *isolate->factory()->nan_value();
2301 }
2302
2303 if (max == 0) {
2304 return Smi::FromInt(0);
2305 }
2306 DCHECK_GT(max, 0);
2307
2308 // Kahan summation to avoid rounding errors.
2309 // Normalize the numbers to the largest one to avoid overflow.
2310 double sum = 0;
2311 double compensation = 0;
2312 for (int i = 0; i < length; i++) {
2313 double n = abs_values.at(i) / max;
2314 double summand = n * n - compensation;
2315 double preliminary = sum + summand;
2316 compensation = (preliminary - sum) - summand;
2317 sum = preliminary;
2318 }
2319
2320 return *isolate->factory()->NewNumber(std::sqrt(sum) * max);
2321 }
2322
2271 // ES6 section 20.2.2.6 Math.atan ( x ) 2323 // ES6 section 20.2.2.6 Math.atan ( x )
2272 void Builtins::Generate_MathAtan(CodeStubAssembler* assembler) { 2324 void Builtins::Generate_MathAtan(CodeStubAssembler* assembler) {
2273 using compiler::Node; 2325 using compiler::Node;
2274 2326
2275 Node* x = assembler->Parameter(1); 2327 Node* x = assembler->Parameter(1);
2276 Node* context = assembler->Parameter(4); 2328 Node* context = assembler->Parameter(4);
2277 Node* x_value = assembler->TruncateTaggedToFloat64(context, x); 2329 Node* x_value = assembler->TruncateTaggedToFloat64(context, x);
2278 Node* value = assembler->Float64Atan(x_value); 2330 Node* value = assembler->Float64Atan(x_value);
2279 Node* result = assembler->ChangeFloat64ToTagged(value); 2331 Node* result = assembler->ChangeFloat64ToTagged(value);
2280 assembler->Return(result); 2332 assembler->Return(result);
(...skipping 4021 matching lines...) Expand 10 before | Expand all | Expand 10 after
6302 BUILTIN_LIST_H(DEFINE_BUILTIN_ACCESSOR_H) 6354 BUILTIN_LIST_H(DEFINE_BUILTIN_ACCESSOR_H)
6303 BUILTIN_LIST_DEBUG_A(DEFINE_BUILTIN_ACCESSOR_A) 6355 BUILTIN_LIST_DEBUG_A(DEFINE_BUILTIN_ACCESSOR_A)
6304 #undef DEFINE_BUILTIN_ACCESSOR_C 6356 #undef DEFINE_BUILTIN_ACCESSOR_C
6305 #undef DEFINE_BUILTIN_ACCESSOR_A 6357 #undef DEFINE_BUILTIN_ACCESSOR_A
6306 #undef DEFINE_BUILTIN_ACCESSOR_T 6358 #undef DEFINE_BUILTIN_ACCESSOR_T
6307 #undef DEFINE_BUILTIN_ACCESSOR_S 6359 #undef DEFINE_BUILTIN_ACCESSOR_S
6308 #undef DEFINE_BUILTIN_ACCESSOR_H 6360 #undef DEFINE_BUILTIN_ACCESSOR_H
6309 6361
6310 } // namespace internal 6362 } // namespace internal
6311 } // namespace v8 6363 } // namespace v8
OLDNEW
« no previous file with comments | « src/builtins.h ('k') | src/js/math.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698