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

Side by Side Diff: src/runtime.cc

Issue 179533003: Harmony: move implementation of Math.log1p and Math.expm1 to Javascript. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 6 years, 10 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 7682 matching lines...) Expand 10 before | Expand all | Expand 10 after
7693 RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_cbrt) { 7693 RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_cbrt) {
7694 SealHandleScope shs(isolate); 7694 SealHandleScope shs(isolate);
7695 ASSERT(args.length() == 1); 7695 ASSERT(args.length() == 1);
7696 CONVERT_DOUBLE_ARG_CHECKED(x, 0); 7696 CONVERT_DOUBLE_ARG_CHECKED(x, 0);
7697 if (x == 0 || std::isinf(x)) return args[0]; 7697 if (x == 0 || std::isinf(x)) return args[0];
7698 double result = (x > 0) ? CubeRoot(x) : -CubeRoot(-x); 7698 double result = (x > 0) ? CubeRoot(x) : -CubeRoot(-x);
7699 return isolate->heap()->AllocateHeapNumber(result); 7699 return isolate->heap()->AllocateHeapNumber(result);
7700 } 7700 }
7701 7701
7702 7702
7703 RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_log1p) {
7704 SealHandleScope shs(isolate);
7705 ASSERT(args.length() == 1);
7706 CONVERT_DOUBLE_ARG_CHECKED(x, 0);
7707
7708 double x_abs = std::fabs(x);
7709 // Use Taylor series to approximate. With y = x + 1;
7710 // log(y) at 1 == log(1) + log'(1)(y-1)/1! + log''(1)(y-1)^2/2! + ...
7711 // == 0 + x - x^2/2 + x^3/3 ...
7712 // The closer x is to 0, the fewer terms are required.
7713 static const double threshold_2 = 1.0 / 0x00800000;
7714 static const double threshold_3 = 1.0 / 0x00008000;
7715 static const double threshold_7 = 1.0 / 0x00000080;
7716
7717 double result;
7718 if (x_abs < threshold_2) {
7719 result = x * (1.0/1.0 - x * 1.0/2.0);
7720 } else if (x_abs < threshold_3) {
7721 result = x * (1.0/1.0 - x * (1.0/2.0 - x * (1.0/3.0)));
7722 } else if (x_abs < threshold_7) {
7723 result = x * (1.0/1.0 - x * (1.0/2.0 - x * (
7724 1.0/3.0 - x * (1.0/4.0 - x * (
7725 1.0/5.0 - x * (1.0/6.0 - x * (
7726 1.0/7.0)))))));
7727 } else { // Use regular log if not close enough to 0.
7728 result = std::log(1.0 + x);
7729 }
7730 return isolate->heap()->AllocateHeapNumber(result);
7731 }
7732
7733
7734 RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_expm1) {
7735 SealHandleScope shs(isolate);
7736 ASSERT(args.length() == 1);
7737 CONVERT_DOUBLE_ARG_CHECKED(x, 0);
7738
7739 double x_abs = std::fabs(x);
7740 // Use Taylor series to approximate.
7741 // exp(x) - 1 at 0 == -1 + exp(0) + exp'(0)*x/1! + exp''(0)*x^2/2! + ...
7742 // == x/1! + x^2/2! + x^3/3! + ...
7743 // The closer x is to 0, the fewer terms are required.
7744 static const double threshold_2 = 1.0 / 0x00400000;
7745 static const double threshold_3 = 1.0 / 0x00004000;
7746 static const double threshold_6 = 1.0 / 0x00000040;
7747
7748 double result;
7749 if (x_abs < threshold_2) {
7750 result = x * (1.0/1.0 + x * (1.0/2.0));
7751 } else if (x_abs < threshold_3) {
7752 result = x * (1.0/1.0 + x * (1.0/2.0 + x * (1.0/6.0)));
7753 } else if (x_abs < threshold_6) {
7754 result = x * (1.0/1.0 + x * (1.0/2.0 + x * (
7755 1.0/6.0 + x * (1.0/24.0 + x * (
7756 1.0/120.0 + x * (1.0/720.0))))));
7757 } else { // Use regular exp if not close enough to 0.
7758 result = std::exp(x) - 1.0;
7759 }
7760 return isolate->heap()->AllocateHeapNumber(result);
7761 }
7762
7763
7764 static const double kPiDividedBy4 = 0.78539816339744830962; 7703 static const double kPiDividedBy4 = 0.78539816339744830962;
7765 7704
7766 7705
7767 RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_atan2) { 7706 RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_atan2) {
7768 SealHandleScope shs(isolate); 7707 SealHandleScope shs(isolate);
7769 ASSERT(args.length() == 2); 7708 ASSERT(args.length() == 2);
7770 isolate->counters()->math_atan2()->Increment(); 7709 isolate->counters()->math_atan2()->Increment();
7771 7710
7772 CONVERT_DOUBLE_ARG_CHECKED(x, 0); 7711 CONVERT_DOUBLE_ARG_CHECKED(x, 0);
7773 CONVERT_DOUBLE_ARG_CHECKED(y, 1); 7712 CONVERT_DOUBLE_ARG_CHECKED(y, 1);
(...skipping 7191 matching lines...) Expand 10 before | Expand all | Expand 10 after
14965 // Handle last resort GC and make sure to allow future allocations 14904 // Handle last resort GC and make sure to allow future allocations
14966 // to grow the heap without causing GCs (if possible). 14905 // to grow the heap without causing GCs (if possible).
14967 isolate->counters()->gc_last_resort_from_js()->Increment(); 14906 isolate->counters()->gc_last_resort_from_js()->Increment();
14968 isolate->heap()->CollectAllGarbage(Heap::kNoGCFlags, 14907 isolate->heap()->CollectAllGarbage(Heap::kNoGCFlags,
14969 "Runtime::PerformGC"); 14908 "Runtime::PerformGC");
14970 } 14909 }
14971 } 14910 }
14972 14911
14973 14912
14974 } } // namespace v8::internal 14913 } } // 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