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

Side by Side Diff: src/runtime.cc

Issue 183743018: Harmony: implement Math.cbrt in Javascript. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 6 years, 9 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 | Annotate | Revision Log
« no previous file with comments | « src/runtime.h ('k') | no next file » | 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 // 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 7690 matching lines...) Expand 10 before | Expand all | Expand 10 after
7701 RUNTIME_FUNCTION(MaybeObject*, Runtime_ConstructDouble) { 7701 RUNTIME_FUNCTION(MaybeObject*, Runtime_ConstructDouble) {
7702 SealHandleScope shs(isolate); 7702 SealHandleScope shs(isolate);
7703 ASSERT(args.length() == 2); 7703 ASSERT(args.length() == 2);
7704 CONVERT_NUMBER_CHECKED(uint32_t, hi, Uint32, args[0]); 7704 CONVERT_NUMBER_CHECKED(uint32_t, hi, Uint32, args[0]);
7705 CONVERT_NUMBER_CHECKED(uint32_t, lo, Uint32, args[1]); 7705 CONVERT_NUMBER_CHECKED(uint32_t, lo, Uint32, args[1]);
7706 uint64_t result = (static_cast<uint64_t>(hi) << 32) | lo; 7706 uint64_t result = (static_cast<uint64_t>(hi) << 32) | lo;
7707 return isolate->heap()->AllocateHeapNumber(uint64_to_double(result)); 7707 return isolate->heap()->AllocateHeapNumber(uint64_to_double(result));
7708 } 7708 }
7709 7709
7710 7710
7711 // Cube root approximation, refer to: http://metamerist.com/cbrt/cbrt.htm
7712 // Using initial approximation adapted from Kahan's cbrt and 4 iterations
7713 // of Newton's method.
7714 inline double CubeRootNewtonIteration(double approx, double x) {
7715 return (1.0 / 3.0) * (x / (approx * approx) + 2 * approx);
7716 }
7717
7718
7719 inline double CubeRoot(double x) {
7720 static const uint64_t magic = V8_2PART_UINT64_C(0x2A9F7893, 00000000);
7721 uint64_t xhigh = double_to_uint64(x);
7722 double approx = uint64_to_double(xhigh / 3 + magic);
7723
7724 approx = CubeRootNewtonIteration(approx, x);
7725 approx = CubeRootNewtonIteration(approx, x);
7726 approx = CubeRootNewtonIteration(approx, x);
7727 return CubeRootNewtonIteration(approx, x);
7728 }
7729
7730
7731 RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_cbrt) {
7732 SealHandleScope shs(isolate);
7733 ASSERT(args.length() == 1);
7734 CONVERT_DOUBLE_ARG_CHECKED(x, 0);
7735 if (x == 0 || std::isinf(x)) return args[0];
7736 double result = (x > 0) ? CubeRoot(x) : -CubeRoot(-x);
7737 return isolate->heap()->AllocateHeapNumber(result);
7738 }
7739
7740
7741 RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_log1p) { 7711 RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_log1p) {
7742 SealHandleScope shs(isolate); 7712 SealHandleScope shs(isolate);
7743 ASSERT(args.length() == 1); 7713 ASSERT(args.length() == 1);
7744 CONVERT_DOUBLE_ARG_CHECKED(x, 0); 7714 CONVERT_DOUBLE_ARG_CHECKED(x, 0);
7745 7715
7746 double x_abs = std::fabs(x); 7716 double x_abs = std::fabs(x);
7747 // Use Taylor series to approximate. With y = x + 1; 7717 // Use Taylor series to approximate. With y = x + 1;
7748 // log(y) at 1 == log(1) + log'(1)(y-1)/1! + log''(1)(y-1)^2/2! + ... 7718 // log(y) at 1 == log(1) + log'(1)(y-1)/1! + log''(1)(y-1)^2/2! + ...
7749 // == 0 + x - x^2/2 + x^3/3 ... 7719 // == 0 + x - x^2/2 + x^3/3 ...
7750 // The closer x is to 0, the fewer terms are required. 7720 // The closer x is to 0, the fewer terms are required.
(...skipping 7270 matching lines...) Expand 10 before | Expand all | Expand 10 after
15021 // Handle last resort GC and make sure to allow future allocations 14991 // Handle last resort GC and make sure to allow future allocations
15022 // to grow the heap without causing GCs (if possible). 14992 // to grow the heap without causing GCs (if possible).
15023 isolate->counters()->gc_last_resort_from_js()->Increment(); 14993 isolate->counters()->gc_last_resort_from_js()->Increment();
15024 isolate->heap()->CollectAllGarbage(Heap::kNoGCFlags, 14994 isolate->heap()->CollectAllGarbage(Heap::kNoGCFlags,
15025 "Runtime::PerformGC"); 14995 "Runtime::PerformGC");
15026 } 14996 }
15027 } 14997 }
15028 14998
15029 14999
15030 } } // namespace v8::internal 15000 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/runtime.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698