From e44d30e2847dabd6eb4195f99be93d9dbe70ccba Mon Sep 17 00:00:00 2001 From: "erik.corry@gmail.com" Date: Mon, 15 Jun 2009 10:27:52 +0000 Subject: [PATCH] Optimize constant divisions by powers of 2. Review URL: http://codereview.chromium.org/126116 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@2164 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- src/parser.cc | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/parser.cc b/src/parser.cc index 271c3fd16..a9a5e32e2 100644 --- a/src/parser.cc +++ b/src/parser.cc @@ -2647,6 +2647,21 @@ Expression* Parser::ParseBinaryExpression(int prec, bool accept_IN, bool* ok) { } } + // Convert constant divisions to multiplications for speed. + if (op == Token::DIV && + y && y->AsLiteral() && y->AsLiteral()->handle()->IsNumber()) { + double y_val = y->AsLiteral()->handle()->Number(); + int64_t y_int = static_cast(y_val); + // There are rounding issues with this optimization, but they don't + // apply if the number to be divided with has a reciprocal that can + // be precisely represented as a floating point number. This is + // the case if the number is an integer power of 2. + if (static_cast(y_int) == y_val && IsPowerOf2(y_int)) { + y = NewNumberLiteral(1 / y_val); + op = Token::MUL; + } + } + // For now we distinguish between comparisons and other binary // operations. (We could combine the two and get rid of this // code an AST node eventually.) -- 2.34.1