From ea97f413ee70c87c30bb6216a80c15e05e575c9e Mon Sep 17 00:00:00 2001 From: "mstarzinger@chromium.org" Date: Wed, 24 Sep 2014 14:55:13 +0000 Subject: [PATCH] Extend JSBuiltinReducer to cover Math.fround as well. R=bmeurer@chromium.org TEST=compiler-unittests/JSBuiltinReducerTest.MathFround Review URL: https://codereview.chromium.org/594183004 git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@24187 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- src/compiler.cc | 1 - src/compiler/graph-unittest.cc | 1 + src/compiler/graph-unittest.h | 1 + src/compiler/js-builtin-reducer-unittest.cc | 26 +++++++++++++++++++ src/compiler/js-builtin-reducer.cc | 15 +++++++++++ src/compiler/js-builtin-reducer.h | 1 + test/mjsunit/asm/math-fround.js | 40 +++++++++++++++++++++++++++++ 7 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 test/mjsunit/asm/math-fround.js diff --git a/src/compiler.cc b/src/compiler.cc index 4331770..13a1ec8 100644 --- a/src/compiler.cc +++ b/src/compiler.cc @@ -323,7 +323,6 @@ class HOptimizedGraphBuilderWithPositions: public HOptimizedGraphBuilder { OptimizedCompileJob::Status OptimizedCompileJob::CreateGraph() { - DCHECK(isolate()->use_crankshaft()); DCHECK(info()->IsOptimizing()); DCHECK(!info()->IsCompilingForDebugging()); diff --git a/src/compiler/graph-unittest.cc b/src/compiler/graph-unittest.cc index 75e70cb..0071b81 100644 --- a/src/compiler/graph-unittest.cc +++ b/src/compiler/graph-unittest.cc @@ -769,6 +769,7 @@ IS_UNOP_MATCHER(ChangeInt32ToFloat64) IS_UNOP_MATCHER(ChangeInt32ToInt64) IS_UNOP_MATCHER(ChangeUint32ToFloat64) IS_UNOP_MATCHER(ChangeUint32ToUint64) +IS_UNOP_MATCHER(TruncateFloat64ToFloat32) IS_UNOP_MATCHER(TruncateFloat64ToInt32) IS_UNOP_MATCHER(TruncateInt64ToInt32) IS_UNOP_MATCHER(Float64Sqrt) diff --git a/src/compiler/graph-unittest.h b/src/compiler/graph-unittest.h index 1dc9c3d..04d4af0 100644 --- a/src/compiler/graph-unittest.h +++ b/src/compiler/graph-unittest.h @@ -129,6 +129,7 @@ Matcher IsChangeInt32ToFloat64(const Matcher& input_matcher); Matcher IsChangeInt32ToInt64(const Matcher& input_matcher); Matcher IsChangeUint32ToFloat64(const Matcher& input_matcher); Matcher IsChangeUint32ToUint64(const Matcher& input_matcher); +Matcher IsTruncateFloat64ToFloat32(const Matcher& input_matcher); Matcher IsTruncateFloat64ToInt32(const Matcher& input_matcher); Matcher IsTruncateInt64ToInt32(const Matcher& input_matcher); Matcher IsFloat64Sqrt(const Matcher& input_matcher); diff --git a/src/compiler/js-builtin-reducer-unittest.cc b/src/compiler/js-builtin-reducer-unittest.cc index 51561d0..598a553 100644 --- a/src/compiler/js-builtin-reducer-unittest.cc +++ b/src/compiler/js-builtin-reducer-unittest.cc @@ -172,6 +172,32 @@ TEST_F(JSBuiltinReducerTest, MathImul) { } } + +// ----------------------------------------------------------------------------- +// Math.fround + + +TEST_F(JSBuiltinReducerTest, MathFround) { + Handle m = + JSObject::GetProperty(isolate()->global_object(), + isolate()->factory()->NewStringFromAsciiChecked( + "Math")).ToHandleChecked(); + Handle f = Handle::cast( + JSObject::GetProperty(m, isolate()->factory()->NewStringFromAsciiChecked( + "fround")).ToHandleChecked()); + + TRACED_FOREACH(Type*, t0, kNumberTypes) { + Node* p0 = Parameter(t0, 0); + Node* fun = HeapConstant(Unique::CreateUninitialized(f)); + Node* call = graph()->NewNode(javascript()->Call(3, NO_CALL_FUNCTION_FLAGS), + fun, UndefinedConstant(), p0); + Reduction r = Reduce(call); + + ASSERT_TRUE(r.Changed()); + EXPECT_THAT(r.replacement(), IsTruncateFloat64ToFloat32(p0)); + } +} + } // namespace compiler } // namespace internal } // namespace v8 diff --git a/src/compiler/js-builtin-reducer.cc b/src/compiler/js-builtin-reducer.cc index c57ac33..f1aef31 100644 --- a/src/compiler/js-builtin-reducer.cc +++ b/src/compiler/js-builtin-reducer.cc @@ -151,6 +151,19 @@ Reduction JSBuiltinReducer::ReduceMathImul(Node* node) { } +// ES6 draft 08-24-14, section 20.2.2.17. +Reduction JSBuiltinReducer::ReduceMathFround(Node* node) { + JSCallReduction r(node); + if (r.InputsMatchOne(Type::Number())) { + // Math.fround(a:number) -> TruncateFloat64ToFloat32(a) + Node* value = + graph()->NewNode(machine()->TruncateFloat64ToFloat32(), r.left()); + return Replace(value); + } + return NoChange(); +} + + Reduction JSBuiltinReducer::Reduce(Node* node) { JSCallReduction r(node); @@ -163,6 +176,8 @@ Reduction JSBuiltinReducer::Reduce(Node* node) { return ReplaceWithPureReduction(node, ReduceMathMax(node)); case kMathImul: return ReplaceWithPureReduction(node, ReduceMathImul(node)); + case kMathFround: + return ReplaceWithPureReduction(node, ReduceMathFround(node)); default: break; } diff --git a/src/compiler/js-builtin-reducer.h b/src/compiler/js-builtin-reducer.h index 13927f6..e5a1b83 100644 --- a/src/compiler/js-builtin-reducer.h +++ b/src/compiler/js-builtin-reducer.h @@ -33,6 +33,7 @@ class JSBuiltinReducer FINAL : public Reducer { Reduction ReduceMathSqrt(Node* node); Reduction ReduceMathMax(Node* node); Reduction ReduceMathImul(Node* node); + Reduction ReduceMathFround(Node* node); JSGraph* jsgraph_; SimplifiedOperatorBuilder simplified_; diff --git a/test/mjsunit/asm/math-fround.js b/test/mjsunit/asm/math-fround.js new file mode 100644 index 0000000..068917b --- /dev/null +++ b/test/mjsunit/asm/math-fround.js @@ -0,0 +1,40 @@ +// Copyright 2014 the V8 project authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Flags: --turbo-asm + +function Module(stdlib) { + "use asm"; + + var fround = stdlib.Math.fround; + + // f: double -> float + function f(a) { + a = +a; + return fround(a); + } + + return { f: f }; +} + +var f = Module({ Math: Math }).f; + +assertTrue(isNaN(f(NaN))); +assertTrue(isNaN(f(undefined))); +assertTrue(isNaN(f(function() {}))); + +assertEquals("Infinity", String(1/f(0))); +assertEquals("-Infinity", String(1/f(-0))); +assertEquals("Infinity", String(f(Infinity))); +assertEquals("-Infinity", String(f(-Infinity))); +assertEquals("Infinity", String(f(1E200))); +assertEquals("-Infinity", String(f(-1E200))); +assertEquals("Infinity", String(1/f(1E-300))); +assertEquals("-Infinity", String(1/f(-1E-300))); + +assertEquals(0, f(0)); +assertEquals(1, f(1)); +assertEquals(1.5, f(1.5)); +assertEquals(1.3370000123977661, f(1.337)); +assertEquals(-4.300000190734863, f(-4.3)); -- 2.7.4