Commit 203391bc authored by franzih's avatar franzih Committed by Commit bot

[builtins] Migrate Math.abs() to TurboFan builtins.

Like the other Math builtins, Math.abs() is now a TurboFan builtin.
It uses RawMachineAssembler::Float64Abs().

R=bmeurer@chromium.org
BUG=v8:5163, v8:5086
LOG=N

Review-Url: https://codereview.chromium.org/2115493002
Cr-Commit-Position: refs/heads/master@{#37433}
parent bbc44c26
...@@ -1669,6 +1669,9 @@ void Genesis::InitializeGlobal(Handle<JSGlobalObject> global_object, ...@@ -1669,6 +1669,9 @@ void Genesis::InitializeGlobal(Handle<JSGlobalObject> global_object,
Handle<JSObject> math = factory->NewJSObject(cons, TENURED); Handle<JSObject> math = factory->NewJSObject(cons, TENURED);
DCHECK(math->IsJSObject()); DCHECK(math->IsJSObject());
JSObject::AddProperty(global, name, math, DONT_ENUM); JSObject::AddProperty(global, name, math, DONT_ENUM);
Handle<JSFunction> math_abs =
SimpleInstallFunction(math, "abs", Builtins::kMathAbs, 1, true);
native_context()->set_math_abs(*math_abs);
SimpleInstallFunction(math, "acos", Builtins::kMathAcos, 1, true); SimpleInstallFunction(math, "acos", Builtins::kMathAcos, 1, true);
SimpleInstallFunction(math, "asin", Builtins::kMathAsin, 1, true); SimpleInstallFunction(math, "asin", Builtins::kMathAsin, 1, true);
SimpleInstallFunction(math, "atan", Builtins::kMathAtan, 1, true); SimpleInstallFunction(math, "atan", Builtins::kMathAtan, 1, true);
......
...@@ -2240,6 +2240,16 @@ BUILTIN(JsonStringify) { ...@@ -2240,6 +2240,16 @@ BUILTIN(JsonStringify) {
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// ES6 section 20.2.2 Function Properties of the Math Object // ES6 section 20.2.2 Function Properties of the Math Object
// ES6 section - 20.2.2.1 Math.abs ( x )
void Builtins::Generate_MathAbs(CodeStubAssembler* assembler) {
using compiler::Node;
Node* x = assembler->Parameter(1);
Node* context = assembler->Parameter(4);
Node* x_value = assembler->TruncateTaggedToFloat64(context, x);
Node* value = assembler->Float64Abs(x_value);
Node* result = assembler->ChangeFloat64ToTagged(value);
assembler->Return(result);
}
// ES6 section 20.2.2.2 Math.acos ( x ) // ES6 section 20.2.2.2 Math.acos ( x )
BUILTIN(MathAcos) { BUILTIN(MathAcos) {
...@@ -2250,7 +2260,6 @@ BUILTIN(MathAcos) { ...@@ -2250,7 +2260,6 @@ BUILTIN(MathAcos) {
return *isolate->factory()->NewHeapNumber(std::acos(x->Number())); return *isolate->factory()->NewHeapNumber(std::acos(x->Number()));
} }
// ES6 section 20.2.2.4 Math.asin ( x ) // ES6 section 20.2.2.4 Math.asin ( x )
BUILTIN(MathAsin) { BUILTIN(MathAsin) {
HandleScope scope(isolate); HandleScope scope(isolate);
......
...@@ -311,6 +311,7 @@ class CodeStubAssembler; ...@@ -311,6 +311,7 @@ class CodeStubAssembler;
V(MathAtanh, 2) \ V(MathAtanh, 2) \
V(MathCeil, 2) \ V(MathCeil, 2) \
V(MathCbrt, 2) \ V(MathCbrt, 2) \
V(MathAbs, 2) \
V(MathExpm1, 2) \ V(MathExpm1, 2) \
V(MathClz32, 2) \ V(MathClz32, 2) \
V(MathCos, 2) \ V(MathCos, 2) \
...@@ -631,6 +632,8 @@ class Builtins { ...@@ -631,6 +632,8 @@ class Builtins {
static void Generate_InternalArrayCode(MacroAssembler* masm); static void Generate_InternalArrayCode(MacroAssembler* masm);
static void Generate_ArrayCode(MacroAssembler* masm); static void Generate_ArrayCode(MacroAssembler* masm);
// ES6 section 20.2.2.1 Math.abs ( x )
static void Generate_MathAbs(CodeStubAssembler* assembler);
// ES6 section 20.2.2.6 Math.atan ( x ) // ES6 section 20.2.2.6 Math.atan ( x )
static void Generate_MathAtan(CodeStubAssembler* assembler); static void Generate_MathAtan(CodeStubAssembler* assembler);
// ES6 section 20.2.2.8 Math.atan2 ( y, x ) // ES6 section 20.2.2.8 Math.atan2 ( y, x )
......
...@@ -108,6 +108,7 @@ class Schedule; ...@@ -108,6 +108,7 @@ class Schedule;
V(Word64Ror) V(Word64Ror)
#define CODE_ASSEMBLER_UNARY_OP_LIST(V) \ #define CODE_ASSEMBLER_UNARY_OP_LIST(V) \
V(Float64Abs) \
V(Float64Atan) \ V(Float64Atan) \
V(Float64Atanh) \ V(Float64Atanh) \
V(Float64Cos) \ V(Float64Cos) \
......
...@@ -89,6 +89,7 @@ enum BindingFlags { ...@@ -89,6 +89,7 @@ enum BindingFlags {
V(MATH_EXP_INDEX, JSFunction, math_exp) \ V(MATH_EXP_INDEX, JSFunction, math_exp) \
V(MATH_FLOOR_INDEX, JSFunction, math_floor) \ V(MATH_FLOOR_INDEX, JSFunction, math_floor) \
V(MATH_LOG_INDEX, JSFunction, math_log) \ V(MATH_LOG_INDEX, JSFunction, math_log) \
V(MATH_ABS_INDEX, JSFunction, math_abs) \
V(MATH_SQRT_INDEX, JSFunction, math_sqrt) V(MATH_SQRT_INDEX, JSFunction, math_sqrt)
#define NATIVE_CONTEXT_IMPORTED_FIELDS(V) \ #define NATIVE_CONTEXT_IMPORTED_FIELDS(V) \
......
...@@ -13,22 +13,13 @@ ...@@ -13,22 +13,13 @@
// The first two slots are reserved to persist PRNG state. // The first two slots are reserved to persist PRNG state.
define kRandomNumberStart = 2; define kRandomNumberStart = 2;
var GlobalFloat64Array = global.Float64Array;
var GlobalMath = global.Math; var GlobalMath = global.Math;
var GlobalObject = global.Object;
var NaN = %GetRootNaN(); var NaN = %GetRootNaN();
var nextRandomIndex = 0; var nextRandomIndex = 0;
var randomNumbers = UNDEFINED; var randomNumbers = UNDEFINED;
var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol"); var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol");
//------------------------------------------------------------------- //-------------------------------------------------------------------
// ECMA 262 - 15.8.2.1
function MathAbs(x) {
x = +x;
return (x > 0) ? x : 0 - x;
}
// ECMA 262 - 15.8.2.14 // ECMA 262 - 15.8.2.14
function MathRandom() { function MathRandom() {
// While creating a startup snapshot, %GenerateRandomNumbers returns a // While creating a startup snapshot, %GenerateRandomNumbers returns a
...@@ -83,7 +74,7 @@ function MathHypot(x, y) { // Function length is 2. ...@@ -83,7 +74,7 @@ function MathHypot(x, y) { // Function length is 2.
var length = arguments.length; var length = arguments.length;
var max = 0; var max = 0;
for (var i = 0; i < length; i++) { for (var i = 0; i < length; i++) {
var n = MathAbs(arguments[i]); var n = %math_abs(arguments[i]);
if (n > max) max = n; if (n > max) max = n;
arguments[i] = n; arguments[i] = n;
} }
...@@ -119,7 +110,6 @@ utils.InstallConstants(GlobalMath, [ ...@@ -119,7 +110,6 @@ utils.InstallConstants(GlobalMath, [
// set their names. // set their names.
utils.InstallFunctions(GlobalMath, DONT_ENUM, [ utils.InstallFunctions(GlobalMath, DONT_ENUM, [
"random", MathRandom, "random", MathRandom,
"abs", MathAbs,
"sign", MathSign, "sign", MathSign,
"asinh", MathAsinh, "asinh", MathAsinh,
"acosh", MathAcosh, "acosh", MathAcosh,
...@@ -133,7 +123,6 @@ utils.InstallFunctions(GlobalMath, DONT_ENUM, [ ...@@ -133,7 +123,6 @@ utils.InstallFunctions(GlobalMath, DONT_ENUM, [
// Exports // Exports
utils.Export(function(to) { utils.Export(function(to) {
to.MathAbs = MathAbs;
to.MathRandom = MathRandom; to.MathRandom = MathRandom;
}); });
......
...@@ -9,23 +9,19 @@ ...@@ -9,23 +9,19 @@
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Imports // Imports
var GlobalArray = global.Array;
var GlobalNumber = global.Number; var GlobalNumber = global.Number;
var GlobalObject = global.Object; var GlobalObject = global.Object;
var iteratorSymbol = utils.ImportNow("iterator_symbol"); var iteratorSymbol = utils.ImportNow("iterator_symbol");
var MakeRangeError; var MakeRangeError;
var MakeSyntaxError; var MakeSyntaxError;
var MakeTypeError; var MakeTypeError;
var MathAbs;
var NaN = %GetRootNaN(); var NaN = %GetRootNaN();
var ObjectToString = utils.ImportNow("object_to_string"); var ObjectToString = utils.ImportNow("object_to_string");
var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol");
utils.Import(function(from) { utils.Import(function(from) {
MakeRangeError = from.MakeRangeError; MakeRangeError = from.MakeRangeError;
MakeSyntaxError = from.MakeSyntaxError; MakeSyntaxError = from.MakeSyntaxError;
MakeTypeError = from.MakeTypeError; MakeTypeError = from.MakeTypeError;
MathAbs = from.MathAbs;
}); });
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
...@@ -367,7 +363,7 @@ function NumberIsSafeInteger(number) { ...@@ -367,7 +363,7 @@ function NumberIsSafeInteger(number) {
if (NumberIsFinite(number)) { if (NumberIsFinite(number)) {
var integral = TO_INTEGER(number); var integral = TO_INTEGER(number);
if (integral == number) { if (integral == number) {
return MathAbs(integral) <= kMaxSafeInteger; return %math_abs(integral) <= kMaxSafeInteger;
} }
} }
return false; return false;
......
...@@ -84,6 +84,6 @@ function testScriptMirror(f, file_name, file_lines, type, compilation_type, ...@@ -84,6 +84,6 @@ function testScriptMirror(f, file_name, file_lines, type, compilation_type,
// Test the script mirror for different functions. // Test the script mirror for different functions.
testScriptMirror(function(){}, 'mirror-script.js', 90, 2, 0); testScriptMirror(function(){}, 'mirror-script.js', 90, 2, 0);
testScriptMirror(Math.abs, 'native math.js', -1, 0, 0); testScriptMirror(Math.random, 'native math.js', -1, 0, 0);
testScriptMirror(eval('(function(){})'), null, 1, 2, 1, '(function(){})', 87); testScriptMirror(eval('(function(){})'), null, 1, 2, 1, '(function(){})', 87);
testScriptMirror(eval('(function(){\n })'), null, 2, 2, 1, '(function(){\n })', 88); testScriptMirror(eval('(function(){\n })'), null, 2, 2, 1, '(function(){\n })', 88);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment