[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}
This commit is contained in:
franzih 2016-06-30 03:25:16 -07:00 committed by Commit bot
parent bbc44c2696
commit 203391bcc0
8 changed files with 21 additions and 19 deletions

View File

@ -1669,6 +1669,9 @@ void Genesis::InitializeGlobal(Handle<JSGlobalObject> global_object,
Handle<JSObject> math = factory->NewJSObject(cons, TENURED);
DCHECK(math->IsJSObject());
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, "asin", Builtins::kMathAsin, 1, true);
SimpleInstallFunction(math, "atan", Builtins::kMathAtan, 1, true);

View File

@ -2240,6 +2240,16 @@ BUILTIN(JsonStringify) {
// -----------------------------------------------------------------------------
// 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 )
BUILTIN(MathAcos) {
@ -2250,7 +2260,6 @@ BUILTIN(MathAcos) {
return *isolate->factory()->NewHeapNumber(std::acos(x->Number()));
}
// ES6 section 20.2.2.4 Math.asin ( x )
BUILTIN(MathAsin) {
HandleScope scope(isolate);

View File

@ -311,6 +311,7 @@ class CodeStubAssembler;
V(MathAtanh, 2) \
V(MathCeil, 2) \
V(MathCbrt, 2) \
V(MathAbs, 2) \
V(MathExpm1, 2) \
V(MathClz32, 2) \
V(MathCos, 2) \
@ -631,6 +632,8 @@ class Builtins {
static void Generate_InternalArrayCode(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 )
static void Generate_MathAtan(CodeStubAssembler* assembler);
// ES6 section 20.2.2.8 Math.atan2 ( y, x )

View File

@ -108,6 +108,7 @@ class Schedule;
V(Word64Ror)
#define CODE_ASSEMBLER_UNARY_OP_LIST(V) \
V(Float64Abs) \
V(Float64Atan) \
V(Float64Atanh) \
V(Float64Cos) \

View File

@ -89,6 +89,7 @@ enum BindingFlags {
V(MATH_EXP_INDEX, JSFunction, math_exp) \
V(MATH_FLOOR_INDEX, JSFunction, math_floor) \
V(MATH_LOG_INDEX, JSFunction, math_log) \
V(MATH_ABS_INDEX, JSFunction, math_abs) \
V(MATH_SQRT_INDEX, JSFunction, math_sqrt)
#define NATIVE_CONTEXT_IMPORTED_FIELDS(V) \

View File

@ -13,22 +13,13 @@
// The first two slots are reserved to persist PRNG state.
define kRandomNumberStart = 2;
var GlobalFloat64Array = global.Float64Array;
var GlobalMath = global.Math;
var GlobalObject = global.Object;
var NaN = %GetRootNaN();
var nextRandomIndex = 0;
var randomNumbers = UNDEFINED;
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
function MathRandom() {
// While creating a startup snapshot, %GenerateRandomNumbers returns a
@ -83,7 +74,7 @@ function MathHypot(x, y) { // Function length is 2.
var length = arguments.length;
var max = 0;
for (var i = 0; i < length; i++) {
var n = MathAbs(arguments[i]);
var n = %math_abs(arguments[i]);
if (n > max) max = n;
arguments[i] = n;
}
@ -119,7 +110,6 @@ utils.InstallConstants(GlobalMath, [
// set their names.
utils.InstallFunctions(GlobalMath, DONT_ENUM, [
"random", MathRandom,
"abs", MathAbs,
"sign", MathSign,
"asinh", MathAsinh,
"acosh", MathAcosh,
@ -133,7 +123,6 @@ utils.InstallFunctions(GlobalMath, DONT_ENUM, [
// Exports
utils.Export(function(to) {
to.MathAbs = MathAbs;
to.MathRandom = MathRandom;
});

View File

@ -9,23 +9,19 @@
// ----------------------------------------------------------------------------
// Imports
var GlobalArray = global.Array;
var GlobalNumber = global.Number;
var GlobalObject = global.Object;
var iteratorSymbol = utils.ImportNow("iterator_symbol");
var MakeRangeError;
var MakeSyntaxError;
var MakeTypeError;
var MathAbs;
var NaN = %GetRootNaN();
var ObjectToString = utils.ImportNow("object_to_string");
var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol");
utils.Import(function(from) {
MakeRangeError = from.MakeRangeError;
MakeSyntaxError = from.MakeSyntaxError;
MakeTypeError = from.MakeTypeError;
MathAbs = from.MathAbs;
});
// ----------------------------------------------------------------------------
@ -367,7 +363,7 @@ function NumberIsSafeInteger(number) {
if (NumberIsFinite(number)) {
var integral = TO_INTEGER(number);
if (integral == number) {
return MathAbs(integral) <= kMaxSafeInteger;
return %math_abs(integral) <= kMaxSafeInteger;
}
}
return false;

View File

@ -84,6 +84,6 @@ function testScriptMirror(f, file_name, file_lines, type, compilation_type,
// Test the script mirror for different functions.
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(){\n })'), null, 2, 2, 1, '(function(){\n })', 88);